A Simple Problem with Integers - POJ 3468 - 线段树 区间更新
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 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
题意:给你n个数,下标1~n,有m个询问:C a b c
代表把区间[a,b]
中的每一个数都加上c。Q a b
代表查询区间[a,b]
间的总和。
思路:可以用树状数组,也可以用线段树。因为树状数组我基本不会,所以只写了线段树版本。
写了结构体版本和数组版本两种,两种写法时间复杂度相同,数组稍微快一点点,但在空间复杂度上数组版本就很占优了。
还有一个小问题就是我写数组版本的时候怎么样都是WA,问了问学长,发现是爆int了。不知道为什么写结构体的时候鬼使神差般全用的long long,写数组就全用int了。
Lazy标记什么的详见我的另一篇博客:线段树 - Lazy标记 - 单点/区间更新 - 模板
特别的,lazy在我的数组版本里是laz在结构体版本里是add。
结构体版本AC代码:
//
// main.cpp
// L
//
// Created by LucienShui on 2017/5/24.
// Copyright © 2017年 LucienShui. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#define memset(a,b) memset(a,b,sizeof(a))
#define il inline
#define ull unsigned long long
#define ll long long
using namespace std;
#define ls (u<<1)
#define rs (u<<1|1)
const int maxn = 200000+7;
ll n,m,ans;
struct node {
ll l,r,sum,add;
}tree[maxn<<2];
il void pushup(ll u) {
tree[u].sum = tree[ls].sum + tree[rs].sum;
}
il void pushdown(ll u) {
tree[ls].add += tree[u].add;
tree[rs].add += tree[u].add;
tree[ls].sum += tree[u].add*(tree[ls].r-tree[ls].l+1);
tree[rs].sum += tree[u].add*(tree[rs].r-tree[rs].l+1);
tree[u].add = 0;
}
il void build(ll u, ll l, ll r) {
tree[u].l = l;
tree[u].r = r;
tree[u].add = 0;
if(l==r) {
scanf("%lld",&tree[u].sum);
return ;
}
ll mid = (l+r) >> 1;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(u);
}
il void update(ll u, ll l, ll r, ll num) {
if(r<tree[u].l || l>tree[u].r) return ;
if(l <= tree[u].l && r >= tree[u].r) {
tree[u].add += num;
tree[u].sum += num * (tree[u].r-tree[u].l+1);
return ;
}
if(tree[u].add) pushdown(u);
update(ls,l,r,num);
update(rs,l,r,num);
pushup(u);
}
il void query(ll u, ll l, ll r) {
if(r < tree[u].l || l > tree[u].r) return ;
if(l <= tree[u].l && r >= tree[u].r) {
ans += tree[u].sum;
return ;
}
if(tree[u].add) pushdown(u);
ll mid = (tree[u].l+tree[u].r)>>1;
if(r<=mid) query(ls,l,r);
else if(l>mid) query(rs,l,r);
else {
query(ls,l,mid);
query(rs,mid+1,r);
}
}
int main ()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(~scanf("%lld %lld",&n,&m))
{
build(1,1,n);
char str[5];
while(m--)
{
scanf("%s",str);
if(str[0]=='Q')
{
ll l,r;
scanf("%lld %lld",&l,&r);
ans=0;
query(1,l,r);
printf("%lld\n",ans);
}
else
{
ll l,r,c;
scanf("%lld %lld %lld",&l,&r,&c);
update(1,l,r,c);
}
}
}
return 0;
}
数组版本AC代码:
//
// main.cpp
// L
//
// Created by LucienShui on 2017/5/26.
// Copyright © 2017年 LucienShui. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#define memset(a,b) memset(a,b,sizeof(a))
#define il inline
#define ull unsigned long long
#define ll long long
using namespace std;
#define ls (u<<1)
#define rs (u<<1|1)
const int maxn = 200000+7;
ll n,m,ans;
ll node[maxn<<2],laz[maxn<<2];//node是树节点,laz是lazy标记
il void pushdown(ll u, ll l, ll r) {
ll mid = (l+r)>>1;
laz[ls] += laz[u];
laz[rs] += laz[u];
node[ls] += laz[u]*(mid-l+1);
node[rs] += laz[u]*(r-mid);
laz[u] = 0;
}
il void build(ll u, ll l, ll r) {
if(l==r) {
scanf("%lld",node+u);
return ;
}
ll mid = (l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
node[u] = node[ls] + node[rs];
}
il void update(ll u, ll l, ll r, ll b, ll e, ll num) {
if(e<l || b>r) return ;
if(b<=l && r<=e) {
laz[u] += num;
node[u] += num * (r-l+1);
return ;
}
if(laz[u]) pushdown(u,l,r);
ll mid = (l+r)>>1;
update(ls,l,mid,b,e,num);
update(rs,mid+1,r,b,e,num);
node[u] = node[ls] + node[rs];
}
il void query(ll u, ll l, ll r, ll b, ll e) {
if(e<l || b>r) return ;
if(b<=l && r<=e) {
ans += node[u];
return ;
}
if(laz[u]) pushdown(u,l,r);
ll mid = (l+r)>>1;
if(e<=mid) query(ls,l,mid,b,e);
else if(b>mid) query(rs,mid+1,r,b,e);
else {
query(ls,l,mid,b,e);
query(rs,mid+1,r,b,e);
}
}
int main ()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
ll l,r,c;
while(~scanf("%lld %lld",&n,&m)) {
build(1,1,n);
char str[5];
while(m--)
{
scanf(" %s",str);
if(str[0]=='Q')
{
scanf("%lld %lld",&l,&r);
ans=0;
query(1,1,n,l,r);
printf("%lld\n",ans);
}
else
{
scanf("%lld %lld %lld",&l,&r,&c);
update(1,1,n,l,r,c);
}
}
}
return 0;
}