HDU 4879 ZCC loves march (并查集,set,map)
生活随笔
收集整理的這篇文章主要介紹了
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\n", 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
總結
以上是生活随笔為你收集整理的HDU 4879 ZCC loves march (并查集,set,map)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 都江堰
- 下一篇: Nginx下完美解决WordPress的