A Simple Problem with Integers POJ
来源:互联网 发布:ff14国际服数据库app 编辑:程序博客网 时间:2024/06/02 23:08
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
Hint
The sums may exceed the range of 32-bit integers.
题意:对这样一组数进行下列操作: Q操作表示读取该区间上的和,C操作表示为i到j区间的数全部增加c。
#include<stdio.h>#include<string.h>#include<stdalign.h>int num[400000];struct node{ int l,r; long long nsum; long long add;}seg[400000];void Build(int i,int l,int r)//建立线段树{ seg[i].l=l; seg[i].r=r; seg[i].add=0; if(l==r) { seg[i].nsum = num[l]; return ; } int mid = (l+r)/2; Build(i*2,l,mid); Build(i*2+1,mid+1,r); seg[i].nsum = seg[i*2].nsum+seg[i*2+1].nsum;}void Add(int i,int l,int r,long long c)//区间上的每个数增加{ if(seg[i].l==l&&seg[i].r==r) { seg[i].add+=c; return ; } seg[i].nsum += c*(r-l+1);//这个区间总共增加了这些 int mid = (seg[i].l+seg[i].r)/2; if(r<=mid) Add(i*2,l,r,c); else if(l>mid) Add(i*2+1,l,r,c); else { Add(i*2,l,mid,c); Add(i*2+1,mid+1,r,c); }}long long Query(int i,int a,int b)//查询a-b的总和{ if(seg[i].l==a&&seg[i].r==b) { return seg[i].nsum+(b-a+1)*seg[i].add; } seg[i].nsum+=(seg[i].r-seg[i].l+1)*seg[i].add; int mid=(seg[i].l+seg[i].r)/2; Add(i*2,seg[i].l,mid,seg[i].add); Add(i*2+1,mid+1,seg[i].r,seg[i].add); seg[i].add=0; if(b<=mid) return Query(i*2,a,b); else if(a>mid) return Query(i*2+1,a,b); else return Query(i*2,a,mid)+Query(i*2+1,mid+1,b);}int main(){ int n,m,i; int a,b,c; char s[10]; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) { scanf("%d",&num[i]); } Build(1,1,n); for(i=0;i<m;i++) { scanf("%s",s); if(s[0]=='C') { scanf("%d%d%d",&a,&b,&c); Add(1,a,b,c); } else { scanf("%d%d",&a,&b); printf("%lld\n",Query(1,a,b)); } } } return 0;}
阅读全文
0 0
- 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
- POJ 3468 A Simple Problem with Integers
- LibreOJ #6165. 一道水题
- 二叉树-前序遍历
- bzoj 1304(树形dp)
- 一句话概括hashMap底层
- 设计模式--装饰器(Decorator)模式
- A Simple Problem with Integers POJ
- 进程的周期Sched
- UVA 1635 Irrelevant Elements(杨辉三角+递推式求组合数+算数基本定理)
- Python学习笔记-17.09.25
- python基础 输入与输出
- Shell编程基础
- Opencv-Python :图片读取、保存、显示
- Windows10上安装tensorflow-gpu流程
- mysql的学习路程--DDL