线段树入门(建树,查询,更新)hdu1754

先来一道纯线段树的题目:

I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 75617    Accepted Submission(s): 29141


Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
 

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 <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;
const int min1 = -1e9;
int main()
{
    int N, M;
    int score[maxn];
    while (scanf("%d%d",&N,&M)!=EOF){
        for (int i=0; i<N; i++){
            scanf("%d",&score[i]);
        }
        char query;
        int a, b;
        for (int k=0; k<M; k++){
            getchar();//妈的字符输入有毒,好不容易才知道原因~~
            scanf("%c%d%d",&query, &a, &b);
            int ans = min1;
            if (query == 'Q'){
                for (int j = a-1; j<=b-1;j++){
                    if(score[j]>ans){
                        ans = score[j];
                    }
                }
                printf("%d\n",ans);
            }
            else{
                score[a-1] = b;
            }
        }

    }
    return 0;
}

结果可想而知,必定是TLE;
好了那么线段树有什么优点呢?
他的查询的效率高,从O(n)降到了O(logn)的复杂度,AC代码如下:

#include <bits/stdc++.h>
using namespace std;

const int MAX_NODE = 1<<19;
const int maxn = 2e6+5;

struct Node{
    int value;
    int left, right;
}node[MAX_NODE];

int father[maxn];

void buildtree(int i, int l, int r){

    node[i].left = l;
    node[i].right = r;
    node[i].value = 0;
    if (l == r){//这一步要放在下面写,千万不要放在上面,否则会报错~~~被坑了好长时间
        father[l] = i;
        return;
    }
    buildtree(i*2, l, (int)(floor(l+r)/2.0));
    buildtree(i*2+1, (int)(floor(l+r)/2.0)+1, r);
}


void update_tree(int children){
    if (children == 1) return;
    int father = children/2;
    int a = node[father*2].value;
    int b = node[father*2+1].value;
    node[father].value = max(a, b);
    update_tree(father);
}

int MAX_NUMBER;

void query(int i, int l, int r){
    if (node[i].left == l && node[i].right == r) {
        MAX_NUMBER = max(node[i].value, MAX_NUMBER);
        return;
    }
    i = i*2;//这里一开始移位有问题QAQ~~
    if (l<=node[i].right){
        if (r<=node[i].right)
            query(i, l, r);
        else
            query(i, l, node[i].right);
    }
    i++;
    if (r >= node[i].left){
        if (l >= node[i].left)
            query(i, l, r);
        else
            query(i, node[i].left, r);
    }
    return ;
}

int main()
{
    int n, m, g;
    ios::sync_with_stdio(false);
    while(cin>>n>>m){
        buildtree(1, 1, n);
//        for (int i=1; i<=9; i++){
//            cout<<father[i]<<" ";
//        }
        for (int i=1; i<=n; i++){
            cin>>g;
            node[father[i]].value = g;
            update_tree(father[i]);
        }
        string op;
        int a, b;
        while (m--){
            cin>>op>>a>>b;
            if (op[0]=='Q'){
                MAX_NUMBER = -10000;
                //cout<<op<<" "<<a<<" "<<b<<endl;
                query(1, a, b);
                cout<<MAX_NUMBER<<endl;
            }
            else{
                node[father[a]].value = b;
                update_tree(father[a]);
            }
        }
    }
    return 0;
}


这里说一下大概的思路,首先,不用知道每个点的权值就可以先,建立一棵树,至于father[]数组的用处,可以打印出来看到它的作用是记录单个数组成的区间的编号:
当n = 5时:
father[1] = 8, father[2] = 9,依次类推:5, 6, 7;


学习资料的来源: 线段树入门(1)
这么经典的题目,乖乖滚回去再敲一遍(逃

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值