HDU-4614 Vases and Flowers (线段树+二分)

来源:互联网 发布:淘宝网大毛衣外套 编辑:程序博客网 时间:2024/06/06 07:43

Vases and Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3891    Accepted Submission(s): 1596


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
210 51 3 52 4 51 1 82 3 61 8 810 61 2 52 3 41 0 82 2 51 4 41 2 3
 

Sample Output
[pre]3 721 94Can not put any one.2 620 944 52 3[/pre]


#include <bits/stdc++.h>using namespace std;#define maxn 50055int c[maxn << 2], lazy[maxn << 2];void build(int o, int l, int r){c[o] = 0;lazy[o] = -1;if(l == r) return;int mid = l + r >> 1;build(o << 1, l, mid);build(o << 1 | 1, mid + 1, r);}void pushdown(int o, int l, int r){if(l == r || lazy[o] == -1) return;int mid = l + r >> 1;if(lazy[o] == 1){c[o << 1] = mid - l + 1;c[o << 1 | 1] = r - mid;lazy[o << 1] = lazy[o << 1 | 1] = 1;}else{c[o << 1] = c[o << 1 | 1] = lazy[o << 1] = lazy[o << 1 | 1] = 0;}lazy[o] = -1;}int query(int o, int l, int r, int L, int R){pushdown(o, l, r);if(l >= L && r <= R){return c[o];}int mid = l + r >> 1, ans = 0;if(mid >= L) ans += query(o << 1, l, mid, L, R);if(mid < R) ans += query(o << 1 | 1, mid + 1, r, L, R);return ans;}void add(int o, int l, int r, int L, int R, int v){pushdown(o, l, r);if(l >= L && r <= R){if(v == 0){c[o] = lazy[o] = 0; return;}c[o] = r - l + 1;lazy[o] = 1;return;}int mid = l + r >> 1;if(mid >= L) add(o << 1, l, mid, L, R, v);if(mid < R) add(o << 1 | 1, mid + 1, r, L, R, v);c[o] = c[o << 1] + c[o << 1 | 1];}int main(){int T;scanf("%d", &T);while(T--){int n, m, op, u, v, l, r, mid, L, R;scanf("%d %d", &n, &m);build(1, 1, n);for(int i = 1; i <= m; ++i){scanf("%d %d %d", &op, &u, &v);if(op == 1){u++;if(query(1, 1, n, u, n) == n - u + 1){printf("Can not put any one.\n");continue;}l = u; r = n;while(r > l){mid = l + r >> 1;if(query(1, 1, n, l, mid) < mid - l + 1) r = mid;else l = mid + 1;}printf("%d ", l - 1);L = l;if(query(1, 1, n, u, n) + v > n - u + 1){v = n - u + 1 - query(1, 1, n, u, n);}l = u; r = n;while(r > l){mid = l + r >> 1;if(query(1, 1, n, u, mid) + v <= mid - u + 1) r = mid;else l = mid + 1;}printf("%d\n", l - 1);R = l;add(1, 1, n, L, R, 1);}else{u++; v++;printf("%d\n", query(1, 1, n, u, v));add(1, 1, n, u, v, 0);}}puts("");}}/*题意:50000个花瓶,现在50000次操作,每次要么从A开始插f朵花,每个花瓶只能插一朵,如果不能插则向后顺延,输出最左和最后的插入位置。要么将区间[L,R]内的花瓶清空,输出清空前区间内共有多少朵花。思路:线段树维护区间信息,第一个和最后一个插入位置L,R可以用二分然后线段树询问区间和来求。然后将区间[L,R]全部置1,清空的话全部置0。*/


原创粉丝点击