Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3263 Accepted Submission(s): 1299
Problem Description
Alice
is so popular that she can receive many flowers everyday. She has N
vases numbered from 0 to N-1. When she receive some flowers, she will
try to put them in the vases, one flower in one vase. She randomly
choose the vase A and try to put a flower in the vase. If the there is
no flower in the vase, she will put a flower in it, otherwise she skip
this vase. And then she will try put in the vase A+1, A+2, ..., N-1,
until there is no flower left or she has tried the vase N-1. The left
flowers will be discarded. Of course, sometimes she will clean the
vases. Because there are too many vases, she randomly choose to clean
the vases numbered from A to B(A <= B). The flowers in the cleaned
vases will be discarded.
Input
The first line contains an integer T, indicating the number of test cases.
For
each test case, the first line contains two integers N(1 < N <
50001) and M(1 < M < 50001). N is the number of vases, and M is
the operations of Alice. Each of the next M lines contains three
integers. The first integer of one line is K(1 or 2). If K is 1, then
two integers A and F follow. It means Alice receive F flowers and try to
put a flower in the vase A first. If K is 2, then two integers A and B
follow. It means the owner would like to clean the vases numbered from A
to B(A <= B).
Output
For
each operation of which K is 1, output the position of the vase in
which Alice put the first flower and last one, separated by a blank. If
she can not put any one, then output ‘Can not put any one.‘. For each
operation of which K is 2, output the number of discarded flowers.
Output one blank line after each test case.
Sample Input
2
10 5
1 3 5
2 4 5
1 1 8
2 3 6
1 8 8
10 6
1 2 5
2 3 4
1 0 8
2 2 5
1 4 4
1 2 3
Sample Output
3 7
2
1 9
4
Can not put any one.
2 6
2
0 9
4
4 5
2 3
题意:
操作1:从x位置往后插y枝花,前提是只能给空花瓶插,并求出最左边和最右边插花的位置。
操作2:求[x, y]内有多少只花,并清空区间内的花瓶。
题解:
因为每个花瓶只能插一枝花,所以用区间和可以判断能插几只花。
对于操作1,我们可以判断[x, n]的空花瓶数cnt,如果为0,直接GG;否则看它能插最多y枝花还是cnt枝花。
通过二分可以找到这个l, r的位置;
对于操作2,就是区间求和,区间更新,裸的。
代码:
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <bitset> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <cmath> 10 #include <list> 11 #include <set> 12 #include <map> 13 #define rep(i,a,b) for(int i = a;i <= b;++ i) 14 #define per(i,a,b) for(int i = a;i >= b;-- i) 15 #define mem(a,b) memset((a),(b),sizeof((a))) 16 #define FIN freopen("in.txt","r",stdin) 17 #define FOUT freopen("out.txt","w",stdout) 18 #define IO ios_base::sync_with_stdio(0),cin.tie(0) 19 #define mid ((l+r)>>1) 20 #define ls (id<<1) 21 #define rs ((id<<1)|1) 22 using namespace std; 23 typedef long long LL; 24 typedef pair<int, int> PIR; 25 const int N = 50005; 26 struct Node{ 27 int sum, lazy; 28 }node[N*4]; 29 int T, n, m, op, x, y; 30 31 void pushUp(int id, int l, int r){ 32 node[id].sum = node[ls].sum+node[rs].sum; 33 } 34 void pushDown(int id, int l, int r){ 35 node[ls].sum = node[id].lazy*(mid-l+1); 36 node[rs].sum = node[id].lazy*(r-mid); 37 node[ls].lazy = node[id].lazy; 38 node[rs].lazy = node[id].lazy; 39 node[id].lazy = -1; 40 } 41 void build(int id, int l, int r){ 42 node[id].lazy = -1; 43 if(l == r){ 44 node[id].sum = 0; 45 return ; 46 } 47 build(ls, l, mid); 48 build(rs, mid+1, r); 49 pushUp(id, l, r); 50 } 51 void update(int id, int l, int r, int ql, int qr, int p){ 52 if(l == ql && r == qr){ 53 node[id].lazy = p; 54 node[id].sum = (r-l+1)*p; 55 return ; 56 } 57 if(node[id].lazy != -1) 58 pushDown(id, l, r); 59 if(qr <= mid) update(ls, l, mid, ql, qr, p); 60 else if(ql > mid) 61 update(rs, mid+1, r, ql, qr, p); 62 else{ 63 update(ls, l, mid, ql, mid, p); 64 update(rs, mid+1, r, mid+1, qr, p); 65 } 66 pushUp(id, l, r); 67 } 68 int query(int id, int l, int r, int ql, int qr){ 69 if(l == ql && r == qr){ 70 return node[id].sum; 71 } 72 if(node[id].lazy != -1) pushDown(id, l, r); 73 if(qr <= mid) return query(ls, l, mid, ql, qr); 74 else if(ql > mid) 75 return query(rs, mid+1, r, ql, qr); 76 else{ 77 return query(ls, l, mid, ql, mid)+query(rs, mid+1, r, mid+1, qr); 78 } 79 } 80 int main() 81 {IO; 82 //FIN; 83 cin >> T; 84 while(T--){ 85 cin >> n >> m; 86 build(1, 1, n); 87 rep(i, 1, m){ 88 cin >> op >> x >> y; 89 if(op == 1){ 90 x++; 91 int cnt = n-x+1-query(1, 1, n, x, n); 92 if(cnt == 0){ 93 cout << "Can not put any one." << endl; 94 continue; 95 } 96 cnt = min(cnt, y); 97 int low = x, high = n, ansl, ansr; 98 while(low <= high){ 99 int md = (low+high)>>1; 100 if(query(1, 1, n, x, md) < md-x+1){ 101 ansl = md; 102 high = md-1; 103 } 104 else{ 105 low = md+1; 106 } 107 } 108 low = x, high = n; 109 while(low <= high){ 110 int md = (low+high)>>1; 111 if(md-x+1-query(1, 1, n, x, md) >= cnt){ 112 ansr = md; 113 high = md-1; 114 } 115 else{ 116 low = md+1; 117 } 118 } 119 update(1, 1, n, ansl, ansr, 1); 120 cout << ansl-1 << " " << ansr-1 << endl; 121 } 122 else{ 123 x++; 124 y++; 125 int ans = query(1, 1, n, x, y); 126 update(1, 1, n, x, y, 0); 127 cout << ans << endl; 128 } 129 } 130 cout << endl; 131 } 132 return 0; 133 }
Author