比赛 |
[不是Rapiz出的]农场主钦定NOIP模拟赛1 |
评测结果 |
WWWTTTTTTT |
题目名称 |
Color the Axis |
最终得分 |
0 |
用户昵称 |
cwm大佬%%% |
运行时间 |
7.037 s |
代码语言 |
C++ |
内存使用 |
10.97 MiB |
提交时间 |
2016-11-08 19:21:39 |
显示代码纯文本
#include<cstdio>
const int N=200000+10;
struct SEG{
struct P{
int delta,tot;
bool set;
int l,r;
int lc,rc;
};
P tree[N*2];
int top;
SEG(){top=0;}
int build_tree(int l,int r){
int p=top++;
P &now=tree[p];
now=(P){0,0,0,l,r,-1,-1};
if(l<r){
now.lc=build_tree(l,(l+r)/2);
now.rc=build_tree((l+r)/2+1,r);
}
return p;
}
void push_down(int p){
P &now=tree[p];
if(now.set){
tree[now.lc].delta=now.delta;
tree[now.rc].delta=now.delta;
tree[now.lc].set=1;
tree[now.rc].set=1;
tree[now.lc].tot=now.delta*(tree[now.lc].r-tree[now.lc].l+1);
tree[now.rc].tot=now.delta*(tree[now.rc].r-tree[now.rc].l+1);
now.set=0;
}else{
if(tree[now.lc].set)push_down(now.lc);
if(tree[now.rc].set)push_down(now.rc);
tree[now.lc].delta+=now.delta;
tree[now.rc].delta+=now.delta;
tree[now.lc].tot+=now.delta*(tree[now.lc].r-tree[now.lc].l+1);
tree[now.rc].tot+=now.delta*(tree[now.rc].r-tree[now.rc].l+1);
}
now.delta=0;
}
void add(int l,int r,int delta,int p=0){
if(p==-1)return;
P &now=tree[p];
if(now.l>r||now.r<l)return;
push_down(p);
if(now.l>=l&&now.r<=r){
now.delta+=delta;
now.tot+=delta*(now.r-now.l+1);
}else{
add(l,r,delta,now.lc);
add(l,r,delta,now.rc);
now.tot=tree[now.lc].tot+tree[now.rc].tot;
}
}
void change(int l,int r,int set,int p=0){
if(p==-1)return;
P &now=tree[p];
if(now.l>r||now.r<l)return;
push_down(p);
if(now.l>=l&&now.r<=r){
now.delta=set;
now.set=1;
now.tot=set*(now.r-now.l+1);
}else{
change(l,r,set,now.lc);
change(l,r,set,now.rc);
now.tot=tree[now.lc].tot+tree[now.rc].tot;
}
}
int query(int l,int r,int p=0){
if(p==-1)return 0;
P &now=tree[p];
if(now.l>r||now.r<l)return 0;
if(now.l>=l&&now.r<=r)return now.tot;
push_down(p);
return query(l,r,now.lc)+query(l,r,now.rc);
}
//void put(){for(int i=0;i<top;i++,putchar('\n'))printf("%d %d | %d %d %d",tree[i].l,tree[i].r,tree[i].delta,tree[i].tot,tree[i].set);putchar('\n');}
};
SEG tree;
int main()
{
freopen("axis.in","r",stdin);
freopen("axis.out","w",stdout);
int n,m;
scanf("%d%d",&n,&m);
tree.build_tree(1,n);
tree.add(1,n,1);
for(int i=0;i<m;i++){
int l,r;
scanf("%d%d",&l,&r);
tree.change(l,r,0);
printf("%d\n",tree.query(1,n));
}
return 0;
}