Title Description
I did not learn again today. MDZ
No learning MDZ love to play all kinds of pinball games, so this is a pinball theme.
It has a grid line $ $ $ m $ n-columns, wherein the n-$ $ $ and $ m $ m $ coprime and an odd number. $ Bottom left coordinates (0,0) $, top right coordinates $ (m, n) $.
A negligibly small initial size of balls in $ (m / 2,0) $, the initial velocity vector is $ (- 1 / 2,1 / 2) $ (i.e., an initial down to the left).
There are certain grid block, when encountering a boundary or border grid box, mirror reflection will be issued. If the ball hit a box, the box will disappear.
After the location of each block has now been asked how long all square disappears.
data range
$1 \leq n, m, k \leq 10^{5}, \quad k \leq mn-1$
answer
Consider violence, each block is split into four sides, with the set maintenance and x + y equal xy edges.
If you hit a boundary to consider how to do, found that if the arrival of a boundary by a direction from a boundary, the boundary that the next direction by this time or will reach this boundary, then compressed with a disjoint-set about the path to .
Code
#include <bits/stdc++.h> using namespace std; const int N=4e5+5; int m,n,k,t,f[N<<3]; struct P{int x,y;}p[N]; long long ans,g[N<<3]; struct O{ int x,y,i; friend bool operator < (const O& A,const O& B){ if (A.x==B.x){ if (A.y==B.y) return A.i<B.i; return A.y<B.y; } return A.x<B.x; } }; set<O>s[2][N]; set<O>::iterator it; #define pi pair<int,int> #define M make_pair #define fi first #define se second map<pair<pi,pi>,int>mp; pair<pi,pi>h[N<<3]; void ins(int x,int y,int i){ s[0][x+y].insert((O){x,y,i}); s[1][x-y+n].insert((O){x,y,i}); } void era ( you get, you y, you i) { s [ 0 ] [x + y] .erase (s [ 0 ] [x + y] .find ((O) {x, y, i})); s [ 1 ] [x-y + n] .erase (s [ 1 ] [x-y + n] .find ((O) {x, y, i})); } void era(int i){ int x=p[i].x,y=p[i].y; was (x - 2 , y - 1 , i) was (x- 1 , and, i); was (x - 1 , y - 2 , i) was (x, y- 1 , i); K-- ; } int get(int x){ return f[x]==x?x:f[x]=get(f[x]); } int main () { cin>>m>>n>>k;m<<=1;n<<=1; for (int i=1,x,y;i<=k;i++){ scanf("%d%d",&x,&y); x<<=1;y<<=1;p[i]=(P){x,y}; ins(x-2,y-1,i);ins(x-1,y,i); ins(x-1,y-2,i);ins(x,y-1,i); } int x=m>>1,y=0,dx=-1,dy=1,o,v; if (!mp.count(M(M(x,y),M(dx,dy)))) mp[M(M(x,y),M(dx,dy))]=++t, f[t]=t,h[t]=M(M(x,y),M(dx,dy)); while(k){ o=0; if (mp.count(M(M(x,y),M(dx,dy)))) o=mp[M(M(x,y),M(dx,dy))]; if (dx<0 && dy>0){ it=s[0][x+y].lower_bound((O){x,0,0}); if (it==s[0][x+y].begin()){ if (x+y>n) g[get(o)]+=n-y,ans+=n-y,x=x+y-n,y=n,dy=-1; else g[get(o)]+=x,ans+=x,y=x+y,x=0,dx=1; if (!mp.count(M(M(x,y),M(dx,dy)))) mp[M(M(x,y),M(dx,dy))]=++t, f[t]=t,h[t]=M(M(x,y),M(dx,dy)); v=mp[M(M(x,y),M(dx,dy))]; ans+=g[get(v)]; x = h [ get (v)]. fi.fi, y = h [ get (v)]. fi.se, dx=h[get(v)].se.fi,dy=h[get(v)].se.se; if (!o) continue; g[get(v)]+=g[get(o)]; f[get(o)]=get(v); continue; } it--;O u=*it;era(u.i);ans+=x-u.x;x=u.x;y=u.y; if (y&1) dx=1; else dy=-1; continue; } if (dx>0 && dy<0){ it=s[0][x+y].upper_bound((O){x,0,0}); if (it==s[0][x+y].end()){ if (x+y-m>0) g[get(o)]+=m-x,ans+=m-x,y=x+y-m,x=m,dx=-1; else g[get(o)]+=y,ans+=y,x=x+y,y=0,dy=1; if (!mp.count(M(M(x,y),M(dx,dy)))) mp[M(M(x,y),M(dx,dy))]=++t, f[t]=t,h[t]=M(M(x,y),M(dx,dy)); v=mp[M(M(x,y),M(dx,dy))]; ans+=g[get(v)]; x = h [ get (v)]. fi.fi, y = h [ get (v)]. fi.se, dx=h[get(v)].se.fi,dy=h[get(v)].se.se; if (!o) continue; g[get(v)]+=g[get(o)]; f[get(o)]=get(v); continue; } O u=*it;era(u.i);ans+=u.x-x;x=u.x;y=u.y; if (y&1) dx=-1; else dy=1; continue; } if (dx>0 && dy>0){ it=s[1][x-y+n].upper_bound((O){x,0,0}); if (it==s[1][x-y+n].end()){ if (m-x+y<n) g[get(o)]+=m-x,ans+=m-x,y=m-x+y,x=m,dx=-1; else g[get(o)]+=n-y,ans+=n-y,x=x-y+n,y=n,dy=-1; if (!mp.count(M(M(x,y),M(dx,dy)))) mp[M(M(x,y),M(dx,dy))]=++t, f[t]=t,h[t]=M(M(x,y),M(dx,dy)); v=mp[M(M(x,y),M(dx,dy))]; ans+=g[get(v)]; x = h [ get (v)]. fi.fi, y = h [ get (v)]. fi.se, dx=h[get(v)].se.fi,dy=h[get(v)].se.se; if (!o) continue; g[get(v)]+=g[get(o)]; f[get(o)]=get(v); continue; } O u=*it;era(u.i);ans+=u.x-x;x=u.x;y=u.y; if (y&1) dx=-1; else dy=-1; continue; } if (dx<0 && dy<0){ it=s[1][x-y+n].lower_bound((O){x,0,0}); if (it==s[1][x-y+n].begin()){ if (y-x>0) g[get(o)]+=x,ans+=x,y=y-x,x=0,dx=1; else g[get(o)]+=y,ans+=y,x=x-y,y=0,dy=1; if (!mp.count(M(M(x,y),M(dx,dy)))) mp[M(M(x,y),M(dx,dy))]=++t, f[t]=t,h[t]=M(M(x,y),M(dx,dy)); v=mp[M(M(x,y),M(dx,dy))]; ans+=g[get(v)]; x = h [ get (v)]. fi.fi, y = h [ get (v)]. fi.se, dx=h[get(v)].se.fi,dy=h[get(v)].se.se; if (!o) continue; g[get(v)]+=g[get(o)]; f[get(o)]=get(v); continue; } it--;O u=*it;era(u.i);ans+=x-u.x;x=u.x;y=u.y; if (y&1) dx=1; else dy=1; continue; } } cout<<ans<<endl; return 0; }