Naive Operations(HDU6315)

本文介绍了一种利用线段树解决区间更新与查询问题的方法,通过维护区间最大值和最小值,有效处理区间加法操作及求和查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

思路:

维护一个区间 a 的最大值,b的最小值,用一个mark数组来标记区间增加过的次数,当maxa<minb就可以不用向下更新了。

具体细节 当更新到单点并且 maxa >= minb 时,区间答案加一,minb+=b[l] 

其他基本是线段树板子。

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
struct Node
{
    int l, r, maxa, minb, cnt;
}t[maxn*4];
int b[maxn], mark[maxn*4];

void push_up(int root)
{
    t[root].maxa = max(t[root*2].maxa, t[root*2+1].maxa);
    t[root].minb = max(t[root*2].minb, t[root*2+1].minb);
    t[root].cnt = t[root*2].cnt+t[root*2+1].cnt;
}
void build(int root, int l, int r)
{
    t[root].l = l;
    t[root].r = r;
    if(l == r)
    {
        t[root].minb = b[l];
        t[root].maxa = 0;
        t[root].cnt = 0;
        return ;
    }
    int mid = (l+r)>>1;
    build(root*2, l, mid);
    build(root*2+1, mid+1, r);
    t[root].maxa = max(t[root*2].maxa, t[root*2+1].maxa);
    t[root].minb = min(t[root*2].minb, t[root*2+1].minb);
    t[root].cnt = t[root*2].cnt+t[root*2+1].cnt;
}

void push_down(int root)
{
    if(mark[root])
    {
        t[root*2].maxa += mark[root];
        t[root*2+1].maxa += mark[root];
        mark[root*2] += mark[root];
        mark[root*2+1] += mark[root];
        mark[root] = 0;
    }
    //t[root].maxa = max(t[root*2].maxa, t[root*2+1].maxa);
}

void update(int root, int l, int r, int val)
{
    if(t[root].l>=l && t[root].r<=r)
    {
        t[root].maxa += val;
        if(t[root].maxa < t[root].minb)
        {
            mark[root] += val;
            return ;
        }
        if(t[root].l==t[root].r && t[root].maxa >= t[root].minb)
        {
            t[root].cnt++;
            t[root].minb += b[t[root].l];
            return;
        }
    }
    push_down(root);
    int mid = (t[root].l+t[root].r)>>1;
    if(l <= mid) update(root*2, l, r, 1);
    if(r > mid)  update(root*2+1, l, r, 1);
    t[root].maxa = max(t[root*2].maxa, t[root*2+1].maxa);
    t[root].minb = min(t[root*2].minb, t[root*2+1].minb);
    t[root].cnt = t[root*2].cnt+t[root*2+1].cnt;
}

int query(int root, int l, int r)
{
    if(t[root].l>=l && t[root].r<=r)
    {
        return t[root].cnt;
    }
    push_down(root);
    int mid = (t[root].l+t[root].r)>>1;
    int ans = 0;
    if(l <= mid) ans += query(root*2, l, r);
    if(r > mid)  ans += query(root*2+1, l, r);
    return ans;

}

int main()
{
    string s;
    int n, q;
    ios::sync_with_stdio(false);
    while(cin >> n >> q)
    {
        memset(mark, 0, sizeof(mark));
        for(int i = 1; i <= n; i++)
        {
            cin >> b[i];
        }
        build(1, 1, n);
        for(int i = 1; i <= q; i++)
        {
            cin >> s;
            if(s[0] == 'a')
            {
                int l, r;
                cin >> l >> r;
                update(1, l, r, 1);
            }
            if(s[0] == 'q')
            {
                int l, r;
                cin >> l >> r;
                cout << query(1, l, r) << endl;
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值