線段樹、樹狀數組

線段樹

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

不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程序,模擬老師的詢問。當然,老師有時候需要更新某位同學的成績。
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

Hint
Huge input,the C function scanf() will work better than cin

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int maxx[200010*3],s[200010];
void pushup(int num)
{
    maxx[num]=max(maxx[num<<1],maxx[(num<<1)+1]);
}
void build(int l,int r,int num)
{
    if(l==r)
    {
        maxx[num]=s[l];
        //printf("%d %d\n",num,maxx[num]);
        return ;
    }
    int mid=(l+r)/2;
    build(l,mid,num<<1);
    build(mid+1,r,(num<<1)+1);
    pushup(num);
}
int query(int l1,int r1,int l,int r,int num)
{
    if(l1<=l && r1>=r) return maxx[num];
    int mid=(l+r)/2;
    int ret=0;
    if(l1<=mid)  ret=max(ret,query(l1,r1,l,mid,num<<1));
    if(r1>mid)   ret=max(ret,query(l1,r1,mid+1,r,(num<<1)+1));
    return ret;
}
void update(int flag,int s,int l,int r,int num)
{
    if(l==r)
    {
        maxx[num]=s;
        return ;
    }
    int mid=(l+r)/2;
    if(flag<=mid){
    update(flag,s,l,mid,num<<1);
    }
    else{
    update(flag,s,mid+1,r,(num<<1)+1);
    }
    pushup(num);
}
int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=1;i<=n;i++){
            scanf("%d",&s[i]);}
        build(1,n,1);
        char ch;
        int l1,r1;
        while(m--)
        {
            scanf(" %c %d %d",&ch,&l1,&r1);
            if(ch=='Q')
                printf("%d\n",query(l1,r1,1,n,1));
            else
                update(l1,r1,1,n,1);
        }
    }
    return 0;
}

樹狀數組

例題
Stars
Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.
在這裏插入圖片描述

For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it’s formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.

You are to write a program that will count the amounts of the stars of each level on a given map.
Input
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample Output
1
2
1
1
0
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

樹狀數組更容易解決

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstring>
using namespace std;
#define lowbit(x) ((x)&-(x))
int tree[32010],lv[15010];
int n;
void add(int k, int val)
{
	while (k <= 32010)
	{
		tree[k] += val;
		k += lowbit(k);
	}
}

int sum(int x)
{
    int sum=0;
    while(x>0)
    {
        sum+=tree[x];
        x-=lowbit(x);
    }
    return sum;
}
int main()
{
    while(~scanf("%d",&n))
    {
        int x,y;
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&x,&y);
            x++;//x,y可以取到0,但樹狀數組下標是不能爲0的,所以x++
            y++;
            add(x,1);
            int temp=sum(x);
            lv[temp-1]++;
        }
        for(int i=0;i<n;i++){
            printf("%d\n",lv[i]);
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章