线段树(结构体)模板

#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)的更多相关文章

  1. [BZOJ3681]Arietta(可持久化线段树合并优化建图+网络流)

    暴力建图显然就是S->i连1,i->j'连inf(i为第j个力度能弹出的音符),j'->T连T[j]. 由于是“某棵子树中权值在某区间内的所有点”都向某个力度连边,于是线段树优化建图 ...

  2. [COCI2017.1]Deda —— 解锁线段树的新玩法

    众所周知,能用线段树做的题一定可以暴力 但考场上也只能想到暴力了,毕竟还是对线段树不熟练. deda 描述 有一辆车上有n个小孩,年龄为1~n,然后q个询问,M X A代表在第X站时年龄为A的小孩会下 ...

  3. hdu 1754(基础线段树) I Hate It

    http://acm.hdu.edu.cn/showproblem.php?pid=1754 数据比较大,暴力会超时,所以明显是线段树,普通的线段树,结构体中多开一个值sum储存每个子区间的最大成绩, ...

  4. 【bzoj4383】[POI2015]Pustynia 线段树优化建图+差分约束系统+拓扑排序

    题目描述 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息,每条信息包含三个数l,r,k以及接下来k个正整数,表示a[l],a[l+1],...,a[r- ...

  5. 主席树总结(经典区间第k小问题)(主席树,线段树)

    接着上一篇总结--可持久化线段树来整理吧.点击进入 这两种数据结构确实有异曲同工之妙.结构是很相似的,但维护的主要内容并不相同,主席树的离散化.前缀和等思想也要更难理解一些. 闲话 话说刚学习主席树的 ...

  6. codeforces 242E. XOR on Segment 线段树

    题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...

  7. Billboard HDU 2795 (线段树)

    题目链接 Problem Description At the entrance to the university, there is a huge rectangular billboard of ...

  8. Codeforces 786B. Legacy 线段树+spfa

    题目大意: 给定一个\(n\)的点的图.求\(s\)到所有点的最短路 边的给定方式有三种: \(u \to v\) \(u \to [l,r]\) \([l,r] \to v\) 设\(q\)为给定边 ...

  9. 【洛谷2605】[ZJOI2010] 基站选址(线段树维护DP)

    点此看题面 大致题意: 有\(n\)个村庄,每个村庄有\(4\)个属性:\(D_i\)表示与村庄\(1\)的距离,\(C_i\)表示建立基站的费用,\(S_i\)表示能将其覆盖的建基站范围,\(W_i ...

随机推荐

  1. 0160 十分钟看懂时序数据库(I)-存储

    摘要:2017年时序数据库忽然火了起来.开年2月Facebook开源了beringei时序数据库:到了4月基于PostgreSQL打造的时序数据库TimeScaleDB也开源了,而早在2016年7月, ...

  2. IntelliJ Idea清除Open Recent里面的项目列表

    2种方法清除IntelliJ Idea 中 Open Recent里面的项目列表 第一种方法: 如下图: Open Recent -> Manage Projects Recent Projec ...

  3. (三)Activiti之第一个程序以及Activiti插件的使用和Activiti表的解释

    一.案例 1.1 建立Activiti Diagram图 new -> activiti ->Activiti Diagram,创建一个HelloWorld文件,后缀自动为bpmn,如下图 ...

  4. (三)第一个 Hibernate项目

    1.创建java工程,并导入hibernate所需要的jar包 2.通过IDE构建一个基础的Hibernate工程. 产生    hibernate.cfg.xml的框架总配置文件.        H ...

  5. Java Web 深入分析(1)B/S架构概述

    B/S结构即浏览器和服务器结构.它是随着Internet技术的兴起,对C/S结构的一种变化或者改进的结构.在这种结构下,用户工作界面是通过WWW浏览器来实现,极少部分事务逻辑在前端(Browser)实 ...

  6. 前端开发 Vue -1windows环境搭建Vue Node开发环境

    解决几个疑问: 想学习下vue.js,我理解的它是一个前端的框架,主要作用是对数据的处理,和juqery类似,所以不太理解为什么要在nodejs中npm install vue呢?在html文件中引入 ...

  7. spring源码(1)---idea基础环境搭建

    一.环境准备 1. jdk1.8.1 做java开发的这个应该能自己找到 2.gradle-4.9 https://services.gradle.org/distributions/ 没用过grad ...

  8. man手册--iostat

    iostat手册 IOSTAT(1) Linux User's Manual IOSTAT(1) NAME iostat - Report Central Processing Unit (CPU) ...

  9. Computer Vision_33_SIFT:Remote Sensing Image Registration With Modified SIFT and Enhanced Feature Matching——2017

    此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...

  10. Codeforces 853B Jury Meeting

    题意 从城市1-n来的评审团到城市0商讨国家大事,离开和抵达的那一天不能讨论,飞机均当天抵达,给出所有飞机起飞抵达代价情况,问能否使所有评审员聚齐连续k天并返回,并求最小代价 思路 从前向后扫一遍,求 ...