hdu 3971

 

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]. 

 

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. 

 

Output
For each query, print the result. Example
 

Sample Input
      
      
10 10 10 4 -5 8 8 3 0 -2 4 7 C -9 8 2 C -4 10 -3 C -10 0 5 Q -9 -1 C -9 -5 8 C -7 4 3 Q -2 7 C -10 -3 2 C -4 -1 -6 Q 7 10
 

Sample Output
      
      
1 10 4
Hint
(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值