poj 3468 A Simple Problem with Integers
来源:互联网 发布:新浪短网址api js 编辑:程序博客网 时间:2024/05/01 09:57
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Case Time Limit: 2000MS
这里是题目链接
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.
嗯……线段树的成段更新以及区间求和,主要就是在更新的时候并不全部加到每个节点上,而是加到对应区间的父节点上,并做mark标记,而当query到相关子节点时,再将相应的mark加到子节点上,并分别更新父节点和子节点的mark。
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <cmath>#include <string>#include <queue>#include <stack>#define INF 0x3f3f3f3f#define mem(x,y) memset(x,y,sizeof(x))#define SI(x) scanf("%d",&x)#define MAX 100000+10using namespace std;typedef long long ll;int a[MAX];struct node{ int left,right,mid; ll sum,mark;}b[MAX<<2];void build(int left,int right,int i){ b[i].left=left; b[i].right=right; b[i].mark=0; b[i].mid=(right+left)>>1; int mid; if(left==right) { b[i].sum=a[left]; return; } mid=(right+left)>>1; build(left,mid,i<<1); build(mid+1,right,(i<<1)+1); b[i].sum=b[i<<1].sum+b[(i<<1)+1].sum;}void add(int left,int right, int num,int i){ if(b[i].left==left&&b[i].right==right) { // b[i].sum+=num; b[i].mark+=num; return; }else { b[i].sum+=(ll)num*(right-left+1);// int mid ;// mid= (b[i].right+ b[i].left)>>1; if(right<=b[i].mid) add(left,right,num,i<<1); else if(left>b[i].mid) add(left,right,num,(i<<1)+1); else{ add(left,b[i].mid,num,i<<1); add(b[i].mid+1,right,num,(i<<1)+1); } }}ll query(int left,int right,int i){ // int mid; if(b[i].left==left&&b[i].right==right) return b[i].sum+b[i].mark*(ll)(right-left+1); if(b[i].mark!=0){ b[i<<1].mark+=b[i].mark; b[(i<<1)+1].mark+=b[i].mark; b[i].sum+=(ll)(b[i].right-b[i].left+1)*b[i].mark; b[i].mark=0; } // mid=(b[i].left+b[i].right)>>1; if(right<=b[i].mid) return query(left,right,i<<1); else if(left>b[i].mid) return query(left,right,(i<<1)+1); else return query(left,b[i].mid,i<<1)+query(b[i].mid+1,right,(i<<1)+1);}int main(){ int n,q,l,r,val; char str[10]; while(scanf("%d%d",&n,&q)!=EOF) { for(int i=1;i<=n;i++) SI(a[i]); build(1,n,1); for(int i=0;i<q;i++) { scanf("%s",str); if(str[0]=='Q'){ scanf("%d%d",&l,&r); printf("%lld\n",query(l,r,1)); }else{ scanf("%d%d%d",&l,&r,&val); add(l,r,val,1); } } } return 0;}
- 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
- 分区
- iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
- cocos 屏幕适配 Http网络 粒子系统
- 百度地图调用报错Uncaught TypeError:Cannot read property 'fc' of undefined
- 魔术师发牌问题
- poj 3468 A Simple Problem with Integers
- 文件上传Upload
- $(window).load(function(){}) 和 $(document).ready(function(){})的区别
- cocos 动画 触摸事件
- Servlet
- Sequence小文件合并
- Java transient关键字使用小记
- 几个简单的博弈例子
- 【学习笔记】第八章 多态