poj 3243:A Simple Problem with Integers
来源:互联网 发布:麒麟芯片 知乎 编辑:程序博客网 时间:2024/06/04 19:36
3243:A Simple Problem with Integers
- 查看
- 提交
- 统计
- 提示
- 提问
- 总时间限制:
- 5000ms
- 单个测试点时间限制:
- 2000ms
- 内存限制:
- 131072kB
- 描述
- 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.
- 输入
- 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. - 输出
- You need to answer all Q commands in order. One answer in a line.
- 样例输入
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
- 样例输出
455915
- 提示
- The sums may exceed the range of 32-bit integers.
- 来源
- POJ Monthly--2007.11.25, Yang Yi
- 线段树
#include<iostream>#include<cmath>#include<cstring>#include<algorithm>#include<iomanip>#include<queue>#include<stack>#include<vector>#include<set>#include<map>using namespace std;int n,q,a,b,c,cnt=0;struct Node{int l,r;long long sum,inc;Node*left;Node*right; }T[200010];void Build(Node*root,int l,int r){root->l=l;root->r=r;root->sum=0;root->inc=0;if(l==r)return;int mid=(l+r)/2;cnt++;root->left=T+cnt;cnt++;root->right=T+cnt;Build(root->left,l,mid);Build(root->right,mid+1,r);}void Insert(Node*root,int index,int a) {root->sum+=a;if(root->l==root->r)return;int mid=(root->l+root->r)/2;if(index<=mid){Insert(root->left,index,a);}else{Insert(root->right,index,a);}}void Add(Node*root,int l,int r,int c){if(root->l==l&&root->r==r){root->inc+=c;return; }root->sum+=(r-l+1)*c;int mid=(root->l+root->r)/2;if(r<=mid){Add(root->left,l,r,c);}else if(l>mid){Add(root->right,l,r,c);}else{Add(root->left,l,mid,c);Add(root->right,mid+1,r,c);}}long long Query(Node*root,int l,int r){if(root->l==l&&root->r==r){return root->sum+root->inc*(r-l+1);}int mid=(root->l+root->r)/2;root->sum+=(root->r-root->l+1)*root->inc;Add(root->left,root->l,mid,root->inc);Add(root->right,mid+1,root->r,root->inc);root->inc=0;if(r<=mid){return Query(root->left,l,r);}else if(l>mid){return Query(root->right,l,r);}else{return Query(root->left,l,mid)+Query(root->right,mid+1,r);}}int main(){cin>>n>>q;Build(&T[0],1,n);for(int i=1;i<=n;++i){cin>>a;Insert(&T[0],i,a);}for(int i=0;i<q;++i){string s;cin>>s;if(s[0]=='C') {cin>>a>>b>>c;Add(T,a,b,c);}else{cin>>a>>b;cout<<Query(T,a,b)<<endl;}}return 0;}
阅读全文
0 0
- poj 3243:A Simple Problem with Integers
- POJ A Simple Problem with Integers
- POJ 3468 A Simple Problem With Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ-3468-A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 - A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- 深入理解HTTP协议(转)
- 2017年之年中总结
- poj 1469:COURSES
- Mysql数据库中不能插入中文
- python 05
- poj 3243:A Simple Problem with Integers
- 源文件与模块生成时的文件不同
- CUDA编程学习二
- 排序:冒泡排序
- 《Effective C++》读书摘要
- File
- Qt设置应用程序图标
- JS数据类型之间的转换
- .net C#使用私钥sign公钥验证 验证JWS signature data