HDU-4879-ZCC loves march(map+set+并查集)
Description
Then there are t minutes, in each minute one of the following two events will occur:
(1)the x-th troop moves towards a direction( Up(U) Down(D) Left(L) Right(R))for d units;(You can suppose that the troops won't move out of the boundary)
(2)the x-th troop needs to regroup the troops which stations in the same row or column with the x-th troop. That is, these troops need to move to the x-th troop's station.
Suggest the cost of i-th troop moving to the j-th troop is (xi-xj)^2+(yi-yj)^2, every time a troop regroups, you should output the cost of the regrouping modulo 10^9+7.
Input
Next n lines each line contain two numbers xi,yi(1<=xi,yi<=m)
Next line contains a number t.(t<=100000)
Next t lines, each line's format is one of the following two formats:
(1)S x d, S∈{U,L,D,R}, indicating the first event(1<=x<=n,0<=d<m)
(2)Q x, indicating the second event(1<=x<=n)
In order to force you to answer the questions online, each time you read x', x=x'�lastans("�" means "xor"), where lastans is the previous answer you output. At the beginning lastans=0.
Output
Sample Input
5 3
1 3
2 1
2 2
2 3
3 2
6
Q 1
L 0 2
L 5 2
Q 5
R 3 1
Q 3
Sample Output
1
1
7
Hint
The input after decode: Q 1 L 1 2 L 4 2 Q 4 R 2 1 Q 2
思路:分别用map来维护与x坐标平行和垂直的线上面的集合,集合里面直接存相应元素的下标,并用并查集维护,根节点能够用来表示所在的集合。
当移动一个元素时,先找到该元素下标相应的根。根相应的num-1,并把移动的元素插到新的位置上,他的根就是他自己。运行Q操作时,通过map找到同一行和同一列的全部集合进行计算,再把这些计算过的集合删掉,同一时候下标指向新的根(即移动之后形成的新的集合)。
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
using namespace std; struct TROOP{
LL x,y,num;
TROOP(){}
TROOP(LL nx,LL ny,LL nnum){x=nx,y=ny,num=nnum;}
}troop[200005]; map<LL,set<LL> >MX;
map<LL,set<LL> >MY;
set<LL>::iterator it; LL node[200005]; LL findroot(LL x)
{
if(node[x]!=x) node[x]=findroot(node[x]); return node[x];
} int main()
{
LL m,x,y,n,i,t,cnt,root,a,b,ans;
char s[5]; while(~scanf("%I64d%I64d",&n,&m))
{
MX.clear();
MY.clear(); for(i=1;i<=n;i++)
{
scanf("%I64d%I64d",&x,&y); troop[i].x=x;
troop[i].y=y;
troop[i].num=1; node[i]=i; MX[x].insert(i);
MY[y].insert(i);
} cnt=n+1; ans=0; scanf("%I64d",&t); while(t--)
{
scanf("%s",s); if(s[0]=='Q')
{
scanf("%I64d",&a); a^=ans; root=findroot(a);//找到a所在的集合 x=troop[root].x;
y=troop[root].y; LL num=0; ans=0; for(it=MX[x].begin();it!=MX[x].end();it++)
{
num+=troop[*it].num; LL temp=abs(troop[*it].y-y); temp%=mod; ans=(temp*temp%mod*troop[*it].num%mod+ans)%mod; node[*it]=cnt;//指向cnt,cnt是运行Q操作之后新的根。用来标记新的集合 MY[troop[*it].y].erase(*it);//*it已经计算过。从MY[]集合里删掉,避免反复计算
} for(it=MY[y].begin();it!=MY[y].end();it++)
{
num+=troop[*it].num; LL temp=abs(troop[*it].x-x); temp%=mod; ans=(temp*temp%mod*troop[*it].num%mod+ans)%mod; node[*it]=cnt;//同理 MX[troop[*it].x].erase(*it);//同理
} node[cnt]=cnt;//指向自己,别忘了
troop[cnt]=TROOP(x,y,num);//运行Q操作之后形成的新集合
MX[x].clear();
MY[y].clear();
MX[x].insert(cnt);//在目标集合中插入
MY[y].insert(cnt); cnt++; printf("%I64d\n",ans);
}
else
{
scanf("%I64d%I64d",&a,&b); a^=ans; root=findroot(a);//找到a所在的集合,即a的根节点 x=troop[root].x;
y=troop[root].y; troop[root].num--;//集合里的计数减一 if(!troop[root].num)//假设集合的计数为0则把该集合删掉
{
MX[x].erase(root);
MY[y].erase(root);
} if(s[0]=='U')
{
troop[a]=TROOP(x-b,y,1); node[a]=a;//a指向自己,作为新的根 MX[x-b].insert(a);//在目标位置插入
MY[y].insert(a);
}
else if(s[0]=='L')//下面同理
{
troop[a]=TROOP(x,y-b,1); node[a]=a; MX[x].insert(a);
MY[y-b].insert(a);
}
else if(s[0]=='D')
{
troop[a]=TROOP(x+b,y,1); node[a]=a; MX[x+b].insert(a);
MY[y].insert(a);
}
else if(s[0]=='R')
{
troop[a]=TROOP(x,y+b,1); node[a]=a; MX[x].insert(a);
MY[y+b].insert(a);
}
}
}
}
}
HDU-4879-ZCC loves march(map+set+并查集)的更多相关文章
- HDU 4879 ZCC loves march (并查集,set,map)
题面以及思路:https://blog.csdn.net/glqac/article/details/38402101 代码: #include <bits/stdc++.h> #defi ...
- hdu 4876 ZCC loves cards(暴力)
题目链接:hdu 4876 ZCC loves cards 题目大意:给出n,k,l,表示有n张牌,每张牌有值.选取当中k张排列成圈,然后在该圈上进行游戏,每次选取m(1≤m≤k)张连续的牌,取牌上值 ...
- HDU 4876 ZCC loves cards(暴力剪枝)
HDU 4876 ZCC loves cards 题目链接 题意:给定一些卡片,每一个卡片上有数字,如今选k个卡片,绕成一个环,每次能够再这个环上连续选1 - k张卡片,得到他们的异或和的数,给定一个 ...
- hdu 4873 ZCC Loves Intersection(大数+概率)
pid=4873" target="_blank" style="">题目链接:hdu 4873 ZCC Loves Intersection ...
- HDU 4873 ZCC Loves Intersection(可能性)
HDU 4873 ZCC Loves Intersection pid=4873" target="_blank" style="">题目链接 ...
- hdu 4882 ZCC Loves Codefires(数学题+贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4882 ------------------------------------------------ ...
- hdu 5288 ZCC loves straight flush
传送门 ZCC loves straight flush Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K ...
- HDU 4882 ZCC Loves Codefires (贪心)
ZCC Loves Codefires 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/B Description Though ...
- HDU 4882 ZCC Loves Codefires(贪心)
ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- android SurfaceView绘制 重新学习--控制动画移动
直接上demo,图是自己切的,将就用吧.点击左右两边分别向左右移动. public class MySurfaceView extends SurfaceView implements Callbac ...
- Linux发行版
Linux 发行版(英语:Linux distribution,也被叫做GNU/Linux 发行版),为一般用户预先集成好的Linux操作系统及各种应用软件.一般用户不需要重新编译,在直接安装之后,只 ...
- 保存项目文件“XXX.csprj”时出错。类没有注册。
[出错提示]保存项目文件“XXX”时出错.类没有注册.正在查找具有CLSID的对象:{D9B3211D-E57F-4426-AAEF-30A806ADD397}. [解决办法] 需要安装:MSXML_ ...
- DM8168 debug continue... ...
1.boot VFS: Unable to mount root fs via NFS, trying floppy. VFS: Cannot open root device "n ...
- MinGW中的头文件路径级环境变量设置
整理自 MinGW中的头文件路径 C头文件目录%MINGW_PATH%/include下有头文件,里面有strcpy等c函数的声明. C++头文件目录%MINGW_PATH%/lib/gcc/mi ...
- 【网络流24题】 No.2 太空飞行计划问题 (最大闭合权图 最大流 )
原题: W教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合 E={E1,E2,...,Em},和进行这些实验需 ...
- 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
[题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...
- javascript中通过replace函数搜索和替换指定字符串
javascript中我们可以通过replace函数替换部分字符串为指定字符串,本文展示了replace的详细用法,并且通过范例演示了如何进行部分替换.完整替换和不区分大小写替换. javascrip ...
- ASP.NET MVC 下 引用阿里巴巴和IconFont字体路径404问题
参考:http://stackoverflow.com/questions/28169365/font-wont-get-found-on-server-for-firefox# http://blo ...
- USACO3.42American Heritage(二叉树)
已知中前 求后序 递归一下 有一些小细节 /* ID: shangca2 LANG: C++ TASK: heritage */ #include <iostream> #include& ...
