Transmission Door
The name is very high-end actually very simple algorithm HHH
Heuristic merging
Simply put, some of the questions that merge a bunch of queues can be combined with heuristics, or it's a thought. Each time the small merge into the large part, the $o (MLOGN) $.
Bzoj 1483//by cydiater//2016.10.25#include <iostream> #include <cstring> #include <cstdlib># Include <cstdio> #include <queue> #include <map> #include <bitset> #include <cstring># Include <string> #include <algorithm> #include <cstdio> #include <iomanip>using namespace std;# Define ll long long#define up (i,j,n) for (int. i=j;i<=n;i++) #define DOWN (i,j,n) for (int i=j;i>=n;i--) const int maxn= 1e6+5;const int Oo=0x3f3f3f3f;inline int read () {char ch=getchar (); int x=0,f=1;while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int n,realc[maxn],a[maxn],next[maxn],head[maxn],nd[maxn],siz[maxn],ans=0,m;namespace solution{void init () {N=read ( ); M=read (); up (i,1,n) {a[i]=read (); Siz[a[i]]++;if (A[i]!=a[i-1]) ans++;if (!nd[a[i]]) nd[a[i]]=i;next[i]=head[a[i]; Head[a[i]]=i;realc[a[i]]=a[i];}} void Updata (int x,int y) {if (x==y) return;if (Siz[realc[x]]>siz[realc[y]]) sWAP (Realc[x],realc[y]); X=realc[x];y=realc[y];if (siz[x]==0) return;for (int i=head[x];i;i=next[i]) {if (a[i+1]==y) Ans--;if (a[i-1]==y) ans--;} for (int i=head[x];i;i=next[i]) a[i]=y;siz[y]+=siz[x];next[nd[y]]=head[x];nd[y]=nd[x];head[x]=nd[x]=siz[x]=0;} void Slove () {while (m--) {int opt=read (), x,y;if (opt==2) printf ("%d\n", ans); Else{x=read (); Y=read (); Updata (x, y);}}} int main () {//freopen ("input.in", "R", stdin), using namespace Solution;init (); Slove (); return 0;}
BZOJ1483: [HNOI2009] Fantasy pudding