HDU 4879 ZCC loves march (并查集,set,map)

题面以及思路:https://blog.csdn.net/glqac/article/details/38402101

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const LL mod = 1000000007;
const int maxn = 2000010;
struct node {
	LL x, y;
	int cnt;
	node(){}
	node(LL x, LL y, int cnt) {
		this -> x = x;
		this -> y = y;
		this -> cnt = cnt;
	}
};
node pos[maxn];
map<LL, set<int> > row, col;
set<int> :: iterator it;
int f[maxn];
int get(int x) {
	if(x == f[x]) return x;
	return f[x] = get(f[x]);
}
int tot;
int main() {
	int n, m, T, p;
	LL x, y, ans, d;
	char s[10];
	while(~scanf("%d%d", &n, &m)) {
		row.clear();
		col.clear();
		for (int i = 1; i <= n; i++) {
			scanf("%lld%lld", &x, &y);
			pos[i] = node(x, y, 1);
			f[i] = i;
			row[x].insert(i);
			col[y].insert(i);
		}
		f[n + 1] = n + 1;
		tot = n + 1;
		scanf("%d", &T);
		ans = 0;
		while(T--) {
			scanf("%s",s + 1);
			if(s[1] == 'Q') {
				scanf("%d", &p);
				p = p ^ ans;
				p = get(p);
				node tmp = pos[p];
				int num = 0;
				ans = 0;
				LL xx = pos[p].x, yy = pos[p].y;
				for (it = row[xx].begin(); it != row[xx].end(); it++) {
					node& tmp = pos[*it];
					f[*it] = tot;
					col[yy].erase(*it);
					num += tmp.cnt;
					LL tmp1 = (abs(yy - tmp.y)) % mod;
					ans = (ans + (tmp1 * tmp1) % mod * tmp.cnt % mod) % mod;
					tmp.y = yy;
				}
				for (it = col[yy].begin(); it != col[yy].end(); it++) {
					node& tmp = pos[*it];
					f[*it] = tot;
					num += tmp.cnt;
					row[xx].erase(*it);
					LL tmp1 = (abs(xx - tmp.x)) % mod;
					ans = (ans + (tmp1 * tmp1) % mod) % mod;
					tmp.y = yy;
				}
				col[yy].clear();
				row[xx].clear();
				pos[tot] = node(xx, yy, num);
				col[yy].insert(tot);
				row[xx].insert(tot);
				tot++;
				f[tot] = tot;
				printf("%lld
", ans);
			} else {
				scanf("%d%lld", &p, &d);
				p = p ^ ans;
				int p1 = p;
				p = get(p);
				node& tmp = pos[p];
				LL xx = tmp.x, yy = tmp.y;
				tmp.cnt--;
				if(tmp.cnt == 0) {
					row[xx].erase(p);
					col[yy].erase(p);
				}
				if(s[1] == 'L') {
					yy -= d;
				} else if(s[1] == 'U') {
					xx -= d;
				} else if(s[1] == 'D') {
					xx += d; 
				} else {
					yy += d;
				}
				f[p1] = p1;
				pos[p1] = node(xx, yy, 1);
				row[xx].insert(p1);
				col[yy].insert(p1);
			}
		}
	}
	
}

  

原文地址:https://www.cnblogs.com/pkgunboat/p/10547905.html