1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #include<bits/stdc++.h> using namespace std; #define ll long long const int N = 1000010; int n, m; ll a[N]; struct SegTree { int l, r; int ls, rs; ll sum; ll tot; }tr[N << 3]; int idx; void segadd(int p, int pos, ll k) { if(tr[p].l == tr[p].r) { tr[p].sum += k; tr[p].tot = tr[p].sum * tr[p].l; return; } int mid = (tr[p].l + tr[p].r) >> 1; if(pos <= mid) { if(!tr[p].ls) { tr[p].ls = ++idx; tr[tr[p].ls].l = tr[p].l; tr[tr[p].ls].r = mid; } segadd(tr[p].ls, pos, k); } else if(pos > mid) { if(!tr[p].rs) { tr[p].rs = ++idx; tr[tr[p].rs].l = mid + 1; tr[tr[p].rs].r = tr[p].r; } segadd(tr[p].rs, pos, k); } tr[p].sum = tr[tr[p].ls].sum + tr[tr[p].rs].sum; tr[p].tot = tr[tr[p].ls].tot + tr[tr[p].rs].tot; return; }
ll segsum(int p, int l, int r) { if(!p)return 0; if(tr[p].l >= l && tr[p].r <= r)return tr[p].sum; int mid = (tr[p].l + tr[p].r) >> 1; ll res = 0; if(l <= mid)res += segsum(tr[p].ls, l, r); if(r > mid)res += segsum(tr[p].rs, l, r); return res; } ll segtot(int p, int l, int r) { if(!p)return 0; if(tr[p].l >= l && tr[p].r <= r)return tr[p].tot; int mid = (tr[p].l + tr[p].r) >> 1; ll res = 0; if(l <= mid)res += segtot(tr[p].ls, l, r); if(r > mid)res += segtot(tr[p].rs, l, r); return res; } int main() { scanf("%d%d", &n, &m); int maxn = 0; tr[++idx] = { 0,100000001,0,0,0,0 }; while(m--) { string op; int x, k; cin >> op; scanf("%d%d", &x, &k); if(op[0] == 'U') { if(k > 0) { segadd(1, k, 1); } if(a[x] > 0) { segadd(1, a[x], -1); } a[x] = k; maxn = max(maxn, k); } else if(op[0] == 'Z') { ll pos = segsum(1, 1, maxn), cnt = segsum(1, 1, k); if(pos < x) { puts("NIE"); continue; } if(pos - cnt >= x) { puts("TAK"); continue; } ll tot = segtot(1, 1, k); if(tot >= (x + cnt - pos) * k) puts("TAK"); else puts("NIE"); } } return 0; }
|