2013
11-12

# A Simple Problem with Integers

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.

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.

You need to answer all Q commands in order. One answer in a line.

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


4
55
9
15

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

//* @author
import java.util.*;
import java.io.*;

public class Main
{
public static void main(String []args)
{
SegmentTree segmentTree=new SegmentTree();
}
};
class SegmentTree
{
public SegmentTree()
{
tot=0;
int n,m,left,right,value;
String str;
Scanner input=new Scanner(System.in);
n=input.nextInt();
m=input.nextInt();
for(int i=1;i<=n;i++)
a[i]=input.nextInt();
creatTree(1,1,n);
for(int i=0;i< m;i++)
{
str=input.next();
if(str.charAt(0)=='Q')
{
left=input.nextInt();
right=input.nextInt();
System.out.println(query(1,left,right,0));
}
else
{
left=input.nextInt();
right=input.nextInt();
value=input.nextInt();
insert(1,left,right,value);
}
}
}
public long creatTree(int now,int left,int right)
{
if(now>tot)
tot=now;
_left[now]=left;
_right[now]=right;
long lSum=0,rSum=0;
if(left< right)
{
lSum=creatTree(2*now,left,(left+right)/2);
rSum=creatTree(2*now+1,(left+right)/2+1,right);
_sum[now]=lSum+rSum;
}
else
_sum[now]=a[left];
return _sum[now];
}
public void insert(int now,int left,int right,int value)
{
if(now>tot)
return ;
if(left<=_left[now]&&right>=_right[now])
{
_d[now]+=value;
return ;
}
long lSum=0,rSum=0;
if(left<=(_left[now]+_right[now])/2)
insert(2*now,left,right,value);
if(right>(_left[now]+_right[now])/2)
insert(2*now+1,left,right,value);
if(2*now<=tot)
lSum=_sum[2*now]+_d[2*now]*(_right[2*now]-_left[2*now]+1);
if(2*now+1<=tot)
rSum=_sum[2*now+1]+_d[2*now+1]*(_right[2*now+1]-_left[2*now+1]+1);
_sum[now]=lSum+rSum;
}
public long query(int now,int left,int right,long d)
{
if(now>tot)
return 0;
if(left<=_left[now]&&right>=_right[now])
return _sum[now]+(_d[now]+d)*(_right[now]-_left[now]+1);
long lSum=0,rSum=0;
if(left<=(_left[now]+_right[now])/2)
lSum=query(2*now,left,right,d+_d[now]);
if(right>(_left[now]+_right[now])/2)
rSum=query(2*now+1,left,right,d+_d[now]);
return lSum+rSum;
}
public static final int N=100005;
public int tot;
public int[] a=new int[N];
public int[] _left=new int [3*N];
public int[] _right=new int [3*N];
public long[] _sum=new long [3*N];
public long[] _d=new long [3*N];
}

1. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

2. int half(int *array,int len,int key)
{
int l=0,r=len;
while(l<r)
{
int m=(l+r)>>1;
if(key>array )l=m+1;
else if(key<array )r=m;
else return m;
}
return -1;
}
这种就能避免一些Bug
l,m,r
左边是l,m;右边就是m+1,r;

3. 我还有个问题想请教一下，就是感觉对于新手来说，递归理解起来有些困难，不知有没有什么好的方法或者什么好的建议？