CodeForces413E Maze 2D

來源:互聯網
上載者:User

標籤:%s   efi   題意   can   bit   pen   for   min   tor   

題意

給一個\(2*n\)的迷宮,有障礙物,\(m\)次詢問,每次查詢兩點間最短路

題解

把上下當成一個點,每個點有上下兩個介面
用線段樹維護即可

#include<bits/stdc++.h>using namespace std;#define gc c=getchar()#define r(x) read(x)#define ls (rt<<1)#define rs (rt<<1|1)template<typename T>inline void read(T&x){    x=0;T k=1;char gc;    while(!isdigit(c)){if(c==‘-‘)k=-1;gc;}    while(isdigit(c)){x=x*10+c-‘0‘;gc;}x*=k;}const int INF=1e9;const int N=2e5;char s[2][N];struct seg{    int dist[2][2];        const int* operator [](const int &x)const{        return dist[x];    }        int* operator [](const int &x){        return dist[x];    }    }tr[N<<2];inline seg operator +(const seg &a,const seg &b){    seg ret;    int ans;    ans=INF;    ans=min(ans,a[0][0]+b[0][0]+1);    ans=min(ans,a[0][1]+b[1][0]+1);    ret[0][0]=ans;    ans=INF;    ans=min(ans,a[0][0]+b[0][1]+1);    ans=min(ans,a[0][1]+b[1][1]+1);    ret[0][1]=ans;    ans=INF;    ans=min(ans,a[1][0]+b[0][1]+1);    ans=min(ans,a[1][1]+b[1][1]+1);    ret[1][1]=ans;    ans=INF;    ans=min(ans,a[1][0]+b[0][0]+1);    ans=min(ans,a[1][1]+b[1][0]+1);    ret[1][0]=ans;    return ret;}inline void build(int rt,int l,int r){    if(l==r){        tr[rt][0][0]=tr[rt][0][1]=tr[rt][1][1]=tr[rt][1][0]=INF;        if(s[0][l]==‘.‘){            tr[rt][0][0]=0;            if(s[1][l]==‘.‘)tr[rt][0][1]=1;        }        if(s[1][l]==‘.‘){            tr[rt][1][1]=0;            if(s[0][l]==‘.‘)tr[rt][1][0]=1;        }        return;    }    int mid=(l+r)>>1;    build(ls,l,mid);    build(rs,mid+1,r);    tr[rt]=tr[ls]+tr[rs];}inline seg query(int rt,int l,int r,int x,int y){    if(x<=l&&r<=y)return tr[rt];    int mid=(l+r)>>1;    if(y<=mid)return query(ls,l,mid,x,y);    else if(x>mid)return query(rs,mid+1,r,x,y);    else return query(ls,l,mid,x,y)+query(rs,mid+1,r,x,y);}int n,m;inline int Query(int x,int y){    int l=(x-1)%n+1;    int r=(y-1)%n+1;    if(l>r)swap(l,r),swap(x,y);    return query(1,1,n,l,r)[x>n][y>n];}int main(){    r(n),r(m);    scanf("%s",s[0]+1);    scanf("%s",s[1]+1);    build(1,1,n);    for(int a,b;m;--m){        r(a),r(b);        int ans=Query(a,b);        if(ans==INF)ans=-1;        printf("%d\n",ans);    }}

CodeForces413E Maze 2D

聯繫我們

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