2018 Multi-University Training Contest 2 Naive Operations

 

题意:

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⌋

代码:

#include <cstdio>
#include<iostream>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#define mid ((l+r)/2)
#define ls (o<<1)
#define rs ((o<<1)+1)
using namespace std;
typedef long long int ll;
const int maxn = 100000;
const ll mod=1000000007;
int n,m,a[maxn+5];
int val[maxn*4+5],add[maxn*4+5],sum[maxn*4+5];
void pushup(int o)
{
    val[o] = min(val[ls],val[rs]);
    sum[o] = sum[ls]+sum[rs];
}
void pushdown(int o)
{
    if(add[o]!=0)
    {
        val[ls]+=add[o];
        val[rs]+=add[o];
        add[ls]+=add[o];
        add[rs]+=add[o];
        add[o]=0;
    }
    pushup(o);
}
void build(int o,int l,int r)
{
    add[o] = sum[o] = 0;
    if(l==r)
    {
        val[o] = a[l];
        return;
    }
    build(ls,l,mid);
    build(rs,mid+1,r);
    pushup(o);
}
void updata(int o,int l,int r,int L,int R)
{
    if(R<l||r<L) return;
    if(L<=l&&r<=R)
    {
        if(val[o]>1)
        {
            val[o]--, add[o]--;
        }
        else
        {
            if(l==r)
            {
                val[o]=a[l],add[o]=0,sum[o]++;
                return;
            }
            else
            {
                pushdown(o);
                updata(ls,l,mid,L,R);
                updata(rs,mid+1,r,L,R);
                pushup(o);
            }
        }
    }
    else
    {
        pushdown(o);
        if(L<=mid) updata(ls,l,mid,L,R);
        if(mid<R) updata(rs,mid+1,r,L,R);
        pushup(o);
    }
}
int query(int o,int l,int r,int L,int R)
{
    if(R<l||r<L) return 0;
    if(L<=l&&r<=R) return sum[o];
    int Sum = 0;
    if(L<=mid) Sum+=query(ls,l,mid,L,R);
    if(mid<R) Sum+=query(rs,mid+1,r,L,R);
    return Sum;
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        build(1,1,n);
        while(m--)
        {
            char opt[10];
            int L,R;
            scanf("%s %d %d",opt,&L,&R);
            if(opt[0]=='a') updata(1,1,n,L,R);
            else printf("%d\n",query(1,1,n,L,R));
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值