uoj118 [UR # 8] Beijing exam

topic

Not difficult to find us directly through the herd

Taking into account the first \ (i \) constructor line is to \ (b \) array as a template, and each number \ (a_i \) XOR click on it

So difficult to find for a continuous period equal to \ (A \) , they form several rows on the same matrix

While on the nature of the matrix only two different rows, one is \ (B \) and \ (1 \) XOR obtained, and one is \ (0 \) XOR obtained

Obviously we are from \ ((x_s, y_s) \ ) went \ ((x_e, y_e) \ ) to switch from the past it is equivalent to any of the middle row, because from the \ (y_s \) went \ (y_e \) the cost of any line are the same

So we put the problem into two problems into one-dimensional, that is, from \ (x_s \) went \ (x_e \) price plus from \ (y_s \) went \ (y_e \)

The continuous identical period shrunk to a point, what is seek distance ring

Code

#include<bits/stdc++.h>
#define re register
#define min(a,b) ((a)<(b)?(a):(b))
const int maxn=1e5+5;
inline int read() {
    char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
int col[2][maxn],a[maxn],b[maxn],x[2],y[2],n,m,Q,L[2];
inline int dis(int o,int x,int y) {
    if(x>y) std::swap(x,y);
    return min(y-x,L[o]-y+x);
}
int main() {
    n=read(),m=read();
    for(re int i=1;i<=n;i++) a[i]=read();
    for(re int j=1;j<=m;j++) b[j]=read();
    int tot=1;col[0][1]=1;
    for(re int i=2;i<=n;i++)
        tot+=(a[i]!=a[i-1]),col[0][i]=tot;
    if(a[n]==a[1]) {
        for(re int i=n;i&&col[0][i]==tot;--i) col[0][i]=1;
        --tot;
    }
    L[0]=tot;tot=1;col[1][1]=1;
    for(re int i=2;i<=m;i++)
        tot+=(b[i]!=b[i-1]),col[1][i]=tot;
    if(b[m]==b[1]) {
        for(re int i=m;i&&col[1][i]==tot;--i) col[1][i]=1;
        --tot;
    }
    L[1]=tot;Q=read();
    while(Q--) {
        x[0]=read(),y[0]=read(),x[1]=read(),y[1]=read();
        printf("%d\n",dis(0,col[0][x[0]],col[0][x[1]])+dis(1,col[1][y[0]],col[1][y[1]]));
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/asuldb/p/11449049.html