[cf1217F]Forced Online Queries Problem
可以用并查集维护连通性,删除可以用按置合并并查集,但删掉一条边后无法再维护两点的联通性了(因为产生环的边是不加入的)
暴力思路是, 考虑前i个操作后边的集合,暴力加入即可,但复杂度是$o(n^2)$的
用分块,对于每一个块,先求出前面所有块操作后边的集合,去掉这个块内删掉的边,这个并查集一定是之后这个块内每一个点都有的并查集,即计算每一个点时都恢复到这个并查集(恢复时记录下修改的点,因此也不能路径压缩)
之后用暴力的做法,这个集合大小是$o(\sqrt{n})$的,那么总复杂度就是$o(n\sqrt{n})$,可以通过
(这个算法是离线,因为它需要之后那个块内的操作,但这些操作最多只会衍生出两种操作,只要存在一个就都不要放入原并查集中即可)

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define K 5000
4 #define N 200005
5 #define pii pair<int,int>
6 #define fi first
7 #define se second
8 int n,m,ans,p[N],x[N],y[N],f[N],g[N],sz[N],v1[N];
9 pii v2[N];
10 set<pii>g1,g2;
11 set<pii>::iterator it;
12 int find(int k){
13 if (k==f[k])return k;
14 return find(f[k]);
15 }
16 void check(int x,int y){
17 if (x>y)swap(x,y);
18 pii o=make_pair(x,y);
19 if (g1.find(o)!=g1.end()){
20 g1.erase(o);
21 g2.insert(o);
22 }
23 }
24 void update(pii o){
25 if (g2.find(o)==g2.end())g2.insert(o);
26 else g2.erase(o);
27 }
28 void add(int x,int y){
29 x=find(x);
30 y=find(y);
31 if (x==y)return;
32 if (sz[x]<sz[y])swap(x,y);
33 v1[++v1[0]]=y;
34 f[y]=x;
35 v2[v1[0]]=make_pair(x,sz[x]);
36 sz[x]=max(sz[x],sz[y]+1);
37 }
38 int main(){
39 scanf("%d%d",&n,&m);
40 for(int i=1;i<=m;i++)scanf("%d%d%d",&p[i],&x[i],&y[i]);
41 for(int i=1;i<=m;i+=K){
42 int k=min(m,i+K-1);
43 g2.clear();
44 for(int j=i;j<=k;j++)
45 if (p[j]==1){
46 check(x[j],y[j]);
47 check(x[j]%n+1,y[j]%n+1);
48 }
49 for(int j=1;j<=n;j++)f[j]=j;
50 for(it=g1.begin();it!=g1.end();it++)add((*it).fi,(*it).se);
51 v1[0]=0;
52 for(int j=i;j<=k;j++){
53 x[j]=(x[j]+ans-1)%n+1;
54 y[j]=(y[j]+ans-1)%n+1;
55 if (x[j]>y[j])swap(x[j],y[j]);
56 if (p[j]==1)update(make_pair(x[j],y[j]));
57 else{
58 for(int l=1;l<=v1[0];l++){
59 f[v1[l]]=v1[l];
60 sz[v2[l].fi]=v2[l].se;
61 }
62 v1[0]=0;
63 for(it=g2.begin();it!=g2.end();it++)add((*it).fi,(*it).se);
64 printf("%d",ans=(find(x[j])==find(y[j])));
65 }
66 }
67 for(it=g2.begin();it!=g2.end();it++)g1.insert(*it);
68 }
69 }
[cf1217F]Forced Online Queries Problem的更多相关文章
- 【CF1217F】Forced Online Queries Problem
题意 题目链接 动态图连通性,加密方式为 \((x+l-1)\bmod n +1\) (\(l=[上一次询问的两点连通]\)). 点数 \(n\),操作数 \(m\) \(\le 2\times 10 ...
- @codeforces - 1217F@ Forced Online Queries Problem
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 个点的无向图,标号从 1 到 n.一开始没有任何边 ...
- [Codeforces 863D]Yet Another Array Queries Problem
Description You are given an array a of size n, and q queries to it. There are queries of two types: ...
- 863D - Yet Another Array Queries Problem(思维)
原题连接:http://codeforces.com/problemset/problem/863/D 题意:对a数列有两种操作: 1 l r ,[l, r] 区间的数字滚动,即a[i+1]=a[i] ...
- Yet Another Array Queries Problem CodeForces - 863D (暴力/思维)
You are given an array a of size n, and q queries to it. There are queries of two types: 1 li ri — p ...
- Educational Codeforces Round 72 (Rated for Div. 2) Solution
传送门 A. Creating a Character 设读入的数据分别为 $a,b,c$ 对于一种合法的分配,设分了 $x$ 给 $a$ 那么有 $a+x>b+(c-x)$,整理得到 $x&g ...
- Codechef Dynamic Trees and Queries
Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...
- CodeChef---- February Challenge 2018----Chef and odd queries(复杂度分块计算)
链接 https://www.codechef.com/FEB18/problems/CHANOQ/ Chef and odd queries Problem Code: CHANOQ Chef ...
- CodeChef Gcd Queries
Gcd Queries Problem code: GCDQ Submit All Submissions All submissions for this problem are ava ...
随机推荐
- 感恩笔记之二_SQL语句扩展功能
前言导读: 本章是对SQL语句基础功能中,一些功能用法的扩展使用的总结,都是实际工作中一些经验的积累. 1 select列查询功能组合使用 --1 函数处理+列计算+列改名 select 函数(列) ...
- css新增属性之边框
css3新增属性 边框属性 背景属性 文字属性 颜色属性 边框属性 属性 说明 border-radius 设置边框圆角 border-image 设置图像边框 border-shadow 设置边框阴 ...
- postgresql高可用集群部署
一.概况 1.概念 pgsql高可用集群采用postgresql+etcd+patroni+haproxy+keepalived等软件实现,以postgresql做数据库,etcd存储集群状态,pat ...
- pip安装加速
PIP国内镜像源 名称 源地址 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/s ...
- Vuex 基础
其他章节请看: vue 快速入门 系列 Vuex 基础 Vuex 是 Vue.js 官方的状态管理器 在vue 的基础应用(上)一文中,我们已知道父子之间通信可以使用 props 和 $emit,而非 ...
- Catch That Cow 经典广搜
链接:http://poj.org/problem?id=3278 题目: Farmer John has been informed of the location of a fugitive co ...
- fastjson及其反序列化分析--TemplatesImpl
fastjson及其反序列化分析 源码取自 https://www.github.com/ZH3FENG/PoCs-fastjson1241 参考 (23条消息) Json详解以及fastjson使用 ...
- javascript-jquery对象的其他处理
一.对元素进行遍历操作 如果要遍历一个jquery对象,对其中每个匹配元素进行相应处理,那么可以使用each()方法. $("div").each(function(index,e ...
- C++编译Dlib库出现LNK2001错误(原因是在Python中安装过Dlib)
问题 使用CMake编译Dlib库,编译得到lib文件后,新建一个VS工程想使用Dlib,却出现LNK2001:无法解析的外部符号的错误,且都与JPEG和PNG相关: 1>dlib19.17.9 ...
- 【UE4 C++】绘制函数 Debug drawing functions
基于UKismetSystemLibrary /** Draw a debug line */ UFUNCTION(BlueprintCallable, Category="Renderin ...