[kuangbin帶你飛]專題七 線段樹

A - 敵兵佈陣 HDU - 1166

C國的死對頭A國這段時間正在進行軍事演習,因此C國間諜頭子Derek和他手下Tidy又開始忙乎了。A國在海岸線沿直線佈置了N個工兵營地,Derek和Tidy的任務就是要監視這些工兵營地的活動狀況。因爲採起了某種先進的監測手段,因此每一個工兵營地的人數C國都掌握的一清二楚,每一個工兵營地的人數都有可能發生變更,可能增長或減小若干人手,但這些都逃不過C國的監視。 
中央情報局要研究敵人究竟演習什麼戰術,因此Tidy要隨時向Derek彙報某一段連續的工兵營地一共有多少人,例如Derek問:「Tidy,立刻彙報第3個營地到第10個營地共有多少人!」Tidy就要立刻開始計算這一段的總人數並彙報。但敵兵營地的人數常常變更,而Derek每次詢問的段都不同,因此Tidy不得不每次都一個一個營地的去數,很快就精疲力盡了,Derek對Tidy的計算速度愈來愈不滿:"你個死肥仔,算得這麼慢,我炒你魷魚!」Tidy想:「你本身來算算看,這可真是一項累人的工做!我巴不得你炒我魷魚呢!」無奈之下,Tidy只好打電話向計算機專家Windbreaker求救,Windbreaker說:「死肥仔,叫你平時作多點acm題和看多點算法書,如今嚐到苦果了吧!」Tidy說:"我知錯了。。。"但Windbreaker已經掛掉電話了。Tidy很苦惱,這麼算他真的會崩潰的,聰明的讀者,你能寫個程序幫他完成這項工做嗎?不過若是你的程序效率不夠高的話,Tidy仍是會受到Derek的責罵的. 

Input第一行一個整數T,表示有T組數據。 
每組數據第一行一個正整數N(N<=50000),表示敵人有N個工兵營地,接下來有N個正整數,第i個正整數ai表明第i個工兵營地裏開始時有ai我的(1<=ai<=50)。 
接下來每行有一條命令,命令有4種形式: 
(1) Add i j,i和j爲正整數,表示第i個營地增長j我的(j不超過30) 
(2)Sub i j ,i和j爲正整數,表示第i個營地減小j我的(j不超過30); 
(3)Query i j ,i和j爲正整數,i<=j,表示詢問第i到第j個營地的總人數; 
(4)End 表示結束,這條命令在每組數據最後出現; 
每組數據最多有40000條命令 
Output對第i組數據,首先輸出「Case i:」和回車, 
對於每一個Query詢問,輸出一個整數並回車,表示詢問的段中的總人數,這個數保持在int之內。 
Sample Input
ios

1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End 

Sample Output算法

Case 1:
6
33
59

簡單的線段樹模板的應用,具體的細節能夠看代碼的註釋。這題涉及到到區間求和和單點修改。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>
using namespace std; typedef long long LL; const int maxn=50005; const int INF=0x3f3f3f3f; int n,p,q,c; int a[maxn]; char s[10]; struct Tree//存儲這個線段的端點值和線段維護的信息
{ int l,r; int sum; }tree[maxn<<2]; void build(int k,int l,int r)//建樹的過程
{ tree[k].l=l;tree[k].r=r; if(l==r) { tree[k].sum=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum; } void change(int k,int l,int r,int val)/*更新的過程,我習慣性將區間更新和單點更新記一塊兒,區間更新就是多了一個pushdown,單點更新的時候只須要在傳參數的時候把l,r傳同一個值就能夠了。*/ { if(tree[k].l==l&&tree[k].r==r) { tree[k].sum+=val; return; } if(tree[k].l==tree[k].r) return; int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r,val); else if(mid<l) change(k<<1|1,l,r,val); else { change(k<<1,l,mid,val); change(k<<1|1,mid+1,r,val); } tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum; } int query(int k,int l,int r)/*區間查詢,道理和上面同樣,這個真的看我的習慣怎麼寫*/ { int ans=0; if(tree[k].l==l&&tree[k].r==r) return tree[k].sum; int mid=(tree[k].l+tree[k].r)>>1; if(r<=mid) ans+=query(k<<1,l,r); else if(l>=mid+1) ans+=query(k<<1|1,l,r); else { ans+=query(k<<1,l,mid); ans+=query(k<<1|1,mid+1,r); } return ans; } int main() { int T,casee=1; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(tree,0,sizeof(tree)); build(1,1,n); printf("Case %d:\n",casee++); while(scanf("%s",s)) { if(s[0]=='E') break; else if(s[0]=='A') { scanf("%d %d",&p,&c); change(1,p,p,c); } else if(s[0]=='S') { scanf("%d %d",&p,&c); change(1,p,p,-c); } else if(s[0]=='Q') { scanf("%d %d",&p,&q); printf("%d\n",query(1,p,q)); } } } return 0; }

