hdu 6315 Naive Operations (线段树)

Naive Operations

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


 

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

题意:

给出一个长度为n初值为0的数组,以及长度为n的b数组,然后q次操作,add(l,r) 使得区间l~r所有元素+1,或者查询l~r区间a[i]/b[i]的和

思路:

我们可以维护区间内a的最大值,和区间内数组b的最小值,这样只有maxa>minb的情况下,区间才有可能向下更新,否则连最大值都小于最小值,a[i]/b[i]必定全为0,而且在叶子节点的时候满足maxa>=minb的时候我们可以让minb+=b[l],区间答案+1。然后就是线段树区间更新的模板了

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct node
{
    int maxa,minb,cnt,num;
}t[N<<2];
int b[N];
void pushup(int x)
{
    t[x].minb=min(t[x*2].minb,t[x*2+1].minb);
    t[x].maxa=max(t[x*2].maxa,t[x*2+1].maxa);
    t[x].cnt=t[x*2].cnt+t[x*2+1].cnt;
}
void pushdown(int x)
{
    if(t[x].num)
    {
        int v=t[x].num;
        t[x*2].num+=v;
        t[x*2+1].num+=v;
        t[x*2+1].maxa+=v;
        t[x*2].maxa+=v;
        t[x].num=0;
    }
}
void build(int x,int l,int r)
{
    t[x].num=0;
    if(l==r)
    {
        t[x].minb=b[l];
        t[x].maxa=t[x].cnt=0;
        return;
    }
    int mid=(l+r)/2;
    build(x*2,l,mid);
    build(x*2+1,mid+1,r);
    pushup(x);
}
void update(int L,int R,int l,int r,int x)
{
    if(l<=L&&r>=R)
    {
        t[x].maxa++;
        if(t[x].maxa<t[x].minb)
        {
            t[x].num++;
            return;
        }
        if(L==R&&t[x].maxa>=t[x].minb)
        {
            t[x].cnt++;
            t[x].minb+=b[L];
            return;
        }
    }
    pushdown(x);
    int mid=(L+R)/2;
    if(l<=mid)update(L,mid,l,r,x*2);
    if(r>mid)update(mid+1,R,l,r,x*2+1);
    pushup(x);
}
int query(int L,int R,int l,int r,int x)
{
    if(l<=L&&r>=R)
    {
        return t[x].cnt;
    }
    pushdown(x);
    int mid=(L+R)/2;
    int ans=0;
    if(l<=mid)ans+=query(L,mid,l,r,x*2);
    if(r>mid)ans+=query(mid+1,R,l,r,x*2+1);
    return ans;
}
int main()
{
    int n,q;
    while(~scanf("%d%d",&n,&q))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
        }
        build(1,1,n);
        char s[10];
        while(q--)
        {
            scanf("%s",s+1);
            int x,y;
            scanf("%d%d",&x,&y);
            if(s[1]=='a')
            {
                update(1,n,x,y,1);
            }
            else
            {
                int ans=query(1,n,x,y,1);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/imzxww/article/details/81215934
今日推荐