POJ 3468-A Simple Problem with Integers(區間更新線段樹)
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 88545 | Accepted: 27517 | |
Case 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 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
Hint
Source
From:http://www.cnblogs.com/littlex/archive/2012/01/30/2331670.html
這題很好的體現了lazy_tag的思想,當要增加的區間覆蓋當前區間,則直接打上標記返回,當下次查詢這個區間兒子區間的時候,直接標記往下傳。可以想象,這個標記表示的是這個區間表示的整個區間的標記是整個子樹的性質,當你不需要用到這個區間的兒子區間的時候,你可以不把操作都做到底,而是要用到的時候再往下傳!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAX 500000
struct node
{
int l,r;
long long val;//增量
long long num;//總和
} tree[4*MAX];
long long num[MAX];
void build(int l,int r,int index)//建樹
{
tree[index].l=l;
tree[index].r=r;
if(tree[index].l==tree[index].r)
{
tree[index].num=num[l-1];
return ;
}
int mid=(l+r)/2;
build(l,mid,2*index);
build(mid+1,r,2*index+1);
tree[index].num=tree[index*2].num+tree[index*2+1].num;//每個節點和
}
void update(int l,int r,int c,int index)//更新
{
if(tree[index].l>=l&&tree[index].r<=r)
{
tree[index].num+=(tree[index].r-tree[index].l+1)*c;
tree[index].val+=c;
return ;
}
else
{
if(tree[index].val)//最關鍵部分就是這個標記下傳,將增加值val傳給他的左右孩子
{
tree[index*2].num+=tree[index].val*(tree[index*2].r-tree[index*2].l+1);
tree[index*2].val+=tree[index].val;
tree[index*2+1].num+=tree[index].val*(tree[index*2+1].r-tree[index*2+1].l+1);
tree[index*2+1].val+=tree[index].val;
tree[index].val=0;
}
if(r>=tree[index*2+1].l)
update(l,r,c,2*index+1);
if(l<=tree[index*2].r)
update(l,r,c,2*index);
tree[index].num=tree[index*2].num+tree[index*2+1].num;//每個節點和
}
}
long long query(int a,int b,int index)//查詢
{
if(tree[index].l>=a&&tree[index].r<=b)
return tree[index].num;
if(tree[index].val) //求和同樣需要標記下傳
{
tree[index*2].num+=tree[index].val*(tree[index*2].r-tree[index*2].l+1);
tree[index*2].val+=tree[index].val;
tree[index*2+1].num+=tree[index].val*(tree[index*2+1].r-tree[index*2+1].l+1);
tree[index*2+1].val+=tree[index].val;
tree[index].val=0;
}
int mid=(tree[index].l+tree[index].r)/2;
long long max1=0,max2=0;
if(mid<a)
max1= query(a,b,2*index+1);
else if(b<=mid)
max1=query(a,b,2*index);
else
{
max1=query(a,mid,2*index);
max2=query(mid+1,b,2*index+1);
}
return max1+max2;
}
int main()
{
int n,i,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(tree,0,sizeof(tree));
for(i=0; i<n; i++)
scanf("%I64d",&num[i]);
build(1,n,1);
for(i=0; i<m; i++)
{
char st;
cin>>st;
if(st=='C')
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1);
}
else
{
int a,b;
long long sum=0;
scanf("%d%d",&a,&b);
sum=query(a,b,1);
printf("%I64d\n",sum);
}
}
}
return 0;
}
相關文章
- POJ 3468 A Simple Problem with Integers (線段樹 區間更新)
- POJ 3468 A Simple Problem with Integers(線段樹區間操作)
- POJ 3468 A Simple Problem with Integers (線段樹 區間共加)
- (poj3468)A Simple Problem with Integers(區間更新)
- POJ3468 A Simple Problem with Integers---樹狀陣列(區間問題)陣列
- 【poj3468】A Simple Problem with Integers
- bzoj3212: Pku3468 A Simple Problem with Integers(線段樹)
- POJ 2528 Mayor's posters (線段樹 區間更新+離散化)
- POJ 2528 Mayor's posters (線段樹區間更新 + 離散化)
- NC17383 A Simple Problem with Integers
- hihocoder 1078 線段樹的區間修改 (線段樹 區間更新 模板)
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- HDU 1698 Just a Hook (線段樹區間更新)Hook
- Codeforces 52C (線段樹區間更新)
- POJ 2777-Count Color(線段樹-區間染色查詢)
- POJ 3264-Balanced Lineup詳解(線段樹區間求值)
- poj 3237 樹鏈剖分(區間更新,區間查詢)
- Codeforces 272C Dima and Staircase (線段樹區間更新 或 線性掃)AI
- 線段樹(3)——區間操作疊加
- 芻議線段樹 2 (區間修改,區間查詢)
- 線段樹維護區間等差數列
- HDU 1754 I Hate It (線段樹 區間最值)
- Java 演算法-區間求和I(線段樹)Java演算法
- 區間k小值(可持久化線段樹)持久化
- POJ 1195-Mobile phones(二維樹狀陣列-區間更新區間查詢)陣列
- POJ 2991 Crane(線段樹+計算幾何)
- hdu 2665 可持久化線段樹求區間第K大值(函式式線段樹||主席樹)持久化函式
- 1082 線段樹練習 3 區間查詢與區間修改
- POJ 2777 Count Color 線段樹入門題
- 區間演算法題用線段樹可以秒解?演算法
- POJ 2828 Buy Tickets 線段樹入門(建樹稍微有點抽象)抽象
- A - Yet Another Two Integers Problem ACMACM
- HDU1698 Just a Hook【線段樹基礎:區間修改+區間查詢】Hook
- 線段樹學習筆記(更新中)筆記
- POJ 2886 Who Gets the Most Candies?(線段樹+反素數)
- poj 3468 區間更新 整個區間加一個數和區間求和操作
- POJ 2891 Strange Way to Express IntegersExpress
- 【樹狀陣列 區間更新區間查詢】code陣列