HDU 6315(线段树)

Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 790    Accepted Submission(s): 293


 

Problem Description

In a galaxy far, far away, there are two integer sequence a and b of length n.
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=l⌊ai/bi⌋

 

 

Input

There are multiple test cases, please read till the end of input file.
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there're no more than 5 test cases.

 

 

Output

Output the answer for each 'query', each one line.

 

 

Sample Input

 

5 12 1 5 2 4 3 add 1 4 query 1 4 add 2 5 query 2 5 add 3 5 query 1 5 add 2 4 query 1 4 add 2 5 query 2 5 add 2 2 query 1 5

 

 

Sample Output

 

1 1 2 4 4 6

 

 

Source

2018 Multi-University Training Contest 2

 

线段树。

可以这么考虑,最差情况下答案最多为n/1+n/2+...+n/n

这个数最大在2e6左右,意味着每次某个位置的答案+1的时候来一次单点更新复杂度是可行的。

我们可以让a数组赋值为b,每次区间更新让[l,r]内所有数-1

如果某个区间内的最小值为0,说明存在需要更新的值,找到他,在另外一棵线段树上更新,之后将这个位置恢复为b[i]

这样就可以通过了


#include<cstdio>
#include<cstring>
#define lchild x<<1,l,mid
#define rchild x<<1|1,mid+1,r
int n,q,b[100005],st[400005],st2[400005],mark[400005],mark2[400005];
void push_up2(int x){
    st2[x]=st2[x<<1]+st2[x<<1|1];
}
void push_down2(int x,int len,int l,int r){
    st2[x<<1]+=mark2[x]*(len-(len>>1));
    mark2[x<<1]+=mark2[x];
    st2[x<<1|1]+=mark2[x]*(len>>1);
    mark2[x<<1|1]+=mark2[x];
    mark2[x]=0;
}
int query2(int L,int R,int x=1,int l=1,int r=n){
    if(L<=l&&r<=R) return st2[x];
    if(mark2[x]) push_down2(x,r-l+1,l,r);
    int mid=(l+r)>>1,ret=0;
    if(L<=mid) ret+=query2(L,R,lchild);
    if(R>mid) ret+=query2(L,R,rchild);
    return ret;
}
void update2(int L,int R,int val,int x=1,int l=1,int r=n){
    if(L<=l&&r<=R){
        st2[x]+=val*(r-l+1);
        mark2[x]+=val;
        return;
    }
    if(mark2[x]) push_down2(x,r-l+1,l,r);
    int mid=(l+r)>>1;
    if(L<=mid) update2(L,R,val,lchild);
    if(R>mid) update2(L,R,val,rchild);
    push_up2(x);
}
int min(int a,int b){
    if(a<b) return a;
    return b;
}
void push_up(int x){
    st[x]=min(st[x<<1],st[x<<1|1]);
}
void push_down(int x,int len,int l,int r){
    st[x<<1]+=mark[x];
    mark[x<<1]+=mark[x];
    st[x<<1|1]+=mark[x];
    mark[x<<1|1]+=mark[x];
    mark[x]=0;
}
int query(int L,int R,int x=1,int l=1,int r=n){
    if(L<=l&&r<=R) return st[x];
    if(mark[x]) push_down(x,r-l+1,l,r);
    int mid=(l+r)>>1,ret=0x3f3f3f3f;
    if(L<=mid) ret=min(ret,query(L,R,lchild));
    if(R>mid) ret=min(ret,query(L,R,rchild));
    return ret;
}
void update(int L,int R,int val,int x=1,int l=1,int r=n){
    if(L<=l&&r<=R){
        st[x]+=val;
        mark[x]+=val;
        return;
    }
    if(mark[x]) push_down(x,r-l+1,l,r);
    int mid=(l+r)>>1;
    if(L<=mid) update(L,R,val,lchild);
    if(R>mid) update(L,R,val,rchild);
    push_up(x);
}
void build(int x=1,int l=1,int r=n){
    if(l==r){
        st[x]=b[l];
        return;
    }
    int mid=(l+r)>>1;
    build(lchild);
    build(rchild);
    push_up(x);
}
void find(int x=1,int l=1,int r=n){
    if(query(l,r)!=0) return;
    if(l==r){
        update(l,r,b[l]);
        update2(l,r,1);
        return;
    }
    int mid=(l+r)>>1;
    find(lchild);
    find(rchild);
}
int main(){
    while(scanf("%d%d",&n,&q)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&b[i]);
        }
        memset(st,0,sizeof(st));
        memset(st2,0,sizeof(st2));
        memset(mark,0,sizeof(mark));
        memset(mark2,0,sizeof(mark2));
        build();
        while(q--){
            char s[10];
            int l,r;
            scanf("%s%d%d",s,&l,&r);
            if(strcmp(s,"add")==0){
                update(l,r,-1);
                find();
            }else{
                printf("%d\n",query2(l,r));
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值