比赛写了一小时还是改不出来有bug就弃疗回寝室了
自己其实是很怕写这种题的
虽然状态不好昨晚,但是还是不熟练
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
typedef long long ll;
#define sz(x) ((int)(x).size())
int n,m,k;
vector<int> m1[N], m2[N];
int vis[2][N];
int ans[N];
int u[N], v[N];
int fl;
void add(int &po) {
if(fl) {
po += m;
if(po >= n){ po = 2*n - po; fl = 0; }
}else {
po -= m;
if(po <= 0) { po = -po; fl = 1; }
}
}
int main(){
while(~scanf("%d %d %d",&n,&m,&k)) {
memset(vis,0,sizeof(vis));
memset(ans,-1,sizeof(ans));
for(int i = 1; i <= k; ++i) {
scanf("%d %d",&u[i],&v[i]);
}
if(n < m) {
swap(n,m);
for(int i = 1; i <= k; ++i) swap(u[i], v[i]);
}
for(int i = 0; i <= n; ++i) m1[i].clear(), m2[i].clear();
for(int i = 1; i <= k; ++i) {
if(u[i]-v[i] >= 0) m1[u[i]-v[i]].push_back(i);
else m2[v[i]-u[i]].push_back(i);
if(u[i]+v[i] < n) m2[u[i]+v[i]].push_back(i);
else m1[2*n-v[i]-u[i]].push_back(i);
}
int po = 0; ll ti = 0; fl = 1;
while(1) {
if(vis[fl][po]) break;
// printf("%d %d
",po,fl);
vis[fl][po] = 1;
for(int i = 0; i < sz(m1[po]); ++i) {
int x = m1[po][i];
if(ans[x] == -1) {
if(fl) ans[x] = ti + v[x];
else ans[x] = ti - v[x];
}
}
for(int i = 0; i < sz(m2[po]); ++i) {
int x = m2[po][i];
if(ans[x] == -1) {
if(fl && po != 0) ans[x] = ti-v[x];
else ans[x] = ti+v[x];
}
}
add(po); add(po);
ti += 2*m;
}
for(int i = 1; i <= k; ++i) printf("%lld
",ans[i]);
}
return 0;
}