[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 ...
随机推荐
- Ysoserial Commons Collections2分析
Ysoserial Commons Collections2分析 About Commons Collections2 CC2与CC1不同在于CC2用的是Commons Collections4.0; ...
- JUC之Executor,ExecutorService接口,AbstractExecutorService类
java多线程的Executor中定义了一个execut方法,ExecutorService接口继承了Executor接口,并进行了功能的扩展组合,定义了shutdown,shutdownNow,su ...
- xml文件报Element 'beans' cannot have character [children],because the type's content type is element
写springMvc.xml文件时,偶然遇到 Element 'beans' cannot have character [children],because the type's content t ...
- CountBoard 是一个基于Tkinter简单的,开源的桌面日程倒计时应用
CountBoard 是一个基于Tkinter简单的,开源的桌面日程倒计时应用. 项目地址 https://github.com/Gaoyongxian666/CountBoard 基本功能 置顶功能 ...
- ❤️这应该是Postman最详细的中文使用教程了❤️(新手使用,简单明了)
️这应该是Postman最详细的中文使用教程了️(新手使用,简单明了) 在前后端分离开发时,后端工作人员完成系统接口开发后,需要与前端人员对接,测试调试接口,验证接口的正确性可用性.而这要求前端开发进 ...
- 【JAVA】【作业向】第一题:本学期一班级有n名学生,m门课程。现要求对每门课程的成绩进行统计:平均成绩、最高成绩、最低成绩,并统计考试成绩的分布律。
1.预备知识:动态数组Array实现: 2.解题过程需要理解的知识:吧唧吧唧吧唧吧唧 不想做了 就用了最简单的方法 和c语言类似 java版本 `import java.util.Scanner; / ...
- mac无坑安装nginx
mac无坑安装nginx 首先需要mac下有一个缺失的软件包的管理器------->homebrew 1.打开终端输入 brew update 说明homebrew已经安装好了 2.继续执行以下 ...
- SharkCTF2021 pwn“初见”1
(无内鬼 今日不想学了 水一篇) nc nc nc easyoverflow Intoverflow
- ORA-19815: WARNING: db_recovery_file_dest_size闪回区爆满问题处理
问题描述:有一个数据库起不来了,根据层层排查,是因为归档设置在了闪回区,文件的大小已经超出了闪回区限制.最后直接给数据库拖挂 环境:windows server2012 , oracle 19c,单机 ...
- 【UE4 C++】抛物线路径、发射轨道相关
基于UGameplayStatics Blueprint_PredictProjectilePath_ByObjectType 根据 Object Type,算出抛物线的点集合和检测结果 static ...