[JSOI2008]最大數

來源:互聯網
上載者:User

標籤:可以轉化   oid   getc   ring   else   git   href   ret   can   

OJ題號:洛穀P1198、BZOJ1012

思路:

本題可以轉化成一個線段樹問題。總的操作(修改、查詢)不超過M次,說明修改的次數一定≤M。因此我們可以建一棵大小為M的線段樹。修改操作即為單點修改,查詢操作可以轉化成區間最大值,則本題實質上就是單點修改、區間最值的線段樹。

 1 #include<cstdio> 2 #include<cctype> 3 #include<cstring> 4 #include<algorithm> 5 #define maxn 200000 6 #define root 1 7 #define _left <<1 8 #define _right <<1|1 9 char x;10 int m,d,t=0,y,k=0;11 inline char getupper() {12     register char ch;13     while(!isupper(ch=getchar()));14     return ch;15 }16 inline int getint() {17     register char ch;18     while(!isdigit(ch=getchar()));19     register int x=ch-‘0‘;20     while(isdigit(ch=getchar())) x=x*10+ch-‘0‘;21     return x;22 }23 struct SegmentTree {24     int val[maxn<<2];25     void initialize() {26         for(int i=1;i<=m<<1;i++) val[i]=-0x80000000;27     }28     void push_up(const int p) {29         val[p]=std::max(val[p _left],val[p _right]);30     }31     void modify(const int p,const int b,const int e,const int x,const int d) {32         if(b==e) {33             val[p]=d;34             return;35         }36         int mid=(b+e)>>1;37         if(x<=mid) {38             modify(p _left,b,mid,x,d);39         }40         else {41             modify(p _right,mid+1,e,x,d);42         }43         push_up(p);44     }45     int query(const int p,const int b,const int e,const int l,const int r) {46         if((b==l)&&(e==r)) return val[p];47         int max=-0x80000000,mid=(b+e)>>1;48         if(l<=mid) max=std::max(max,query(p _left,b,mid,l,std::min(mid,r)));49         if(r>mid) max=std::max(max,query(p _right,mid+1,e,std::max(mid+1,l),r));50         return max;51     }52 };53 SegmentTree tree;54 int main() {55     scanf("%d%d",&m,&d);56     tree.initialize();57     for(int i=1;i<=m;i++) {58         x=getupper();59         y=getint();60         if(x==‘Q‘) {61             printf("%d\n",t=tree.query(root,1,m,k-y+1,k));62         }63         else {64             tree.modify(root,1,m,++k,(y+t)%d);65         }66     }67     return 0;68 }

 

 

[JSOI2008]最大數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.