- #include <bits/stdc++.h>
- using namespace std;
- #define MAXN 200005
- int input[MAXN], SQRSIZE;
- int arr[MAXN]; // original array
- int block[500]; // decomposed array
- int blk_sz, n, q, indx; // block size
- // Time Complexity : O(1)
- void update(int idx, int val)
- {
- int blockNumber = idx / blk_sz;
- block[blockNumber]--;
- arr[idx] = val;
- }
- void show(){for(int i = 0; i <indx; i++) cout<<arr[i]<<" ";
- cout<<"the array\n";
- }
- void add(int val)
- {
- int blockNumber = indx / blk_sz;
- block[blockNumber]++;
- arr[indx] = val;
- indx++;
- //show();
- }
- int query(int ix)
- {
- if(ix>=indx) return -1;
- int sum = 0,l = 0, ans = -1;
- while (sum+block[l] < ix)
- {
- sum+= block[l];
- l++;
- }
- int c = l*blk_sz;
- l = sum;
- while (c!=indx)
- {
- if(l == ix&& arr[c]!=-1 ) {ans = arr[c];arr[c] = -1;update(c, -1);return ans;}
- if(arr[c]!= -1) l++, c++;
- else c++;
- }
- return -1;
- }
- void preprocess(int input[], int n)
- {
- int blk_idx = -1;
- indx = n;
- for (int i=0; i<n; i++)
- {
- arr[i] = input[i];
- if (i%blk_sz == 0)
- {
- blk_idx++;
- }
- block[blk_idx]++;
- }
- }
- // Driver code
- int main()
- {
- int ts;
- scanf("%d", &ts);
- for(int p = 1; p <=ts; p++){
- printf("Case %d:\n", p);
- scanf("%d%d", &n, &q);
- for(int i = 0; i <n; i++) scanf("%d", &input[i]);
- SQRSIZE = n+q;
- blk_sz = (int)sqrt(n+q) + 1;
- //cout<<"block size"<<blk_sz<<" ";
- memset(arr, -1, sizeof(arr));
- preprocess(input, n);
- //for(int i = 0; i <n; i++)cout<<arr[i]<<" ";
- for(int i = 0; i <q; i++){
- char ch;
- getchar();
- scanf("%c", &ch);
- if(ch == 'a') {
- int id;
- scanf("%d", &id);
- add(id);
- }
- else {
- int id;
- scanf("%d", &id);
- int ans = query(id-1);
- if(ans == -1) printf("none\n");
- else printf("%d\n", ans);
- }
- }
- }
- return 0;
- }
- //may be a little bug but i can't get it
- /*
- 1 5 8
- 6 5 3 2 1
- c 1
- c 1
- a 20
- c 4
- c 4
- c 3
- c 2
- c 1
- */
Wednesday 26 July 2017
Lightoj 1087 Diablo (SQRT decomposition)
Subscribe to:
Post Comments (Atom)
Most Featured Post
Lightoj 1159 - Batman
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
-
Problem link: Problem Analysis: It is actually a basic Bisection problem , as we can see here we can not actually find a formula fo...
-
http://lightoj.com/volume_showproblem.php?problem=1382 Problem analysis: This is a rare problem i wrote about so far. After much strugg...
No comments:
Post a Comment