Poj 3468-A Simple Problem with Integers 线段树,树状数组

 
A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 85851   Accepted: 26685
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

 
题意:给一串数字,每次区间加上一个数,或询问区间和。
 
题解:
线段树水过,打个lazy标记就好。。。
线段树程序:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 #define LL long long
 9 #define MAXN 100010
10 struct node
11 {
12     LL left,right,sum,tag;
13 }tree[MAXN*4];
14 LL A[MAXN];
15 LL read()
16 {
17     LL s=0,fh=1;char ch=getchar();
18     while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
19     while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
20     return s*fh;
21 }
22 void Pushup(LL k)
23 {
24     tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
25 }
26 void Pushdown(LL k,LL l,LL r)
27 {
28     if(tree[k].tag!=0)
29     {
30         tree[k*2].tag+=tree[k].tag;
31         tree[k*2+1].tag+=tree[k].tag;
32         LL mid=(l+r)/2;
33         tree[k*2].sum+=tree[k].tag*(mid-l+1);
34         tree[k*2+1].sum+=tree[k].tag*(r-mid);
35         tree[k].tag=0;
36     }
37 }
38 void Build(LL k,LL l,LL r)
39 {
40     tree[k].left=l;tree[k].right=r;
41     if(l==r)
42     {
43         tree[k].sum=A[l];
44         return;
45     }
46     LL mid=(l+r)/2;
47     Build(k*2,l,mid);Build(k*2+1,mid+1,r);
48     Pushup(k);
49 }
50 void Add(LL k,LL ql,LL qr,LL add)
51 {
52     //Pushdown(k,tree[k].left,tree[k].right);
53     if(ql<=tree[k].left&&tree[k].right<=qr){tree[k].tag+=add;tree[k].sum+=(tree[k].right-tree[k].left+1)*add;return;}
54     Pushdown(k,tree[k].left,tree[k].right);
55     LL mid=(tree[k].left+tree[k].right)/2;
56     if(qr<=mid)Add(k*2,ql,qr,add);
57     else if(ql>mid)Add(k*2+1,ql,qr,add);
58     else {Add(k*2,ql,mid,add);Add(k*2+1,mid+1,qr,add);}
59     Pushup(k);
60 }
61 LL Sum(LL k,LL ql,LL qr)
62 {
63     //Pushdown(k,tree[k].left,tree[k].right);
64     if(ql<=tree[k].left&&tree[k].right<=qr)return tree[k].sum;
65     Pushdown(k,tree[k].left,tree[k].right);
66     LL mid=(tree[k].left+tree[k].right)/2;
67     if(qr<=mid)return Sum(k*2,ql,qr);
68     else if(ql>mid)return Sum(k*2+1,ql,qr);
69     else return Sum(k*2,ql,mid)+Sum(k*2+1,mid+1,qr);
70 }
71 int main()
72 {
73     LL N,Q,i,a,b,c;
74     char fh[2];
75     N=read();Q=read();
76     for(i=1;i<=N;i++)A[i]=read();
77     Build(1,1,N);
78     for(i=1;i<=Q;i++)
79     {
80         scanf("\n%s",fh);
81         if(fh[0]=='Q')
82         {
83             a=read();b=read();
84             printf("%lld\n",Sum(1,a,b));
85         }
86         else
87         {
88             a=read();b=read();c=read();
89             Add(1,a,b,c);
90         }
91     }
92     return 0;
93 }
View Code

树状数组不会。。。

先附上fhq神犇的题解:

上次NOI集训的时候,一位福建女神牛和我探讨过这题能不能用BIT,我当时
的答复是可以,因为“扩展树状数组”这个东西理论上可以实现一般线段树
可以实现的东西,且空间上的常数好一点。但是对于“扩展树状数组”,这
个东西是我一时兴起想到的玩意,没有进行更多的研究,没查到任何的资料
,更没有想过如何把线段树著名的lazy思想照搬上去,以及动态开内存的解
决方案。

权衡利弊,我想了一个使用两棵BIT的替代方法来解决这题,并且成功地将
内存使用做到了1728K。这恐怕是带标记的扩展树状数组达不到的。

记录两个BIT,
设数列为A[i],BIT1的每个元素B1[i]=A[i]*i,
BIT2的每个元素B2[i]=A[i]

则:sum{A[i]|i<=a}=sum{B1[i]|i<=a}+(sum{B2[i]|1<=i<=N}-sum{B2[i]|i<=a})*a
sum{A[i]|a<=i<=b}=sum{A[i]|i<=b}-sum{A[i]|i<a}
这样就十分巧妙的解决了!

关键代码:
int N;
struct BIT{
	long long a[NMax];
	void ins(int x,long long k){
		for (;x<N;x+=((x+1)&-(x+1)))a[x]+=k;
	}
	long long ask(int x){
		long long ret;
		for (ret=0;x>=0;x-=((x+1)&-(x+1)))ret+=a[x];
		return ret;
	}
}B1,B2;
long long B2S;
void Add(int a,long long x){
	//printf("Add %d %I64d\n",a,x);
	B1.ins(a,x*((long long)a+1));
	B2.ins(a,x);B2S+=x;
}
void Add(int a,int b,long long x){
	Add(b,x);
	if (a)Add(a-1,-x);
}
long long Ask(int a){
	//printf("Ask %d\n",a);
	long long ret=B1.ask(a)+(B2S-B2.ask(a))*(long long)(a+1);
	//printf("=%I64d\n",ret);
	return ret;
}
long long Ask(int a,int b){
	long long ret;
	ret=Ask(b);
	if (a)ret-=Ask(a-1);
	return ret;
}

 填坑中。。。。。。

posted @ 2016-03-14 15:08  微弱的世界  阅读(166)  评论(0编辑  收藏  举报