#pragma GCC optimize("Ofast") #pragma GCC target("avx2") #define rd_init() char*rp=({char*mmap();mmap(0l,1l<<25,1,2,0,0ll);}) #define rd_skip() while(*rp++>=48) //#define rd() ({int _v=0,_c;while(_c=*rp++-48,_c>=0)_v=_v*10+_c;_v;}) //#define rd_signed() ({int _s=*rp=='-'&&++rp,_v=rd();_s?-_v:_v;}) #define rd() getuint(&rp) #define rd_signed() getint(&rp) #define WTHI(v) {unsigned _z=v;int _n=0;ulong _d=0;while(++_n,_d=_d<<8|0x30|_z%10,_z/=10);*(ulong*)wp=_d;wp+=_n;} #define WTLO(v) {unsigned _z=v;int _n=8;ulong _d=0;while(_d=_d<<8|0x30|_z%10,_z/=10,--_n);*(ulong*)wp=_d;wp+=8;} #define wt(v) if(v>=100000000){WTHI(v/100000000);WTLO(v);}else{WTHI(v);} typedef unsigned long ulong; // https://yukicoder.me/submissions/482915 const ulong ma0 = 1157442765409226768; const ulong ma1 = 1085102592571150095; const ulong ma2 = 71777214294589695; const ulong ma3 = 281470681808895; const ulong ma4 = 4294967295; static inline int getuint(char**rpp) { char*rp=*rpp; ulong tmp = *(ulong*)rp; int dig = ((tmp & ma0) ^ ma0) ? 68 - __builtin_ctzl((tmp & ma0) ^ ma0) : 0; tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; rp += (72 - dig >> 3); *rpp=rp; return tmp; } static inline int getint(char**rpp) { char*rp=*rpp; int pn = *rp=='-'&&++rp; int tmp=getuint(&rp); *rpp=rp; return pn?-tmp:tmp; } #define UFSIZE 500002 struct{int u,v;}ufbuf[UFSIZE]; void uf_init(){ for(int i=0;i=0) z+=ufbuf[t].v,t=b; while(b=ufbuf[a].u,b>=0) y=ufbuf[a].v,ufbuf[a].v=z,z-=y,ufbuf[a].u=t,a=b; return t; } void uf_unite(int a,int b){ a=uf_root(a); b=uf_root(b); if(a!=b){ if(ufbuf[a].u=0); if(z<0){ *wp++='-'; z=-z; } wt(z); *wp++='\n'; } } write(1,wbuf,wp-wbuf); _exit(0); }