线段树(结构体建法_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 ...
随机推荐
- laravel中引入composer安装在vendor中的第三方组件
一.安装第三方组件 方法一:使用命令行安装第三方(已phpword为例): composer require phpoffce/phpword ^v0..* 方法二: 修改主项目composer.js ...
- JS 百度地图 地图线路描绘
JS 百度地图 地图线路描绘 <script type="text/javascript" src="http://api.map.baidu.com/api?v= ...
- SAP WebIDE里UI5应用的隐藏文件project.json
在SAP WebIDE UI5应用编辑器里的菜单View->Show Hidden files点击后,即可发现项目文件夹下有一个隐藏文件project.json: 内容如下: 这也解释了为什么b ...
- Input system (输入子系统)
Input system (输入子系统) 以前写一些输入设备(键盘,鼠标等)的驱动都是字符设备,混杂设备处理的,linux开源社区的大神门看到了这大量的输入设备如此分散不堪,就想有木有一种机制,可以对 ...
- 批量删除redis
批量删除"aso_"开头的key:redis-cli keys aso_* | xargs redis-cli del
- 彻底弄懂HTTP缓存机制及原理-转载
首先附上原文地址,非常感谢博主大神的分享彻底弄懂HTTP缓存机制及原理 前言 Http 缓存机制作为 web 性能优化的重要手段,对于从事 Web 开发的同学们来说,应该是知识体系库中的一个基 ...
- wakelock查看
Android的wakelock分为两层 待机异常https://wenku.baidu.com/view/6b765c8802020740be1e9bd8.html Linux层和应用层 查看Lin ...
- k2系列-服务器管理篇
k2服务器即K2 WORKSPACE管理介绍: k2 管理平台统一管理基于K2开发的所有流程的跟踪调试以及基本配置信息. 具体完成的操作有以下几个部分: 1 配置K2环境相关属性.包括全局变量等 2 ...
- MySQL-进阶7-子查询 - select后/where后/from后/ []where后/having后] / exists后面 的相关子查询
/*SQL-进阶7-子查询 含义:出现在其他语句中的select 语句,称为子查询或内查询 外部的查询语句,称为主查询 或者 外查询 分类1:按子查询出现的位置———— select 后面:仅仅支持标 ...
- 《流畅的Python》Object References, Mutability, and Recycling--第8章
Object References, Mutability, and Recycling 本章章节: Variables Are Not Boxes identity , Equality , Al ...