【POJ3468】【zkw线段树】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
Source
/*
宋代陆游
《临安春雨初霁》 世味年来薄似纱,谁令骑马客京华。
小楼一夜听春雨,深巷明朝卖杏花。
矮纸斜行闲作草,晴窗细乳戏分茶。
素衣莫起风尘叹,犹及清明可到家。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int INF = 0x7fffffff;
const int MAXN = + ;
const int maxnode = ;
using namespace std;
struct Node{
int l, r;
long long sum, d;
}tree[maxnode];
int data[MAXN], M;//M为总结点个数 //初始化线段树
void build(int l, int r){
for (int i = * M - ; i > ; i--){
if (i >= M){
tree[i].sum = data[i - M];
tree[i].l = tree[i].r = (i - M);//叶子节点
}
else {
tree[i].sum = tree[i<<].sum + tree[(i<<)+].sum;
tree[i].l = tree[i<<].l;
tree[i].r = tree[(i<<)+].r;
}
}
}
long long query(int l, int r){
long long sum = ;//sum记录和
int numl = , numr = ;
l += M - ;
r += M + ;
while ((l ^ r) != ){
if (~l&){//l取反,表示l是左节点
sum += tree[l ^ ].sum;
numl += (tree[l ^ ].r - tree[l ^ ].l + );
}
if (r & ){
sum += tree[r ^ ].sum;
numr += (tree[r ^ ].r - tree[r ^ ].l + );
}
l>>=;
r>>=;
//numl,numr使用来记录标记的
sum += numl * tree[l].d;
sum += numr * tree[r].d;
}
for (l >>= ; l > ; l >>= ) sum += (numl + numr) * tree[l].d;
return sum;
} void add(int l, int r, int val){
int numl = , numr = ;
l += M - ;
r += M + ;
while ((l ^ r) != ){
if (~l&){//l取反
tree[l ^ ].d += val;
tree[l ^ ].sum += (tree[l ^ ].r - tree[l ^ ].l + ) * val;
numl += (tree[l ^ ].r - tree[l ^ ].l + );
}
if (r & ){
//更新另一边
tree[r ^ ].d += val;
tree[r ^ ].sum += (tree[r ^ ].r - tree[r ^ ].l + ) * val;
numr += (tree[r ^ ].r - tree[r ^ ].l + );
}
l>>=;
r>>=;
tree[l].sum += numl * val;
tree[r].sum += numr * val;
}
//不要忘了往上更新
for (l >>= ; l > ; l >>= ) tree[l].sum += (numl+numr) * val;
}
int n, m; void init(){
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) scanf("%d", &data[i]);
M = ;while (M < (n + )) M <<= ;//找到最大的段
build(, M - );
}
void work(){
while (m--){
char str[];
scanf("%s", str);
if (str[] == 'Q'){
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", query(l, r));
}
else{
int val, l, r;
scanf("%d%d%d", &l, &r, &val);
if (val != ) add(l, r, val);
}
}
} int main(){ init();
work();
return ;
}
【POJ3468】【zkw线段树】A Simple Problem with Integers的更多相关文章
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3468:A Simple Problem with Integers(线段树模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 149972 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
随机推荐
- std::numeric_limits<int>::max() error C2589: '(' : illegal token on right side of '::' 解决办法
int max =std::numeric_limits<int>::max(); 根据错误提示: f:\code\cpp\webspider\main.cpp(47) : war ...
- Performance Optimization (2)
DesktopGood performance is critical to the success of many games. Below are some simple guidelines f ...
- poj 1287 Networking【最小生成树prime】
Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7321 Accepted: 3977 Descri ...
- string字符串转成16进制
package util; public class EscapeUnescape { public static String escape(String src) { int i; char j; ...
- 使用PDO连接数据库 查询和插入乱码的解决方法
问题:PDO连接数据库后,查询和插入中文到数据库,出现乱码,如图: 解决方法: 法1: try{ $opts_values = array(PDO::MYSQL_ATTR_INIT_COMMAND=& ...
- Winform 中 DesignMode 返回值不正确的问题。
本文转载:http://blog.csdn.net/sabty/article/details/5325260 以前也曾遇到这样的问题,不过影响不大也没有去详细了解.今天又重新遇到此问题,实在太不便. ...
- 应用层协议实现系列(一)——HTTPserver之仿nginx多进程和多路IO的实现
近期在尝试自己写一个Httpserver,在粗略研究了nginx的代码之后,决定仿照nginx中的部分设计自己实现一个高并发的HTTPserver,在这里分享给大家. 眼下使用的较多的Httpserv ...
- C# richTextBox 重下往上依次查找关键字
private void richTextBox1_SelectionChanged(object sender, EventArgs e) { pos = richTextBox1.Sele ...
- 基于XMPP实现的Openfire的配置安装+Android客户端的实现[转]
最近在整理一些这方面的资料,闲话少说,咱还是直奔主题吧 :) http://blog.csdn.net/sk719887916/article/details/40541163 https://git ...
- linux下sqlite3可视化工具
1.介绍:sqlite3是linux上的小巧的数据库,一个文件就是一个数据库. 2.安装:要安装sqlite3,可以在终端提示符后运行下列命令:sudo apt-get install sqlite3 ...