【線段樹延遲更新】Codeforces Round #104 (Div. 1) E

來源:互聯網
上載者:User
#define N 1000005int c4[N*5],c7[N*5];int c47[N*5],c74[N*5];bool mark[N*5];char str[N];void up(int id){    c4[id] = c4[id<<1] + c4[id<<1|1];    c7[id] = c7[id<<1] + c7[id<<1|1];    c47[id] = max(c47[id<<1] + c7[id<<1|1] , c4[id<<1] + c47[id<<1|1]);    c74[id] = max(c74[id<<1] + c4[id<<1|1] , c7[id<<1] + c74[id<<1|1]);}void build(int s,int t,int id){    if(s>t)return ;    mark[id] = 0;    if(s == t){        if(str[s] == '4'){            c4[id] = 1;            c7[id] = 0;        } else {            c4[id] = 0;            c7[id] = 1;        }        c47[id] = c74[id] = 1;        return ;    }    int mid = (s + t)>>1;    build(s,mid,id<<1);    build(mid+1,t,id<<1|1);    up(id);}void rev(int id){    mark[id] = !mark[id];    swap(c47[id],c74[id]);    swap(c4[id],c7[id]);}void down(int id){    if(mark[id]){        mark[id] = 0;        rev(id<<1);        rev(id<<1|1);    }}void sw(int s,int t,int id,int l,int r){    if(s<=l && r<=t){        rev(id);        return ;    }    down(id);    int mid = (l+r)>>1;    if(s<=mid)sw(s,t,id<<1,l,mid);    if(mid+1<=t)sw(s,t,id<<1|1,mid+1,r);    up(id);}int main(){    int n,m;    while(scanf("%d%d",&n,&m) != -1){        int i,j;        scanf("%s",str+1);        build(1,n,1);        while(m--){            char op[10];            scanf("%s",op);            if(op[0] == 'c'){                printf("%d\n",c47[1]);            }else {                int x,y;                scanf("%d%d",&x,&y);                sw(x,y,1,1,n);            }        }    }    return 0;}

http://codeforces.com/contest/145/problem/E

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.