杭电多校 Naive Operations 线段树+思维

Naive Operations

时间限制: 3 Sec  内存限制: 512 MB
提交: 17  解决: 9
[提交] [状态] [讨论版] [命题人:外部导入]

题目描述

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 

 

输入

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 the answer for each 'query', each one line.

 

样例输入

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

 

样例输出

1
1
2
4
4
6

 

[提交][状态]

因为a/b向下取整,所以每次当a加到等于b时,才会产生一个贡献,可以倒过来想,直接减b,减到0就增加1的贡献

用线段树来维护

代码:

#include <bits/stdc++.h>
using namespace std;
const  int maxx=1e5+100;
const int INF=1e9;
const int MOD=1e9+7;
typedef long long ll;
int b[maxx];
struct node
{
    int pos,v;
}mx[maxx<<2];
int lazy[maxx<<2];
void pushup(int rt)
{
    mx[rt].pos=mx[rt<<1].pos+mx[rt<<1|1].pos;
    mx[rt].v=min(mx[rt<<1].v,mx[rt<<1|1].v);
}
void pushdown(int rt)
{
    if(lazy[rt]){
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        mx[rt<<1].v-=lazy[rt];
        mx[rt<<1|1].v-=lazy[rt];
        lazy[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    lazy[rt]=0;
    if(l==r){
        mx[rt].pos=0;
        mx[rt].v=b[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void change(int l,int r,int rt)
{
    if(l==r){
        if(mx[rt].v==0){
            mx[rt].pos++;
            mx[rt].v=b[l];
        }
        lazy[rt]=0;
        return ;
    }
    pushdown(rt);
    int mid=(l+r)>>1;
    if(mx[rt<<1].v==0)  change(l,mid,rt<<1);
    if(mx[rt<<1|1].v==0)  change(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int L,int R,int l,int r,int rt)
{
    if(L<=l && r<=R){
        lazy[rt]++;
        mx[rt].v--;
        if(mx[rt].v==0){
            change(l,r,rt);
        }
        return ;
    }
    pushdown(rt);
    int mid=(l+r)>>1;
    if(L<=mid)   update(L,R,l,mid,rt<<1);
    if(R>mid)    update(L,R,mid+1,r,rt<<1|1);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l && r<=R)  return mx[rt].pos;
    pushdown(rt);
    int mid=(l+r)>>1;
    int ret=0;
    if(L<=mid)  ret+=query(L,R,l,mid,rt<<1);
    if(R>mid)   ret+= query(L,R,mid+1,r,rt<<1|1);
    //else        return query(L,R,l,mid,rt<<1)+query(L,R,mid+1,r,rt<<1|1);
    return ret;
}
int main()
{
    int n,q;
    while(~scanf("%d%d",&n,&q)){
        for(int i=1; i<=n; i++){
            scanf("%d",&b[i]);
        }
        build(1,n,1);
        char s[10];
        int l,r;
        while(q--){
            scanf("%s%d%d",s,&l,&r);
            if(s[0]=='a')  update(l,r,1,n,1);
            else           printf("%d\n",query(l,r,1,n,1));
        }
    }
 
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值