hdu 3971
来源:互联网 发布:搜索引擎优化实战培训 编辑:程序博客网 时间:2024/05/18 15:23
Play With Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 452 Accepted Submission(s): 228
Problem Description
When the girl was solving GSSX, a serious of tough problems about data structure on SPOJ, something intriguing once again comes to GYZ's mind. That is, for a changing sequences, how to count how many elements in a specific range efficiently.
Without any beneficial idea, as usual, GYZ asks her friend, CLJ for help. But this time, unfortunately, CLJ is playing a gal-game at present, does not have sparse time.
So now , it is your turn...
Cause the original problem is not as easy as first glance, let's examine a simplified one:
you are given a sequence A[1], A[2],..., A[N]. On this sequence you have to apply M operations: Add all the elements whose value are in range [l, r] with d or, ask for a query how many element are in range [l, r].
Without any beneficial idea, as usual, GYZ asks her friend, CLJ for help. But this time, unfortunately, CLJ is playing a gal-game at present, does not have sparse time.
So now , it is your turn...
Cause the original problem is not as easy as first glance, let's examine a simplified one:
you are given a sequence A[1], A[2],..., A[N]. On this sequence you have to apply M operations: Add all the elements whose value are in range [l, r] with d or, ask for a query how many element are in range [l, r].
Input
There are only one test case, Process until the end of the file. The first line of each case contains two numbers, N, M, described as above. And then start from the second line, have N numbers described the sequence's initial value.
( 1≤ N ≤ 250,000, M ≤ 50,000), |A[i]|≤ 1,000,000,000 .)
The following M lines described the operation:
C l r d: Add all the element whose value are in range [l, r] with d. (Redeclare: Not its Position! .. ) Q l r: ask for a query how many elements, whose value are in range [l, r].
( l ≤ r, |l|,|r|,|d|≤ 1,000,000,000 )
We guarantee every elements are suits 32-integer, and will not cause overflow, even during the running-time. (.. but still be careful ;) Besides, all the test-data are generated randomly.
( 1≤ N ≤ 250,000, M ≤ 50,000), |A[i]|≤ 1,000,000,000 .)
The following M lines described the operation:
C l r d: Add all the element whose value are in range [l, r] with d. (Redeclare: Not its Position! .. ) Q l r: ask for a query how many elements, whose value are in range [l, r].
( l ≤ r, |l|,|r|,|d|≤ 1,000,000,000 )
We guarantee every elements are suits 32-integer, and will not cause overflow, even during the running-time. (.. but still be careful ;) Besides, all the test-data are generated randomly.
Output
For each query, print the result. Example
Sample Input
10 1010 4 -5 8 8 3 0 -2 4 7C -9 8 2C -4 10 -3C -10 0 5Q -9 -1C -9 -5 8C -7 4 3Q -2 7C -10 -3 2C -4 -1 -6Q 7 10
Sample Output
1104Hint(In the first example, after the two operations, the sequences are become to, {-4, -3, -2, -1, 0, 11, 12, 13, 14, 15}, so there are no elements whose value are in range [1, 10]. )
又get到了新姿势。。 每隔4000次给排个序重新建树。。 这样做只知道可以优化复杂度
#include <bits/stdc++.h>using namespace std;const int MAXN = 250010;int n,m;int a[MAXN];struct node{ int MAX,MIN; int l,r; int lazy;}tree[MAXN*4];void push_up(int i){ tree[i].MAX = max(tree[i<<1].MAX, tree[i<<1|1].MAX); tree[i].MIN = min(tree[i<<1].MIN, tree[i<<1|1].MIN);}void build(int i, int l, int r){ tree[i].l = l; tree[i].r = r; tree[i].lazy = 0; if(l == r){ tree[i].MAX = tree[i].MIN = a[l]; return; } int mid = (l+r)>>1; build(i<<1, l, mid); build(i<<1|1, mid+1, r); push_up(i);}void push_down(int i){ if(tree[i].lazy){ tree[i<<1].MAX += tree[i].lazy; tree[i<<1].MIN += tree[i].lazy; tree[i<<1|1].MIN += tree[i].lazy; tree[i<<1|1].MAX += tree[i].lazy; tree[i<<1].lazy += tree[i].lazy; tree[i<<1|1].lazy += tree[i].lazy; tree[i].lazy = 0; }}void update(int i, int l, int r, int val){ if(tree[i].MIN > r || tree[i].MAX < l) return; if(tree[i].MIN >=l && tree[i].MAX <= r){ tree[i].MIN += val; tree[i].MAX += val; tree[i].lazy += val; return; } push_down(i); update(i<<1, l, r, val); update(i<<1|1, l, r, val); push_up(i);}int query(int i, int l, int r){ if(tree[i].MIN > r || tree[i].MAX < l) return 0; if(tree[i].MIN >= l && tree[i].MAX <= r){ return tree[i].r - tree[i].l + 1; } push_down(i); return query(i<<1, l, r) + query(i<<1|1, l, r);}void getA(int i){ if(tree[i].l == tree[i].r){ a[tree[i].l] = tree[i].MAX; return; } push_down(i); getA(i<<1); getA(i<<1|1);}char cmd[3];int main(){ int x,y,z; while(cin>>n>>m){ for(int i=1; i<=n; i++) scanf("%d", a+i); sort(a+1, a+n+1); build(1, 1, n); for(int i=1; i<=m; i++){ scanf("%s", cmd); if(cmd[0] == 'C'){ scanf("%d %d %d", &x, &y, &z); update(1, x, y, z); } else{ scanf("%d %d", &x, &y); int res = query(1, x, y); printf("%d\n", res); } if(i % 4000 == 0){ getA(1); sort(a+1, a+n+1); build(1, 1, n); } } } return 0;}
0 0
- hdu 3971
- hdu 3971
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- CRC-CCITT算法
- Proxy
- 安卓控件使用系列25:Spinner下拉列表控件的使用方法
- 双链表的实现C++封装
- Ubuntu用Wine运行Windows应用程序和卸载
- hdu 3971
- 中兴f402 412等破解超级密码、破解用户限制、关闭远程控制、恢复路由器拨号
- 学习与实践Cadence总结
- Ubuntu 15.04 clang++ 3.6 编译boost 1.59/1.55
- 山峰
- 将矩阵顺时针旋转90度 leetCode:Rotate Image
- Convert Sorted List to Binary Search Tree
- Java中的进制转换,练习
- linux 几个简单shell脚本demo