首页 > 代码库 > Codeforces 707D - Persistent Bookcase

Codeforces 707D - Persistent Bookcase

在线版本:可持久化线段树。

 

/************************************************Author        :DarkTongCreated Time  :2016/8/26 17:16:28File Name     :D.cpp*************************************************/#include <bits/stdc++.h>using namespace std;typedef unsigned long long ULL;typedef long long LL;const int INF = 0x3f3f3f3f;const double eps = 1e-9;const int maxn = 100000 + 100;int tr[maxn];int sz = 0;struct Node{    int l, r, s;    bitset<1000 + 10> v;//    Node(){l=r=s=0; memset(v, 0, sizeof(v));}}nxt[maxn*20];int getone(int x){    int ans = 0;    for(int i=0;i<32;++i)if(((x>>i)&1)==1) ++ans;    return ans;}bitset<1000 + 10> Base;void insert(int &x, int pre, int l, int r, int L, int R, int f, int j){    x = ++sz;    nxt[x] = nxt[pre];//    cout<<"s:"<<nxt[x].s<<" l:"<<l<<" r:"<<r<<" L:"<<L<<" R:"<<R<<" ll:"<<nxt[x].l<<" rr:"<<nxt[x].r<<" pre:"<<pre<<endl;//    cout<<"pl:"<<nxt[pre].l<<" pr:"<<nxt[pre].r<<endl;    if(L<=l&&r<=R)    {        if(f==1)        {            if(!nxt[x].v.test(j)) nxt[x].s++, nxt[x].v.flip(j);        }        else if(f==2)        {            if(nxt[x].v.test(j)) nxt[x].s--, nxt[x].v.flip(j);        }        else if(f==3)        {            int ta = 0;            for(int i=0;i<=j;++i) if(nxt[x].v.test(i)) ta++;            nxt[x].s -= ta;            nxt[x].s += j-ta+1;            for(int i=0;i<=j;++i) nxt[x].v.flip(i);        }        return;    }    int m = (l+r)>>1;    int tx = x;//    cout<<"l:"<<l<<" m:"<<m<<" r:"<<r<<endl;    if(L<m) insert(nxt[tx].l, nxt[pre].l, l, m, L, R, f, j);    if(m<R) insert(nxt[tx].r, nxt[pre].r, m, r, L, R, f, j);    nxt[tx].s = nxt[nxt[tx].l].s + nxt[nxt[tx].r].s;}int main(){    int T, cas=1;    int n, m, q;    scanf("%d%d%d", &n, &m, &q);    for(int i=0;i<1000 + 10;++i) Base[i]=1;    int op, i, j;    for(int z=1;z<=q;++z)    {        scanf("%d%d", &op, &i);        if(op<=2) scanf("%d", &j);        else j=m;        if(op<=3) insert(tr[z], tr[z-1], 1, n+1, i, i+1, op, j-1);        else tr[z] = tr[i];        printf("%d\n", nxt[tr[z]].s);    }        return 0;}

Codeforces 707D - Persistent Bookcase