您的位置:首页 > 其它

【线段树延迟更新】Codeforces Round #104 (Div. 1) E

2012-02-13 19:27 288 查看
#define N 1000005
int c4[N*5],c7[N*5];
int c47[N*5],c74[N*5];
bool mark[N*5];
char str
;
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: