HDU-6315 Naive Operations 线段树

Naive Operations

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


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=lai/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.
1n,q1000001lrn, 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的最小值,使用lazy标记就不需要更新到每个点了。每次更新当当前最大值a小于最小的b,那么下面子节点就都不需要查询了

大于时我们暴力找到l==r然后cnt+1,同时分母+上b[l],比如当前是2/2 我们就把它变成 2/4 这样我们还需要增加2次才能到达4/4 再变成4/6.

#include<bits/stdc++.h>
using namespace std;
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
#define pll pair<int,int>
#define mp make_pair
#define ll long long
#define INF 0x3f3f3f3f
const int maxn=1e5+5;
int n,q;
struct node
{
    int cnt,addv,minb,maxa;
} tree[maxn<<2];
int b[maxn];
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].addv)
    {
        int v=tree[rt].addv;
        tree[rt].addv=0;
        tree[rt<<1].maxa+=v;
        tree[rt<<1|1].maxa+=v;
        tree[rt<<1].addv+=v;
        tree[rt<<1|1].addv+=v;
    }
}
void build(int l,int r,int rt)
{
    tree[rt].addv=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].addv++;
            return;
        }
        if(l==r&&tree[rt].maxa>=tree[rt].minb)
        {
            tree[rt].cnt++;
            tree[rt].minb+=b[l];
            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 n,x,y;
    while(~scanf("%d %d",&n,&q))
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&b[i]);
        }
        build(1,n,1);
        char pp[6];
        int l,r;
        while(q--)
        {
            scanf("%s%d%d",pp,&l,&r);
            if(pp[0]=='a')
            {
                updata(l,r,1,n,1);
            }
            else
            {
                printf("%d\n",query(l,r,1,n,1));
            }
        }
    }
    return 0;
}
View Code

 

 

 

 

PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

 

 

转载于:https://www.cnblogs.com/MengX/p/9368257.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值