2018 Multi-University Training Contest 2 G(Naive Operations)

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

思路:根据直播的杜教分析,因为a[i]<=q,那么a[i]/b[i]<=q/b[i],那么将题目中2加起来就是调和级数得出调和级数其为nlong n的复杂度。(b[i]是n的全排列,a[i]/b[i]发生质的变化是当a[i]=b[i]时)

所以直接线段树维护区间最小值,每次操作将最小值减1,当减到0是深搜将叶子中每一个都恢复原值。

#define MAXN 100010
#define inf 0x3f3f3f3f
struct node{
    int l,r;//区间[l,r]
    int add;//区间的延时标记
    int mn; //区间最小值
}tree[MAXN<<2];//一定要开到4倍多的空间

int b[MAXN<<2];

int c[MAXN],n;
int lowbit(int i) {return i&(-i);}
void update(int i,int val)
{
    while(i<=n)
    {
        c[i]+=val;
        i+=lowbit(i);
    }
}
int getsum(int i)
{
    int sum=0;
    while(i>0)
    {
        sum+=c[i];
        i-=lowbit(i);
    }
    return sum;
}
void pushup(int index)
{
    tree[index].mn = min(tree[index<<1].mn,tree[index<<1|1].mn);
}
void pushdown(int index)
{
    if(tree[index].add)
    {
        tree[index<<1].mn += tree[index].add;
        tree[index<<1|1].mn += tree[index].add;
        tree[index<<1].add += tree[index].add;
        tree[index<<1|1].add += tree[index].add;
        tree[index].add = 0;
    }
}
void build(int l,int r,int index)
{
    tree[index].l = l;
    tree[index].r = r;
    tree[index].add = 0;
    if(l == r)
    {
        scanf("%d",&b[index]);
        tree[index].mn=b[index];
        return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,index<<1);
    build(mid+1,r,index<<1|1);
    pushup(index);
}
void solve(int index)
{
    if(tree[index].l==tree[index].r)
    {
        if(tree[index].mn==0) tree[index].mn=b[index];
        update(tree[index].l,1);
        return ;
    }
    pushdown(index);
    int mid=(tree[index].l+tree[index].r)>>1;
    if(tree[index*2].mn==0) solve(index*2);
    if(tree[index*2+1].mn==0) solve(index*2+1);
    pushup(index);
}
void updata(int l,int r,int index,int val)
{
    if(l <= tree[index].l &&r>=tree[index].r)
    {
        tree[index].mn += val;
        tree[index].add += val;
        if(tree[index].mn==0)
        {
            solve(index);
        }
        return ;
    }
    pushdown(index);
    int mid = (tree[index].l+tree[index].r)>>1;
    if(l <= mid) updata(l,r,index<<1,val);
    if(r > mid) updata(l,r,index<<1|1,val);
    pushup(index);
}
int main()
{
    int q,l,r;
    char s[20];
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(tree,0,sizeof(tree));
        build(1,n,1);
        while(q--)
        {
            scanf("%s%d%d",s,&l,&r);
            if(s[0]=='a')
            {
                updata(l,r,1,-1);
            }
            else
            {
                cout<<getsum(r)-getsum(l-1)<<endl;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/snayf/article/details/81227930