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 181 182 183 184 185 186 187
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ putchar(x?'1':'0'); } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } template<class T> inline T abs(T x){ return x<0?-x:x; } using Pir=std::pair<int,int>; #define fir first #define sec second #define Mkr std::make_pair const int MAXN=5e2+10; int N,Q,Tot; Pir Oper[MAXN]; int pre[MAXN][MAXN],pres[MAXN][MAXN],prec[MAXN][MAXN]; int val[MAXN][MAXN],vals[MAXN][MAXN],valc[MAXN][MAXN]; std::map<Pir,int>Hash; struct BIT { int Tre[MAXN],Cnt[MAXN],Siz; inline void build(int n){ Siz=n;for(int i=1;i<=n;++i) Tre[i]=Cnt[i]=0; } inline int lowbit(int x){ return x&(-x); } inline void add(int x,int v) { for(;x<=Siz;x+=lowbit(x)) Tre[x]+=v,++Cnt[x]; } inline Pir query(int x) { int tr=0,cn=0; for(;x;x-=lowbit(x)) tr+=Tre[x],cn+=Cnt[x]; return {tr,cn}; } inline Pir get(int l,int r) { if(l>r) return {0,0}; auto _l=query(l-1),_r=query(r); return {_r.fir-_l.fir,_r.sec-_l.sec}; } }Tre[MAXN]; inline void rebuild() { for(int i=1;i<=Tot;++i) { auto p=Oper[i]; val[p.fir][p.sec]+=p.sec; vals[p.fir][p.sec]+=p.fir; ++valc[p.fir][p.sec]; Tre[p.fir].add(p.sec,p.sec); } for(int i=1;i<=N;++i) for(int j=1;j<=N;++j) { pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+val[i][j]; pres[i][j]=pres[i-1][j]+pres[i][j-1]-pres[i-1][j-1]+vals[i][j]; prec[i][j]=prec[i-1][j]+prec[i][j-1]-prec[i-1][j-1]+valc[i][j]; } Tot=0; } inline int getSum(int x,int y,int l,int r) { if(x>y||l>r) return 0; return pre[y][r]-pre[x-1][r]-pre[y][l-1]+pre[x-1][l-1]; } inline int getStum(int x,int y,int l,int r) { if(x>y||l>r) return 0; return pres[y][r]-pres[x-1][r]-pres[y][l-1]+pres[x-1][l-1]; } inline int getCnt(int x,int y,int l,int r) { if(x>y||l>r) return 0; return prec[y][r]-prec[x-1][r]-prec[y][l-1]+prec[x-1][l-1]; } int main() { read(N,Q); for(int i=1;i<=N;++i) Tre[i].build(N); for(int opt,x,y;Q--;) { read(opt,x,y); if(x>y) std::swap(x,y); if(opt==1) { if(x==y) continue; Oper[++Tot]=Mkr(x,y); if(Tot>N) rebuild(); } else { ll ans=0; int mid=(x+y)>>1; ans+=getSum(1,x,1,N)-2*getSum(1,x,mid+1,y)-getStum(1,x,1,N); ans+=getCnt(1,x,mid+1,y)*(x+y); ans+=getCnt(1,x,y+1,N)*(x-y); for(int st=x+1;st<=mid;++st) { int newmid=st+mid-x; ans+=getSum(st,st,st,N)-2*getSum(st,st,newmid+1,y); ans+=2*getStum(st,st,newmid+1,N)-getStum(st,st,st,N); ans+=(y-x)*getCnt(st,st,newmid+1,y)-(x+y)*getCnt(st,st,y+1,N); } ans+=getSum(mid+1,N,1,N)-getStum(mid+1,N,1,N); for(int i=1;i<=Tot;++i) { auto p=Oper[i]; ans+=std::min(p.sec-p.fir,abs(p.sec-y)+abs(p.fir-x)); } write(ans,'\n'); } } return 0; }
|