SHOW:
|
|
- or go back to the newest paste.
1 | /* CPP Tempelate | |
2 | * @author Devashish Tyagi | |
3 | */ | |
4 | ||
5 | #include <algorithm> | |
6 | #include <cctype> | |
7 | #include <cmath> | |
8 | #include <cstdio> | |
9 | #include <cstdlib> | |
10 | #include <cstring> | |
11 | #include <iostream> | |
12 | #include <map> | |
13 | #include <queue> | |
14 | #include <set> | |
15 | #include <sstream> | |
16 | #include <stack> | |
17 | #include <string> | |
18 | #include <vector> | |
19 | - | #include <list> |
19 | + | |
20 | using namespace std; | |
21 | - | #define s(a) scanf("%d",&a) |
21 | + | |
22 | - | #define ss(a,b) scanf("%d %d",&a,&b) |
22 | + | |
23 | - | #define p(a) printf("%d\n",a) |
23 | + | struct Node{ |
24 | - | #define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++) |
24 | + | |
25 | - | #define pi pair<int,int> |
25 | + | |
26 | - | #define vi vector<int> |
26 | + | |
27 | - | #define all(v) v.begin(),v.end() |
27 | + | |
28 | Node(){ | |
29 | - | #define PB push_back |
29 | + | p[0] = 0; |
30 | - | #define MP make_pair |
30 | + | p[1] = 0; |
31 | - | #define sz(a) (int)(a).size() |
31 | + | |
32 | } | |
33 | - | #define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i)) |
33 | + | |
34 | - | #define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i)) |
34 | + | |
35 | - | #define CLEAR(a) memset((a),0,sizeof(a)) |
35 | + | |
36 | case ' ': upd = 'I';break; | |
37 | - | #define INF 100000000 |
37 | + | |
38 | - | #define PI 2*acos(0.0) |
38 | + | |
39 | case 'F': upd = 'E';break; | |
40 | } | |
41 | } | |
42 | else if (cmd != ' '){ | |
43 | - | string convertInt(int number) |
43 | + | |
44 | - | { |
44 | + | |
45 | - | stringstream ss;//create a stringstream |
45 | + | |
46 | - | ss << number;//add number to the stream |
46 | + | |
47 | - | return ss.str();//return a string with the contents of the stream |
47 | + | |
48 | switch(upd){ | |
49 | case 'I': temp=p[0]; p[0]=p[1]; p[1]=temp; break; | |
50 | - | int convertString(string s){ |
50 | + | |
51 | - | int num; |
51 | + | |
52 | - | stringstream sstr(s); // create a stringstream |
52 | + | |
53 | - | sstr>>num; // push the stream into the num |
53 | + | |
54 | - | return num; |
54 | + | |
55 | }; | |
56 | ||
57 | - | class Node{ |
57 | + | Node tree[4028000]; |
58 | - | public: |
58 | + | char pirates[1024010]; |
59 | ||
60 | void buildTree(int index, int i, int j){ | |
61 | if (i>j) | |
62 | return; | |
63 | if (i==j){ | |
64 | - | CLEAR(p); |
64 | + | |
65 | tree[index].p[1] = 1; | |
66 | tree[index].p[0] = 0; | |
67 | } | |
68 | else{ | |
69 | tree[index].p[0] = 1; | |
70 | tree[index].p[1] = 0; | |
71 | } | |
72 | tree[index].upd = ' '; | |
73 | return; | |
74 | } | |
75 | int mid = (i+j)/2; | |
76 | buildTree(2*index+1,i,mid); | |
77 | buildTree(2*index+2,mid+1,j); | |
78 | tree[index].p[0] = tree[2*index+1].p[0] + tree[2*index+2].p[0]; | |
79 | tree[index].p[1] = tree[2*index+1].p[1] + tree[2*index+2].p[1]; | |
80 | tree[index].upd = ' '; | |
81 | - | //cout<<"before"<<upd<<" "<<p[0]<<" "<<p[1]<<endl; |
81 | + | |
82 | ||
83 | void update(int index,int i,int j,int l,int r,char c){ | |
84 | if (i == l && j == r){ | |
85 | tree[index].update(c); | |
86 | } | |
87 | tree[2*index+1].update(tree[index].upd); | |
88 | tree[2*index+2].update(tree[index].upd); | |
89 | - | //cout<<"after"<<upd<<" "<<p[0]<<" "<<p[1]<<endl; |
89 | + | |
90 | if (i == l && j ==r) | |
91 | return; | |
92 | if (l>j || r<i || l > r) | |
93 | - | vector<Node> tree(2048100); |
93 | + | |
94 | - | char pirates[1024001]; |
94 | + | |
95 | update(2*index+1,i,mid,l,min(r,mid),c); | |
96 | update(2*index+2,mid+1,j,max(l,mid+1),r,c); | |
97 | tree[index].p[0] = tree[2*index+1].p[0] + tree[2*index+2].p[0]; | |
98 | tree[index].p[1] = tree[2*index+1].p[1] + tree[2*index+2].p[1]; | |
99 | } | |
100 | ||
101 | int query(int index,int i,int j,int l,int r){ | |
102 | if (l>j || r<i) | |
103 | return 0; | |
104 | tree[2*index+1].update(tree[index].upd); | |
105 | tree[2*index+2].update(tree[index].upd); | |
106 | tree[index].S(); | |
107 | if (i == l && j == r){ | |
108 | return tree[index].p[0]; | |
109 | } | |
110 | int mid = (i+j)/2; | |
111 | int b1 = query(2*index+1,i,mid,l,min(mid,r)); | |
112 | int b2 = query(2*index+2,mid+1,j,max(mid+1,l),r); | |
113 | return b1+b2; | |
114 | } | |
115 | ||
116 | int main(void){ | |
117 | int t; | |
118 | scanf("%d",&t); | |
119 | for(int z=0; z<t; z++){ | |
120 | int m,len=0,T,q,querycnt=1; | |
121 | char str[64]; | |
122 | scanf("%d",&m); | |
123 | for(int i=0; i<m; i++){ | |
124 | scanf("%d %s",&T,str); | |
125 | for(int j=0; j<T; j++){ | |
126 | int length = strlen(str); | |
127 | for(int k=0; k<length; k++){ | |
128 | pirates[len] = str[k]; | |
129 | len++; | |
130 | } | |
131 | } | |
132 | } | |
133 | buildTree(0,0,len-1); | |
134 | scanf("%d",&q); | |
135 | printf("Case %d:\n",z+1); | |
136 | for(int i=0; i<q; i++){ | |
137 | char cmd; | |
138 | int l,r; | |
139 | scanf(" %c %d %d", &cmd, &l, &r); | |
140 | if (cmd == 'S'){ | |
141 | int ans = query(0,0,len-1,l,r); | |
142 | printf("Q%d: %d\n",querycnt++,ans); | |
143 | } | |
144 | else{ | |
145 | update(0,0,len-1,l,r,cmd); | |
146 | } | |
147 | } | |
148 | } | |
149 | return 0; | |
150 | } |