HDU 1754 I Hate It 線段樹

來源:互聯網
上載者:User

題意:
本題目包含多組測試,請處理到檔案結束。
在每個測試的第一行,有兩個正整數 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

題解:當然是線段樹,不過下面的兩種代碼差別還挺大的。

對於有些靜態RMQ,它給予了初始的狀態,則可以根據所有狀態一次性建造線段樹,

而像有些未給定初始狀態,只是每次會添加一些資訊的,可以開始時只建空樹。

1046MS:

#include <iostream>using namespace std;#define N 200005struct item{int l, r, num;} node[N*3];int max ( int a, int b ){return a > b ? a : b;}void build ( int l, int r, int u ){node[u].l = l;node[u].r = r;node[u].num = 0;if ( l == r ) return;int mid = ( l + r ) >> 1;build ( l, mid, u << 1 );build ( mid + 1, r, (u<<1) + 1 );}void update ( int pos, int val, int u ){if ( node[u].l == pos && node[u].r == pos ){node[u].num = val;return;}int mid = ( node[u].l + node[u].r ) >> 1;if ( pos <= mid )update ( pos, val, u << 1 );else update ( pos, val, (u << 1) + 1 );node[u].num = max ( node[u<<1].num, node[(u<<1)+1].num );}int query ( int l, int r, int u ){if ( node[u].l == l && r == node[u].r )return node[u].num;int mid = ( node[u].l + node[u].r ) >> 1;if ( r <= mid )return query ( l, r, u << 1 );else if ( l > mid )return query ( l, r, ( u << 1 ) + 1 );elsereturn max ( query ( l, mid, u << 1 ), query ( mid + 1, r, (u << 1 ) + 1 ) );}int main(){char ch[3];int n, m, score, a, b;//freopen("a.txt","r",stdin);while ( scanf("%d%d",&n,&m) != EOF ){build ( 1, n, 1 );for ( int i = 1; i <= n; ++i ){scanf("%d",&score);update ( i, score, 1 ); }while ( m-- ){scanf( "%s %d %d", ch, &a, &b );if ( ch[0] == 'Q' )printf( "%d\n", query ( a, b, 1 ) );elseupdate ( a, b, 1 );}}return 0;}

500MS

#include <iostream>using namespace std;#define N 200005int score[N];struct item{int l, r, num;} node[N*3];int max ( int a, int b ){return a > b ? a : b;}void build ( int l, int r, int u ){node[u].l = l;node[u].r = r;if ( l == r ){node[u].num = score[l];return;}int mid = ( l + r ) >> 1;build ( l, mid, u << 1 );build ( mid + 1, r, (u<<1) + 1 );node[u].num = max ( node[u<<1].num, node[(u<<1)+1].num );}void update ( int pos, int val, int u ){if ( node[u].l == pos && node[u].r == pos ){node[u].num = val;return;}int mid = ( node[u].l + node[u].r ) >> 1;if ( pos <= mid )update ( pos, val, u << 1 );else update ( pos, val, (u << 1) + 1 );node[u].num = max ( node[u<<1].num, node[(u<<1)+1].num );}int query ( int l, int r, int u ){if ( node[u].l == l && r == node[u].r )return node[u].num;int mid = ( node[u].l + node[u].r ) >> 1;if ( r <= mid )return query ( l, r, u << 1 );else if ( l > mid )return query ( l, r, ( u << 1 ) + 1 );elsereturn max ( query ( l, mid, u << 1 ), query ( mid + 1, r, (u << 1 ) + 1 ) );}int main(){char ch[3];int n, m, a, b;//freopen("a.txt","r",stdin);while ( scanf("%d%d",&n,&m) != EOF ){for ( int i = 1; i <= n; ++i )scanf("%d",score+i);    build ( 1, n, 1 );while ( m-- ){scanf( "%s %d %d", ch, &a, &b );if ( ch[0] == 'Q' )printf( "%d\n", query ( a, b, 1 ) );elseupdate ( a, b, 1 );}}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.