bzoj5286: [Hnoi2018] Turntable [Line Segment Tree]

portal

Problem solving ideas:

First of all, there is a conclusion: the optimal solution must be to wait for a while at a certain starting point, and then complete a circle in one breath, that is, first multiply the original sequence:

a n s = m i n i = 1 n { m a x j = i i + n 1 { T j ( j i ) } + n 1 }

is also equal to:
a n s = m i n i = 1 n { m a x j = i 2 n { T j j } + i } + n 1

This can be maintained with a line segment tree, each node records the interval T i i The maximum value of , the influence of the right sub-interval on the left sub-interval is considered when merging, which is similar to the question of building reconstruction. For details, please refer to the code.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int getint()
{
    int i=0,f=1;char c;
    for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
    if(c=='-')c=getchar(),f=-1;
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}
const int N=200005,INF=1e9;
int n,m,tp,ans,p,a[N];
int f[N<<2],mx[N<<2];
int query(int k,int l,int r,int lmtR,int x)
{
    if(l==r)return min(l==lmtR?INF:l+1+x,max(x,mx[k])+l);
    int mid=l+r>>1;
    if(x>=mx[k<<1|1])return query(k<<1,l,mid,lmtR,x);
    else return min(f[k],query(k<<1|1,mid+1,r,lmtR,x));
}
void update(int k,int l,int mid)
{
    mx[k]=max(mx[k<<1],mx[k<<1|1]);
    if(l<=n)f[k]=query(k<<1,l,mid,mid,mx[k<<1|1]);
}
void build(int k,int l,int r)
{
    if(l==r){mx[k]=a[l];return;}
    int mid=l+r>>1;
    build(k<<1,l,mid),build(k<<1|1,mid+1,r);
    update(k,l,mid);
}
void modify(int k,int l,int r,int x,int y)
{
    if(l==r){mx[k]=y;return;}
    int mid=l+r>>1;
    x<=mid?modify(k<<1,l,mid,x,y):modify(k<<1|1,mid+1,r,x,y);
    update(k,l,mid);
}
int main()
{
    //freopen("lx.in","r",stdin);
    n=getint(),m=getint(),tp=getint();
    for(int i=1;i<=n;i++)a[i]=getint()-i,a[i+n]=a[i]-n;
    build(1,1,n<<1);
    ans=f[1]+n-1;cout<<ans<<'\n';
    while(m--)
    {
        int x=getint(),y=getint();
        if(tp)x^=ans,y^=ans;
        y-=x;
        modify(1,1,n<<1,x,y);
        modify(1,1,n<<1,x+n,y-n);
        ans=f[1]+n-1;cout<<ans<<'\n';
    }
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326130310&siteId=291194637