Naive Operations HDU多校(线段树上线段果)

本文详细解析了一道关于线段树的数据结构题目,通过实例解释了如何使用线段树进行区间更新与查询操作,包括代码实现细节。

摘要生成于 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=lai/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.
1n,q100000 , 1lrn , 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
 
这题写不出的我不敢说话,
线段树上线段果,线段树上你和我!
我都要自挂线段树了
 求query ri=lai/bi⌋ 
  这个就要转化一下模型
以为向下取整 所以一开始ai=0  
所以可以每次add操作就相当于bi--
当bi等于0的时候 sum++;然后bi变回最开始的值
构造一个线段树维护区间最小值和sum
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 1e5 + 10;
 5 int n,m,a[maxn];
 6 struct node {
 7     LL val, key, lazy, sum;
 8 } tree[maxn << 2];
 9 void pushup(int rt) {
10     tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
11     tree[rt].val = min(tree[rt << 1].val, tree[rt << 1 | 1].val);
12 }
13 void pushdown(int rt) {
14     tree[rt << 1].lazy += tree[rt].lazy;
15     tree[rt << 1 | 1].lazy += tree[rt].lazy;
16     tree[rt << 1].val -= tree[rt].lazy;
17     tree[rt << 1 | 1].val -= tree[rt].lazy;
18     tree[rt].lazy = 0;
19 }
20 void build(int l, int r, int rt) {
21     tree[rt].lazy = tree[rt].sum = 0;
22     if (l == r) {
23         tree[rt].key = tree[rt].val = a[l];
24         return ;
25     }
26     int m = (l + r) >> 1;
27     build(l, m, rt << 1);
28     build(m + 1, r, rt << 1 | 1);
29     pushup(rt);
30 }
31 void update(int L, int R, int l, int r, int rt) {
32     if(l > r) return ;
33     if (tree[rt].val > 1 && l == L && r == R) {
34         tree[rt].val--;
35         tree[rt].lazy++;
36         return ;
37     }
38     if (tree[rt].val == 1 && l == r) {
39         tree[rt].sum++;
40         tree[rt].val = tree[rt].key;
41         tree[rt].lazy = 0;
42         return ;
43     }
44     if (tree[rt].lazy > 0) pushdown(rt);
45     int m = (l + r) >> 1;
46     if (R <= m) update(L, R, l, m, rt << 1);
47     else if (L > m) update(L, R, m + 1, r, rt << 1 | 1);
48     else {
49         update(L, m, l, m, rt << 1);
50         update(m + 1, R, m + 1, r, rt << 1 | 1);
51     }
52     pushup(rt);
53 }
54 LL query(int L, int R, int l, int r, int rt) {
55     if(l > r) return 0;
56     if (l == L && r == R) return tree[rt].sum;
57     int m = (l + r) >> 1;
58     if (R <= m) return query(L, R, l, m, rt << 1);
59     else if (L > m) return query(L, R, m + 1, r, rt << 1 | 1);
60     else return query(L, m, l, m, rt << 1) + query(m + 1, R, m + 1, r, rt << 1 | 1);
61 }
62 int main() {
63     while(scanf("%d%d", &n, &m) != EOF) {
64         for (int i = 1 ; i <= n ; i++ ) scanf("%d", &a[i]);
65         build(1, n, 1);
66         char str[10];
67         int x, y;
68         while(m--) {
69             scanf("%s%d%d", str, &x, &y);
70             if (str[0] == 'a')  update(x, y, 1, n, 1);
71             else printf("%lld\n", query(x, y, 1, n, 1));
72         }
73     }
74     return 0;
75 }

 

 

转载于:https://www.cnblogs.com/qldabiaoge/p/9368789.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值