1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <cctype> #include <ctime> #include <queue> #include <cmath> #define ll long long #define ri register int #define ull unsigned loong long const int maxn=200015; const int inf=0x7fffffff; template <class T>inline void read(T &x){ x=0;int ne=0;char c; while(!isdigit(c=getchar()))ne=c=='-'; x=c-48; while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48; x=ne?-x:x;return; } int n,m,N; bool p[maxn<<1],q[maxn<<1]; int L,R,dta,t; struct Segment_Tree_1{ int ns[maxn<<2],odds[maxn<<2]; void update(int now,int l,int r,int fff){ if(l==r){ if(l&1)odds[now]++; else ns[now]++; return ; } int mid=(l+r)>>1; if(t<=mid)update(now<<1,l,mid,fff); else update(now<<1|1,mid+1,r,fff); odds[now]=odds[now<<1]+odds[now<<1|1]; ns[now]=ns[now<<1]+ns[now<<1|1]; return ; } int odd_query(int now,int l,int r){ if(L<=l&&r<=R){ return odds[now]; } int mid=(l+r)>>1,ans=0; if(L<=mid)ans+=odd_query(now<<1,l,mid); if(mid<R)ans+=odd_query(now<<1|1,mid+1,r); return ans; } int n_query(int now,int l,int r){ if(L<=l&&r<=R){ return ns[now]; } int mid=(l+r)>>1,ans=0; if(L<=mid)ans+=n_query(now<<1,l,mid); if(mid<R)ans+=n_query(now<<1|1,mid+1,r); return ans; } }P; struct Segment_Tree_2{ int ns[maxn<<1],odds[maxn<<1]; void update(int now,int l,int r,int fff){ if(l==r){ if(l&1)odds[now]++; else ns[now]++; return ; } int mid=(l+r)>>1; if(t<=mid)update(now<<1,l,mid,fff); else update(now<<1|1,mid+1,r,fff); odds[now]=odds[now<<1]+odds[now<<1|1]; ns[now]=ns[now<<1]+ns[now<<1|1]; return ; } int odd_query(int now,int l,int r){ if(L<=l&&r<=R){ return odds[now]; } int mid=(l+r)>>1,ans=0; if(L<=mid)ans+=odd_query(now<<1,l,mid); if(mid<R)ans+=odd_query(now<<1|1,mid+1,r); return ans; } int n_query(int now,int l,int r){ if(L<=l&&r<=R){ return ns[now]; } int mid=(l+r)>>1,ans=0; if(L<=mid)ans+=n_query(now<<1,l,mid); if(mid<R)ans+=n_query(now<<1|1,mid+1,r); return ans; } }Q1,Q2; inline void solve(){ int x,y;ll ans=0; while(m--){ read(x),read(y); if(!p[x+y]){ t=x+y; P.update(1,1,N,0); p[x+y]=1; if(x+y<=n){ int k=x+y-2; ans+=x+y-1; int lxl=-k,rr=k; if(lxl==rr){ L=R=0; if(q[n])ans--; } else { L=0,R=abs(lxl); if(R&1)ans-=Q1.odd_query(1,0,n); else ans-=Q1.n_query(1,0,n); L=1,R=rr; if(R&1)ans-=Q2.odd_query(1,1,n); else ans-=Q2.n_query(1,1,n); } } else{ int k=n*2-(x+y); ans+=k+1; int lxl=(x+y)-n*2,rr=n*2-(x+y); if(lxl==rr){ L=R=0; if(q[n])ans--; } else { L=0,R=abs(lxl); if(R&1)ans-=Q1.odd_query(1,0,n); else ans-=Q1.n_query(1,0,n); L=1,R=rr; if(R&1)ans-=Q2.odd_query(1,1,n); else ans-=Q2.n_query(1,1,n); } } } if(!q[x-y+n]){ t=x-y; if(t<=0){ t=abs(t); Q1.update(1,0,n,1); } else { Q2.update(1,1,n,1); } q[x-y+n]=1; if(x-y<=0){ int k=x-y+n-1; ans+=n+(x-y); L=n+1-k,R=n+1+k; if(L&1)ans-=P.odd_query(1,1,N); else ans-=P.n_query(1,1,N); } else{ int k=x-y-n+1; ans+=n-(x-y); L=n+1+k,R=n+1-k; if(R&1)ans-=P.odd_query(1,1,N); else { ans-=P.n_query(1,1,N); } } } } printf("%lld\n",1ll*n*n-ans); } int main(){ int x,y; ll ans=0; double st=clock(); freopen("dat.in","r",stdin); freopen("std.out","w",stdout); read(n),read(m);N=n*2; solve(); double ed=clock(); printf("%lf\n",ed-st); return 0; }
|