2018 Multi-University Training Contest 2 Naive Operations (线段树 lazy标记)

Naive Operations

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


 

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

 

 

Source

2018 Multi-University Training Contest 2

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6349 6348 6347 6346 6345 

题解:

对于每个区间我们维护区间分子的最大值a,分母的最小值b,只有当区间最大的分子大于最小的分母时该区间分子都加1才有可能对答案有贡献,这个时候我们暴力的找到这个分子大于等于分母的点然后更新该区间的sum,并且让分母变成原来的两倍,这样做的意义在于只有当前节点再增加原来的分母次数的话才会对答案造成贡献。否则的话我们直接对当前区间打上标记后返回。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
#define LL long long
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define sca(x) scanf("%d",&x);
#define N 100005
struct node
{
    int a,b,sum,lazy;
}t[N<<2];
int c[N];
void pushup(int rt)
{
    t[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;
    t[rt].a=max(t[rt<<1].a,t[rt<<1|1].a);
    t[rt].b=min(t[rt<<1].b,t[rt<<1|1].b);
}

void pushdown(int rt)
{
    if(t[rt].lazy)
    {
        t[rt<<1].lazy+=t[rt].lazy;
        t[rt<<1|1].lazy+=t[rt].lazy;
        t[rt<<1].a+=t[rt].lazy;
        t[rt<<1|1].a+=t[rt].lazy;
        t[rt].lazy=0;
    }
}
void build(int rt,int l,int r)
{
    t[rt].sum=0;
    t[rt].lazy=0;
    if(l==r)
    {
        t[rt].b=c[l];
        t[rt].a=0;
        return ;
    }
    int m=(l+r)>>1;
    build(rt<<1,l,m);
    build(rt<<1|1,m+1,r);
    pushup(rt);
}

void upda(int rt,int l,int r,int ql,int qr)
{
    if(l>=ql && r<=qr)
    {
        t[rt].a++;
        if(t[rt].a<t[rt].b)
        {
            t[rt].lazy++;
            return ;
        }
        if(l==r && t[rt].a>=t[rt].b)
        {
            t[rt].sum++;
            t[rt].b+=c[l];
            return ;
        }
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(ql<=m) upda(rt<<1,l,m,ql,qr);
    if(qr>m) upda(rt<<1|1,m+1,r,ql,qr);
    pushup(rt);
}

int query(int rt,int l,int r,int ql,int qr)
{
    if(l>=ql && r<=qr)
    {
        return t[rt].sum;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    int ans=0;
    if(ql<=m) ans+=query(rt<<1,l,m,ql,qr);
    if(qr>m) ans+=query(rt<<1|1,m+1,r,ql,qr);
    return ans;
}

int main()
{
    int n,m;
    int l,r;
    while(cin>>n>>m){
    rep(i,1,n) sca(c[i]);
    build(1,1,n);
    char s[10];
    while(m--)
    {
        scanf("%s%d%d",s,&l,&r);
        if(s[0]=='a') upda(1,1,n,l,r);
        else printf("%d\n",query(1,1,n,l,r));
    }
    }
}

参考博客:http://www.cnblogs.com/MengX/p/9368257.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值