B - I Hate It HDU - 1754

不少學校流行一種比較的習慣。老師們很喜歡詢問,從某某到某某當中,分數最高的是多少。 
這讓不少學生很反感。 

無論你喜不喜歡,如今須要你作的是,就是按照老師的要求,寫一個程序,模擬老師的詢問。固然,老師有時候須要更新某位同窗的成績。
Input本題目包含多組測試,請處理到文件結束。 
在每一個測試的第一行,有兩個正整數 N 和 M ( 0<N<=200000,0<M<5000 ),分別表明學生的數目和操做的數目。 
學生ID編號分別從1編到N。 
第二行包含N個整數,表明這N個學生的初始成績,其中第i個數表明ID爲i的學生的成績。 
接下來有M行。每一行有一個字符 C (只取'Q'或'U') ,和兩個正整數A,B。 
當C爲'Q'的時候,表示這是一條詢問操做,它詢問ID從A到B(包括A,B)的學生當中,成績最高的是多少。 
當C爲'U'的時候,表示這是一條更新操做,要求把ID爲A的學生的成績更改成B。 
Output對於每一次詢問操做,在一行裏面輸出最高成績。Sample Input
5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5
Sample Output
5
6
5
9

區間查詢和單點更新
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>
using namespace std; typedef long long LL; const int maxn=200005; const int INF=0x3f3f3f3f; int n,m,p,q,c; int a[maxn]; char s[10]; struct Tree { int l,r; int sum; }tree[maxn<<2]; void build(int k,int l,int r) { tree[k].l=l;tree[k].r=r; if(l==r) { tree[k].sum=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].sum=max(tree[k<<1].sum,tree[k<<1|1].sum); } void change(int k,int l,int r,int val) { if(tree[k].l==l&&tree[k].r==r) { tree[k].sum=val; return; } if(tree[k].l==tree[k].r) return; int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r,val); else if(mid<l) change(k<<1|1,l,r,val); else { change(k<<1,l,mid,val); change(k<<1|1,mid+1,r,val); } tree[k].sum=max(tree[k<<1].sum,tree[k<<1|1].sum); } int query(int k,int l,int r) { int ans=0; if(tree[k].l==l&&tree[k].r==r) return tree[k].sum; int mid=(tree[k].l+tree[k].r)>>1; if(r<=mid) ans=query(k<<1,l,r); else if(l>=mid+1) ans=query(k<<1|1,l,r); else { ans=max(query(k<<1,l,mid),query(k<<1|1,mid+1,r)); } return ans; } int main() { while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(tree,0,sizeof(tree)); build(1,1,n); while(m--) { scanf("%s",s); if(s[0]=='Q') { scanf("%d %d",&p,&q); printf("%d\n",query(1,p,q)); } else { scanf("%d %d",&p,&c); change(1,p,p,c); } } } return 0; }

C - A Simple Problem with Integers POJ - 3468 

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.less

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.ide

Output

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

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

