洛谷T31018 经典题丶改(LCT+离线)
真的是一个大好题啊!
QWQ首先我们考虑这种问题,如果直接在线做,估计应该是做不了,那我们是不是可以直接考虑离线。
将所有询问都按照\(r\)来排序。
然后依次加入每条边,计算\(a[i]<=nowr\)的答案
离线下来之后呢。
我们对于依次加入的边,实际上就相当于一个维护生成树的过程,那么qwq应该以什么为关键字呢?
由于我们可以精妙的发现,对于\(r\)不减(或者说不变)来说,编号小的边总是比大的边要没用,因为既然\([l+x,r]\)能使联通的话,那么\([l,r]\)也一定可以。
我们令每条边的边权是他的编号
那么我们维护的就是一个最小边权最大的生成树
然后类似\(two \ pointer\)
对于每一个\(i\),我们都暴力把\(r<=i\)的指针++,然后看一下是否联通,联通的条件是\(findroot(x)==findroot(y) 且 mn[y]>=l\),后面这项表示\(x->y\)的路径上最小的编号,要在他要求的\(l\)之后才行。
QWQ嘤嘤嘤
还有一个问题一定要注意!!!
就是点的编号的问题。
我们一定要强制让每个边对应的点的编号是\(i+n\),不要忽略重边和自环(如果你是用一个\(tot\)记录的话)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define mk makr_pair
#define ll long long
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int maxn = 5e5+1e2;
const int maxm = maxn;
struct Node{
int l,r,a,b,id;
};
Node a[maxn];
int ch[maxn][3];
int rev[maxn],fa[maxn];
int n,m,cnt;
int mn[maxn],mnpos[maxn];
int val[maxn];
int xx[maxm],yy[maxm];
int ans[maxn];
int k;
int son(int x)
{
if (ch[fa[x]][0]==x) return 0;
else return 1;
}
bool notroot(int x)
{
if(!x) return 0;
return ch[fa[x]][0]==x || ch[fa[x]][1]==x;
}
void update(int x)
{
if (!x) return;
mn[x]=val[x];
mnpos[x]=x;
if (ch[x][0])
{
if (mn[x]>mn[ch[x][0]])
{
mn[x]=mn[ch[x][0]];
mnpos[x]=mnpos[ch[x][0]];
}
}
if (ch[x][1])
{
if(mn[x]>mn[ch[x][1]])
{
mn[x]=mn[ch[x][1]];
mnpos[x]=mnpos[ch[x][1]];
}
}
}
void reverse(int x)
{
swap(ch[x][0],ch[x][1]);
rev[x]^=1;
}
void pushdown(int x)
{
if(rev[x])
{
if (ch[x][0]) reverse(ch[x][0]);
if (ch[x][1]) reverse(ch[x][1]);
rev[x]=0;
}
}
void rotate(int x)
{
int y=fa[x],z=fa[y];
int b=son(x),c=son(y);
if (notroot(y)) ch[z][c]=x;
fa[x]=z;
ch[y][b]=ch[x][!b];
fa[ch[x][!b]]=y;
ch[x][!b]=y;
fa[y]=x;
update(y);
update(x);
}
int st[maxn];
void splay(int x)
{
int y=x,cnt=0;
st[++cnt]=y;
while (notroot(y)) y=fa[y],st[++cnt]=y;
while (cnt) pushdown(st[cnt--]);
while (notroot(x))
{
int y=fa[x],z=fa[y];
int b=son(x),c=son(y);
if (notroot(y))
{
if(b==c) rotate(y);
else rotate(x);
}
rotate(x);
}
update(x);
}
void access(int x)
{
for (int y=0;x;y=x,x=fa[x])
{
splay(x);
ch[x][1]=y;
update(x);
}
}
void makeroot(int x)
{
access(x);
splay(x);
reverse(x);
}
int findroot(int x)
{
access(x);
splay(x);
while (ch[x][0])
{
pushdown(x);
x=ch[x][0];
}
// cout<<x<<endl;
return x;
}
void split(int x,int y)
{
makeroot(x);
access(y);
splay(y);
}
void link(int x,int y)
{
makeroot(x);
if (findroot(y)!=x)
{
fa[x]=y;
}
}
void cut(int x,int y)
{
split(x,y);
if (ch[x][0] || ch[x][1] || fa[x]!=y || ch[y][1]) return;
fa[x]=ch[y][0]=0;
update(y);
}
bool cmp(Node a,Node b)
{
return a.r<b.r;
}
int main()
{
val[0]=1e9;
n=read(),m=read(),k=read();
for (int i=1;i<=n;i++) val[i]=1e9;
for (int i=1;i<=m;i++) xx[i]=read(),yy[i]=read();
for (int i=1;i<=k;i++)
{
a[i].l=read();
a[i].r=read();
a[i].a=read();
a[i].b=read();
a[i].id=i;
}
sort(a+1,a+1+k,cmp);
int tot=n;
int now=1;
for (int i=1;i<=m && now<=k;i++)
{
int x = xx[i],y=yy[i];
//int rr = a[now].r
++tot;
val[tot]=i;
if(x!=y)
{
split(x,y);
if (x!=findroot(y))
{
link(x,tot);
//cout<<1<<endl;
link(y,tot);
}
else
{
int nnow = mnpos[y];
cut(xx[nnow-n],nnow);
cut(yy[nnow-n],nnow);
link(x,tot);
link(y,tot);
}
}
while (now<=k && a[now].r<=i)
{
if (a[now].a==a[now].b) ans[a[now].id]=1;
else
{
split(a[now].a,a[now].b);
if (a[now].a==findroot(a[now].b))
{
// cout<<a[now].a<<" "<<a[now].b<<" "<<mn[a[now].b]<<endl;
if (mn[a[now].b]>=a[now].l) ans[a[now].id]=1;
}
}
now++;
}
// cout<<findroot(1)<<" "<<findroot(3)<<endl;
}
for (int i=1;i<=k;i++)
{
if (ans[i])
{
cout<<"ye5\n";
}
else cout<<"n0\n";
}
return 0;
}
洛谷T31018 经典题丶改(LCT+离线)的更多相关文章
- [洛谷P1707] 刷题比赛
洛谷题目连接:刷题比赛 题目背景 nodgd是一个喜欢写程序的同学,前不久洛谷OJ横空出世,nodgd同学当然第一时间来到洛谷OJ刷题.于是发生了一系列有趣的事情,他就打算用这些事情来出题恶心大家-- ...
- 洛谷P5274 优化题(ccj)
洛谷P5274 优化题(ccj) 题目背景 CCJCCJ 在前往参加 Universe \ OIUniverse OI 的途中... 题目描述 有一个神犇 CCJCCJ,他在前往参加 Universe ...
- 洛谷 P4148 简单题 KD-Tree 模板题
Code: //洛谷 P4148 简单题 KD-Tree 模板题 #include <cstdio> #include <algorithm> #include <cst ...
- 洛谷 P1167 刷题
洛谷 P1167 刷题 洛谷传送门 题目描述 noip临近了,小A却发现他已经不会写题了.好在现在离竞赛还有一段时间,小A决定从现在开始夜以继日地刷题.也就是说小A废寝忘食,一天二十四小时地刷题. 今 ...
- 【noip】跟着洛谷刷noip题2
noip好难呀. 上一个感觉有点长了,重开一个. 36.Vigenère 密码 粘个Openjudge上的代码 #include<cstdio> #include<iostream& ...
- 洛谷P4332 [SHOI2014]三叉神经树(LCT,树剖,二分查找,拓扑排序)
洛谷题目传送门 你谷无题解于是来补一发 随便百度题解,发现了不少诸如树剖\(log^3\)LCT\(log^2\)的可怕描述...... 于是来想想怎么利用题目的性质,把复杂度降下来. 首先,每个点的 ...
- 洛谷P2542 [AHOI2005]航线规划(LCT,双连通分量,并查集)
洛谷题目传送门 太弱了不会树剖,觉得LCT好写一些,就上LCT乱搞,当LCT维护双连通分量的练手题好了 正序删边是不好来维护连通性的,于是就像水管局长那样离线处理,逆序完成操作 显然,每个点可以代表一 ...
- 洛谷U19464 山村游历(Wander)(LCT,Splay)
洛谷题目传送门 LCT维护子树信息常见套路详见我的总结 闲话 题目摘自WC模拟试题(by Philipsweng),原题目名Wander,"山村游历"是自己搞出来的中文名. 数据自 ...
- 洛谷U19464 山村游历(Wander)(LCT)
洛谷题目传送门 LCT维护子树信息常见套路详见我的总结 闲话 题目摘自WC模拟试题(by Philipsweng),原题目名Wander,"山村游历"是自己搞出来的中文名. 数据自 ...
随机推荐
- springboot静态资源路径制定
spring.resources.static-location参数指定了Spring Boot-web项目中静态文件存放地址, 该参数默认设置为: classpath:/static, classp ...
- 取消Ubuntu开机硬盘自检
修改/etc/fstab文件,最后一列全改为0,测试能正常启动,尚未出现再次自检的情况 sudo gedit /etc/fstab
- PXC 5.7.14 安装部署
http://www.dbhelp.net/2017/01/06/pxc-5-7-14-%E5%AE%89%E8%A3%85%E9%83%A8%E7%BD%B2-pxc-install.html PX ...
- GUI常用监听事件
概念 对鼠标.键盘等一系列事件做出相应的反馈 事件监听 //创建监听事件 public class Demo { public static void main(String[] args) { Fr ...
- Shell 脚本如何输出帮助信息?
作者展示了一个技巧,将帮助信息写在 Bash 脚本脚本的头部,然后只要执行"脚本名 + help",就能输出这段帮助信息 https://samizdat.dev/help-mes ...
- react + layui 坑总结
与react 结合的时候,layui 是纯dom操作,而react是虚拟dom ,二者的结合难免会出现诸多问题. 1 select 下拉框 默认值的修改要通过defaultValue 属性来修改,并且 ...
- Linux查看英伟达GPU信息
命令: nvidia-smi 结果:
- noip模拟41
A. 你相信引力吗 很明显的单调栈的题,考场上没有想到平移最大值,而是想着复制一倍序列破环成链,然后发现最大值的部分特别难维护,而且耗费时间过长,只好牺牲时间复杂度加了个 \(map\) 去重. 首先 ...
- docker for zabbix
docker run -d -v /home/zabbix/mysql --name zabbix-db-storage busybox:latest docker run -d --name zab ...
- 尚硅谷Java高级笔记
尚硅谷Java高级笔记 idea的使用: 一些小区别: 其他细节参考idea配置pdf 多线程: 基本概念: 多线程的优点: 何时需要多线程: 线程的创建和使用: 创建多线程的第一种方式: /** * ...