POJ 3468 A Simple Problem with Integers 线段树
来源:互联网 发布:java fix 编辑:程序博客网 时间:2024/05/21 06:35
Memory Limit: 131072KTotal Submissions: 56971
Accepted: 17284Case 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 andQ. 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
Source
传送门:POJ 3468 A Simple Problem with Integers
题目分析:赤果果的区间修改区间查询,线段树搞之。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>typedef long long BigInt ;#define ll o << 1#define rr o << 1 | 1#define lson l , m , ll#define rson m + 1 , r , rrconst int maxN = 1000005 ;struct Seg_Tree {BigInt sum , add ;int l , r ;int len () {return r - l + 1 ;}int mid () {return ( l + r ) >> 1 ;}} tree[maxN] ;void PushUp ( int o ) {tree[o].sum = tree[ll].sum + tree[rr].sum ;}void PushDown ( int o ) {if ( tree[o].add ) {int len = tree[o].len () ;tree[ll].add += tree[o].add ;tree[rr].add += tree[o].add ;tree[ll].sum += tree[o].add * ( len - ( len >> 1 ) ) ;tree[rr].sum += tree[o].add * ( len >> 1 ) ;tree[o].add = 0 ;}}void Build ( int l , int r , int o ) {tree[o].l = l ;tree[o].r = r ;tree[o].add = 0 ;if ( tree[o].l == tree[o].r ) {scanf ( "%lld" , &tree[o].sum ) ;return ;}int m = tree[o].mid () ;Build ( lson ) ;Build ( rson ) ;PushUp ( o ) ;}void Update ( int L , int R , int o , int v ) {if ( L <= tree[o].l && tree[o].r <= R ) {tree[o].add += v ;tree[o].sum += v * tree[o].len () ;return ;}PushDown ( o ) ;int m = tree[o].mid () ;if ( L <= m ) Update ( L , R , ll , v ) ;if ( m < R ) Update ( L , R , rr , v ) ;PushUp ( o ) ;}BigInt anssum ;void Query ( int L , int R , int o ) {if ( L <= tree[o].l && tree[o].r <= R ) {anssum += tree[o].sum ;return ;}PushDown ( o ) ;int m = tree[o].mid () ;if ( L <= m ) Query ( L , R , ll ) ;if ( m < R ) Query ( L , R , rr ) ;}void work () {int n , m , L , R , v ;char ch[5] ;while ( ~scanf ( "%d%d" , &n , &m ) ) {Build ( 1 , n , 1 ) ;while ( m -- ) {scanf ( "%s" , ch ) ;if ( ch[0] == 'Q' ) {anssum = 0 ;scanf ( "%d%d" , &L , &R ) ;Query ( L , R , 1 ) ;printf ( "%lld\n" , anssum ) ;}if ( ch[0] == 'C' ) {scanf ( "%d%d%d" , &L , &R , &v ) ;Update ( L , R , 1 , v ) ;}}}}int main () {work () ;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(线段树)
- dfs
- 在Delphi环境下基于SPCOMM控件开发串口通讯报文字节丢失的问题解决
- SSI (Server Side Include) in one page
- Apache和tomcat的区别
- 艾非
- POJ 3468 A Simple Problem with Integers 线段树
- Spring HandlerInterceptor的使用
- flash builder语言设置
- Fedora20上播放视频
- QT学习
- 菜鸟详解iar的icf文件(链接文件)
- Android ActionBar完全解析,使用官方推荐的最佳导航栏(上)
- Source Insight 添加新类型文件
- Hibernate进阶之如何使用Hql内连接,外连接以及自连接