【线段树】模板-HDU-1166-点改区查

2024-09-03 22:08
文章标签 模板 hdu 线段 1166 改区查

本文主要是介绍【线段树】模板-HDU-1166-点改区查,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

存个线段树模板,已经用过好多次了,好用的

【来自hh大神的模板】


例题链接: HDU-1166-敌兵布阵

#include <cstdio>
#include <iostream>
using namespace std;#define lson l, m ,rt<<1
#define rson m+1, r, rt<<1|1
const int maxn = 50050;
int sum[maxn<<2];
void PushUP(int rt)
{sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}void build(int l,int r,int rt)
{if(l == r){scanf("%d",&sum[rt]);return;}int m = (l+r) >>1;build(lson);build(rson);PushUP(rt);
}void update(int p,int add,int l,int r,int rt)
{if(l==r){sum[rt] += add;return;}int m = (l+r) >>1 ;if(p <= m) update(p,add,lson);else update(p,add,rson);PushUP(rt);
}int query(int L,int R,int l,int r,int rt)
{if(L<=l && r<=R) return sum[rt];int m = (l+r) >>1;int ret = 0;if(L <= m)ret += query(L, R, lson);if(R > m) ret += query(L, R, rson);return ret;
}int main()
{freopen("input.txt","r",stdin);int T,n,cc=1;cin>>T;while(T--){printf("Case %d:\n",cc);cc++;scanf("%d",&n);build(1,n,1);char op[10];while(scanf("%s",op)){if(op[0] == 'E')break;int a,b;scanf("%d%d",&a,&b);switch(op[0]){case 'A': update(a,b,1,n,1);break;case 'S': update(a,-b,1,n,1);break;case 'Q': printf("%d\n",query(a,b,1,n,1));break;}}}return 0;
}


这篇关于【线段树】模板-HDU-1166-点改区查的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1134180

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :