成段更新

A Simple Problem with Integers http://poj.org/problem?id=3468

 1 #include<cstdio>
 2 #define lrrt int L,int R,int rt
 3 #define iall 1,n,1
 4 #define imid int mid=(L+R)>>1
 5 #define lson L,mid,rt<<1
 6 #define rson mid+1,R,rt<<1|1
 7 typedef __int64 LL;
 8 const int M=100010;
 9 int a[M];
10 struct T{
11     LL sum,lazy;
12 }tree[M<<2];
13 void pushup(int rt){
14     tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
15 }
16 void build(lrrt){
17     tree[rt].lazy=0;
18     if(L==R){
19         tree[rt].sum=a[L];
20         return ;
21     }
22     imid;
23     build(lson);
24     build(rson);
25     pushup(rt);
26 }
27 void pushdown(int mid,lrrt){
28     if(tree[rt].lazy){
29         tree[rt<<1].lazy+=tree[rt].lazy;
30         tree[rt<<1|1].lazy+=tree[rt].lazy;
31         tree[rt<<1].sum+=(mid-L+1)*tree[rt].lazy;
32         tree[rt<<1|1].sum+=(R-mid)*tree[rt].lazy;
33         tree[rt].lazy=0;
34     }
35 }
36 void update(int x,int y,int z,lrrt){
37     if(x<=L&&R<=y){
38         tree[rt].sum+=(R-L+1)*z;
39         tree[rt].lazy+=z;
40         return ;
41     }
42     imid;
43     pushdown(mid,L,R,rt);
44     if(mid>=x) update(x,y,z,lson);
45     if(mid<y)  update(x,y,z,rson);
46     pushup(rt);
47 }
48 LL query(int x,int y,lrrt){
49     if(x<=L&&R<=y) return tree[rt].sum;
50     imid;
51     pushdown(mid,L,R,rt);
52     LL ans=0;
53     if(mid>=x) ans+=query(x,y,lson);
54     if(mid<y)  ans+=query(x,y,rson);
55     return ans;
56 }
57 int main(){
58     int n,m;
59     while(~scanf("%d%d",&n,&m)){
60         for(int i=1;i<=n;i++){
61             scanf("%d",&a[i]);
62         }
63         build(iall);
64         while(m--){
65             char op[4];
66             int x,y,z;
67             scanf("%s%d%d",op,&x,&y);
68             if(op[0]=='C'){
69                 scanf("%d",&z);
70                 update(x,y,z,iall);
71             }
72             else{
73                 printf("%I64d\n",query(x,y,iall));
74             }
75         }
76     }
77     return 0;
78 }
View Code

 Mayor's posters http://poj.org/problem?id=2528

离散化

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<map>
 5 #define mt(a,b) memset(a,b,sizeof(a))
 6 #define lrrt int L,int R,int rt
 7 #define iall 1,n,1
 8 #define imid int mid=(L+R)>>1
 9 #define lson L,mid,rt<<1
10 #define rson mid+1,R,rt<<1|1
11 using namespace std;
12 const int M=10000010;
13 struct Input{
14     int x,y;
15 }in[10010];
16 int tos[20010];
17 int tohash[M];
18 int tree[M<<2];
19 void build(lrrt){
20     tree[rt]=0;
21     if(L==R) return ;
22     imid;
23     build(lson);
24     build(rson);
25 }
26 void pushdown(int rt){
27     if(tree[rt]){
28         tree[rt<<1]=tree[rt];
29         tree[rt<<1|1]=tree[rt];
30         tree[rt]=0;
31     }
32 }
33 void update(int x,int y,int z,lrrt){
34     if(x<=L&&R<=y){
35         tree[rt]=z;
36         return ;
37     }
38     pushdown(rt);
39     imid;
40     if(mid>=x) update(x,y,z,lson);
41     if(mid<y)  update(x,y,z,rson);
42 }
43 bool vis[M];
44 void query(lrrt){
45     if(L==R){
46         vis[tree[rt]]=true;
47         return ;
48     }
49     pushdown(rt);
50     imid;
51     query(lson);
52     query(rson);
53 }
54 int main(){
55     int t,m;
56     while(~scanf("%d",&t)){
57         while(t--){
58             scanf("%d",&m);
59             int lt=0;
60             for(int i=1;i<=m;i++){
61                 scanf("%d%d",&in[i].x,&in[i].y);
62                 tos[lt++]=in[i].x;
63                 tos[lt++]=in[i].y;
64             }
65             sort(tos,tos+lt);
66             lt=unique(tos,tos+lt)-tos;
67             int n=1;
68             for(int i=0;i<lt;i++){
69                 tohash[tos[i]]=n;
70                 n++;
71                 if(tos[i+1]>tos[i]+1) n++;
72             }
73             build(iall);
74             for(int i=1;i<=m;i++){
75                 update(tohash[in[i].x],tohash[in[i].y],i,iall);
76             }
77             mt(vis,0);
78             query(iall);
79             int ans=0;
80             for(int i=1;i<=m;i++){
81                 if(vis[i]) ans++;
82             }
83             printf("%d\n",ans);
84         }
85     }
86     return 0;
87 }
View Code

 

 

 

end

posted on 2014-07-30 21:53  gaolzzxin  阅读(143)  评论(0编辑  收藏  举报