线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)
A Simple Problem with Integers
Time Limit: 10000MS
Memory Limit: 65536K
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.
解题心得:
- 这是一个最简单的线段树,题意就是给你一系列数,每次询问l到r的和或者每次在l到r之间的每一个数加上一个数。很简单啊,但是万万没想到比赛居然崩在这个题上面,将r和R传参的时候写反了,tm样例和所有自己造的数据都过了,哎,已砍手。google翻译还吧这题翻译成了神题,我去。
- 这个题还是很有教训的,线段树的代码比较麻烦,要不停的向上维护,向下传递,不停的传递参数,所以在写的时候一定要注意,写慢一点,不然线段树出现了bug很难找,思路不复杂别死在了手贱上面。线段树的lazy标记的时候一定要记得下移,以及在每次递归之后记得向上维护,向上维护的时候一定要注意父节点和两个子节点的关系,该传递一些什么,别和lazy下移的时候搞混了。
#include<cstring>
#include<stdio.h>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<math.h>
using namespace std;
const int maxn = 1e6+100;
struct node
{
long long l,r,sum,lazy;
}tree[maxn<<2];
void pushup(long long root)
{
tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
}
void pushdown(long long root)
{
if(tree[root].lazy == 0)
return ;
tree[root<<1].lazy += tree[root].lazy;
tree[root<<1|1].lazy += tree[root].lazy;
tree[root<<1].sum += (tree[root<<1].r - tree[root<<1].l + 1)*tree[root].lazy;
tree[root<<1|1].sum += (tree[root<<1|1].r - tree[root<<1|1].l + 1)*tree[root].lazy;
tree[root].lazy = 0;
}
void buildtree(long long l,long long r,long long root)
{
tree[root].l = l;
tree[root].r = r;
tree[root].lazy = 0;
if(l == r)
{
scanf("%lld",&tree[root].sum);
return ;
}
long long mid = (l + r) >> 1;
buildtree(l,mid,root<<1);
buildtree(mid+1,r,root<<1|1);
pushup(root);
}
long long query(long long L,long long R,long long l,long long r,long long root)
{
if(l == L && R ==r)
{
return tree[root].sum;
}
long long mid = (l + r)>>1;
pushdown(root);
if(R <= mid)
{
return query(L,R,l,mid,root<<1);
}
else if(L > mid)
{
return query(L,R,mid+1,r,root<<1|1);
}
else
return query(mid+1,R,mid+1,r,root<<1|1)+ query(L,mid,l,mid,root<<1);//这里手贱找了一个小时的bug
pushup(root);
}
void add(long long L,long long R,long long l,long long r,long long root,long long h)
{
if(l == L && R ==r)
{
tree[root].lazy += h;
tree[root].sum += (r - l +1)*h;
return;
}
pushdown(root);
long long mid = (l + r) >> 1;
if(R <= mid)
add(L,R,l,mid,root<<1,h);
else if(L > mid)
add(L,R,mid+1,r,root<<1|1,h);
else
{
add(L,mid,l,mid,root<<1,h);
add(mid+1,R,mid+1,r,root<<1|1,h);
}
pushup(root);
}
int main()
{
long long n,m;
while(scanf("%lld%lld",&n,&m) != EOF)
{
long long sum = 0;
buildtree(1,n,1);
while(m--)
{
long long a,b,h;
char c[10];//尽量别输入%c,容易挂掉,%s挺好的
scanf("%s",&c);//这里也要注意一下输入的问题,不同的字符对应的不同个数的int输入
if(c[0] == 'C')
{
long long h;
scanf("%lld%lld%lld",&a,&b,&h);
add(a,b,1,n,1,h);
}
else if(c[0] == 'Q')
{
scanf("%lld%lld",&a,&b);
sum = query(a,b,1,n,1);
printf("%lld\n",sum);
}
}
}
return 0;
}
线段树:POJ3468-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: 1 ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- 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 Description You have N integers, A1, A2, ... , AN. You need to deal w ...
随机推荐
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- Spark Mllib里如何将如温度、湿度和风速等数值特征字段用除以***进行标准化(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第18章 决策树回归分类Bike Sharing数据集
- windows下指定端口号起步memcached
双击.exe启动的话,默认启动的端口是11211 ,要指定端口的话加 -p + 端口号,如: E:\tools\memcached-1.4.5-win32>memcached-1.4.5.exe ...
- Spring 顾问
1.名称匹配方法切入点顾问 接口:ISomeService public interface ISomeService { public void doSome(); public void doSe ...
- 分享eclipse自动生成java注释方法
设置方法介绍: eclipse中:Windows->Preferences->Java->Code Style->Code Template->Comments,然后对应 ...
- 【Python图像特征的音乐序列生成】关于数据库到底在哪里下载
毕竟原网站一个是14年前的一个是16年前的…… 1,http://ifdo.ca/~seymour/nottingham/nottingham.html 这个网站可以下载zip包. 2,https:/ ...
- Codeforces 666E Forensic Examination SAM or SA+线段树合并
E. Forensic Examination http://codeforces.com/problemset/problem/666/E 题目大意:给模式串S以及m个特殊串,q个询问,询问S的子串 ...
- python基础教程总结15——1.即时标记
1. 测试文档: # test_input.txt Welcome to World Wide Spam. Inc. These are the corporate web pages of *Wor ...
- 日常-acm-鸡兔同笼
已知鸡和兔总数量n,总腿数m.输入n和m,依次输出鸡的数量和兔的数量.如果无解,则输出No answer. 样例输入: 14 32 样例输出: 12 2 样例输入: 10 16 样例输出: No an ...
- UVA 1153 Keep the Customer Satisfied 顾客是上帝(贪心)
因为每增加一个订单,时间是会增加的,所以先按截止时间d排序, 这样的话无论是删除一个订单,或者增加订单,都不会影响已经选好的订单. 然后维护一个已经选好的订单的大根堆(优先队列),如果当前无法选择的话 ...