HDU - 6315 Naive Operations

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...aral,al+1...ar 
2. query l r: query ∑ri=l⌊ai/bi⌋∑i=lr⌊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≤1000001≤n,q≤100000, 1≤l≤r≤n1≤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

题意:已经很明显了,给定一个数组,将对它进行两种操作,add和query

明显需要用线段树,但查的是区间[ai/bi]的和,非线性的操作,既不能按普通的区间求和来操作,也不能暴力单点查询。

这时我们需要一些转化,首先我们观察b数组和a数组的关系,只有当ai>=bi时,ai才会对区间的和做出恭喜,这就给了我们一个不需要每次都更新的理由。假设当前bi=2,初始化时ai=0,那么当ai==2时,它将对区间和尝生1的贡献,当ai==4时,它将对区间产生2的贡献,......依次推导下去,我们可以在线段数中维护一个cnt值,每当ai对区间做成贡献时就对它+1,这时我们可以将a再次变为0,这样当下次ai==2时(等价于==4),再次给cnt+1。当然,也可以将bi+=bi,2-->4,效果也是一样的。

因此我们需要维护a区间的最大值和b区间的最小值,因为只有当a的最大值>=b的最小值时,我们才需要去考虑更新(更新的时候是暴力单点更新的)。

下面给出代码:

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
#include <string.h>
#include <math.h>
#include <sstream>
using namespace std;
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
const int maxn=1e5+10;
typedef long long ll;
const int INF=0x3f3f3f3f;
int b[maxn];
struct Tree
{
    int cnt;
    int Maxa,Minb;
    int add;
}tree[maxn<<2];
void push_up(int rt)
{
    tree[rt].Minb=min(tree[rt<<1].Minb,tree[rt<<1|1].Minb);
    tree[rt].cnt=tree[rt<<1].cnt+tree[rt<<1|1].cnt;
    tree[rt].Maxa=max(tree[rt<<1].Maxa,tree[rt<<1|1].Maxa);
}
void push_down(int rt)
{
    if(tree[rt].add)
    {
        int v=tree[rt].add;
        tree[rt].add=0;
        tree[rt<<1].Maxa+=v;
        tree[rt<<1|1].Maxa+=v;
        tree[rt<<1].add+=v;
        tree[rt<<1|1].add+=v;
    }
}
void Build(int l,int r,int rt)
{
    tree[rt].add=0;
    if(l==r)
    {
        tree[rt].cnt=tree[rt].Maxa=0;
        tree[rt].Minb=b[l];
        return;
    }
    int m=l+r>>1;
    Build(Lson);
    Build(Rson);
    push_up(rt);
}
void updata(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        tree[rt].Maxa++;
        if(tree[rt].Maxa<tree[rt].Minb)
        {
            tree[rt].add++;
            return;
        }
        if(l==r&&tree[rt].Maxa>=tree[rt].Minb)
        {
            tree[rt].cnt++;
            tree[rt].Minb+=b[l];
            return;
        }
        //return;
    }
    push_down(rt);
    int m=l+r>>1;
    if(L<=m) updata(L,R,Lson);
    if(R>m) updata(L,R,Rson);
    push_up(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return tree[rt].cnt;
    }
    int m=l+r>>1;
    push_down(rt);
    int ans=0;
    if(L<=m)  ans+=query(L,R,Lson);
    if(R>m)  ans+=query(L,R,Rson);
    return ans;
}
int main(int argc, char const *argv[])
{
    int n,x,y,q;
    while(~scanf("%d%d",&n,&q))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
        }
        Build(1,n,1);
        char s[8];
        while(q--)
        {
            scanf("%s",s);
            scanf("%d%d",&x,&y);
            if(s[0]=='a')
            {
                updata(x,y,1,n,1);
            }
            else
            {
                cout<<query(x,y,1,n,1)<<endl;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81226008
今日推荐