湖南省第七屆大學生電腦程式設計競賽 RMQ with Shifts (線段樹),rmqshifts

來源:互聯網
上載者:User

湖南省第七屆大學生電腦程式設計競賽 RMQ with Shifts (線段樹),rmqshifts
RMQ with Shifts時間限制:1000 ms  |  記憶體限制:65535 KB難度:3

描述
    In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) (L<=R), we report the minimum value among A[L], A[L+1], …, A[R]. Note that the indices start from 1, i.e. the left-most element is A[1].      In this problem, the array A is no longer static: we need to support another operation shift(i1, i2, i3, …, ik) (i1<i2<...<ik, k>1): we do a left “circular shift” of A[i1], A[i2], …, A[ik].       For example, if A={6, 2, 4, 8, 5, 1, 4}, then shift(2, 4, 5, 7) yields {6, 8, 4, 5, 4, 1, 2}. After that, shift(1,2) yields {8, 6, 4, 5, 4, 1, 2}. 
輸入
There will be only one test case, beginning with two integers n, q (1<=n<=100,000, 1<=q<=120,000), the number of integers in array A, and the number of operations. The next line contains n positive integers not greater than 100,000, the initial elements in array A. Each of the next q lines contains an operation. Each operation is formatted as a string having no more than 30 characters, with no space characters inside. All operations are guaranteed to be valid. Warning: The dataset is large, better to use faster I/O methods.
輸出
For each query, print the minimum value (rather than index) in the requested range.
範例輸入
7 5 6 2 4 8 5 1 4 query(3,7) shift(2,4,5,7) query(1,4) shift(1,2) query(2,2) 
範例輸出
1 4 6 
來源

湖南省第七屆大學生電腦程式設計競賽


算是線段樹的基礎題了,單點更新,基本的操作都沒有什麼難度,這道題主要要搞清楚那個迴圈左移的,對於這個操作,開始不知道怎麼操作,還有輸入的時候也要注意,考驗很多細節的處理,還有處理問題的一些技巧,對於這種變形還要多加練習,積累一些處理問題的經驗。

可以歸納總結自己的一些模板,很多基本操作的時候可以套用,具體問題都是考察一些細節和技巧的處理。

下面是代碼:

#include <cstdio>#include <algorithm>using namespace std;const int maxn=100000+10;int a[maxn];int index[maxn];struct node//線段樹的節點結構{    int l,r,val;}t[maxn*3];void build(int root,int l,int r)//建樹{    t[root].l=l;    t[root].r=r;    if(l==r)    {        t[root].val=a[l];        return ;    }    int m=(l+r)>>1;    build(root<<1,l,m);    build(root<<1|1,m+1,r);    t[root].val=min(t[root<<1].val,t[root<<1|1].val);}void update(int root,int l,int r,int val)//更新操作{    if(t[root].l==l && t[root].r==r)    {        t[root].val=val;        return ;    }    int m=(t[root].l+t[root].r)>>1;    if(m>=r) update(root<<1,l,r,val);    else        update(root<<1|1,l,r,val);    t[root].val=min(t[root<<1].val,t[root<<1|1].val);}int query(int root,int l,int r)//查詢{    if(t[root].l==l && t[root].r==r)        return t[root].val;    int m=(t[root].l+t[root].r)>>1;    if(m>=r) return query(root<<1,l,r);    else if(l>m) return query(root<<1|1,l,r);    else        return min(query(root<<1,l,m),query(root<<1|1,m+1,r));}int main(){     int n,q,l,r;     char s[100];     scanf("%d%d",&n,&q);     for(int i=1;i<=n;i++) scanf("%d",&a[i]);     build(1,1,n);     for(int i=0;i<q;i++)     {         scanf("%s",s);         if(s[0]=='q')         {            sscanf(s+5,"(%d,%d)",&l,&r);            printf("%d\n",query(1,l,r));         }         else//主要是對迴圈左移的操作         {             int k,l=0,t=0;             for(k=0;s[k];k++)  if(s[k]=='(') break;             for(int j=k+1;s[j];j++)//處理輸入的字元,提出需要的數字             {                 while(s[j]!=',' && s[j]!=')')                 {                     t=t*10+s[j]-'0';                     j++;                 }                 index[l++]=t;                 t=0;             }            t=a[index[0]];            for(int k=0;k<l;k++)//處理迴圈左移            {                if(k==0) update(1,index[l-1],index[l-1],a[index[0]]);                else                update(1,index[k-1],index[k-1], a[index[k]]) ;            }             for(int k=0;k<l-1;k++)                a[ index[k]]=a[index[k+1]];            a[index[l-1]]=t;         }     }    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.