[线段树] POJ 3468 A Simple Problem with Integers
来源:互联网 发布:淘宝iphone官换机骗局 编辑:程序博客网 时间:2024/06/06 06:38
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
Hint
1)对某个区间Ai … Aj的每个数都加n(n可变)2) 求某个区间Ai … Aj的数的和
#include <iostream>#include <cstdio>using namespace std;struct CNode{ int L,R; CNode *pLeft,*pRight; long long nSum; //原来的和 long long Inc; //增量C的累加};CNode Tree[400010];int nCount=0;int Mid(CNode *pRoot){ return (pRoot->L + pRoot->R)/2;}void BuildTree(CNode *pRoot,int L,int R){ pRoot->L=L; pRoot->R=R; pRoot->nSum=0; pRoot->Inc=0; if(L==R) return ; nCount++; pRoot->pLeft=Tree+nCount; nCount++; pRoot->pRight=Tree+nCount; BuildTree(pRoot->pLeft,L,(L+R)/2); BuildTree(pRoot->pRight,(L+R)/2+1,R);}void Insert (CNode *pRoot,int i ,int v){ if (pRoot->L == pRoot->R ){ pRoot->nSum=v; return; } pRoot->nSum +=v; if (i<=Mid(pRoot)) Insert(pRoot->pLeft,i,v); else Insert(pRoot->pRight,i,v);}void Add(CNode *pRoot,int a,int b,long long c){ if(pRoot->L == a && pRoot->R==b){ pRoot->Inc += c; return ; } pRoot->nSum += c*(b-a+1); if(b<=(pRoot->L + pRoot->R)/2) Add(pRoot->pLeft,a,b,c); else if(a>=(pRoot->L + pRoot->R)/2+1) Add(pRoot->pRight,a,b,c); else{ Add(pRoot->pLeft,a,(pRoot->L+pRoot->R)/2,c); Add(pRoot->pRight,(pRoot->L+pRoot->R)/2+1,b,c); }}long long QuerynSum(CNode *pRoot,int a,int b){ if(pRoot->L==a &&pRoot->R==b) return pRoot->nSum+(pRoot->R - pRoot->L +1)*pRoot->Inc; pRoot->nSum += (pRoot->R - pRoot->L + 1)* pRoot->Inc; Add(pRoot->pLeft , pRoot->L , Mid(pRoot) , pRoot->Inc ); Add(pRoot->pRight, Mid(pRoot)+1 , pRoot->R , pRoot->Inc); pRoot->Inc=0; if( b <= Mid(pRoot) ) return QuerynSum(pRoot->pLeft,a,b); else if ( a >= Mid(pRoot)+1 ) return QuerynSum(pRoot->pRight,a,b); else { return ( QuerynSum(pRoot->pLeft , a , Mid(pRoot) ) + QuerynSum(pRoot->pRight, Mid(pRoot)+1, b)); }}int main(){ int n,q,a,b,c; char cmd[10]; scanf("%d%d",&n,&q); int i; nCount=0; BuildTree(Tree,1,n); for (i=1 ; i <= n;i++) { scanf("%d",&a); Insert(Tree,i,a); } for (i=0;i<q;i++) { scanf("%s",cmd); if(cmd[0]=='C') { scanf("%d%d%d",&a,&b,&c); Add(Tree,a,b,c); } else { scanf("%d%d",&a,&b); printf("%I64d\n",QuerynSum(Tree,a,b)); } } 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(线段树)
- 耐得住寂寞才能守得住繁华
- 链表(四)——带表头的单向链表
- Android开发问题总结 --- 2014/07/20
- java.lang.ClassCastException: cn.study.model.User_$$_javassist_0 cannot be cast to javassist.util.pr
- 大牛android博客
- [线段树] POJ 3468 A Simple Problem with Integers
- Libgdx: android单机斗地主支持局域网wifi联网的网络模块核心代码
- 代理服务器proxy server
- 随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
- Windows下Subversion和Apache的安装及配置(一)
- A. Jzzhu and Children
- jquery的ajax实现登录名的校验
- Hadoop学习笔记之Hbase Shell不能删除问题解决
- Struts2的处理流程和对Action的管理方式