Meaning of the questions: to give you the n battleship, the i-th initial warships are on the i-th row, there are two modes of operation: M ij, the battleship i column j according to the original order of receiving the end of the bar, C ij, query interval i and j several battleship USS, the
If the output is not a -1.
Ideas: the disjoint-set slightly changed on the line, with an array of recording several compartments, with a further record a current capacity of each array column.
#include<cstring> #include<algorithm> #include<vector> #include<map> #include<queue> #include<cstdio> #include<cmath> #define ll long long using namespace std; int fa[30010],num[30010],dis[30010]; int get(int x) { if(x!=fa[x]) { int k=fa[x]; fa[x]=get(fa[x]); dis[x]+ Push [k]; Surely [x] = num [fa [x]]; } Return fa [x]; } Void merge ( int x, int y) { int r1 = get (x), R2 = get (j); if (r1 = r2) { f [r1] = r2; push [r1] = push [R2] + num [R2]; Surely [R2] + = num [r1]; Surely [r1] = num [R2]; } } Int query ( int x, int y) { int r1=get(x),r2=get(y); if(r1!=r2) return -1; else return abs(dis[x]-dis[y])-1; } int main() { int t; for(int i=1;i<=30008;i++) { fa[i]=i; num[i]=1; } scanf("%d",&t); while(t--) { char c[2]; int x,y; scanf("%s",c); // getchar(); scanf("%d%d",&x,&y); //printf("%c\n",c); if(c[0]=='M') merge(x,y); else { printf("%d\n",query(x,y)); } } }