HDU 6315 Naive Operations 【线段树】【树状数组】

本文介绍了一个涉及数值查询与更新操作的问题解决方法。在一个长度为n的数组中,初始所有元素均为0,通过特定操作实现数组元素的增加,并能快速查询指定范围内元素值除以对应静态排列元素值向下取整后的总和。

Naive Operations

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


 

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

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
const int MAX = 1e5 + 7;
int n, m;
int c[MAX], a[MAX << 2], lz[MAX << 2];
int J[MAX];
int lowbit(int x){
    return x & -x;
}
void add(int pos, int v){
    while(pos <= MAX){
        c[pos] += v;
        pos += lowbit(pos);
    }
}
ll query(int pos){
    ll ans = 0;
    while(pos){
        ans += c[pos];
        pos -= lowbit(pos);
    }
    return ans;
}
void pushup(int rt){
    a[rt] = min(a[rt << 1], a[rt << 1 | 1]);
}
void pushdown(int rt, int l, int r){
    if(lz[rt] != 0){
        a[rt << 1] += lz[rt];
        a[rt << 1 | 1] += lz[rt];
        lz[rt << 1] += lz[rt];
        lz[rt << 1 | 1] += lz[rt];
        lz[rt] = 0;
    }
}
void build(int rt, int l, int r){
    if(l == r){
        a[rt] = J[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int rt, int l, int r, int x, int y){
    if(x <= l && r <= y){
        if(a[rt] > 1){
            a[rt] -= 1;
            lz[rt] -= 1;
        }
        else{
            pushdown(rt, l, r);
            if(l == r){
                add(l, 1);
                a[rt] = J[l];
                return;
            }
            int mid = (l + r) >> 1;
            if(a[rt << 1] > 1){
                a[rt << 1] -= 1;
                lz[rt << 1] -= 1;
            }
            else
                update(lson, x, y);
            if(a[rt << 1 | 1] > 1){
                a[rt << 1 | 1] -= 1;
                lz[rt << 1 | 1] -= 1;
            }
            else
                update(rson, x, y);
            pushup(rt);
        }
        return;
    }
    pushdown(rt, l, r);
    int mid = (l + r) >> 1;
    if(x <= mid)
        update(lson, x, y);
    if(mid < y)
        update(rson, x, y);
    pushup(rt);
}
int main(){
    while(scanf("%d%d", &n, &m) != EOF){
        memset(a, 0, sizeof a);
        memset(lz, 0, sizeof lz);
        memset(c, 0, sizeof c);
        for(int i = 1; i <= n; i++)
            scanf("%d", &J[i]);
        build(1, 1, n);
        while(m--){
            char op[15];
            int l, r;
            scanf("%s%d%d", op, &l, &r);
            if(op[0] == 'a')
                update(1, 1, n, l, r);
            else
                printf("%lld\n", query(r) - query(l - 1));
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值