poj3468 线段树区间更新+区间查询
题目链接:
http://poj.org/problem?id=3468
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 149649 Accepted: 46438 Case Time Limit: 2000MS Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4
Sample Output
4 55 9 15
Hint
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly--2007.11.25, Yang Yi
[Submit] [Go Back] [Status] [Discuss]
区间更新和和区间查询的模板
This is the code
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x3f3f3f3f //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};const int MAXN=100005;
LL sum[MAXN<<2],lazy[MAXN<<2];
//sum[]模拟线段树维护区间和,lazy[]为懒惰标记void PushUp(int i)
{sum[i]=sum[i<<1]+sum[i<<1|1];
}void PushDown(int i,int ln,int rn)//ln表示左子树元素结点个数,rn表示右子树结点个数
{if (lazy[i]){lazy[i<<1]+=lazy[i];lazy[i<<1|1]+=lazy[i];sum[i<<1]+=lazy[i]*ln;sum[i<<1|1]+=lazy[i]*rn;lazy[i]=0;}
}
void Build(int l,int r,int i)//建树,Build(1,n,1);
{if (l==r){scanf("%lld",&sum[i]);return;}int mid=(l+r)>>1;Build(l,mid,i<<1);Build(mid+1,r,i<<1|1);PushUp(i);
}void Add(int L,int C,int l,int r,int i)//单点更新,Add(L,C,1,n,1);
{if (l==r){sum[i]+=C;return;}int mid=(l+r)>>1;PushDown(i,mid-l+1,r-mid); //若既有点更新又有区间更新,需要这句话if (L<=mid)Add(L,C,l,mid,i<<1);elseAdd(L,C,mid+1,r,i<<1|1);PushUp(i);
}void Update(int L,int R,int C,int l,int r,int i)//区间更新,Update(L,R,C,1,n,1);
{if (L<=l&&r<=R){sum[i]+=C*(r-l+1);lazy[i]+=C;return;}int mid=(l+r)>>1;PushDown(i,mid-l+1,r-mid);if (L<=mid)Update(L,R,C,l,mid,i<<1);if (R>mid)Update(L,R,C,mid+1,r,i<<1|1);PushUp(i);
}LL Query(int L,int R,int l,int r,int i)//区间查询,Query(L,R,1,n,1);
{if (L<=l&&r<=R)return sum[i];int mid=(l+r)>>1;PushDown(i,mid-l+1,r-mid);//若更新只有点更新,不需要这句LL ANS=0;if (L<=mid)ANS+=Query(L,R,l,mid,i<<1);if (R>mid)ANS+=Query(L,R,mid+1,r,i<<1|1);return ANS;
}int main()
{//freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);//freopen("D:\\chnegxubianji\\inORout\\out.txt", "w", stdout);int n,q;scanf("%d%d",&n,&q);Build(1,n,1);while(q--){char str;scanf(" %c",&str);if(str=='Q'){int x,y;scanf("%d%d",&x,&y);printf("%lld\n",Query(x,y,1,n,1));}else{int x,y,w;scanf("%d%d%d",&x,&y,&w);Update(x,y,w,1,n,1);}}return 0;
}
poj3468 线段树区间更新+区间查询相关推荐
- 石油大 2019年第二阶段我要变强个人训练赛第十八场 Problem N 扶桑号战列舰(线段树+区间更新+区间查询)
链接:http://icpc.upc.edu.cn/problem.php?cid=1803&pid=13 题意:给出一个n,接下来一行给出n个数.才开始所有数为0,每次操作可以选一个区间[l ...
- POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新
直接将这3题 放一起了 今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...
- python:线段树区间修改 + 区间查询 模板 + 坑点总结
from functools import reduceclass SegTree:'''支持增量更新,覆盖更新,序列更新,任意RMQ操作基于二叉树实现初始化:O(1)增量更新或覆盖更新的单次操作复杂 ...
- hdu 5692 Snacks(dfs序+线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5692 解题思路:这道题是树节点的点权更新,而且涉及到子树,常用的思路是利用dfs序,用线段树来对区间进 ...
- hdu 1698(线段树区间更新)
解题思路:线段树区间更新水题. #include<iostream> #include<cstdio> #include<cstring> using namesp ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- hdu 3966(树链剖分+线段树区间更新)
传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...
- Just a Hook(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 In the game of DotA, Pudge's meat hook is actual ...
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,"模拟都市"是他们非常喜欢的一个游戏 ...
最新文章
- Typescript学习笔记(二)
- 利用 .NET Framework 命令行工具
- pythonexe32位-如何使用pyinstaller打包32位的exe
- hibernate中List一对多映射关系详解
- hive mysql5.7_安装并使用mysql5.7作为hive的metastore
- Java核心(五)深入理解BIO、NIO、AIO
- windows安装MySQL数据库【附安装文档和安装包】
- Linux高级权限管理
- 【MySQL】明明加了索引,为何不生效?
- android开发mvp,Android开发模式之MVP
- 集合A和集合B的并运算图示
- 百度相关搜索是怎么出现的如何利用
- python打印大写字母_Python: 打印出大写字符C
- 数学与计算机科学研究生大学排名,计算数学专业考研院校排名
- 华为设备配置PoE功能
- 阿里巴巴校招三面面经
- 安卓 下载pdf到手机存储
- 进阶六之Android UI介面之(介面3D旋转)
- LindedList相关介绍
- 矩阵的乘法口诀(二)
热门文章
- JS高德地图计算两地之间的实际距离
- 正则表达式如何获取指定数字
- 这些题目,据说只有程序员才懂得答案?
- mysql注入和预防
- 南山中学2021级2班高考成绩查询,四川省绵阳南山中学2021年排名
- ISO/OSI参考模型总结
- 编写一个c语言程序 求e的值,编写一个程序求e的值_相关文章专题_写写帮文库
- python批量读取文件赋值给一个参数_python txt中的文件,逐行读取,每行赋值给变量...
- 我看BenQ和Siemens
- python操作数据库出现错误 : Unknown column 'XXXX' in 'where clause'amp;amp;amp;amp;amp;amp;amp;amp;quot;