HDU 1754 I Hate It 线段树 && Splay && zkw线段树

来源:互联网 发布:域名查询服务 编辑:程序博客网 时间:2024/05/03 03:33

Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N200000,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

线段树裸题。所以就不给出裸的代码了。
给出Splay Tree 以及 zkw线段树 的写法

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <vector>#include <map>#include <stack>#include <queue>#include <set>#include <cmath>#include <algorithm>#include <ctime>using namespace std;inline bool Read ( int &x ) { char c = getchar() ; x = 0 ; bool f = 0 ; while ( !isdigit(c) ) { if ( c == '-' ) f = 1 ; if ( c == EOF ) return false ;  c = getchar() ; } while ( isdigit(c) ) { x = 10 * x + c - '0' ; c = getchar() ; } if (f) x = -x ;return true ; }inline void Print ( int x ) { int len = 0, a[30] ; if ( x == 0 ) { putchar('0') ; return ; } if ( x < 0 ) { putchar('-') ; x = -x ; } while (x) { a[++len] = x%10 ; x /= 10 ; } while (len) putchar(a[len--]+'0') ;}const int maxn = 200010;static int n, m, ch[maxn][2], fa[maxn], val[maxn], cnt, root=1, maxx[maxn], a[maxn] ;#define L ch][0#define R ch][1inline void new_node ( int rt ) {    rt[val] = a[rt-1] ;    rt[L] = rt[R] = rt[fa] = 0 ;    }inline void push_up ( int x ) { x[maxx] = max ( max ( x[R][maxx], x[L][maxx] ), x[val] ) ; }inline int create_tree ( int l, int r ) {    if ( l > r ) return 0 ;    register int rt = l+r >> 1 ;    new_node(rt) ;     val[rt] = a[rt-1] ;     (rt[L] = create_tree ( l, rt-1 )) [fa] = rt ;    (rt[R] = create_tree ( rt+1, r )) [fa] = rt ;    push_up(rt) ;    return rt ;}inline void Rotate ( int x ) {    register int y = x[fa], z = y[fa], T = y[R]==x, son = ch[x][T^1] ;    if ( y == root ) root = x ;    if ( y != root ) ch[z][ z[R]==y ] = x ;    fa[x] = z ;    fa[y] = x ;    if ( son ) fa[son] = y ;    ch[y][T] = son ;    ch[x][T^1] = y ;    push_up(y) ;}inline void splay ( int x, int tar = 0 ) {    while ( x!=root && fa[x]!=tar ) {        register int y = x[fa], z = y[fa] ;        bool f1 = y[R]==x, f2 = z[R] == y ;        if ( y == root || z == tar ) ;        else if ( f1^f2 ) Rotate(x) ;        else Rotate(y) ;        Rotate(x) ;    }    push_up(x) ;}char cmd[5] ;inline void update ( int x ) {    push_up(x) ;    if ( x != root ) update ( x[fa] ) ;}int main() {    register int i, j, k, x, y ;    while ( Read(n) ) {        Read(m) ;        cnt = 0;        a[n+1] = a[n+2] = a[n+3] = a[n+4] = a[0] = -1000000000 ;        for ( i = 1 ; i <= n ; i ++ )             Read(a[i]) ;        root = n+4>>1;        create_tree(1,n+3) ;        for ( i = n+3 ; i ; i -- )             push_up(i) ;        while (m--) {            scanf ( "%s", cmd ) ;            if ( cmd[0] == 'Q' ) {                Read(x) ; Read(y) ;                splay(x) ;                splay(y+2,x) ;                printf ( "%d\n", root[R][L][maxx] ) ;            } else {                Read(x) ; Read(k) ;                ++ x ;                x[val] = k ;                splay(x) ;                update(x) ;            }        }    }    return 0 ;}

zkw线段树:

#include <bits/stdc++.h>using namespace std ;inline bool Read ( int &x ) {    char c = getchar() ; bool f = 0 ; x = 0 ;    while ( !isdigit(c) ) {        if ( c == '-' ) f = 1 ;        if ( c == EOF ) return false ;        c = getchar() ;    }    while ( isdigit(c) ) {        x = 10 * x + c - '0' ;        c = getchar() ;    } if (f) x = -x ;    return true ;}const int maxn = 200010 ;int n, m, tree[maxn*3], Base ;int Max ( int x, int y ) { return x > y ? x : y ; }void push_up ( int h ) { tree[h] = Max ( tree[h<<1], tree[h<<1|1] ) ; }void create_tree() {    for ( Base = 1 ; Base <= n ; Base <<= 1 ) ;    for ( register int i = 1 ; i <= n ; i ++ )         Read(tree[Base+i]) ;    for ( register int i = Base-1 ; i>0 ; i -- )         push_up(i) ;}int query ( int s, int t ) {    int rec = -1 ;    for ( s = s+Base-1, t = t+Base+1 ; s^t^1 ; s >>= 1, t >>= 1 ) {        if ( ~s&1 ) rec = Max ( rec, tree[s^1] ) ;        if ( t&1 ) rec = Max ( rec, tree[t^1] ) ;    }    return rec ;}void update ( int s, int val ) { for ( tree[s+=Base] = val, s>>=1 ; s ; s >>= 1 ) push_up(s) ; }char cmd[5] ;int main() {    register int x, y ;    Read(n) ; Read(m) ;    create_tree() ;    while (m--) {        scanf ( "%s", cmd ) ;        Read(x) ; Read(y) ;        if ( cmd[0] == 'Q' ) printf ( "%d\n", query(x,y) ) ;        else update ( x, y ) ;    }    return 0 ;}
1 0
原创粉丝点击