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 ...
随机推荐
- Scrapy-redis改造scrapy实现分布式多进程爬取
一.基本原理: Scrapy-Redis则是一个基于Redis的Scrapy分布式组件.它利用Redis对用于爬取的请求(Requests)进行存储和调度(Schedule),并对爬取产生的项目(it ...
- [.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序
[.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序 一.练习项目: http://www.asp.net/mvc/tutorials/mvc-4/gettin ...
- c++模板函数声明定义分离编译错误详解
今天看到accelerated c++上有个简单的vector容器的实现Vec,就再vs2008上编译了下: ///// Vec.h #ifndef GUARD_VEC_H #define GUARD ...
- ORM与hibernate概述
JDBC是什么? JDBC代表java数据库连接,并提供一组java API,用于java成许访问关系数据库.这些Java的API允许Java程序执行SQL语句,并与任何SQL兼容的数据库进行交互. ...
- Dev GridView-Bind Detail Grid during runtime
Here is a simple example. ASPX <%@ Page Language="C#" AutoEventWireup="true" ...
- Linux 系统信息查询
众所周知,Linux大部分操作是通过命令实现的,并不像windows那么直观.linux查看硬件信息也是需要通过linux查看硬件信息命令查询的,下面小编来分享一下linux查看硬件信息的方法和命令写 ...
- jmeter随笔(34)-WebSocket协议接口测试实战
2017年春节结束了,一切再次回归到正轨,我们飞测也开始继续分享,小怪在这里预祝大家在2017年工作顺利,满满的收获. 背景:今天研发哥们QQ我,请教websocket协议的接口测试,这哥们自己开发了 ...
- liunx用户管理的基本命令
1.passwd 修改用户密码 2.useradd 用户组名 增加用户组 3.su 用户名 切换用户名 4.usermod 用户更改 5.userdel 用户删除
- ubuntu mongodb backup/restore (备份和恢复)
备份(导出) 1.导出单个collection-.json格式 mongoexport --host:127.0.0.1 --port:27017 --db test --collection tes ...
- 带CookieContainer进行post
1.获取CookieContainer ——用户登录 CookieContainer cookie = new CookieContainer(); UserLoginPost("post地 ...