HDU 6315(线段树+技巧)

Naive Operations

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

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

题意

  给出两个序列,a序列全为0,b序列为输入。然后有两种操作,add x y就是把a数组[x,y]区间内全部+1,query x y是查询[x,y]区间内∑ai/bi。

解题思路

  比赛的时候维护了三个线段树,一直TLE。题目是要求每个商向下取整的和,所以比赛的时候一直想着要更新到叶子节点,然后再pushup上去。后来下来看了大佬的解题报告才知道,维护一个线段树就行了,每个区间维护a的最大值和b的最小值。
  按照常规解法,这个题目无法只更改对应区间节点的信息来节省时间,必须更改到每一个叶子节点,这样一来就特别浪费时间。大佬们就想到了维护每个区间的最大a和最小b。如果一个区间最大的a都比最小的b小,那么这个区间对总和的贡献一定是0,到了这里就不用往下update了,剩下的交给懒惰数组来下推了,这样一来就节省了很多更新叶子节点的时间。

#include
using namespace std;
const int maxn=1e5+5;

struct node
{
    int sum,minb,maxa;
} tree[maxn<<2];
int b[maxn],add[maxn<<2];
char str[10];

void pushup(int rt)
{
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;//往上推和
    tree[rt].minb=min(tree[rt<<1].minb,tree[rt<<1|1].minb);//维护区间最小的b
    tree[rt].maxa=max(tree[rt<<1].maxa,tree[rt<<1|1].maxa);//维护区间最大的a
}
void pushdown(int rt)
{
    if(add[rt])
    {
        tree[rt<<1].maxa+=add[rt];
        tree[rt<<1|1].maxa+=add[rt];
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];
        add[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    add[rt]=0;
    if(l==r)
    {
        tree[rt].sum=tree[rt].maxa=0;
        tree[rt].minb=b[l];
        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,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        tree[rt].maxa++;
        if(tree[rt].maxareturn;
        }
        if(l==r&&tree[rt].maxa>=tree[rt].minb)
        {
            tree[rt].sum++;
            tree[rt].minb+=b[l];
            return;
        }
    }
    pushdown(rt);
    int m=l+r>>1;
    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,int r,int rt)
{
    if(L<=l&&r<=R) return tree[rt].sum;
    int m=l+r>>1;
    pushdown(rt);
    int ans=0;
    if(L<=m) ans+=query(L,R,l,m,rt<<1);
    if(R> m) ans+=query(L,R,m+1,r,rt<<1|1);
    return ans;
}
int main()
{
    int n,m,l,r;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1; i<=n; i++) scanf("%d",&b[i]);
        getchar();
        build(1,n,1);
        while(m--)
        {
            scanf("%s%d%d",str,&l,&r);
            if(str[0]=='a') update(l,r,1,n,1);
            else printf("%d\n",query(l,r,1,n,1));
        }
    }
    return 0;
}

你可能感兴趣的:(数据结构-线段树)