Poj--2777(数据结构,线段树)

2014-09-24 20:52:31

思路:经典的区间赋值+计数。自己搞搞就出来了。

 1 /*************************************************************************
 2     > File Name: p2777.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Tue 23 Sep 2014 11:59:27 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <queue>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 typedef long long ll;
18 const int INF = 1 << 29;
19 const int maxn = 100005;
20 
21 int L,T,O,a,b,c;
22 int col[maxn << 2],setc[maxn << 2];
23 
24 void Build_tree(int pos,int l,int r){
25     for(int i = 0; i < L * 4; ++i)
26         col[i] = 1;
27     memset(setc,0,sizeof(setc));
28 }
29 
30 void Push_down(int pos){
31     if(setc[pos]){
32         setc[pos << 1] = setc[pos << 1 | 1] = setc[pos];
33         col[pos << 1] = col[pos << 1 | 1] = col[pos];
34         setc[pos] = 0;
35     }
36 }
37 
38 void Update(int pos,int l,int r){
39     if(a <= l && r <= b){
40         setc[pos] = c;
41         col[pos] = (1 << (c - 1));
42         return;
43     }
44     Push_down(pos);
45     int mid = l + (r - l) / 2;
46     if(a <= mid) Update(pos << 1,l,mid);
47     if(b > mid) Update(pos << 1 | 1,mid + 1,r);
48     col[pos] = col[pos << 1] | col[pos << 1 | 1];
49 }
50 
51 int Query(int pos,int l,int r){
52     if(setc[pos] || (a <= l && r <= b))
53         return col[pos];
54     else{
55         int res = 0,mid = l + (r - l) / 2;
56         if(a <= mid) res |= Query(pos << 1,l,mid);
57         if(b > mid) res |= Query(pos << 1 | 1,mid + 1,r);
58         return res;
59     }
60 }
61 
62 int main(){
63     char s[5];
64     scanf("%d%d%d",&L,&T,&O);
65     Build_tree(1,1,L);
66     while(O--){
67         scanf("%s%d%d",s,&a,&b);
68         if(a > b) swap(a,b);
69         if(s[0] == 'C'){
70             scanf("%d",&c);
71             Update(1,1,L);
72         }
73         else{
74             int ans = Query(1,1,L),cnt = 0;
75             while(ans){
76                 cnt += (ans & 1);
77                 ans >>= 1;
78             }
79             printf("%d\n",cnt);
80         }
81     }
82     return 0;
83 }

 

posted @ 2014-09-24 20:53  Naturain  阅读(136)  评论(0)    收藏  举报