博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
阅读量:6575 次
发布时间:2019-06-24

本文共 2992 字,大约阅读时间需要 9 分钟。

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 67511   Accepted: 20818
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 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.
 
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define root 1,n,1#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define lr rt<<1#define rr rt<<1|1typedef long long LL;const int oo = 1e9+7;const double PI = acos(-1.0);const double eps = 1e-6 ;const int N = 100010;const int mod = 2333333;int n , m ;LL sum[N<<2] , lazy[N<<2] , e[N] , tot ;void Up( int rt ) { sum[rt] = sum[lr] + sum[rr];}void Down( int l , int r , int rt ) { if( lazy[rt] != 0 ) { int mid = (l+r)>>1; sum[lr] += lazy[rt]*(mid-l+1) , sum[rr] += lazy[rt]*(r-mid); lazy[lr] += lazy[rt] , lazy[rr] += lazy[rt]; lazy[rt] = 0 ; }}void build( int l , int r , int rt ){ lazy[rt] = 0 ; if( l == r ) { sum[rt] = e[tot++]; return ; } int mid = (l+r)>>1; build(lson),build(rson); Up(rt);}void update( int l , int r , int rt , int L , int R , LL val ) { if( L == l && r == R ) { sum[rt] += val*(r-l+1) ; lazy[rt] += val; return ; } Down( l , r , rt ); int mid = (l+r)>>1; if( R <= mid ) update(lson,L,R,val); else if( L > mid ) update(rson,L,R,val); else update(lson,L,mid,val) , update(rson,mid+1,R,val); Up(rt);}LL query( int l , int r , int rt , int L , int R ) { if( L == l && r == R ) { return sum[rt]; } Down(l,r,rt); int mid = (l+r)>>1; if( R <= mid ) return query(lson,L,R); else if( L > mid ) return query(rson,L,R); else return query(lson,L,mid) + query(rson,mid+1,R);}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout); #endif // LOCAL char s[10]; int x , y ; LL c ; while( ~scanf("%d%d",&n,&m ) ) { tot = 0 ; for( int i = 0 ; i < n ; ++i ) { scanf("%I64d",&e[i]); } build(root); while(m--) { scanf("%s",s); if( s[0] == 'Q' ) { scanf("%d%d",&x,&y); printf("%I64d\n",query(root,x,y)); } else { scanf("%d%d%I64d",&x,&y,&c); update(root,x,y,c); } } }}
View Code

 

转载于:https://www.cnblogs.com/hlmark/p/4247916.html

你可能感兴趣的文章
stark组件(1):动态生成URL
查看>>
169. Majority Element
查看>>
下拉菜单
查看>>
[清华集训2014]玛里苟斯
查看>>
Doctype作用?严格模式与混杂模式如何区分?它们有何意义
查看>>
【MVC+EasyUI实例】对数据网格的增删改查(上)
查看>>
第三章:如何建模服务
查看>>
Project Euler 345: Matrix Sum
查看>>
你可能不知道的技术细节:存储过程参数传递的影响
查看>>
HTML转义字符大全(转)
查看>>
[摘录]调动员工积极性的七个关键
查看>>
Backup Volume 操作 - 每天5分钟玩转 OpenStack(59)
查看>>
.htaccess 基础教程(四)Apache RewriteCond 规则参数
查看>>
转: maven进阶:一个多模块项目
查看>>
Android控件之HorizontalScrollView 去掉滚动条
查看>>
UVM中的class--2
查看>>
ORACLE 存储过程异常捕获并抛出
查看>>
博客园博客美化相关文章目录
查看>>
root用户重置其他密码
查看>>
Oracle推断值为非数字
查看>>