A Simple Problem with Integersios
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 53810 | Accepted: 16150 | |
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.ide
Inputspa
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.code
Outputblog
You need to answer all Q commands in order. One answer in a line.ip
Sample Inputget
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 Outputstring
4 55 9 15
Hintit
Source
給一個數字序列A1,A2,A3,…,An,對它進行以下兩種操做:
一、C a b c 對Aa,Aa+1,…,Ab每一個數都加上c
二、Q a b 求Aa,Aa+1,…,Ab的和
首先想到涉及區間維護的能夠用線段樹求解,每一個結點k中維護以下兩個信息:
一、結點k所對應的區間[l,r)中每一個Ai都要加上的數add[k]
二、結點k中各個數本身的值以及它們各自加上的值的和
代碼以下:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int n,q,a,b,c; 8 char w; 9 long long num[100050],sum[300000],add[300000]; 10 11 void Init(int k,int l,int r) 12 { 13 if(l>=r-1) 14 { 15 sum[k]=num[l]; 16 add[k]=0; 17 return ; 18 } 19 20 int mid=(l+r)/2; 21 Init(k*2+1,l,mid); 22 Init(k*2+2,mid,r); 23 sum[k]=sum[k*2+1]+sum[k*2+2]; 24 add[k]=0; 25 } 26 27 void Add(int x,int a,int b,int k,int l,int r) 28 { 29 if(a>=r||b<=l) 30 return ; 31 if(a<=l&&b>=r) 32 { 33 add[k]+=x; 34 return ; 35 } 36 sum[k]+=(long long)x*(min(b,r)-max(a,l)); 37 Add(x,a,b,k*2+1,l,(l+r)/2); 38 Add(x,a,b,k*2+2,(l+r)/2,r); 39 } 40 41 long long Query(int a,int b,int k,int l,int r) 42 { 43 if(a>=r||b<=l) 44 return 0; 45 if(a<=l&&b>=r) 46 return add[k]*(r-l)+sum[k]; 47 return Query(a,b,k*2+1,l,(l+r)/2)+Query(a,b,k*2+2,(l+r)/2,r)+(long long)add[k]*(min(b,r)-max(a,l)); 48 } 49 50 51 52 int main() 53 { 54 while(scanf("%d %d",&n,&q)==2) 55 { 56 memset(sum,0,sizeof(sum)); 57 memset(add,0,sizeof(add)); 58 59 for(int i=1;i<=n;i++) 60 scanf("%lld",&num[i]); 61 62 Init(0,1,n+1); 63 for(int i=1;i<=q;i++) 64 { 65 getchar(); 66 scanf("%c",&w); 67 if(w=='C') 68 { 69 scanf("%d %d %d",&a,&b,&c); 70 Add(c,a,b+1,0,1,n+1); 71 } 72 else if(w=='Q') 73 { 74 scanf("%d %d",&a,&b); 75 printf("%lld\n",Query(a,b+1,0,1,n+1)); 76 } 77 } 78 } 79 80 return 0; 81 }
以後用數狀數組又寫了一遍,比線段樹要快一些
仍是維護兩個樹狀數組bit1和bit2,當要在區間[l,r]上同時加上x時,咱們就執行以下四個操做:
一、在bit1的l位置加上-x(l-1)
二、在bit1的r+1位置加上xr
三、在bit2的l位置加上x
四、在bit2的r+1位置加上-x
設sum(bit,i)表示樹狀數組bit的前i項和,那麼A1~Ai的和即爲sum(bit1,i)+sum(bit2,i)*i(畫個圖很容易理解)
代碼以下:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define MAX 100050 5 6 using namespace std; 7 8 int n,q; 9 long long bit1[MAX],bit2[MAX]; 10 11 int lowbit(int x) 12 { 13 return x&(-x); 14 } 15 16 long long sum(long long *c,int x) 17 { 18 long long ret=0; 19 20 while(x>0) 21 { 22 ret+=c[x]; 23 x-=lowbit(x); 24 } 25 26 return ret; 27 } 28 29 void add(long long *c,int x,long long t) 30 { 31 while(x<=MAX) 32 { 33 c[x]+=t; 34 x+=lowbit(x); 35 } 36 } 37 38 int main() 39 { 40 while(scanf("%d %d",&n,&q)==2) 41 { 42 char ch; 43 int a,b,c; 44 long long temp; 45 for(int i=1;i<=n;i++) 46 { 47 scanf("%lld",&temp); 48 add(bit1,i,temp); 49 } 50 for(int i=0;i<q;i++) 51 { 52 getchar(); 53 ch=getchar(); 54 if(ch=='C') 55 { 56 scanf("%d %d %d",&a,&b,&c); 57 add(bit1,a,(long long)c*(1-a)); 58 add(bit1,b+1,(long long)c*b); 59 add(bit2,a,c); 60 add(bit2,b+1,-c); 61 } 62 else if(ch=='Q') 63 { 64 scanf("%d %d",&a,&b); 65 printf("%lld\n",sum(bit1,b)+sum(bit2,b)*b-sum(bit1,a-1)-sum(bit2,a-1)*(a-1)); 66 } 67 } 68 } 69 70 return 0; 71 }
最近寫線段樹和數狀數組發現,這類題目最關鍵的地方在於結點中要維護什麼樣的信息,以及這些信息如何組合出答案