bzoj4153 [Ipsc2015]Familiar Couples
Description
Input
Output
- #include<cstdio>#include<vector>
- const int P=1e9+,N=1e6+;
- inline int read(){
- int x=,c=getchar();
- while(c>''||c<'')c=getchar();
- while(c>=''&&c<='')x=x*+c-'',c=getchar();
- return x;
- }
- inline void exch(int&a,int&b){int c=a;a=b;b=c;}namespace Map{
- const int mx=;
- unsigned int nw=;
- unsigned int xs[mx],ys[mx],zs[mx],ds[mx];
- inline void clear(){
- nw++;
- }
- inline int get(unsigned int x,unsigned int y,int inc){
- unsigned int w=(x*+y*+)%mx;
- while(ds[w]==nw){
- if(xs[w]==x&&ys[w]==y){
- int v=zs[w];
- zs[w]+=inc;
- return v;
- }
- w+=;
- if(w>=mx)w-=mx;
- }
- ds[w]=nw;
- xs[w]=x;ys[w]=y;zs[w]=inc;
- return ;
- }
- }
- int T,n,m,now=,Ans,ans;
- int h1[N],h2[N],f1[N],f2[N],sz1[N],sz2[N],nx1[N],nx2[N];
- int t[N],d[N];
- std::vector<int>v1[N],v2[N];
- int main(){
- T=read();
- while(T--){
- n=read();m=read();
- Ans=ans=;
- Map::clear();
- for(int i=;i<=n;i++){
- Map::get(i,i,);
- f1[i]=f2[i]=i;
- v1[i].clear();v2[i].clear();
- v1[i].push_back(i);
- v2[i].push_back(i);
- }
- for(int i=,op,a,b;i<=m;++i){
- op=read();a=read();b=read();
- ++now;
- if(op==){
- if(f1[a]!=f1[b]){
- if(v1[f1[a]].size()>v1[f1[b]].size())exch(a,b);
- std::vector<int>&vc=v1[f1[a]];
- for(int x=;x<vc.size();x++){
- int p=vc[x];
- int f=f2[p];
- if(d[f]!=now)d[f]=now,ans=(ans+Map::get(f1[p],f,-)*1ll*Map::get(f1[b],f,)%P)%P;
- else Map::get(f1[p],f,-),Map::get(f1[b],f,);
- f1[p]=f1[b];
- v1[f1[b]].push_back(p);
- }
- vc.clear();
- }
- }else{
- if(f2[a]!=f2[b]){
- if(v2[f2[a]].size()>v2[f2[b]].size())exch(a,b);
- std::vector<int>&vc=v2[f2[a]];
- for(int x=;x<vc.size();x++){
- int p=vc[x];
- int f=f1[p];
- if(d[f]!=now)d[f]=now,ans=(ans+Map::get(f,f2[p],-)*1ll*Map::get(f,f2[b],)%P)%P;
- else Map::get(f,f2[p],-),Map::get(f,f2[b],);
- f2[p]=f2[b];
- v2[f2[b]].push_back(p);
- }
- vc.clear();
- }
- }
- Ans=(Ans+ans*1ll*i%P)%P;
- }
- printf("%d\n",Ans);
- }
- return ;
- }
bzoj4153 [Ipsc2015]Familiar Couples的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Sicily 1021. Couples
题目地址:1021. Couples 思路: 想清楚了这道题其实很简单.利用夫妻出现的位置作为下标,并设为同一值,第一对夫妻值为1,第二对为2,以此类推,存储完毕即可进入下一步. 利用栈这个数据结构: ...
- Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical
http://julialang.org/ julia | source | downloads | docs | blog | community | teaching | publications ...
- [LeetCode] Couples Holding Hands 两两握手
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...
- [Swift]LeetCode765. 情侣牵手 | Couples Holding Hands
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...
- [CC-COUPLES]Couples sit next to each other
[CC-COUPLES]Couples sit next to each other 题目大意: 有\(n(n\le5\times10^5)\)对小伙伴共\(2n\)个人坐成一圈.刚开始编号为\(i\ ...
- 每日英语:Why Rate Your Marriage? A Numerical Score Can Help Couples Talk About Problems
When marriage therapist Sharon Gilchrest O'Neill met with new clients recently, she asked them why t ...
- ZOJ 3161 Damn Couples 动态规划 难度:2
Damn Couples Time Limit: 1 Second Memory Limit: 32768 KB As mentioned in the problem "Coup ...
- LeetCode765. Couples Holding Hands
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...
随机推荐
- Oracle 与Sql Server常用函数对比
来自:http://topic.csdn.net/u/20080704/08/b2b8c42f-b0d6-4cda-98b1-6e4a279b4ff8.html 感谢楼主 函数 SQLServer和O ...
- 难度2:ASCII码排序
ASCII码排序 难度:2描述: 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符. 输入: 第一行输入一个数N,表示有N组测试数据.后面的N行输入多组数据,每组输入数据 ...
- ora-01830:日期格式图片在转换整个输入字符串之前结束
在to_date的时候因为 fldsj这个字段的大小 与 ‘yyyy-MM-dd’不符合 所以需要截取下fldsj字段的大小 to_char(to_date(substr(fldsj,1,10),' ...
- 20165210 Java第八周学习总结
20165210 Java第八周学习总结 教材内容学习 - 第十二章学习总结 进程与线程 操作系统与进程 Java中的线程 Java的多线程机制 主线程 线程的状态与生命周期 1. 新建 2. 运行 ...
- apsx 页面 if(!ispostback)其用法和作用 什么时候该用?
一个页面第一次显示的时候 isPostBack=false 然后你在这个页面上点击按钮或其它东西提交的时候, isPostBack=true 一般这个函数里面的内容是指第一次打开这个页面的时候要做的事 ...
- Intellij Idea2016.3 svn服务器拉取代码
1.修改idea的默认配置,取消SVN设置里的两个勾 2.拉取代码 3.输入SVN仓库的地址,然后checkout 即可
- HDU 1853
http://acm.hdu.edu.cn/showproblem.php?pid=1853 和下题一模一样,求一个图环的并,此题的题干说的非常之裸露 http://www.cnblogs.com/x ...
- 【c++基础】判断是否到文件末尾-eof函数
前言 读取文件内容时,需要判断是否到文件末尾,此时用到eof函数. 函数定义 Check whether eofbit is set Returns true if theeofbiterror st ...
- 简单介绍Spring的ContextLoaderListener
在开发Spring的Web项目中,通常我们都会在web.xml中配置一个Spring的核心监听器,就是把Spring的IOC容器纳入Servlet容器中,配置如下: <listener> ...
- 粘包、拆包发生原因滑动窗口、MSS/MTU限制、Nagle算法
[TCP协议](3)---TCP粘包黏包 [TCP协议](3)---TCP粘包黏包 有关TCP协议之前写过两篇博客: 1.[TCP协议](1)---TCP协议详解 2.[TCP协议](2)---TCP ...