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
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <iomanip> using namespace std;
const int N = 5e4+10; int n, k;
int sum[N*4], lsum[N*4], rsum[N*4]; int A[N*4]; void buildtree(int k1, int l, int r) { sum[k1] = lsum[k1] = rsum[k1] = r-l+1; A[k1] = -1; if(l == r) return; int mid = l+r >> 1; buildtree(k1*2, l, mid); buildtree(k1*2+1, mid+1, r); }
void change(int k1, int val) { lsum[k1] = lsum[k1*2]; rsum[k1] = rsum[k1*2+1]; if(lsum[k1*2] == val-val/2) { lsum[k1] += lsum[k1*2+1]; } if(rsum[k1*2+1] == val/2) { rsum[k1] += rsum[k1*2]; } sum[k1] = max(lsum[k1*2+1]+rsum[k1*2], max(sum[k1*2], sum[k1*2+1])); }
void pushdown(int k1, int val) { if(A[k1] != -1) { A[k1*2] = A[k1*2+1] = A[k1]; sum[k1*2] = lsum[k1*2] = rsum[k1*2] = A[k1]?0:val-val/2; sum[k1*2+1] = lsum[k1*2+1] = rsum[k1*2+1] = A[k1]?0:val/2; A[k1] = -1; } }
void update(int k1, int l, int r, int L, int R, int val) { if(L <= l && r <= R) { A[k1] = val; if(val) sum[k1] = rsum[k1] = lsum[k1] = 0; else if(val == 0) { sum[k1] = rsum[k1] = lsum[k1] = r-l+1; } return; } int mid = l+r >> 1; pushdown(k1, r-l+1); if(L <= mid) update(k1*2, l, mid, L, R, val); if(R > mid) update(k1*2+1, mid+1, r, L, R, val); change(k1, r-l+1);
}
int Find(int k1, int l, int r, int val) { if(l == r) return l; pushdown(k1, r-l+1); int mid = l+r >> 1; if(val <= sum[k1*2]) { return Find(k1*2, l, mid, val); } else if(rsum[k1*2] + lsum[k1*2+1] >= val) { return mid-rsum[k1*2]+1; } return Find(k1*2+1, mid+1, r, val); }
int main() { while(scanf("%d%d", &n, &k) != EOF) { buildtree(1, 1, n); while(k--) { int op, a; scanf("%d%d", &op, &a); if (op == 1) { if(sum[1] < a) { printf("0\n"); continue; } int p = Find(1, 1, n, a); printf("%d\n", p); update(1, 1, n, p, p+a-1, 1); } else { int b; scanf("%d", &b); update(1, 1, n, a, a+b-1, 0); } } }
return 0; }
|