POJ 3468 A Simple Problem with Integers(线段树区间求和)
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
区间求和:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<vector>
- typedef long long LL;
- using namespace std;
- #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
- #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
- #define CLEAR( a , x ) memset ( a , x , sizeof a )
- const int maxn=100000;
- int num[maxn];
- LL sum[maxn<<2],add[maxn<<2];
- int N,Q;
- void pushup(int rs)
- {
- sum[rs]=sum[rs<<1]+sum[rs<<1|1];
- }
- void pushdown(int rs,int l)
- {
- if(add[rs])
- {
- add[rs<<1]+=add[rs];
- add[rs<<1|1]+=add[rs];
- sum[rs<<1]+=add[rs]*(l-(l>>1));
- sum[rs<<1|1]+=add[rs]*(l>>1);
- add[rs]=0;
- }
- }
- void build(int rs,int l,int r)
- {
- if(l==r)
- {
- scanf("%I64d",&sum[rs]);
- return ;
- }
- int mid=(l+r)>>1;
- build(rs<<1,l,mid);
- build(rs<<1|1,mid+1,r);
- pushup(rs);
- }
- void update(int c,int x,int y,int l,int r,int rs)
- {
- if(l>=x&&r<=y)
- {
- add[rs]+=c;
- sum[rs]+=(LL)c*(r-l+1);
- return ;
- }
- pushdown(rs,r-l+1);
- int mid=(l+r)>>1;
- if(x<=mid) update(c,x,y,l,mid,rs<<1);
- if(y>mid) update(c,x,y,mid+1,r,rs<<1|1);
- pushup(rs);
- }
- LL query(int x,int y,int l,int r,int rs)
- {
- if(l>=x&&r<=y)
- return sum[rs];
- pushdown(rs,r-l+1);
- int mid=(l+r)>>1;
- LL ans=0;
- if(x<=mid) ans+=query(x,y,l,mid,rs<<1);
- if(y>mid) ans+=query(x,y,mid+1,r,rs<<1|1);
- return ans;
- }
- int main()
- {
- int x,y,z;
- std::ios::sync_with_stdio(false);
- while(~scanf("%d%d",&N,&Q))
- {
- CLEAR(sum,0);
- CLEAR(add,0);
- build(1,1,N);
- char str[2];
- while(Q--)
- {
- scanf("%s",str);
- if(str[0]=='C')
- {
- scanf("%d%d%d",&x,&y,&z);
- update(z,x,y,1,N,1);
- }
- else
- {
- scanf("%d%d",&x,&y);
- printf("%I64d\n",query(x,y,1,N,1));
- }
- }
- }
- return 0;
- }
POJ 3468 A Simple Problem with Integers(线段树区间求和)的更多相关文章
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- POJ 3468 A Simple Problem with Integers 线段树区间修改
http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和 C A B ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
随机推荐
- CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径
推荐一篇炒鸡赞的blog. 以下代码中有打印路径. #include <algorithm> #include <iostream> #include <cstring& ...
- Android - 使用Intent来启动Activity
本文地址: http://blog.csdn.net/caroline_wendy/article/details/21455141 Intent 的用途是 绑定 应用程序组件, 并在应用程序之间进行 ...
- kaggle之旧金山犯罪
kaggle地址 github地址 特点: 离散特征 离散特征二值化处理 数据概览 import pandas as pd import numpy as np # 载入数据 train = pd.r ...
- CentOS 6.5下Percona Xtrabackup的安装错误解决方案
1.下载最新版的Xtracbackup 2.安装 yum install perl-DBIyum install perl-DBD-MySQLyum install perl-Time-HiResyu ...
- 面试前的准备---C#知识点回顾----01
过完年来,准备找份新工作,虽然手里的工作不错,但树挪死,人挪活.咱不能一直在一个坑里生活一辈子,外面的世界毕竟是很美好的. 为了能正常的找到自己中意的工作,最近是将所有的基础知识拿出来复习了一次.仅作 ...
- 简单随笔——如何在gridview的页脚显示信息。。。。
我是超级大菜鸟...哈哈 先上图看看是不是你需要的 第一步,右击gridview,在事件中,单击RowdataBond事件. 在这之前一定要记得在gridview属性中的ShowFooter设置为“t ...
- C盘扩容,超详细,史上最简单的扩容技术贴!
http://ideapad.zol.com.cn/55/160_549015.html 很多朋友跟我一样,转到windows 7 64bit后,发现以前所谓的35GB理论不够用了,哪怕你不把任何程序 ...
- c# 异步调用简单例子(转载)
首先来看一个简单的例子: 小明在烧水,等水烧开以后,将开水灌入热水瓶,然后开始整理家务 小文在烧水,在烧水的过程中整理家务,等水烧开以后,放下手中的家务活,将开水灌入热水瓶,然后继续整理家务 这也是日 ...
- 纯Html+Ajax和JSP两者对比的个人理解
最近写个人web,用jsp+servlet做,突然想到一个问题:html+ajax似乎和jsp实现效果一样:那么,两者到底有什么区别呢? 这里参考老猿的一段话: 全站ajax会维护大量的js代码,如何 ...
- linux常用命令--diff
diff是Unix系统的一个很重要的工具程序. 它用来比较两个文本文件的差异,是代码版本管理的基石之一.你在命令行下,输入: $ diff <变动前的文件> <变动后的文件> ...