區間更新和區間查詢,要用到lazy標記。
題意:支持兩種操做,一個是把一個區間內每一個人都加上或都減去同一個數,一個是查詢一個區間內的最大值。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>
using namespace std; typedef long long LL; const int maxn=100005; const int INF=0x3f3f3f3f; int n,m,p,q,c; int a[maxn]; char s[10]; struct Tree { int l,r; LL sum,lazy; }tree[maxn*8]; void build(int k,int l,int r) { tree[k].l=l;tree[k].r=r;tree[k].lazy=0; if(l==r) { tree[k].sum=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum; } void pushdown(int x) { if(tree[x].lazy!=0) { tree[x<<1].lazy+=tree[x].lazy; tree[x<<1|1].lazy+=tree[x].lazy; tree[x<<1].sum+=tree[x].lazy*(tree[x<<1].r-tree[x<<1].l+1); tree[x<<1|1].sum+=tree[x].lazy*(tree[x<<1|1].r-tree[x<<1|1].l+1); tree[x].lazy=0; } } void change(int k,int l,int r,int val) { pushdown(k); if(tree[k].l==l&&tree[k].r==r) { tree[k].lazy+=val; tree[k].sum+=val*(r-l+1); return; } if(tree[k].l==tree[k].r) return; int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r,val); else if(mid<l) change(k<<1|1,l,r,val); else { change(k<<1,l,mid,val); change(k<<1|1,mid+1,r,val); } tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum; } LL query(int k,int l,int r) { pushdown(k); LL ans=0; if(tree[k].l==l&&tree[k].r==r) return tree[k].sum; int mid=(tree[k].l+tree[k].r)>>1; if(r<=mid) ans+=query(k<<1,l,r); else if(l>=mid+1) ans+=query(k<<1|1,l,r); else { ans+=query(k<<1,l,mid); ans+=query(k<<1|1,mid+1,r); } return ans; } int main() { while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(tree,0,sizeof(tree)); build(1,1,n); while(m--) { scanf("%s",s); if(s[0]=='Q') { scanf("%d %d",&p,&q); printf("%lld\n",query(1,p,q)); } else { scanf("%d %d %d",&p,&q,&c); change(1,p,q,c); } } } return 0; }

D - Mayor's posters POJ - 2528

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules: 
  • Every candidate can place exactly one poster on the wall. 
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown). 
  • The wall is divided into segments and the width of each segment is one byte. 
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections. 
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall. 
Input
The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,... , ri.
Output
For each input data set print the number of visible posters after all the posters are placed. 

The picture below illustrates the case of the sample input. 
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4

區間覆蓋,可是這題須要離散化。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>
using namespace std; typedef long long LL; const int maxn=10005; const int INF=0x3f3f3f3f; int n,m,p,q,c; int li[maxn],ri[maxn]; int x[maxn<<1],hashh[10000010]; struct Tree { int l,r; int lazy; }tree[maxn<<3]; void build(int k,int l,int r) { tree[k].l=l;tree[k].r=r;tree[k].lazy=0; if(l==r) { // tree[k].sum=a[l];
        return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); // tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
} //void pushdown(int x) //{ // if(tree[x].l!=tree[x].r) // { // tree[x<<1].lazy+=tree[x].lazy; // tree[x<<1|1].lazy+=tree[x].lazy; // tree[x<<1].sum+=tree[x].lazy*(tree[x<<1].r-tree[x<<1].l+1); // tree[x<<1|1].sum+=tree[x].lazy*(tree[x<<1|1].r-tree[x<<1|1].l+1); // } // tree[x].lazy=0; //}
bool change(int k,int l,int r) { if(tree[k].lazy) return false; if(tree[k].l==l&&tree[k].r==r) { tree[k].lazy=true; // tree[k].sum+=val*(r-l+1);
        return true; } bool temp; int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) temp=change(k<<1,l,r); else if(mid<l) temp=change(k<<1|1,l,r); else { bool t1=change(k<<1,l,mid); bool t2=change(k<<1|1,mid+1,r); temp=t1||t2; } tree[k].lazy=tree[k<<1].lazy&&tree[k<<1|1].lazy; return temp; } //LL query(int k,int l,int r) //{ // if(tree[k].lazy) // pushdown(k); // LL ans=0; // if(tree[k].l==l&&tree[k].r==r) // return tree[k].sum; // int mid=(tree[k].l+tree[k].r)>>1; // if(r<=mid) // ans+=query(k<<1,l,r); // else if(l>=mid+1) // ans+=query(k<<1|1,l,r); // else // { // ans+=query(k<<1,l,mid); // ans+=query(k<<1|1,mid+1,r); // } // return ans; //}
int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(tree,0,sizeof(tree)); int cnt=0; for(int i=0;i<n;i++) { scanf("%d %d",&li[i],&ri[i]); x[cnt++]=li[i]; x[cnt++]=ri[i]; } sort(x,x+cnt); cnt=unique(x,x+cnt)-x; for(int i=0;i<cnt;i++) hashh[x[i]]=i+1; build(1,1,cnt); int ans=0; for(int i=n-1;i>=0;i--) { if(change(1,hashh[li[i]],hashh[ri[i]])) ans++; } printf("%d\n",ans); } return 0; }

E - Just a Hook HDU - 1698 

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. 



