POJ 3468 A Simple Problem with Integers(区间更新)

http://poj.org/problem?id=3468
A Simple Problem with Integers
Time Limit: 5000MSMemory Limit: 131072K
Total Submissions: 67813Accepted: 20922
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

Hint

The sums may exceed the range of 32-bit integers.

Source

线段树区间更新,维护sum值 同时每次更新不用更新到叶子结点,用一个inc域 来表示增加量。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 100000
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;
struct node
{
int left,right;
ll sum,inc;
}nod[3*maxn];
int num[maxn];
bool cmp(int a,int b)
{
return a>b;
}
void build(int i,int l,int r)
{
nod[i].left=l;
nod[i].right=r;
nod[i].inc=0;
if(l==r)
{
nod[i].sum=num[l];
return;
}
int mid=(l+r)/2;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
nod[i].sum=nod[i<<1].sum+nod[i<<1|1].sum;
}
void add(int i,int a,int b,ll c)//在结点i的区间(a,b)上+c
{
if(nod[i].left==a&&nod[i].right==b)
{
nod[i].inc+=c;//找到区间后就不维护sum了
return;
}
nod[i].sum+=c*(b-a+1);
int mid=(nod[i].left+nod[i].right)/2;
if(b<=mid)add(i<<1,a,b,c);
else if(a>mid)add(i<<1|1,a,b,c);
else
{
add(i<<1,a,mid,c);
add(i<<1|1,mid+1,b,c);
}
}
ll query(int i,int a,int b)
{
if(nod[i].left==a&&nod[i].right==b)
{
return nod[i].sum+(b-a+1)*nod[i].inc;
}
nod[i].sum+=(nod[i].right-nod[i].left+1)*nod[i].inc;
int mid=(nod[i].left+nod[i].right)/2;
add(i<<1,nod[i].left,mid,nod[i].inc);
add(i<<1|1,mid+1,nod[i].right,nod[i].inc);/...!
nod[i].inc=0;
if(b<=mid)return query(i<<1,a,b);
else if(a>mid)return query(i<<1|1,a,b);
else return query(i<<1,a,mid)+query(i<<1|1,mid+1,b);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m,a,b,c;
char s;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
build(1,1,n);
for(int i=1;i<=m;i++)
{

cin>>s;
if(s=='C')
{
scanf("%d%d%d",&a,&b,&c);
add(1,a,b,c);
}
else
{
scanf("%d%d",&a,&b);
printf("%I64d\n",query(1,a,b));
}
}
}
return 0;
}



lazy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值