hdoj-6315 18年多校第二场G题-Naive Operations 线段树

本文详细解析HDOJ 6315 Naive Operations题目的解题思路,介绍如何通过线段树实现区间加法与查询操作,特别关注区间最小值与向下取整运算。

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

题目链接

hdoj-6315 Naive Operations

题目

Naive Operations

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


 

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:  6408 6407 6406 6405 6404 

题意

要求有2个对应序列,假设序列长度为n,a全部是0,b分别为1-n这n个数。现在对区域有2种操作:
1.add l r 对于a序列,[l,r]区间全部+1
2.query l r 对于a、b序列,查找[l,r]区间内(数学公式看原题)

题解

        这个题仿佛回到了高中渣生时代,啊X题我知道要用A来做,然后就不会了= =。更吊的是看完题解,woc这么简单然后写不出来。不敢想象能这么菜啊QAQ。

        总之,貌似是第二场比较简单的题了,碰见一直加一的,可以转化成减一,因为是除法并且是向下取整,那么对于序列b,我们可以在每次add的时候,减一,当减到0说明可以发生整除了,此时sum线段树的对应结点+1。因为是向下取整,所以在减不到0的时候永远是sum的值。

        在减到0的时候要恢复原值,多开辟一个数组储存原值就行了。线段树sum初始全为0,正常操作就可以。b线段树结点储存区间最小值。当碰见区间最小值为1的时候,并且此时是叶子结点,那么再对sum进行操作。

        看了很多题解基本一样了,感觉我表达的很菜= =,可以看看其他的解法。基本都是一样的,维护2个线段树。

C++ 1060ms AC

#include<iostream>
#include<vector>
#include<list>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<queue>
#include<sstream>
#include<iterator>
#include<math.h>
#include<malloc.h>
#include<string.h>
#include<stack>
#define TIME std::ios::sync_with_stdio(false)
#define LL long long
#define MAX 100100
#define INF 0x3f3f3f3f

using namespace std;

int tree[MAX*4], yuan[MAX], sum[MAX*4], lazy[MAX*4];
int N,M;

int Min(int a, int b) {
    return a > b ? b : a;
}

void push_down(int node) {
    if (lazy[node]) {
        lazy[node*2] += lazy[node];
        lazy[node*2 + 1] += lazy[node];
        tree[node*2] -= lazy[node]; // 不用担心减成负数,a存最小值,update中2个if不满足才进入push_down
        tree[node*2 + 1] -= lazy[node];
        lazy[node] = 0;
    }
}

void build(int l, int r, int node) {
    lazy[node] = 0;
    sum[node] = 0;
    if (l == r) {
        tree[node] = yuan[l]; // 这样定位比较节省空间,感谢x同学提供的写法,之前是直接写一个线段树用node定位
        return;
    }
    int mid = (l + r) / 2;
    build(l, mid, node * 2);
    build(mid + 1, r, node*2 + 1);
    tree[node] = Min(tree[node*2], tree[node*2 + 1]);
    sum[node] = sum[node*2] + sum[node*2 + 1];
}

void update_range(int L, int R, int l, int r, int node) {
    if (tree[node] > 1 && L <= l && r <= R) {
        lazy[node]++;
        tree[node]--;
        return;
    }
    if (tree[node] == 1 && l == r) {
        sum[node]++;
        lazy[node] = 0;
        tree[node] = yuan[l]; // 重置
        return;
    }
    int mid = (l + r) / 2;
    push_down(node);
    if (L <= mid) update_range(L, R, l, mid, node * 2);
    if (R > mid) update_range(L, R, mid + 1, r, node * 2 + 1);
    tree[node] = Min(tree[node * 2], tree[node * 2 + 1]);
    sum[node] = sum[node * 2] + sum[node * 2 + 1];
}

int query_range(int L, int R, int l, int r, int node) {
    if (L <= l && r <= R) return sum[node];
    int mid = (l + r) / 2;
    int ans = 0;
    push_down(node);
    if (L <= mid) ans += query_range(L, R, l, mid, node * 2);
    if (R > mid) ans += query_range(L, R, mid + 1, r, node * 2 + 1);
    tree[node] = Min(tree[node*2], tree[node*2 + 1]);
    sum[node] = sum[node*2] + sum[node*2 + 1];
    return ans;
}

int main() {
    int l, r;
    string sw;
    while (scanf("%d%d", &N, &M) != EOF) {
        for (int i = 1; i <= N; i++) {
            scanf("%d", &yuan[i]);
        }
        build(1, N, 1);
        while(M--) {
            scanf("%s%d%d", &sw, &l, &r);
            if (sw[0] == 'a') {
                update_range(l, r, 1, N, 1);
            }else {
                printf("%d\n", query_range(l, r, 1, N, 1));
            }
        }
    }

    system("pause");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值