线段树(结构体建法_QAQ)
线段树(结构体)模板
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
#include<string>
using namespace std;
typedef long long ll;
int ans;
struct node
{
int l, r, w;
int f;
};
struct node tree[50000 * 4 + 1];
void BuildSegmentTree(int k, int l, int r) // 建线段树
{
tree[k].l = l;
tree[k].r = r;
if(l == r)
{
scanf("%lld", &tree[k].w);
return ;
}
int m = (tree[k].l + tree[k].r) >> 1;
BuildSegmentTree(k << 1, l, m);
BuildSegmentTree(k << 1 | 1, m + 1, r);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
void down(int k) // 懒标记
{
tree[k << 1].f += tree[k].f;
tree[k << 1 | 1].f += tree[k].f;
tree[k << 1]. w += tree[k].f * (tree[k << 1].r - tree[k <<1].l + 1);
tree[k << 1 |1].w += tree[k].f * (tree[k << 1| 1].r - tree[k << 1| 1].l + 1);
tree[k].f = 0;
}
void Single_Point_Modification(int k, int x, int y) // 单点修改
{
if(tree[k].l == tree[k].r)
{
tree[k].w += y;
return ;
}
int m = (tree[k].l + tree[k].r) >> 1;
if(x <= m) Single_Point_Modification(k << 1,x, y);
else Single_Point_Modification(k << 1 | 1, x, y);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1]. w;
}
void Single_Point_Query(int k, int x) // 单点查询
{
if(tree[k].l == tree[k].r)
{
ans = tree[k].w;
return ;
}
if(tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1;
if(x <= m) Single_Point_Query(k << 1, x);
else Single_Point_Query(k << 1 | 1, x);
}
void Interval_modification(int k, int x, int y,int z) //区间修改
{
if(tree[k].l >= x && tree[k].r <= y)
{
tree[k].w += (tree[k].r - tree[k].l + 1) * z;
tree[k].f += z;
return;
}
if(tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1;
if(x <= m) Interval_modification(k << 1, x, y, z);
if(y > m) Interval_modification(k << 1 | 1, x, y, z);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
void Interval_Query(int k, int x, int y) //区间查询
{
if(tree[k].l >= x && tree[k].r <= y)
{
ans += tree[k].w;
return ;
}
if(tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) / 2;
if(x <= m) Interval_Query(k << 1, x, y);
if(y > m) Interval_Query(k << 1 | 1, x, y);
}
char s[10];
int main()
{
int T, n;
while(~scanf("%d",&T))
{
int cas = 0;
while(T--)
{
scanf("%d", &n);
BuildSegmentTree(1,1,n);
printf("Case %d:\n",++ cas);
while(1)
{
int x, y, z;
getchar();
scanf("%s",s);
if(s[0] == 'E')break;
else if(s[0]== 'A') // 区间修改
{
scanf("%d %d %d", &x, &y, &z);
Interval_modification(1,x,y,z);
}
else if(s[0] =='B') // 区间查询
{
ans = 0;
scanf("%d %d", &x, &y);
Interval_Query(1, x, y);
printf("%d\n",ans);
}
else if(s[0]=='C') // 单点修改
{
scanf("%d %d", &x, &y);
Single_Point_Modification(1,x,y);
}
else if(s[0]=='D') // 单点查询
{
ans = 0;
scanf("%d", &x);
Single_Point_Query(1,x);
printf("%d\n",ans);
}
}
}
}
return 0;
}
线段树(结构体建法_QAQ)的更多相关文章
- [BZOJ3681]Arietta(可持久化线段树合并优化建图+网络流)
暴力建图显然就是S->i连1,i->j'连inf(i为第j个力度能弹出的音符),j'->T连T[j]. 由于是“某棵子树中权值在某区间内的所有点”都向某个力度连边,于是线段树优化建图 ...
- [COCI2017.1]Deda —— 解锁线段树的新玩法
众所周知,能用线段树做的题一定可以暴力 但考场上也只能想到暴力了,毕竟还是对线段树不熟练. deda 描述 有一辆车上有n个小孩,年龄为1~n,然后q个询问,M X A代表在第X站时年龄为A的小孩会下 ...
- hdu 1754(基础线段树) I Hate It
http://acm.hdu.edu.cn/showproblem.php?pid=1754 数据比较大,暴力会超时,所以明显是线段树,普通的线段树,结构体中多开一个值sum储存每个子区间的最大成绩, ...
- 【bzoj4383】[POI2015]Pustynia 线段树优化建图+差分约束系统+拓扑排序
题目描述 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息,每条信息包含三个数l,r,k以及接下来k个正整数,表示a[l],a[l+1],...,a[r- ...
- 主席树总结(经典区间第k小问题)(主席树,线段树)
接着上一篇总结--可持久化线段树来整理吧.点击进入 这两种数据结构确实有异曲同工之妙.结构是很相似的,但维护的主要内容并不相同,主席树的离散化.前缀和等思想也要更难理解一些. 闲话 话说刚学习主席树的 ...
- codeforces 242E. XOR on Segment 线段树
题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...
- Billboard HDU 2795 (线段树)
题目链接 Problem Description At the entrance to the university, there is a huge rectangular billboard of ...
- Codeforces 786B. Legacy 线段树+spfa
题目大意: 给定一个\(n\)的点的图.求\(s\)到所有点的最短路 边的给定方式有三种: \(u \to v\) \(u \to [l,r]\) \([l,r] \to v\) 设\(q\)为给定边 ...
- 【洛谷2605】[ZJOI2010] 基站选址(线段树维护DP)
点此看题面 大致题意: 有\(n\)个村庄,每个村庄有\(4\)个属性:\(D_i\)表示与村庄\(1\)的距离,\(C_i\)表示建立基站的费用,\(S_i\)表示能将其覆盖的建基站范围,\(W_i ...
随机推荐
- Mongo DB分片
分片,指的就是把数据拆分,将其分散到不同机器上的过程.MongoDB支持自动分片,对应用而言,好像始终和一个单机的服务器交互一样. 分片和复制复制是让多台服务器拥有相同的数据副本,而分片是每个分片都拥 ...
- Docker启动Mongo报警告WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
警告信息 2019-11-27T09:28:16.659+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_h ...
- 2019年Java后端工程师常见面试题和感想
来新公司有5个月了,从第二个月开始就参与公司后端工程师的面试工作了,包括校招在内,面试超过100个(包括40个校招的终面)应聘者了,应聘者中有超过10年的技术经理,有6年以上的高级开发,有3到5年的中 ...
- flex布局下img图片变形的解决方法
图片正常效果 图片变形效果 一.flex-shrink: 0 给 img 设置 flex-shrink: 0; flex-shrink 的默认值为1,如果没有显示定义该属性,将会自动按照默认值 ...
- 检测jquery是否正确引入
if(typeof(jQuery)=="undefined"){ alert("jQuery is not imported"); }else{ alert(& ...
- 【Mysql MHA】CentOS7.6+Mysql8.0.16 入坑
1.防火墙 firewall-cmd --add-port=/tcp --permanent firewall-cmd --reload 2.SELINUX sed -i 's/SELINUX=enf ...
- webpack中使用WebpackDevServer实现请求转发
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- C++---初识《通过g++ / makefile 编译和调用动态库so文件》(ubuntu)
C++---初识<通过g++ / makefile 编译和调用动态库so文件>(ubuntu) ------------------------目录------------------- ...
- Paper Reading:word2vec Parameter Learning Explained
论文:word2vec Parameter Learning Explained 发表时间:2016 发表作者:Xin Rong 论文链接:论文链接 为了揭开Word2vec的神秘面纱,不得不重新整理 ...
- B-Tree目录和Hash索引的区别
Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要远高于 B-T ...