Now Pudge wants to do some operations on the hook. 

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks. 
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows: 

For each cupreous stick, the value is 1. 
For each silver stick, the value is 2. 
For each golden stick, the value is 3. 

Pudge wants to know the total value of the hook after performing the operations. 
You may consider the original hook is made up of cupreous sticks. 

InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases. 
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations. 
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind. 
OutputFor each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example. 
Sample Input
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>
using namespace std; typedef long long LL; const int maxn=10005; const int INF=0x3f3f3f3f; int n,m,p,q,c; int li[maxn],ri[maxn]; int x[maxn<<1],hashh[10000010]; struct Tree { int l,r; LL sum,lazy; }tree[maxn<<3]; void build(int k,int l,int r) { tree[k].l=l;tree[k].r=r;tree[k].lazy=0; if(l==r) { tree[k].sum=1; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum; } void pushdown(int x) { if(tree[x].l!=tree[x].r) { tree[x<<1].lazy=tree[x].lazy; tree[x<<1|1].lazy=tree[x].lazy; tree[x<<1].sum=tree[x].lazy*(tree[x<<1].r-tree[x<<1].l+1); tree[x<<1|1].sum=tree[x].lazy*(tree[x<<1|1].r-tree[x<<1|1].l+1); } tree[x].lazy=0; } void change(int k,int l,int r,int val) { if(tree[k].lazy) pushdown(k); if(tree[k].l==l&&tree[k].r==r) { tree[k].sum=val*(r-l+1); tree[k].lazy=val; return; } int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r,val); else if(mid<l) change(k<<1|1,l,r,val); else { change(k<<1,l,mid,val); change(k<<1|1,mid+1,r,val); } tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum; } int query(int k,int l,int r) { if(tree[k].lazy) pushdown(k); int ans=0; if(tree[k].l==l&&tree[k].r==r) return tree[k].sum; int mid=(tree[k].l+tree[k].r)>>1; if(r<=mid) ans+=query(k<<1,l,r); else if(l>=mid+1) ans+=query(k<<1|1,l,r); else { ans+=query(k<<1,l,mid); ans+=query(k<<1|1,mid+1,r); } return ans; } int main() { int T,casee=1; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(tree,0,sizeof(tree)); build(1,1,n); scanf("%d",&m); while(m--) { scanf("%d %d %d",&p,&q,&c); change(1,p,q,c); } printf("Case %d: The total value of the hook is %d.\n",casee++,query(1,1,n)); } return 0; }

F - Count the Colors ZOJ - 1610 

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.

Your task is counting the segments of different colors you can see at last.測試

 

Input

The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.

 

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:

x1 x2 c

x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.ui

All the numbers are in the range [0, 8000], and they are all integers.this

Input may contain several data set, process to the end of file.lua


Output

Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.

 

If some color can't be seen, you shouldn't print it.spa

Print a blank line after every dataset.

Sample Input

5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1
Sample Output

1 1
2 1
3 1

1 1

0 2
1 1

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>
using namespace std; typedef long long LL; const int maxn=8005; const int INF=0x3f3f3f3f; int n,m,p,q,c; int a[maxn],ans[maxn]; struct Tree { int l,r; int lazy; }tree[maxn<<2]; void build(int k,int l,int r) { tree[k].l=l;tree[k].r=r;tree[k].lazy=-1; if(l==r) { return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); } void pushdown(int x) { if(tree[x].l!=tree[x].r) { tree[x<<1].lazy=tree[x].lazy; tree[x<<1|1].lazy=tree[x].lazy; tree[x].lazy=-1; } } void change(int k,int l,int r,int val) { if(tree[k].lazy!=-1) pushdown(k); if(tree[k].l==l&&tree[k].r==r) { tree[k].lazy=val; return; } int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r,val); else if(mid<l) change(k<<1|1,l,r,val); else { change(k<<1,l,mid,val); change(k<<1|1,mid+1,r,val); } } int query(int k,int l,int r) { if(tree[k].lazy!=-1) pushdown(k); if(tree[k].l==l&&tree[k].r==r) return tree[k].lazy; int mid=(tree[k].l+tree[k].r)>>1; if(r<=mid) query(k<<1,l,r); else if(l>=mid+1) query(k<<1|1,l,r); else { query(k<<1,l,mid); query(k<<1|1,mid+1,r); } } int main() { while(scanf("%d",&n)!=EOF) { memset(tree,0,sizeof(tree)); memset(ans,0,sizeof(ans)); memset(a,-1,sizeof(a)); build(1,1,8001); for(int i=0;i<n;i++) { scanf("%d %d %d",&p,&q,&c); change(1,p+1,q,c); } for(int i=1;i<=8001;i++) a[i]=query(1,i,i); for(int i=2;i<=8005;i++) { if(a[i]!=a[i-1]&&a[i-1]!=-1) { ans[a[i-1]]++; } } for(int i=0;i<=8000;i++) { if(ans[i]) printf("%d %d\n",i,ans[i]); } printf("\n"); } return 0; }

