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
| #include <bits/stdc++.h> #define ll long long using namespace std;
const int N = 5e4 + 66;
int n, m;
struct SEGMENTTREE { int lmax, rmax, dat, tag, len; }tree[N << 2];
inline void up(int p) { int l = p << 1, r = (p << 1) | 1; tree[p].lmax = (tree[l].lmax == tree[l].len) ? tree[l].lmax + tree[r].lmax : tree[l].lmax; tree[p].rmax = (tree[r].rmax == tree[r].len) ? tree[r].rmax + tree[l].rmax : tree[r].rmax; return (void)(tree[p].dat = max(max(tree[l].dat, tree[r].dat), tree[l].rmax + tree[r].lmax)); }
inline void down(int p) { if (! tree[p].tag) return; int l = p << 1, r = (p << 1) | 1, c = tree[p].tag; tree[l].tag = tree[r].tag = c; tree[l].dat = tree[l].lmax = tree[l].rmax = (c == 1) ? tree[l].len : 0; tree[r].dat = tree[r].lmax = tree[r].rmax = (c == 1) ? tree[r].len : 0; return (void)(tree[p].tag = 0); }
inline void build(int p, int l, int r) { tree[p].dat = tree[p].lmax = tree[p].rmax = tree[p].len = r - l + 1; if (l == r) return; int mid = (l + r) >> 1; build(p << 1, l, mid), build((p << 1) | 1, mid + 1, r); return (void)(up(p)); }
inline void updata(int p, int l, int r, int nl, int nr, int c) { if (nl <= l && nr >= r) { tree[p].dat = tree[p].lmax = tree[p].rmax = (c == 1) ? tree[p].len : 0; return (void)(tree[p].tag = c); } down(p); int mid = (l + r) >> 1; if (nl <= mid) updata(p << 1, l, mid, nl, nr, c); if (nr > mid) updata((p << 1) | 1, mid + 1, r, nl, nr, c); return (void)(up(p)); }
inline int query(int p, int l, int r, int c) { if (l == r) return l; down(p); int mid = (l + r) >> 1; if (tree[p << 1].dat >= c) return query(p << 1, l, mid, c); if (tree[p << 1].rmax + tree[(p << 1) | 1].lmax >= c) return mid - tree[p << 1].rmax + 1; return query((p << 1) | 1, mid + 1, r, c); }
signed main() { int i, x, y, opt; n = read(), m = read(); build(1, 1, n);
for (i = 1; i <= m; ++ i) { opt = read(), x = read(); if (opt == 1) { if (tree[1].dat < x) put(0); else { int id = query(1, 1, n, x); updata(1, 1, n, id, id + x - 1, 2); put(id); } } if (opt == 2) y = read(), updata(1, 1, n, x, x + y - 1, 1); }
return 0; }
|