[LOJ # 3146] [APIO 2019] lights (set tree tree)

[LOJ # 3146] [APIO 2019] lights (set tree tree)

Face questions

SIZE

answer

The examination room because \ (\ text {bridge} \ ) a \ (\ text {subtask} \ ) no sentence \ (n = 1 \) circumstances led me card \ (3.5h \) or so, then this problem would only rush \ (rush \) a \ (60 \) points violence ......


Consider maintaining a bright consecutive segments each time a lamp, then for consecutive segments \ ([L, R & lt] \) , clearly this will query any set time interval is generated in a contribution.
Because maintaining continuous period is very hard to deal with, so turn on the lights did not consider the impact of each place.
Suppose \ (X \) position does not turn, the open position is not a \ (lt \) , then for the left interval \ ([lt + 1, I] \) , the right interval \ ([lt + 1 , i] \) is generated within the range of contributions, then we can put into the interval point, so the contribution into a number of two-dimensional points.
So take a direct tree cover tree maintenance on the line. (Or \ (CDQ \) and the like is also OK)
statistical method used here is similar to the differential answer, we gave a start to all locations on \ (+ Q \) contributions in order to modify the operation, the range of influence of the contribution to make up just fine.
For example, or \ (X \) and \ (lt \) two positions, the left interval \ ([+ lt. 1, I] \) , the right interval \ ([i + 1, n + 1] \) interval no contributions.
Then to \ ((lt + 1, i + 1) \) position plus a \ (- Q \) , \ ((. 1 + I, + I. 1) \)Location plus a \ (Q \) can be serviced out of this contribution.
Statistics last time to remember the answer if the answer is just a complete range of current, we should not count the rest of the time \ (Q \) time to get rid of. . .

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
#define MAX 300300
inline int read()
{
    int x=0;bool t=false;char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=true,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return t?-x:x;
}
int n,Q;
struct Node{int ls,rs,v;}t[MAX<<6];
int tot,rt[MAX];
void Modify(int &x,int l,int r,int p,int w)
{
    if(!x)x=++tot;t[x].v+=w;
    if(l==r)return;
    int mid=(l+r)>>1;
    if(p<=mid)Modify(t[x].ls,l,mid,p,w);
    else Modify(t[x].rs,mid+1,r,p,w);
}
int Query(int x,int l,int r,int L,int R)
{
    if(!x)return 0;
    if(L<=l&&r<=R)return t[x].v;
    int mid=(l+r)>>1,ret=0;
    if(L<=mid)ret+=Query(t[x].ls,l,mid,L,R);
    if(R>mid)ret+=Query(t[x].rs,mid+1,r,L,R);
    return ret;
}
int lb(int x){return x&(-x);}
void Modify(int x,int y,int v){while(x<=n+1)Modify(rt[x],1,n+1,y,v),x+=lb(x);}
int Query(int x,int y){int s=0;while(x)s+=Query(rt[x],1,n+1,1,y),x-=lb(x);return s;}
set<int> S;set<int>::iterator it,pr,nt;
char ch[MAX];
int main()
{
    n=read();Q=read();scanf("%s",ch+1);
    S.insert(0);S.insert(n+1);Modify(1,1,Q);    
    for(int i=1,j=0;i<=n;++i)
    {
        if(ch[i]=='1')continue;
        S.insert(i);
        Modify(j+1,i+1,-Q);
        Modify(i+1,i+1,Q);
        j=i;
    }
    while(Q--)
    {
        char opt[8];scanf("%s",opt);
        if(opt[0]=='t')
        {
            int x=read(),zt=(ch[x]=='0')?1:-1;
            if(ch[x]=='1')S.insert(x);
            it=S.find(x);
            pr=nt=it;--pr;++nt;
            Modify(*pr+1,x+1,Q*zt);Modify(x+1,x+1,-Q*zt);           
            if(*nt!=n+1)Modify(*pr+1,*nt+1,-Q*zt),Modify(x+1,*nt+1,Q*zt);
            if(ch[x]=='0')S.erase(x);
            ch[x]^=1;
        }
        else
        {
            int x=read(),y=read();
            printf("%d\n",Query(x,y)-Q*(S.lower_bound(x)==S.lower_bound(y)));
        }
    }
    return 0;   
}

Guess you like

Origin www.cnblogs.com/cjyyb/p/11100172.html