POJ 3468 区间加减 区间求和

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define lson l,m,rt<<1
 6 #define rson m+1,r,rt<<1|1
 7 #define INF 0x7fffffff
 8 #define maxn 101000
 9 #define LL long long
10 using namespace std;
11 LL sum[maxn<<2];
12 int add[maxn<<2],n,Q,u,v,w,m;
13 void pushup(int rt){
14     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
15 }
16 void pushdown(int rt,int m){
17     if(add[rt]){
18         add[rt<<1]+=add[rt];
19         add[rt<<1|1]+=add[rt];
20         sum[rt<<1]+=(LL)add[rt]*(m-(m>>1));
21         sum[rt<<1|1]+=(LL)add[rt]*(m>>1);
22         add[rt]=0;
23     }
24 }
25 void build(int l,int r,int rt){
26     add[rt]=0;
27     if(l==r){
28         scanf("%lld",&sum[rt]);
29         return;
30     }
31     int m=(l+r)>>1;
32     build(lson);
33     build(rson);
34     pushup(rt);
35 }
36 void update(int L,int R,int c,int l,int r,int rt){
37     if(l>=L&&r<=R){
38         add[rt]+=c;
39         sum[rt]+=(LL)c*(r-l+1);
40         return ;
41     }
42     pushdown(rt,r-l+1);
43     int m=(l+r)>>1;
44     if(L<=m)update(L,R,c,lson);
45     if(R>m)update(L,R,c,rson);
46     pushup(rt);
47 }
48 LL Query(int L,int R,int l,int r,int rt){
49     if(l>=L&&r<=R){
50         return sum[rt];
51     }
52     pushdown(rt,r-l+1);
53     LL res=0;
54     int m=(l+r)>>1;
55     if(L<=m) res+=Query(L,R,lson);
56     if(R>m) res+=Query(L,R,rson);
57     return res;
58 }
59 int main(){
60     cin>>n>>Q;
61     build(1,n,1);
62     char opt[20];
63     for(int i=1;i<=Q;i++){
64         cin>>opt;
65         if(opt[0]==‘C‘){
66             scanf("%d%d%d",&u,&v,&w);
67             update(u,v,w,1,n,1);
68         }
69         else{
70             scanf("%d%d",&u,&v);
71             cout<<Query(u,v,1,n,1)<<endl;
72         }
73     }
74     return 0;
75 }
时间: 2024-10-12 16:28:01

POJ 3468 区间加减 区间求和的相关文章

POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 56005   Accepted: 16903 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with

二维树状数组的区间加减及查询 tyvj 1716 上帝造题的七分钟

具体解释见小结.http://blog.csdn.net/zmx354/article/details/31740985 #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <stack>

poj 3468(简单线段树区间更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 61936   Accepted: 18934 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj3468区间加减查找——树状数组区间修改查询

题目:http://poj.org/problem?id=3468 增加一个更改量数组,施以差值用法则区间修改变为单位置修改: 利用公式可通过树状数组维护两个数组:f与g而直接求出区间和. 代码如下: #include<iostream> #include<cstdio> using namespace std; long long n,q,a[100005]/*,b[100005],bg[100005]*/,l,r,ad; long long f[100005],g[100005

线段树中区间加减和区间覆盖的双标记问题

HDU 5828 吉老师真厉害. 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define lson q << 1 4 #define rson (q << 1) | 1 5 #define mid ((l + r) >> 1) 6 typedef long long ll; 7 const int maxn = 1e5 + 50; 8 ll maxv[maxn << 2]; 9 ll

静态维护区间加等差数列的求和

维护一个数组,先进行m次操作,然后查询每个位置的值,每个操作给定四个参数l,r,a,k表示从l到r依次加上一个首项为a,公差为k的等差数列. 维护d_2d2?数组,表示原数组的二阶差分. #include<bits/stdc++.h> using namespace std; const int MAXN=100005; int n,m,d2[MAXN],l,r,a,k; void add(int l,int r,int a,int k) { d2[l]+=a; d2[l+1]+=k-a; d

Hdu 4578 Transformation(区间加值,区间乘值,区间赋值,查询区间的p次方)

Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others) Total Submission(s): 4145    Accepted Submission(s): 1027 Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a

Oracle查询结果列的加减、求和、连接、列值相乘

select prod.amount,prod.plansum,(prod.plansum-prod.amount) as borrow,d.enum_value from ----结果集相减(select t.Quo_Prod_List_Price * t.QUO_PROD_VOLUME as amount ,----列值相乘       t.quo_prod_plan_cost * t.quo_prod_volume as plansum , t.broad_class as broad_c

UVA 11992(Fast Matrix Operations-线段树区间加&amp;改)[Template:SegmentTree]

Fast Matrix Operations There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has a location (x,y) where 1<=x<=r,1<=y<=c. Initially, all the elements are zero. You need to handle four kinds of operati