HDU6315:Naive Operations 题解

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 ∑ i = l r ⌊ a i / b i ⌋

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


考虑到b是一个1~n的排列,1这个数最多会贡献n,2这个数最多会贡献n/2,所以根据调和级数,总贡献是nlogn级别的
我们考虑建线段树,线段树的每个节点初始值是b数组中的值,每次add操作相当于一个区间减操作,如果当前区间的最小值大于1的话就直接更新懒标记,否则递归下去,直到那个是1的点,贡献+1,值置为b[i],因为至多有nlogn次暴力访问到叶子的操作,所以总复杂度 O(nlog2n) O ( n l o g 2 n )

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define LB long double
#define ull unsigned long long
#define x first
#define y second
#define pb push_back
#define pf push_front
#define mp make_pair
#define Pair pair<int,int>
#define pLL pair<LL,LL>
#define pii pair<double,double>

const int INF=2e9;
const LL LINF=2e16;
const int magic=348;
const int MOD=998244353;
const double eps=1e-10;
const double pi=acos(-1);

inline int getint()
{
    bool f;char ch;int res;
    while (!isdigit(ch=getchar()) && ch!='-') {}
    if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
    while (isdigit(ch=getchar())) res=res*10+ch-'0';
    return f?res:-res;
}

const int MAXN=1e5;

int n,q;
int b[MAXN+48];

namespace SegmentTree
{
    struct node
    {
        int left,right;
        int sum,lazy,minn;
    }tree[MAXN*4];
    inline void pushup(int cur)
    {
        tree[cur].minn=min(tree[cur<<1].minn,tree[cur<<1|1].minn);
        tree[cur].sum=tree[cur<<1].sum+tree[cur<<1|1].sum;
    }
    inline void pushdown(int cur)
    {
        tree[cur<<1].lazy+=tree[cur].lazy;
        tree[cur<<1|1].lazy+=tree[cur].lazy;
        tree[cur<<1].minn-=tree[cur].lazy;
        tree[cur<<1|1].minn-=tree[cur].lazy;
        tree[cur].lazy=0;
    }
    inline void build(int cur,int left,int right)
    {
        tree[cur].left=left;tree[cur].right=right;
        tree[cur].lazy=0;
        if (left!=right)
        {
            int mid=(left+right)>>1;
            build(cur<<1,left,mid);
            build(cur<<1|1,mid+1,right);
            pushup(cur);
        }
        else
        {
            tree[cur].sum=0;tree[cur].minn=b[left];
        }
    }
    inline void update(int cur,int left,int right)
    {
        if (tree[cur].left==tree[cur].right)
        {
            if (tree[cur].minn==1)
            {
                tree[cur].sum++;
                tree[cur].minn=b[tree[cur].left];
                return;
            }
            tree[cur].minn--;
            return;
        }
        if (left<=tree[cur].left && tree[cur].right<=right && tree[cur].minn>1)
        {
            tree[cur].lazy++;tree[cur].minn--;
            return;
        }
        pushdown(cur);
        int mid=(tree[cur].left+tree[cur].right)>>1;
        if (left<=mid) update(cur<<1,left,right);
        if (mid+1<=right) update(cur<<1|1,left,right);
        pushup(cur);
    }
    inline int query(int cur,int left,int right)
    {
        if (left>tree[cur].right || right<tree[cur].left || left>right) return 0;
        if (left<=tree[cur].left && tree[cur].right<=right) return tree[cur].sum;
        pushdown(cur);int res=query(cur<<1,left,right)+query(cur<<1|1,left,right);
        pushup(cur);return res;
    }
}

int main ()
{
    int i,l,r;char op[10];
    while (scanf("%d%d",&n,&q)!=EOF)
    {
        for (i=1;i<=n;i++) b[i]=getint();
        SegmentTree::build(1,1,n);
        while (q--)
        {
            scanf("%s%d%d",op+1,&l,&r);
            if (op[1]=='a') SegmentTree::update(1,l,r); else printf("%d\n",SegmentTree::query(1,l,r));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值