标签:des style blog io ar color os sp java
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 985 Accepted Submission(s): 247
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 typedef long long ll; 7 const int maxn = 2e5+100; 8 int lson[maxn*20],rson[20*maxn],c[maxn*20]; 9 int tree[maxn],tot,idx,maxv; 10 int build (int l,int r) 11 { 12 int root = tot++; 13 c[root] = 0; 14 if (l != r) 15 { 16 int mid =(l + r) >> 1; 17 lson[root] = build(l,mid); 18 rson[root] = build(mid+1,r); 19 } 20 return root; 21 } 22 int update(int root,int pos,int val) 23 { 24 int newroot = tot++; 25 int tmp = newroot; 26 int l = 1,r = maxv; 27 c[newroot] = c[root] + val; 28 while (l < r) 29 { 30 int mid = (l + r) >> 1; 31 if (pos <= mid) 32 { 33 rson[newroot] = rson[root]; 34 root = lson[root]; 35 lson[newroot] = tot++; 36 newroot = lson[newroot]; 37 r = mid; 38 } 39 else 40 { 41 lson[newroot] = lson[root]; 42 root = rson[root]; 43 rson[newroot] = tot++; 44 newroot = rson[newroot]; 45 l = mid + 1; 46 } 47 c[newroot] = c[root] + val; 48 } 49 return tmp; 50 } 51 int query(int left,int right,int k) 52 { 53 int l_root = tree[left-1]; 54 int r_root = tree[right]; 55 int l = 1, r = maxv; 56 while (l < r) 57 { 58 int mid = (l + r) >> 1; 59 if (c[lson[r_root]] - c[lson[l_root]] >= k) 60 { 61 r = mid; 62 l_root = lson[l_root]; 63 r_root = lson[r_root]; 64 } 65 else 66 { 67 l = mid + 1; 68 k -= c[lson[r_root]] - c[lson[l_root]]; 69 l_root = rson[l_root]; 70 r_root = rson[r_root]; 71 } 72 } 73 return l; 74 } 75 int query(int root,int l,int r,int k) 76 { 77 if (l == r) 78 return l; 79 int mid = (l + r) >> 1; 80 if (k <= mid) 81 return query(lson[root],l,mid,k); 82 else 83 return c[lson[root]] + query(rson[root],mid+1,r,k); 84 } 85 ll bit_arr[maxn]; 86 inline int lowbit (int x) 87 { 88 return x & -x; 89 } 90 void ADD(int x) 91 { 92 while (x < maxn) 93 { 94 bit_arr[x]++; 95 x += lowbit (x); 96 } 97 } 98 ll get_sum(int x) 99 { 100 ll res = 0; 101 while (x) 102 { 103 res += bit_arr[x]; 104 x -= lowbit (x); 105 } 106 return res; 107 } 108 struct 109 { 110 int x,y,k,flag; 111 }Q[maxn]; 112 ll vec[maxn]; 113 int main(void) 114 { 115 #ifndef ONLINE_JUDGE 116 freopen("in.txt","r",stdin); 117 #endif // ONLINE_JUDGE 118 int n,cas = 1; 119 while (~scanf ("%d",&n)) 120 { 121 memset(bit_arr,0,sizeof(bit_arr)); 122 printf("Case %d:\n",cas++); 123 tot = idx = 0; 124 for (int i = 0; i < n; i++) 125 { 126 char op[15]; 127 scanf ("%s",op); 128 if (strcmp(op,"Insert") == 0) 129 { 130 int x; 131 scanf ("%d",&x); 132 Q[i].flag = 0; 133 Q[i].x = x; 134 vec[idx++] = x; 135 } 136 if (strcmp(op,"Query_1") == 0) 137 { 138 int u,v,k; 139 scanf ("%d%d%d",&u,&v,&k); 140 Q[i].flag = 1; 141 Q[i].x = u,Q[i].y = v,Q[i].k = k; 142 } 143 if (strcmp(op,"Query_2") == 0) 144 { 145 int x; 146 scanf ("%d",&x); 147 Q[i].flag = 2; 148 Q[i].x = x; 149 } 150 if (strcmp(op,"Query_3") == 0) 151 { 152 int k; 153 scanf ("%d",&k); 154 Q[i].flag = 3; 155 Q[i].x = k; 156 } 157 } 158 sort(vec,vec+idx); 159 idx = unique(vec,vec+idx) - vec; 160 maxv = idx ; 161 tree[0] = build(1,maxv); 162 int now = 1; 163 ll ans1 = 0, ans2 = 0, ans3 = 0; 164 for (int i = 0; i < n; i++) 165 { 166 if (Q[i].flag == 0) 167 { 168 int tmp = lower_bound(vec,vec+idx,Q[i].x) - vec + 1; 169 tree[now] = update(tree[now-1],tmp,1); 170 ADD(tmp); 171 now++; 172 } 173 if (Q[i].flag == 1) 174 { 175 ans1 += vec[query(Q[i].x,Q[i].y,Q[i].k)-1]; 176 } 177 if (Q[i].flag == 2) 178 { 179 int tmp = lower_bound(vec,vec+idx,Q[i].x) - vec + 1; 180 ans2 += get_sum(tmp); 181 } 182 if (Q[i].flag == 3) 183 { 184 ans3 += vec[query(1,now-1,Q[i].x)-1]; 185 } 186 } 187 printf("%I64d\n%I64d\n%I64d\n",ans1,ans2,ans3); 188 } 189 return 0; 190 }
标签:des style blog io ar color os sp java
原文地址:http://www.cnblogs.com/oneshot/p/4129284.html