[BZOJ3064]Tyvj 1518 CPU monitoring (line tree + persistence)

subject

Portal

Title Solution

According to ddd, it's very disgusting, so I found it to do it. At last, I can adjust it by copying the questions
There are many values that need to be maintained in the segment tree: current maximum, current addition mark, current overlay mark
History max, history Max add mark, history overlay mark.
The pushdown function is relatively long, others are easy to do.
-Historical maximum: consider the historical addition and historical coverage of this interval, as well as the current maximum of the subtree
-History addition: consider the history addition of this interval and the current addition of subtree
-Historical coverage: consider the current coverage of this interval and the historical coverage of the subtree
It is worth noting that if the subtree is not currently covered, the addition can be updated directly, otherwise it should be added to the cover
Each interval will be marked directly, whether or not the interval is completely covered

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=100001;
const int inf=2*1e9;
int T,m,cnt,add[maxn<<2],a[maxn];
char opt;
struct Node{
    int mx,add,cover;
}tree[maxn*20],his[maxn*20];//?

void pushup(int rt)
{
    tree[rt].mx=max(tree[rt<<1].mx,tree[rt<<1|1].mx);
    his[rt].mx=max(his[rt<<1].mx,his[rt<<1|1].mx);
}

void pushdown(int rt)
{
    for (int i=0; i<=1; i++)
    {
        int son=rt<<1|i;//First, update the history tree[rt].cover to tree[son].cover
        his[son].mx=max(his[son].mx,max(his[rt].cover,tree[son].mx+his[rt].add));//It's all down from the parent node 
        if (tree[son].cover!=-inf) his[son].cover=max(his[son].cover,tree[son].cover+his[rt].add);
        else his[son].add=max(his[son].add,tree[son].add+his[rt].add);
        if (tree[rt].add)//Update current value 
        {
            if (tree[son].cover!=-inf) tree[son].cover+=tree[rt].add;
            else tree[son].add+=tree[rt].add;
            tree[son].mx+=tree[rt].add;
        }
        if (tree[rt].cover!=-inf)//Note - inf don't write inf 
        {
            tree[son].cover=tree[son].mx=tree[rt].cover;
            tree[son].add=0;//After overwriting, the current add will change to zero 
        }
        his[son].cover=max(his[son].cover,max(tree[son].cover,his[rt].cover));
        his[son].add=max(his[son].add,tree[son].add);
    }
    tree[rt].add=his[rt].add=0;//Reconsider whether the current update can be included in history 
    tree[rt].cover=his[rt].cover=-inf;
}

void build_tree(int rt,int l,int r)
{
    tree[rt].add=his[rt].add=0;
    tree[rt].cover=his[rt].cover=-inf;
    if (l==r)
    {
        tree[rt].mx=a[l];
        his[rt].mx=a[l]; return;
    }
    int mid=(l+r)>>1;
    build_tree(rt<<1,l,mid);
    build_tree(rt<<1|1,mid+1,r);
    pushup(rt);
}

void change(int rt,int L,int R,int C,int l,int r,bool ifhis)
{
    if (l!=r) pushdown(rt);//Push down every time
    int mid=(l+r)>>1;
    if (L<=l && r<=R)
    {
        if (!ifhis)
        {
            tree[rt].mx+=C; tree[rt].add+=C; his[rt].add+=C; return;
        }
        else tree[rt].mx=tree[rt].cover=his[rt].cover=C;
        his[rt].mx=max(his[rt].mx,tree[rt].mx);
        return;
    }
    if (L<=mid) change(rt<<1,L,R,C,l,mid,ifhis);
    if (R>mid) change(rt<<1|1,L,R,C,mid+1,r,ifhis);
    pushup(rt);
}

int ques(int rt,int L,int R,int l,int r,bool ifhis)
{
    if (l!=r) pushdown(rt);
    int ans=-inf;
    if (L<=l && r<=R) return (ifhis?his[rt].mx:tree[rt].mx);
    int mid=(l+r)>>1;
    if (L<=mid) ans=max(ans,ques(rt<<1,L,R,l,mid,ifhis));
    if (R>mid) ans=max(ans,ques(rt<<1|1,L,R,mid+1,r,ifhis));
    return ans;
}

int main()
{
    scanf("%d",&T);
    for (int i=1; i<=T; i++) scanf("%d",&a[i]); scanf("%d",&m);
    int n=T;
    build_tree(1,1,n);
//   for (int i=1; i<=n*2; i++) printf("%d:  %d\n",i,tree[i].mx); return 0;
    for (int i=1; i<=m; i++)
    {
        int x,y,z; opt=getchar(); 
        while (opt!='Q'&&opt!='A'&&opt!='P'&&opt!='C') opt=getchar();
        if (opt=='Q') 
        {
            scanf("%d%d",&x,&y);
            printf("%d\n",ques(1,x,y,1,n,0));
        }
        if (opt=='A') 
        {
            scanf("%d%d",&x,&y);
            printf("%d\n",ques(1,x,y,1,n,1));
        }
        if (opt=='P') 
        {
            scanf("%d%d%d",&x,&y,&z);
            change(1,x,y,z,1,n,0);
        }
        if (opt=='C') 
        {
            scanf("%d%d%d",&x,&y,&z);
            change(1,x,y,z,1,n,1);
        }
    }
    return 0;
}

summary

It's right to see the code "naked eye debugging" of zyf2000 senior sister. All kinds of 0 and 1, son and father, history and now will be confused. Improve code ability urgently
Write the code accurately, or save a round of GG.

Posted by trevHCS on Fri, 03 Apr 2020 11:44:44 -0700