Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 47659 | Accepted: 14013 | |
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
import java.util.Scanner;
public class poj_3468_线段树 {
//Accepted 13612K 10938MS Java 2657B 2013-08-19 20:13:19
static int []arr= new int[100010];
static int N,Q;
static Tree []tree = new Tree[300000];
private static void build(int p, int left, int right ) {
tree[p]=new Tree();///
int v,mid;
tree[p].l = left;
tree[p].r = right;
tree[p].add = 0;
if(left==right){
tree[p].value = arr[left];
return ;
}
mid = (left+right)>>1;//找分界点,进行分界
v = p<<1;
build( v,left,mid );
build( v+1,mid+1,right );
tree[p].value = tree[v].value + tree[v+1].value;
}
private static void update(int p, int left, int right, long add) {
int v,mid;
if(tree[p].l == left && tree[p].r == right){
tree[p].add += add;
return ;
}
tree[p].value += (right-left+1)*add;
v = p<<1;
mid = ( tree[p].l + tree[p].r )>>1;
if( right <= mid )
update(v,left,right,add);
else if(left >= mid+1 )
update( v+1,left,right,add );
else{
update(v,left,mid,add);
update(v+1,mid+1,right,add);
}
}
private static long query(int p, int left, int right) {
int v,mid;
v = p<<1;
mid =(tree[p].l + tree[p].r)>>1;
if (tree[p].l == left && tree[p].r ==right)
return (tree[p].value + tree[p].add * ( tree[p].r-tree[p].l+1) );
else{
tree[v].add += tree[p].add;
tree[v+1].add += tree[p].add;//增量下移
tree[p].value += (tree[p].r-tree[p].l+1)*tree[p].add;
tree[p].add=0;
}
if(right<=mid)
return query(v,left,right);
else if(left>=mid+1)
return query(v+1,left,right);
else
return query(v,left,mid)+query(v+1,mid+1,right);
}
public static void main(String[] args) {
Scanner sc =new Scanner (System.in);
while(sc.hasNext()){
N = sc.nextInt();
Q = sc.nextInt();
for(int i=1; i<=N; i++)
arr[i] = sc.nextInt();
build(1,1,N);
for(int i=1; i<=Q; i++){
String s = sc.next();
if(s.equalsIgnoreCase("C")){
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
update(1,a,b,c);
}else{
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(query(1,a,b));
}
}
}
}
}
class Tree{
int l,r;
long value,add;
Tree(int l, int r,long value,long add){
this.l = l;
this.r = r;
this.value = value;
this.add = add;
}
Tree(){}
}
C语言版:
#include<stdio.h>
#define N 100010
int w[N];
struct node {
int l,r;
long long value, add;
}tree[N*3];
void build(int p, int left, int right) {
int v,mid;
tree[p].l=left;
tree[p].r=right;
tree[p].add=0;
if(left==right) {
tree[p].value=w[left];
return;
}
mid=(left+right)>>1;
v=p<<1;
build(v,left,mid);
build(v+1,mid+1,right);
tree[p].value=tree[v].value+tree[v+1].value;
}
void updata(int p, int left, int right, long long add) {
int v,mid;
if(tree[p].l==left&&tree[p].r==right){
tree[p].add+=add;
return;
}
tree[p].value+=(right-left+1)*add;
v=p<<1;
mid=(tree[p].l+tree[p].r)>>1;
if(right<=mid)
updata(v,left,right,add);
else if(left>=mid+1) {
updata(v+1,left,right,add);
}
else {
updata(v,left,mid,add);
updata(v+1,mid+1,right,add);
}
}
long long query(int p, int left, int right) {
int mid,v;
v=p<<1;
mid=(tree[p].l+tree[p].r)>>1;
if(tree[p].l==left&&tree[p].r==right) {
return (tree[p].value+tree[p].add*(tree[p].r-tree[p].l+1));
}
else{//可能区间有增量,需要计算并下移
tree[v].add+=tree[p].add;
tree[v+1].add+=tree[p].add;//增量下移
tree[p].value+=(tree[p].r-tree[p].l+1)*tree[p].add;
tree[p].add=0;
}
if(right<=mid)
return query(v,left,right);
else if(left>=mid+1)
return query(v+1,left,right);
else
return query(v,left,mid)+query(v+1,mid+1,right);
}
int main() {
int n,q,i,a,b,c;
char s[3];
while(scanf("%d %d",&n,&q)!=EOF) {
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
build(1,1,n);
for(i=0;i<q;i++) {
scanf("%s",s);
if(s[0]=='C') {
scanf("%d %d %d",&a, &b, &c);
updata(1,a,b,c);
}
else {
scanf("%d %d",&a,&b);
printf("%I64d\n",query(1,a,b));
}
}
}
return 0;
}