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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| #include<bits/stdc++.h> using namespace std; const int N =5e4+10; #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 int n,m; struct node { int l,r; int val; bool put; bool in; int len() { return r-l+1; } }tree[N<<2]; void PushUp(int rt) { tree[rt].val=(tree[rt<<1].val+tree[rt<<1|1].val); } void PushDown(int rt) { if(tree[rt].put) { tree[rt<<1].put=tree[rt<<1|1].put=tree[rt].put; tree[rt<<1].in=tree[rt<<1|1].in=false; tree[rt<<1|1].val=tree[rt<<1].val=0; tree[rt].put=0; } if(tree[rt].in) { tree[rt<<1].in=tree[rt<<1|1].in=tree[rt].in; tree[rt<<1].put=tree[rt<<1|1].put=false; tree[rt<<1].val=tree[rt<<1].len(); tree[rt<<1|1].val=tree[rt<<1|1].len(); tree[rt].in=0; } } void build(int l,int r,int rt) { tree[rt].l=l; tree[rt].r=r; tree[rt].val=0; tree[rt].put=tree[rt].in=false; if(l==r) { tree[rt].val=0; return ; } int mid = (l+r)>>1; build(lson); build(rson); PushUp(rt); } void Update(int rt,int l,int r) { if(tree[rt].r<l||tree[rt].l>r)return ; if(tree[rt].l>=l&&tree[rt].r<=r) { tree[rt].put=0; tree[rt].in=1; tree[rt].val=tree[rt].len(); return ; } PushDown(rt); Update(rt<<1,l,r); Update(rt<<1|1,l,r); PushUp(rt); } void clear(int rt,int l,int r) { if(tree[rt].r<l||tree[rt].l>r)return ; if(tree[rt].l>=l&&tree[rt].r<=r) { tree[rt].put=1; tree[rt].in=0; tree[rt].val=0; return ; } PushDown(rt); clear(rt<<1,l,r); clear(rt<<1|1,l,r); PushUp(rt); } int query(int rt,int l,int r) { if(tree[rt].r<l||tree[rt].l>r)return 0; int ans = 0; if(tree[rt].l>=l&&tree[rt].r<=r) { return tree[rt].val; } PushDown(rt); ans+=query(rt<<1,l,r); ans+=query(rt<<1|1,l,r); return ans; } int bin(int s,int rank) { int l = s; int r = n; int ans = -1; int mid; while(l <= r) { int mid = (l + r)>>1; int tmp = query(1,s,mid); if(tmp + rank == mid - s + 1) { ans = mid; r = mid - 1; } else { if(tmp + rank < mid - s + 1) r = mid - 1; else l = mid + 1; } } return ans; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&m); build(1,n,1); while(m--) { int k,a,b; scanf("%d %d %d",&k,&a,&b); if(k==1&&a==2&&b==3) { k=1; } if(k==1) { a++; a=bin(a,1); if(a==-1)printf("Can not put any one.\n"); else{ int tem = query(1,a,n); tem = n-a+1-tem; if(b>=tem) b=tem; int res = bin(a,b); printf("%d %d\n",a-1,res-1); Update(1,a,res); } } else { a++,b++; printf("%d\n",query(1,a,b)); clear(1,a,b); } } printf("\n"); } }
|