版本比较
题解:P10795 『SpOI - R1』Lamborghini (Demo)
正文
增加 9 行删除 9 行
33 行未改动343536373838394041425 行未改动484950515252535455564 行未改动6162636465656667686948 行未改动1181191201211221221231241251261271281291301301311321331341351361371351361371381391401411421431441451451461471481494 行未改动33 行未改动struct MoonHalo{ int lc,rc,cnt; MoonHalo():lc(0),rc(0),cnt(0){} MoonHalo(int l,int r,int c):lc(l),rc(r),cnt(c){} MoonHalo(int l,int r,int c):lc(l),rc(r),cnt(c){}}a[60*maxn];int newnode(){ a[++cnt]=MoonHalo();5 行未改动}int merge(int x,int y,int l,int r){ if(!x||!y) return x|y; if(l==r) a[x].cnt+=a[y].cnt; if(l==r) a[x].cnt+=a[y].cnt; else { int mid=l+r>>1; a[x].lc=merge(a[x].lc,a[y].lc,l,mid);4 行未改动}void update(int &now,int l,int r,int i,int x){ if(!now) now=newnode(); if(l==r) if(l==r) { a[now].cnt+=x; return; }48 行未改动 vector<int> block; block.push_back(i); for(int j:g[i]) if(vis[j]) block.push_back(Find(j)); // 邻居所在的连通块 block.push_back(Find(j)); // 邻居所在的连通块 sort(block.begin(),block.end()); block.erase(unique(block.begin(),block.end()),block.end()); int s1=0,s2=0,sum=0; for(int j=0;j<block.size();j++) { int smx,smn; // smx 当前连通块中 v>=v[i] 的数量 // smn 当前连通块中 v<=v[i] 的数量 // smn 当前连通块中 v<=v[i] 的数量 if(block[j]==i) smx=1,smn=1; else smx=query(root[block[j]],1,m,v[i],m),smn=query(root[block[j]],1,m,1,v[i]); s1+=smx,s2+=smn,sum+=smx*smn; // s1 所有连通块中 v>=v[i] 的数量 // s2 所有连通块中 v<=v[i] 的数量 // sum 是所有连通块中在同一个连通块里的 v>=v[i] 和 v<=v[i] 的数量 } // s2 所有连通块中 v<=v[i] 的数量 // sum 是所有连通块中在同一个连通块里的 v>=v[i] 和 v<=v[i] 的数量 } ans+=(s1*s2-sum+1)*i; int fx=Find(i); for(int j=0;j<block.size();j++) { int fb=Find(block[j]); if(fb!=fx) { root[fx]=merge(root[fx],root[fb],1,m); // 合并连通块 root[fx]=merge(root[fx],root[fb],1,m); // 合并连通块 p[fb]=fx; } } }4 行未改动