POJ 3468 A Simple Problem with Integers (线段树)
2015-06-11 21:33
459 查看
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
Sample Output
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 72740 | Accepted: 22453 | |
Case Time Limit: 2000MS |
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 模板题。
#include <iostream> #include <cstdio> #include <string> #include <queue> #include <vector> #include <map> #include <algorithm> #include <cstring> #include <cctype> #include <cstdlib> #include <cmath> #include <ctime> #include <climits> using namespace std; const int SIZE = 100005; long long TREE[SIZE * 4],LAZY[SIZE * 4],N,Q; void build(int,int,int); void update(int,int,int,int,int,int); void push_down(int,int,int); long long que(int,int,int,int,int); int main(void) { char op; int a,b,c; while(~scanf("%d%d",&N,&Q)) { build(1,1,N); while(Q --) { scanf(" %c%d%d",&op,&a,&b); if(op == 'Q') printf("%lld\n",que(a,b,1,1,N)); else { scanf("%d",&c); update(a,b,1,1,N,c); } } } return 0; } void build(int node,int left,int right) { LAZY[node] = 0; if(left == right) scanf("%lld",&TREE[node]); else { int mid = (left + right) >> 1; build(node * 2,left,mid); build(node * 2 + 1,mid + 1,right); TREE[node] = TREE[node * 2] + TREE[node * 2 + 1]; } } void update(int L,int R,int node,int left,int right,int add) { if(left >= L && right <= R) { TREE[node] += add * (right - left + 1); LAZY[node] += add ; return ; } if(right < L || left > R) return ; push_down(node,left,right); int mid = (left + right) >> 1; update(L,R,node * 2,left,mid,add); update(L,R,node * 2 + 1,mid + 1,right,add); TREE[node] = TREE[node * 2] + TREE[node * 2 + 1]; } long long que(int L,int R,int node,int left,int right) { if(left >= L && right <= R) return TREE[node]; if(right < L || left > R) return 0; push_down(node,left,right); int mid = (left + right) >> 1; return que(L,R,node * 2,left,mid) + que(L,R,node * 2 + 1,mid + 1,right); } void push_down(int node,int left,int right) { if(LAZY[node]) { int mid = (left + right) >> 1; TREE[node * 2] += LAZY[node] * (mid - left + 1); LAZY[node * 2] += LAZY[node]; TREE[node * 2 + 1] += LAZY[node] * (right - mid); LAZY[node * 2 + 1] += LAZY[node]; LAZY[node] = 0; } }
相关文章推荐
- 覆盖与重载与隐藏——SAP电面(3)
- 《构建之法》10、11、12章
- Android 仿微信对话列表滑动删除效果
- 不用+、-、×、÷对两个数求和
- [leetcode] 77.Combinations
- swift笔记-赋值运算符
- 浅谈Struts2下
- 每天一个linux命令(14):head 命令
- Ubuntu 查看和杀死进程
- 1.SpringMVC开发环境搭建
- hdu 5245 Joyful
- 对常用的软件开发模型的总结与个人理解_Phoenix-晶
- JPA的Column注解总结
- LeetCode 之 Reverse Linked List — C 实现
- 【iOS开发-网络】使用get请求发送和接受数据
- android学习笔记
- Teams(uva11609+组合)
- playframework1.x的eclipse插件开源-playtools
- MMU实验
- oracle 新建用户并赋予SDE用户的权限