The question we look at the data range, N <= 10000, we know, is not to take a two-dimensional array to record each point, and the time complexity can not depend on N, so we can only K start, for each inquiry, the answer is actually very good count, but more difficult is calculated after each answer, the impact on the operation of the initial number of other locations, we can set two structural array x [i] and y [ i], respectively, move, now two elements. Indicates the i-th asked how many were moved in the x and y-axis direction, x [i] .now showing the operation this time, what line of movement x (because the movement of the line, so use to indicate which row y , because only the same y, x is not the same), move to the right illustrate a number, y [i] .now showing the operation of the mobile column which is (again, on a number, x same, y is not the same, the denoted x), move represents a mobile number, because the provisions on the subject Xianxiang right, down again, so we first calculate the time line operation, the computed column.
We each new operation will be saved during previous operation which row move which column number, one by one, out of this number in order to traverse the current real position, making calculations.
For a very simple calculation, two cases, 1 to be larger than the current coordinates reach the coordinates (x coordinate, y coordinate for both), then directly obtained by subtracting the difference operation is to be performed many times, are known xy meet the requirements, you can output the answer. 2- about to reach than it is now small coordinate coordinate our operations can only go down backwards, not looking back, only after reaching the border back to the first division, converted to Question 1.
Code:
#include<iostream> #include<cstdio> using namespace std; struct number{ long long move; long long now; }x[1010],y[1010]; long long n,k,x2,y2; long long num[1010],x1[1010],y1[1010],ans; int main(){ ios::sync_with_stdio(false); cin>>n>>k; for(int i=1;i<=k;i++) cin>>num[i]>>y1[i]>>x1[i]; for(int i=1;i<=k;i++){ ans=0; x2=num[i]%n; if(num[i]%n==0) x2=n; y2=num[i]/n; if(num[i]%n!=0) y2++; for(int j=1;j<i;j++){ if(x[j].now==y2){ x2=(x[j].move+x2)%n; if(x2==0) x2=n; } if(y[j].now==x2){ y2=(y[j].move+y2)%n; if(y2==0) y2=n; } } if(x1[i]<x2){ ans+=n-x2+x1[i]; x[i].move=n-x2+x1[i]; x[i].now=y2; } else if(x1[i]>x2){ ans+=x1[i]-x2; x[i].move=x1[i]-x2; x[i].now=y2; } if(y1[i]<y2){ ans+=n-y2+y1[i]; y[i].move=n-y2+y1[i]; y[i].now=(x2+x[i].move)%n; if(y[i].now==0) y[i].now=n; } else if(y1[i]>y2){ ans+=y1[i]-y2; y[i].move=y1[i]-y2; y[i].now=(x2+x[i].move)%n; if(y[i].now==0) y[i].now=n; } cout<<ans<<endl; } }
thanks for reading.