#118. "UR #8" virtual cessation exam
Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
http://uoj.ac/problem/118
Description High School, high school, short three years. Noi is a high school graduation exam, and the college entrance Examination is held every summer.
Sophomore summer, this is your last chance to take part in the college entrance examination. You have been suspended for the college entrance examination for a long time, the knowledge of Oi long time no tube. You don't have the ability to spend a year in a three-year Oi class. This is your last battle, if you fail, you may not be able to move bricks on the site only to Tsinghua.
This day you carry your Backpack virtual cessation exam. At this time the national traffic mainly by instantaneous transmission device. The National transportation network can be abstracted as a grid of N-Rows M-columns. Rows are numbered 1,..., N, and the columns are numbered 1,..., m.
There is an integer n+m of 0 or 1 A1,..., an,b1,..., BM. For 1≤i≤n,1≤j≤m, if the AI=BJ is so on the grid diagram on row i, Column J is marked with 0 otherwise marked 1.
Your home in the first line of XS, the Entrance Examination examination Room in the first line of the XE row of ye column. Now you want to go from home to the entrance examination. Every time you can:
Move up one line. (If you move in the first line then you will go to the last line)
Move down one line. (If you move in the last line then you will go to the first line)
Move one column to the left. (If you move in the first column then you will go to the last column)
Move one column to the right. (If you move in the last column then you will go to the first column)
For each move, if the number of squares before moving is different from the number of squares on the move, it takes 1 minutes to wait for the teleport device to adjust the configuration, otherwise it will not take time.
Now you want to know how long it will take you to go from home to the entrance examination.
Input
The first line, two positive integer n,m, indicates that the grid is n rows M column.
The second row n integers, respectively, represent A1,..., an. Guaranteed A1,..., an∈{0,1}.
The third row of M integers, respectively, represents B1,..., BM. Guaranteed B1,..., bm∈{0,1}.
Next a positive integer q.
Next Q line, four integers per line xs,ys,xe,ye. Ask if your home is in the first line of XS, the minimum time spent in the entrance examination examination Room at the first line of the XE row.
Output
A total of Q lines, one integer per line, indicates the minimum number of minutes to spend.
Sample Input
1 2
1
0 1
2
1 2 1 2
1 1 1 2
Sample Output
0
1
HINT
n,m≤105 q≤105
Test instructions
Exercises
n,m≤105,q≤105, we find that we can break through the boundaries of the dimension, take each dimension apart and consider it separately, and the final answer is the answer of each dimension.
Why is this right?
For ai≠ai+1, no matter what the value of BJ, you from (I,j) crossing to (I+1,J), will inevitably spend the waiting time, otherwise if ai=ai+1, it will inevitably not spend waiting time. So the total waiting time for a route can be split into the sum of the waiting times for each dimension.
Then the problem becomes a one-dimensional problem, directly with the pointers of the algorithm one can be.
Complexity O (N+M+Q), 100 points can be scored.
Code:
#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineTest Freopen ("Test.txt", "R", stdin)#defineMAXN 2000001#defineMoD 10007#defineEPS 1e-6Const intinf=0x3f3f3f3f;Constll infll =0x3f3f3f3f3f3f3f3fll;inline ll Read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************intA[MAXN];intB[MAXN];intPRE[MAXN];intPRE1[MAXN];intn,m;intSolve1 (intXinty) { if(y>=x)returnpre[y]-Pre[x]; returnpre[n]-pre[x]+pre[y]+ (a[n]^a[1]);}intSolve2 (intXinty) { if(y>=x)returnpre1[y]-Pre1[x]; returnpre1[m]-pre1[x]+pre1[y]+ (b[m]^b[1]);}intMain () {//test;N=read (), m=read (); for(intI=1; i<=n;i++) {A[i]=read (); if(i!=1) Pre[i]=pre[i-1]+ (a[i]^a[i-1]); } for(intI=1; i<=m;i++) {B[i]=read (); if(i!=1) Pre1[i]=pre1[i-1]+ (b[i]^b[i-1]); } intq=read (); for(intI=0; i<q;i++) { intX1=read (), Y1=read (), X2=read (), y2=read (); printf ("%d\n", Min (solve1 (x1,x2), solve1 (x2,x1)) +min (solve2 (y1,y2), Solve2 (y2,y1))); }}
Uoj #118. "UR #8" virtual cessation exam water problem