A Simple Problem with Integers~POJ - 3468
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
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<queue>
- using namespace std;
- #define maxn 100005
- long long sum[maxn<<],add[maxn<<];
- void pushup(int rt) {
- sum[rt]=sum[rt<<]+sum[rt<<|];
- }
- void pushdown(int rt ,int l) {
- if (add[rt]) {
- add[rt<<]+=add[rt];
- add[rt<<|]+=add[rt];
- sum[rt<<]+=add[rt]*(l-(l>>));
- sum[rt<<|]+=add[rt]*(l>>);
- add[rt]=;
- }
- }
- void build(int l,int r,int rt) {
- if (l==r) {
- scanf("%lld",&sum[rt]);
- return ;
- }
- int m=(l+r)>>;
- build(l,m,rt<<);
- build(m+,r,rt<<|);
- pushup(rt);
- }
- void updata(int x,int y,int z,int l,int r,int rt) {
- if (x<=l && r<=y) {
- add[rt]+=z;
- sum[rt]+=(long long)z*(r-l+);
- return ;
- }
- pushdown(rt,r-l+);
- int m=(l+r)>>;
- if (x<=m) updata(x,y,z,l,m,rt<<);
- if (y>m) updata(x,y,z,m+,r,rt<<|);
- pushup(rt);
- }
- long long query(int x,int y,int l,int r,int rt) {
- long long ans=;
- if (x<=l && r<=y ) return sum[rt];
- pushdown(rt,r-l+);
- int m=(l+r)>>;
- if (x<=m) ans+=query(x,y,l,m,rt<<);
- if (y>m ) ans+=query(x,y,m+,r,rt<<|);
- return ans;
- }
- int main() {
- int n,q;
- while(scanf("%d%d",&n,&q)!=EOF) {
- build(,n,);
- char b[];
- int x,y,z;
- while(q--) {
- scanf("%s",b);
- if (b[]=='Q') {
- scanf("%d%d",&x,&y);
- printf("%lld\n",query(x,y,,n,));
- } else {
- scanf("%d%d%d",&x,&y,&z);
- updata(x,y,z,,n,);
- }
- }
- }
- return ;
- }
A Simple Problem with Integers~POJ - 3468的更多相关文章
- A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 69589 ...
- A Simple Problem with Integers POJ - 3468 (分块)
题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
- C - A Simple Problem with Integers - poj 3468(区间更新)
题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每 ...
- C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...
- A Simple Problem with Integers POJ - 3468 (线段树)
思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- C - A Simple Problem with Integers
C - A Simple Problem with Integers POJ - 3468 思路:线段树区间修改区间查询.又出现了 C++ WA G++ AC的尴尬局面. #include& ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
随机推荐
- MOBA战斗服务器设计思路
MOBA作为竞技类的游戏,游戏中实时高精度同步,或者又说延迟容错率的要求还算是比较高的一种. 如何做到这种同步机制呢? 常用的同步机制有两种类型:帧同步 / 指令同步 何谓帧同步? 保证双方客户端逻辑 ...
- BZOJ 3198: [Sdoi2013]spring [容斥原理 哈希表]
3198: [Sdoi2013]spring 题意:n个物品6个属性,求有多少不同的年份i,j满足有k个属性对应相等 一开始读错题了,注意是对应相等 第i个属性只能和第i个属性对应 容斥一下 \[ 恰 ...
- Sql2012数据库还原
Sql2012数据库还原(通过.bak数据库备份文件) 昨天系统挂了,那叫一个悲惨,重装了系统,但是sql2012的数据没有备份,同事帮忙发来备份文件(.bak),开始还原数据. 步骤:1 自己新建一 ...
- jira + confluence 安装和破解
Window环境: 环境准备 安装JAVA1.8以上版本 安装SQL SERVER 或 MySQL: jira安装和破解 下载安装包 https://downloads.atlassian.com/s ...
- 利用Lua读写本地文件
缘由 今天在使用Lua编写脚本时,需要用到读写文件的操作,很久没有使用Lua了,特写下此文来备忘一下. 简介 Lua对文件的操作与C对文件的操作基本一致,不管是参数还是方法.Lua中可以直接通过全局方 ...
- 游戏服务器设计之NPC系统
游戏服务器设计之NPC系统 简介 NPC系统是游戏中非常重要的系统,设计的好坏很大程度上影响游戏的体验.NPC在游戏中有如下作用: 引导玩家体验游戏内容,一般游戏内有很多主线.支线任务,而任务的介绍. ...
- error: Autoconf version 2.67 or higher is required
error: Autoconf version 2.67 or higher is required 今天linux下遇到这种错误,顺便记录下来. #rpm -qf /usr/bin/autoconf ...
- php 生成缩略图
$src = '4.jpg'; list($width,$height) = getimagesize($src); $im = imagecreatefromjpeg($src); $panl = ...
- PHP执行Session与前端JS之间的关系
<?php error_reporting(0); $path = './tmp/'; $sess_name = session_name(); echo $sess_name; $sess_i ...
- CentOS 6下编译安装MySQL 5.6
一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | grep mysql 有的话通过下面的命令来卸载掉 rpm -e mysql //普通删除模式 rpm -e ...