//it can be done using segment tree, sqrt decomposition and vector
// Segment tree version
- #include <bits/stdc++.h>
- using namespace std;
- #define pi 2*acos(0.0)
- #define all(v) v.begin(),v.end()
- #define coff ios_base::sync_with_stdio(0);
- #define ff first
- #define se second
- #define pb push_back
- #define sz(a) ((int)a.size())
- #define ST(v) sort(all(v))
- #define gcd(a,b) __gcd(a,b)
- #define lcm(a,b) (a*(b/gcd(a,b)))
- #define max3(a,b,c) max(a,max(b,c))
- #define min3(a,b,c) min(a,min(b,c))
- #define cover(a,d) memset(a,d,sizeof(a))
- #define popcount(i) __builtin_popcount(i) //count one. in long long use __builtin_popcountll(i)
- #define parity(i) __builtin_parity(i) //evenparity 0 and odd parity 1
- #define btz(a) __builtin_ctz(a) //count binary trailling zero
- #define un(v) ST(v), (v).erase(unique(all(v)),v.end())
- #define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end())
- #define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end())
- typedef long long ll;
- typedef unsigned long long ull;
- template <typename T>string toString( T Number ){stringstream st;st << Number;return st.str();}
- int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;}
- //upper bound and lower bound
- #define LB(a,value) (lower_bound(all(a),value)-a.begin())
- #define UB(a,value) (upper_bound(all(a),value)-a.begin())
- //Debug
- #define dbg(x) cout<<#x<<'='<<x<<endl;
- #define dbgarr(i,a) cout<<#a<<"["<<i<<"] = "<<a[i]<<" "<<endl;
- #define nl puts("")
- //File input/output
- #define input freopen("in.txt","r",stdin)
- #define output freopen("out.txt","w",stdout)
- ll bigmod(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; }return res; }
- ll modInverse(ll a, ll m){return bigmod(a,m-2,m);}
- ////============ CONSTANT ===============////
- #define inf 1<<30 //infinity value
- #define eps 1e-9
- #define mx 100009
- #define mod 1000000007
- ////=====================================////
- struct info //for combine single update segment tree and range update segment tree
- {
- int sum,value,pos;
- info() : sum(0),value(0),pos(0){}
- }tree[6*mx];
- int limit;
- void update_node(int node, int i, int val,int visibility)
- {
- tree[node].pos = i;
- tree[node].value = val;
- tree[node].sum = visibility;
- }
- void marge(int node, int left, int right)
- {
- tree[node].sum= tree[left].sum + tree[right].sum;
- }
- void update(int node, int s, int e, int i, int val, int status)
- {
- if(s==e)
- {
- update_node(node,i,val,status);
- return;
- }
- int left=node<<1;
- int mid=(s+e)>>1;
- if(i<=mid)
- {
- update(left,s,mid,i,val,status);
- }
- else
- {
- update(left+1,mid+1,e,i,val,status);
- }
- marge(node,left,left+1);
- }
- int query(int node, int s, int e, int k)
- {
- if(k>tree[node].sum)
- {
- return -1;
- }
- if(s==e)
- {
- if(k!=tree[node].sum)
- return -1;
- else
- {
- tree[node].sum=0;
- update(1,1,limit,tree[node].pos,tree[node].value,0);
- return tree[node].value;
- }
- }
- int left=node<<1;
- int mid=(s+e)>>1;
- if(k<=tree[left].sum)
- {
- return query(left,s,mid,k);
- }
- else
- {
- return query(left+1,mid+1,e,k-tree[left].sum);
- }
- }
- int main()
- {
- int t,no=0;
- scanf("%d",&t);
- while(t--)
- {
- int n,q,a;
- scanf("%d %d",&n,&q);
- cover(tree,0);
- limit=n+q; // because of initialize segment tree node may not equal after query updating but this is highest limit of each input.
- for(int i=0;i<n;i++)
- {
- scanf("%d",&a);
- update(1,1,limit,i+1,a,1);
- }
- printf("Case %d:n",++no);
- while(q--)
- {
- char ch[3];
- scanf("%s %d",&ch,&a);
- if(ch[0]=='a')
- {
- n++;
- update(1,1,limit,n,a,1);
- }
- else
- {
- int res = query(1,1,limit,a);
- if(res==-1)
- {
- printf("nonen");
- }
- else
- {
- printf("%dn",res);
- }
- }
- }
- }
- return 0;
- }
No comments:
Post a Comment