poj 3468 A Simple Problem with Integers
来源:互联网 发布:ubuntu 找不到安装包 编辑:程序博客网 时间:2024/05/22 17:35
题目链接:点击打开链接
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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915
注意:用scanf,和long long 不然超时和wa
<span style="font-size:18px;">#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;struct node{ int a,b; long long delta; long long sum; struct node *left,*right;}*cur;long long A[100005];void updata(node *cur){ cur->left->sum+=cur->delta*(cur->left->b-cur->left->a); cur->right->sum+=cur->delta*(cur->right->b-cur->right->a); cur->left->delta+=cur->delta; cur->right->delta+=cur->delta; cur->delta=0;}void build(node *cur,int l,int r){ cur->a=l; cur->b=r; cur->delta=0; if(l+1==r)cur->sum=A[l]; if(l+1<r) { cur->left=new node; cur->right=new node; int mid=(l+r)/2; build(cur->left,l,mid); build(cur->right,mid,r); cur->sum=cur->left->sum+cur->right->sum; } else cur->left=cur->right=NULL;}long long query(node *cur,int l,int r){ if(l<=cur->a&&cur->b<=r) { return cur->sum; } else { if(cur->delta!=0) { updata(cur); } long long ans=0; int mid=(cur->a+cur->b)/2; if(l<mid) ans+=query(cur->left,l,r); if(r>mid) ans+=query(cur->right,l,r); return ans; }}void change(node *cur,int l,int r,int delta){ if(l<=cur->a&&cur->b<=r) { cur->sum+=delta*(cur->b-cur->a); cur->delta+=delta; } else { if(cur->delta!=0) { updata(cur); } int mid=(cur->a+cur->b)/2; if(l<mid) { change(cur->left,l,r,delta); } if(r>mid) { change(cur->right,l,r,delta); } cur->sum=cur->left->sum+cur->right->sum; }}int main(){ int n,q; scanf("%d%d",&n,&q); for(int i=0;i<n;i++) { scanf("%lld",&A[i]); } cur=new node ; build(cur,0,n); char ch[5]; int a,b,c; for(int i=0;i<q;i++) { scanf("%s",ch); if(ch[0]=='Q') { scanf("%d%d",&a,&b); a=a-1; long long sum=query(cur,a,b); printf("%lld\n",sum); } else if(ch[0]=='C') { scanf("%d%d%d",&a,&b,&c); a=a-1; change(cur,a,b,c); } } return 0;}</span>
- 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
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- sdut oj2143 图结构练习——最短路径
- ASCII码转换
- 在IntelliJ IDEA中使用JUnit5
- 给WebView添加漂亮的加载进度条
- 机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
- poj 3468 A Simple Problem with Integers
- HDU 5696 区间的价值
- 清楚浮动的几种方式overflow:hidden,overflow:auto,after伪类zoom:1;before div-clear:both;父级定义高度
- linux 内存清理/释放命令
- html超文本标记语言
- 多媒体编程
- 【BZOJ3143】游走,概率计算+高斯消元
- POJ 3461 Oulipo (KMP)
- 华为OJ:百钱百鸡