G - Balanced Lineup POJ - 3264

 

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers, N and Q. 
Lines 2.. N+1: Line i+1 contains a single integer that is the height of cow i 
Lines N+2.. N+ Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

Output

Lines 1.. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>
using namespace std; typedef long long LL; const int maxn=50005; const int INF=0x3f3f3f3f; int n,m,p,q,c; int a[maxn]; int ansmax,ansmin; struct Tree { int l,r; int maxx,minn; }tree[maxn<<3]; void build(int k,int l,int r) { tree[k].l=l;tree[k].r=r;tree[k].minn=INF;tree[k].maxx=0; if(l==r) { tree[k].maxx=a[l]; tree[k].minn=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].minn=min(tree[k<<1].minn,tree[k<<1|1].minn); tree[k].maxx=max(tree[k<<1].maxx,tree[k<<1|1].maxx); } void query(int k,int l,int r) { if(tree[k].l==l&&tree[k].r==r) { ansmax=max(ansmax,tree[k].maxx); ansmin=min(ansmin,tree[k].minn); return; } int mid=(tree[k].l+tree[k].r)>>1; if(r<=mid) query(k<<1,l,r); else if(l>=mid+1) query(k<<1|1,l,r); else { query(k<<1,l,mid); query(k<<1|1,mid+1,r); } } int main() { while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(tree,0,sizeof(tree)); build(1,1,n); while(m--) { scanf("%d %d",&p,&q); ansmax=0;ansmin=INF; query(1,p,q); printf("%d\n",ansmax-ansmin); } } return 0; }

H - Can you answer these queries? HDU - 4027

A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help. 
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line. 

Notice that the square root operation should be rounded down to integer.

InputThe input contains several test cases, terminated by EOF. 
  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000) 
  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63
  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000) 
  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive. 
OutputFor each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.Sample Input
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
Sample Output
Case #1:
19
7
6
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>
using namespace std; typedef long long LL; const int maxn=100005; const int INF=0x3f3f3f3f; int n,m,p,q,flag; LL a[maxn],ans[maxn]; struct Tree { int l,r; LL sum,len; }tree[maxn<<2]; void build(int k,int l,int r) { tree[k].l=l;tree[k].r=r;tree[k].len=r-l+1; if(l==r) { tree[k].sum=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum; } void pushdown(int k,int l,int r) { if(tree[k].l==tree[k].r) { tree[k].sum=(LL)sqrt(tree[k].sum); return; } int mid=(tree[k].l+tree[k].r)>>1; pushdown(k<<1,l,mid); pushdown(k<<1|1,mid+1,r); tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum; } void change(int k,int l,int r) { if(tree[k].l==l&&tree[k].r==r) { if(tree[k].sum==tree[k].len) return; pushdown(k,l,r); return; } int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r); else if(mid<l) change(k<<1|1,l,r); else { change(k<<1,l,mid); change(k<<1|1,mid+1,r); } tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum; } LL query(int k,int l,int r) { if(tree[k].l==l&&tree[k].r==r) return tree[k].sum; LL ans=0; int mid=(tree[k].l+tree[k].r)>>1; if(r<=mid) ans+=query(k<<1,l,r); else if(l>=mid+1) ans+=query(k<<1|1,l,r); else { ans+=query(k<<1,l,mid); ans+=query(k<<1|1,mid+1,r); } return ans; } int main() { int casee=1; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) scanf("%lld",&a[i]); build(1,1,n); scanf("%d",&m); printf("Case #%d:\n",casee++); while(m--) { scanf("%d %d %d",&flag,&p,&q); if(p>q) swap(p,q); if(!flag) change(1,p,q); else printf("%lld\n",query(1,p,q)); } printf("\n"); } return 0; }
相關文章
相關標籤/搜索