杭电多校二 Naive operations (线段树)

 

Naive Operations

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


 

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:  6396 6395 6394 6393 6392 

 

 

终于学会了线段树的原理与操作了,真的是慢的可以了,这个知识点好久之前就接触了,一直迷迷糊糊的.......

题意:有一个空的数组a和一个n个元素的数组b,有两个操作,第一个操作是从l到r这个区间中ai++,另一个操作是统计l到r中ai/bi的和,这道题只需要一个能支持区间修改和区间查询的线段树即可,用线段树维护的是ai/bi和b数组的区间最小值,当进行第一个操作的时候,只需要将那个区间的min--,当发现减到0的时候,当前位置的ans++,同时将当前位置的min再次更新到b,查询的时候直接统计ans的区间和即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
typedef long long ll;

int tree[maxn<<2],a[maxn],lazy[maxn],ans[maxn],n;

void pushup(int rt)
{
    tree[rt]=min(tree[rt<<1],tree[rt<<1|1]);
    ans[rt]=ans[rt<<1]+ans[rt<<1|1];
}
void pushdown(int rt)
{
    if(lazy[rt])
    {
        tree[rt<<1]-=lazy[rt];
        tree[rt<<1|1]-=lazy[rt];
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        lazy[rt]=0;
    }
}

void build(int l,int r,int rt)
{
    ans[rt]=0;
    lazy[rt]=0;
    if(l==r)
    {
        scanf("%d",&a[rt]);
        tree[rt]=a[rt];
        return;
    }
    int m=l+r>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    pushup(rt);
}

void update(int L,int R,int l=1,int r=n,int rt=1)
{
    if(l>=L && r<=R)
    {
        tree[rt]--;
        if(tree[rt])
        {
            lazy[rt]++;
            return;
        }
        else
        {
            if(l==r)
            {
                ans[rt]++;
                tree[rt]=a[rt];
                return;
            }
        }
    }
    int m=l+r>>1;
    pushdown(rt);
    if(L<=m)
        update(L,R,l,m,rt<<1);
    if(R>m)
        update(L,R,m+1,r,rt<<1|1);
    pushup(rt);
}

int query(int L,int R,int l=1,int r=n,int rt=1)
{
    if(l>=L && r<=R)
        return ans[rt];
    int m=l+r>>1;
    int sum=0;
    if(L<=m)
        sum+=query(L,R,l,m,rt<<1);
    if(R>m)
        sum+=query(L,R,m+1,r,rt<<1|1);
    return sum;
}


int main()
{
    int q;
    while(scanf("%d%d",&n,&q)==2)
    {
        build(1,n,1);
        while(q--)
        {
            char s[20];
            scanf("%s",s);
            if(s[0]=='a')
            {
                int a,b;
                scanf("%d%d",&a,&b);
                update(a,b);
            }
            else
            {
                int a,b;
                scanf("%d%d",&a,&b);
                printf("%d\n",query(a,b));
            }
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值