A Simple Problem with Integers
来源:互联网 发布:数据库研究新特点 编辑:程序博客网 时间:2024/05/03 07:25
B - A Simple Problem with Integers
//进一步熟悉了区间查询#include<stdio.h>long long a[100010];struct node{long long v;int book;};node tree[ 400040 ];int k;void pushup( int rt ){tree[ rt ].v = tree[ rt << 1].v + tree[ rt << 1 | 1].v; }void pushdown(int rt,int l,int r){int m=(l + r)/2;if( tree[ rt ].book!=0){tree[ rt << 1].v += ( long long ) ( m - l + 1) * tree[ rt ].book; tree[ rt << 1 | 1].v += (long long ) ( r - m ) * tree[ rt ].book;tree[rt << 1].book +=tree[ rt ].book;tree[ rt << 1 | 1 ].book += tree[rt ].book;tree[ rt ].book=0;}}void build(int l,int r,int rt){if( l== r){tree[ rt ].v = a[ k++ ];tree[ rt ].book = 0;return ;}else {int m=( l + r)/2;build ( l, m, rt << 1 );build ( m+1, r, rt << 1 | 1);pushup(rt);tree[ rt ].book = 0;} } void update( int L,int R,int num, int l,int r,int rt){if( L == l && R == r) {tree[ rt ].v += ( R - L + 1) * num;tree[ rt ].book += num;return ;}pushdown( rt,l,r);int m= ( l + r)/2;if( R <= m)update( L,R,num,l,m,rt << 1);else if ( L > m)update( L,R,num,m+1,r,rt << 1 | 1);else {update( L,m,num,l,m,rt << 1);update( m+1, R,num, m+1, r, rt<< 1 | 1);}pushup(rt);}long long query(int L,int R,int l,int r,int rt){if ( L == l && R == r){return tree[ rt ].v;}pushdown( rt,l,r);int m= ( l + r)/2;if( R <= m)return query( L,R,l,m,rt << 1);else if ( L > m)return query( L,R,m+1,r,rt << 1 | 1);else {return query( L,m,l,m,rt << 1) + \ query( m+1, R, m+1, r, rt<< 1 | 1);}pushup(rt);} int main(){int n,q;while(~scanf("%d%d",&n,&q)){k=0;char str[2];for(int i=0;i<n;i++)scanf("%lld",&a[i]);build(1,n,1);for( int i=0;i<q;i++){scanf("%s",str);if(str[0]=='Q'){int s,e;scanf("%d%d",&s,&e);printf("%lld\n",query(s,e,1,n,1));}else { int l,r,num;scanf("%d%d%d",&l,&r,&num);update(l,r,num,1,n,1); } } } return 0; }
0 0
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- 移动开发测试简略
- 【HTML5】基础
- lintcode之排列问题
- expect自动交互
- 系统编程中的目录操作
- A Simple Problem with Integers
- 数据可视化
- Anaconda3安装遇到的问题fail to failed to create anaconda menus
- LLVM
- 用JNative.jar包做java与底层交互时遇到的大坑!
- http(xhr)
- php缓冲区详解
- 类似于开发者工具的js代码
- 医生值日和星期