Untitled
#include <bits/stdc++.h> using namespace std; #define all(v) ((v).begin()), ((v).end()) #define yes cout << "YES" << '\n'; #define no cout << "NO" << '\n'; //#define endl '\n' typedef long long ll; const int N=1e5+5; ll mod=1e9+7; #define int long long ll n1; ll mrg (ll x ,ll y ) { return (x^y); } struct segment_tree { vector<ll> tree; void clear() { tree.clear(); } void init(ll num, const vector<ll>& a) { n1 = num; tree.assign(4 * n1, 0); build(a); } void build(const vector<ll>& a, int id=0,int ns = 0, int ne = n1-1) { if(ns==ne){ tree[id] = a[ns]; return ; } int l = 2*id+1; int r = l+1; int md = ns+(ne-ns)/2; build(a,l, ns, md); build(a,r, md+1, ne); tree[id] = mrg(tree[l],tree[r]); } ll query(int qs, int qe, int id=0, int ns=0, int ne=n1-1) { if(ns>qe || qs>ne){ return 0; ///infnity } if(qs<=ns && qe>=ne){ return tree[id]; } int l = 2*id+1; int r = l+1; int md = ns+(ne-ns)/2; ll ndl = query(qs, qe, l, ns, md); ll ndr = query(qs, qe,r, md+1,ne); return mrg(ndl,ndr ); } void upd(int pos , int val , int id=0, int ns=0,int ne=n1-1) { if(ns>pos || pos>ne){ return; } if(ns==ne){ tree[id]=val; return ; } int l = 2*id+1; int r = l+1; int md = ns+(ne-ns)/2; upd(pos, val,l, ns, md); upd(pos, val, r, md+1, ne); tree[id] = mrg(tree[l],tree[r]); } } ; pair<ll,ll> query(ll i,ll j) { cout << i << ' ' << j << endl; cout.flush(); pair<ll,ll> pq={0,0}; cin >> pq.first >> pq.second; return pq; } void solve() { //multiset<pair<ll,ll>> ms; ll n; cin >> n; vector<ll> v(n); set<int> m[41]; for (int i=0;i<n;i++) { cin >> v[i]; for (int b=0;b<=40;++b) { if ((1ll<<b)&v[i]) { m[b].insert(i); } } } ll xr=0; pair<ll,ll> q={0,0}; segment_tree st; st.init(n,v); xr=st.query(0,n-1); if (xr==0) { q=query(0,0); } else { ll bit = log2(xr); auto it = m[bit].end(); --it; ll id = *it; ll ele = v[id]; for (int b = 0; b <= 40; ++b) { if ((1ll<<b) & v[id]) { m[b].erase(id); } } xr = st.query(0, id-1) ^ st.query(id+1, n-1); st.upd(id, xr); v[id] = xr; if (xr != 0) { for (int b = 0; b <= 40; ++b) { if ((1ll<<b) & xr) { m[b].insert(id); } } } q = query(id+1, ele-xr); } while(1) { if (q.first == 0 && q.second == 0) { return; } ll id = q.first-1; ll removed = q.second; ll e = v[id]; for (int b = 0; b <= 40; ++b) { if ((1ll<<b) & e) { m[b].erase(id); } } v[id] -= removed; st.upd(id, v[id]); if (v[id] > 0) { for (int b = 0; b <= 40; ++b) { if ((1ll<<b) & v[id]) { m[b].insert(id); } } } xr = st.query(0, n-1); ll bit = log2(xr); auto it = m[bit].end(); --it; id = *it; ll ele = v[id]; for (int b = 0; b <= 40; ++b) { if ((1ll<<b) & v[id]) { m[b].erase(id); } } xr = st.query(0, id-1) ^ st.query(id+1, n-1); st.upd(id, xr); v[id] = xr; if (xr != 0) { for (int b = 0; b <= 40; ++b) { if ((1ll<<b) & xr) { m[b].insert(id); } } } q = query(id+1, ele-xr); } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //cout << setprecision(10) << fixed; int testcases = 1; cin >> testcases; while (testcases--) { solve(); } return 0; }
Leave a Comment