线段树[模板]最大连续子段和以及单节点修改 洛谷SP1716 GSS3 - Can you answer these queries III

版权声明:我这么弱的蒟蒻,虽然博文不是很好,但也请标明转发地址喵! https://blog.csdn.net/ACerAndAKer/article/details/80688457
//SegmenTree
//By AcerMo
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=200500;
struct Tree
{
    int bond[2];
    int lazy;
    int son[2];
    int sum;
    int mks[3];
}tree[M];
int n,m,root=0,cnt=1;
int num[M];
int ans,que;
void pushup(int x)//更新节点 
{
    int lson=tree[x].son[0];
    int rson=tree[x].son[1];
    tree[x].mks[0]=max(tree[lson].mks[0],tree[lson].sum+tree[rson].mks[0]);
    tree[x].mks[1]=max(tree[rson].mks[1],tree[rson].sum+tree[lson].mks[1]);
    tree[x].mks[2]=max(tree[rson].mks[0]+tree[lson].mks[1],max(tree[lson].mks[2],tree[rson].mks[2]));
    tree[x].sum=tree[lson].sum+tree[rson].sum;
    tree[x].bond[0]=tree[lson].bond[0];
    tree[x].bond[1]=tree[rson].bond[1];
    return ; 
}
void change(int poi,int val,int cur)
{
    if (tree[cur].bond[0]==tree[cur].bond[1])
    {
        tree[cur].sum=val;
        tree[cur].mks[2]=tree[cur].mks[1]=tree[cur].mks[0]=val; 
        return ;
    }
    int mid=(tree[cur].bond[0]+tree[cur].bond[1])>>1;
    if (poi<=mid) change(poi,val,tree[cur].son[0]);
    else change(poi,val,tree[cur].son[1]);
    pushup(cur);
    return ;
}
void built(int L,int R,int cur)
{
    if (L==R)
    {
        tree[cur].son[0]=tree[cur].son[1]=-1;
        tree[cur].bond[0]=tree[cur].bond[1]=L;
        tree[cur].sum=num[L];
        tree[cur].mks[0]=tree[cur].mks[1]=tree[cur].mks[2]=num[R];
        tree[cur].lazy=0;
        return ;
    }
    int mid=(L+R)>>1;
    tree[cur].son[0]=cnt++;
    tree[cur].son[1]=cnt++;
    built(L,mid,tree[cur].son[0]);
    built(mid+1,R,tree[cur].son[1]);
    pushup(cur);
    return ;
}
void query(int L,int R,int cur)
{
    if (L<=tree[cur].bond[0]&&tree[cur].bond[1]<=R)
    {
        ans=max(ans,max(tree[cur].mks[2],tree[cur].mks[0]+que));
        que=max(que+tree[cur].sum,tree[cur].mks[1]);
        return ;
    }
    int mid=(tree[cur].bond[0]+tree[cur].bond[1])>>1;
    if (L<=mid) query(L,R,tree[cur].son[0]);
    if (R>mid) query(L,R,tree[cur].son[1]);
    return ;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
        scanf("%d",&num[i]);
    built(1,n,root);
    scanf("%d",&m);
    while (m--)
    {
        int flag,l,r;
        scanf("%d %d %d",&flag,&l,&r);
        if (flag)
        {
            ans=-1e9;
            que=ans;
            query(l,r,root);
            cout<<ans<<endl;
        }
        else change(l,r,root);
    }
    return 0;
}

阅读更多

Can you answer these queries?

06-15

Problem DescriptionnA lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.nYou are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.nnNotice that the square root operation should be rounded down to integer.n nnInputnThe input contains several test cases, terminated by EOF.n For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)n The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.n The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)n For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.n nnOutputnFor each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.n nnSample Inputn10n1 2 3 4 5 6 7 8 9 10n5n0 1 10n1 1 10n1 1 5n0 5 8n1 4 8n nnSample OutputnCase #1:n19n7n6

没有更多推荐了,返回首页