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 ...
随机推荐
- 201621123010《Java程序设计》第13周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以被多个用户通过网 ...
- C++内存管理-重载内存管理函数
记录学习的点点滴滴,参考侯捷<<C++内存管理>> 我们先重载一下C++的几个内存管理函数 operator new, operator new[], operator del ...
- 网页瀑布流布局插件Masonry
CSS3有实现类似瀑布流布局的属性column,但是遗憾的是这种布局是纵向布局而不支持横向布局.所以比较合适用于文字多列布局. 调用JQuery WaterFall布局插件:http://masonr ...
- Thrift之双向通讯
在实际应用中,却经常会有客户端建立连接后,等待服务端数据的长连接模式,也可以称为双向连接.一.双连接,服务端与客户端都开ThriftServer如果网络环境可控,可以让服务端与客户端互相访问,你可以给 ...
- 务实java基础之集合总结
Java 提供了容纳对象(或者对象的句柄)的多种方式.其中内建的类型是数组,此外, Java 的工具库提供了一些 "集合类",利用这些集合类,我们可以容纳乃至操纵自己的对象. 声明 ...
- 程序运行时间c++/matlab
前言 一般在调试程序的过程中,需要查看代码运行速度的快慢,此时则需要计算代码的运行时间. 实验过程: c++: #include<iostream> #include<time.h& ...
- 响应式有利于SEO还是pc+手机端分开url有利于SEO?
一早上都在查这个问题,大家都来讨论一下. 首先,可以肯定的是,如果公司推广重在谷歌,要做响应式.但是对于百度推广呢??虽然响应式是趋势,但是目前而言,对于百度怎样好呢
- ElasticSearch(七):ElasticSearch集群的搭建
由于资源有限,使用是一台机器上安装三个elasticSearch服务端组成的集群. 1. 安装elasticSearch6.3.2 将原本安装的elasticSearch6.3.2复制两份,分别重新命 ...
- android 自动拨打电话 挂断电话代码
页面布局文件代码 ( res下面的layout下面的activity_main.xml代码 ) <RelativeLayout xmlns:android="http://sche ...
- hdu 5310(贪心)
题意:要买n个纪念品,单价p元,有团购价 m个q元,问怎样买钱最少 这个是BC周年庆第一题,水题昂,小学数学题,就是看n个纪念品单买.总体买团购然后零头买单价的.全部买团购价的多买也无所谓的,然后直接 ...