杭电多校第二次 1007 Naive Operations(线段树

Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0

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

解析:对于每次更新,找到产生贡献的值,在另一个线段树中进行点的更新,怎么找到贡献的值呢,我们可以设一个线段树记录最小值,每次操作对最小值–,如果为0,那么就说明这一段有节点产生了贡献,查到这个节点并重置即可
之后对另一个线段树求和就是答案

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define rep(i,a,b) for(int i=a ;i<=b;i++)
#define rep1(i,b,a) for(int i=b;i>=a;i--)
#define inf ox3f3f3f3f
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
using namespace std;
const int maxn = 111111;
int h , w , n;
int sum[maxn<<2];
int mi[maxn<<2];
int tag[maxn<<2];
int arr[maxn];
void PushUp(int rt)
{
    mi[rt] =min (mi[rt<<1] ,mi[rt<<1|1]);
}
void PushUp2(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void push_down(int rt)
{
    if (tag[rt]>0){
        tag[rt<<1]+=tag[rt];tag[rt<<1|1]+=tag[rt];
        mi[rt<<1]-=tag[rt];mi[rt<<1|1]-=tag[rt];
        tag[rt] = 0;
    }
}
void build(int l,int r,int rt)
{
    if (l == r)
    {
        mi[rt]=arr[l];
        return ;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}
void update1(int l,int r,int c,int rt)
{
    if (l==r)
    {
        sum[rt]++;
        return ;
    }
    int m = (l + r) >> 1;
    if (c <= m) update1(l ,m , c , rt<<1);
    else if (c > m) update1(m+1,r, c ,rt<<1|1);
    PushUp2(rt);
}
void fi(int l,int r,int rt)
{
    if(l==r )
    {
        //cout<<"更新"<<l<<endl;
        update1(1,n,l,1);
        mi[rt]=arr[l];
        //cout<<mi[rt]<<endl;
        return ;
    }
    push_down(rt);
    int m=(l+r)/2;
    if(mi[rt<<1]==0) fi(lson);
    if(mi[rt<<1|1]==0) fi(rson);
    PushUp(rt);

}
void update(int L,int R,int l,int r,int rt)
{
    if (L <= l && r <= R)
    {
        mi[rt]--;
        tag[rt]++;
        if(mi[rt]==0)
        {
            fi(l,r,rt);
        }
        return ;
    }
    push_down(rt);
    int m = (l + r) >> 1;
    if (L <= m) update(L , R ,  lson);
    if (R > m) update(L , R , rson);
    PushUp(rt);
}

ll query(int L,int R,int l,int r,int rt)
{
    if (L <= l && r <= R)
    {
        return sum[rt];
    }
    ll ans=0;
    int m = (l + r) >> 1;
    if (L <= m)
        ans+=query(L , R ,  lson);
    if (R > m)
        ans+=query(L , R , rson);
    return ans;
}
int main()
{
    //freopen("D://r.txt","r",stdin);
    int q;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        memset(tag,0,sizeof tag);
        memset(sum,0,sizeof sum);
        rep(i,1,n)
        scanf("%d",&arr[i]);
        build(1,n,1);
        while(q--)
        {
            char c[10];
            int a,b;
            scanf("%s%d%d",&c,&a,&b);
            if(c[0]=='a')
            {
                update(a,b,1,n,1);
            }
            else
            {
                cout<<query(a,b,1,n,1)<<endl;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ffgcc/article/details/81206227
今日推荐