HDU 1754 I Hate It (线段树)

题意:略。

析:裸的线段树。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200000 + 5;
const int mod = 1e9 + 7;
const int dr[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dc[] = {1, 0, -1, 0, 1, 1, -1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int maxv[maxn<<2];

void pushup(int rt){ maxv[rt] = Max(maxv[rt<<1], maxv[rt<<1|1]); }

void build(int l, int r, int rt){
    if(l == r){
        scanf("%d", &maxv[rt]);
        return ;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int x, int val, int l, int r, int rt){
    if(l == r){
        maxv[rt] = val;
        return ;
    }
    int m = (l + r) >> 1;
    if(x <= m)  update(x, val, lson);
    if(x > m)   update(x, val, rson);
    pushup(rt);
}

int query(int L, int R, int l, int r, int rt){
    if(L <= l && r <= R)  return maxv[rt];
    int m = (l + r) >> 1;
    int ans = -INF;
    if(L <= m)  ans = Max(ans, query(L, R, lson));
    if(R > m)   ans = Max(ans, query(L, R, rson));
    return ans;
}

int main(){
    while(scanf("%d %d", &n, &m) == 2){
        build(1, n, 1);
        char s[5];
        int x, y;
        while(m--){
            scanf("%s", s);
            if(s[0] == 'Q'){
                scanf("%d %d", &x, &y);
                printf("%d
", query(x, y, 1, n, 1));
            }
            else{
                scanf("%d %d", &x, &y);
                update(x, y, 1, n, 1);
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/6080943.html