Codechef MARCH14 GERALD07加强版
强制在线不代表不能预处理!
考虑暴力怎么干?
开始n个联通块。now=n
不断加入边,如果连接两个联通块,--now
否则不动。
后者的前提是和[l,id-1]的边构成环
所以,我们考虑每个[l,r]中出现的边能否第一次连接两个联通块
预处理:
编号从小到大加入每条边,LCT维护树上“边”编号最小值和最小值位置
如果加入边e没有环,那么说明无论什么时候[l,r]询问包含e的时候,e总能是第一个连接两个联通块的边,设ti[e]=0,表示不能替换边
如果会成环,那么把编号最小的边删掉,ti[i]=被删边的编号。意义是,只有[l,r]不包含这条被删除的边的时候,e才会贡献。
如果包含,那么这个环一定会连出来;如果不包含,e和这个环上其他的边不会构成环,加入e的时候有贡献。(如果会构成环,那么环上边和被删边之前也能构成环,矛盾)
所以,ti[i]在不在[l,r]之中,唯一对应了i能否真正连接两个联通块!
所以对于询问,主席树维护即可。[l,r]中,ti[i]<l的数的个数sum,n-sum就是ans
实现细节:
LCT的编号别和原边的编号混了。。。。
mi是最小边编号(最小值),id是最小值的lct点的编号!
调半天~~~
吸取重组病毒的教训,把主席树封装进namespace~~
#include<bits/stdc++.h>
#define reg register int
#define il inline
#define numb (ch^'0')
using namespace std;
typedef long long ll;
il void rd(int &x){
char ch;x=;bool fl=false;
while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);
(fl==true)&&(x=-x);
}
namespace Miracle{
const int N=+;
const int inf=0x3f3f3f3f;
struct bian{
int x,y;
}e[N];
int n,m,k,typ;
int ti[N];
namespace seg{
#define mid ((l+r)>>1)
struct node{
int ls,rs;
int val;
}t[N*];
int cnt;
int rt[N];
void upda(int &x,int y,int l,int r,int to){
x=++cnt;
t[x].val=t[y].val+;
if(l==r) return;
t[x].ls=t[y].ls,t[x].rs=t[y].rs;
if(to<=mid) upda(t[x].ls,t[y].ls,l,mid,to);
if(mid<to) upda(t[x].rs,t[y].rs,mid+,r,to);
}
int query(int x,int y,int l,int r,int L,int R){
//cout<<" l r "<<l<<" "<<r<<" : "<<t[x].val<<" "<<t[y].val<<" "<<L<<" "<<R<<endl;
if(L<=l&&r<=R){
//cout<<" l r "<<l<<" "<<r<<" : "<<t[x].val<<" "<<t[y].val<<" "<<L<<" "<<R<<endl;
return t[x].val-t[y].val;
}
int ret=;
if(L<=mid) ret+=query(t[x].ls,t[y].ls,l,mid,L,R);
if(mid<R) ret+=query(t[x].rs,t[y].rs,mid+,r,L,R);
return ret;
}
void add(int x,int to){
upda(rt[x],rt[x-],,m,to);
}
}
namespace lct{
#define ls t[x].ch[0]
#define rs t[x].ch[1]
struct node{
int ch[];
int fa,r;
int id;
int mi;
int d;
}t[*N];
int sta[N];
int cnt;
bool nrt(int x){
return (t[t[x].fa].ch[]==x||t[t[x].fa].ch[]==x);
}
void pushup(int x){
if(t[t[x].ch[]].mi<=t[x].d&&t[t[x].ch[]].mi<=t[t[x].ch[]].mi){
t[x].mi=t[t[x].ch[]].mi;
t[x].id=t[t[x].ch[]].id;
}
else if(t[t[x].ch[]].mi<=t[x].d&&t[t[x].ch[]].mi<=t[t[x].ch[]].mi){
t[x].mi=t[t[x].ch[]].mi;
t[x].id=t[t[x].ch[]].id;
}
else t[x].mi=t[x].d,t[x].id=x;
}
void rev(int x){
swap(t[x].ch[],t[x].ch[]);
t[x].r^=;
}
void pushdown(int x){
if(t[x].r){
rev(t[x].ch[]),rev(t[x].ch[]);
t[x].r=;
}
}
void rotate(int x){
int y=t[x].fa,d=t[y].ch[]==x;
t[t[y].ch[d]=t[x].ch[!d]].fa=y;
if(nrt(y)) t[t[x].fa=t[y].fa].ch[t[t[y].fa].ch[]==y]=x;
else t[x].fa=t[y].fa;
t[t[x].ch[!d]=y].fa=x;
pushup(y);
}
void splay(int x){
int y=x,z=;
sta[++z]=y;
while(nrt(y)) y=t[y].fa,sta[++z]=y;
while(z) pushdown(sta[z--]); while(nrt(x)){
y=t[x].fa,z=t[y].fa;
// cout<<"splaying "<<x<<" "<<y<<" "<<z<<endl;
if(nrt(y)){
rotate(((t[z].ch[]==y)==(t[y].ch[]==x))?y:x);
}
rotate(x);
}
pushup(x);
}
void access(int x){
for(reg y=;x;y=x,x=t[x].fa){ splay(x);t[x].ch[]=y;pushup(x);
// cout<<" access xx "<<x<<" "<<t[x].fa<<endl;
}
}
void makert(int x){
access(x);splay(x);rev(x);
}
int findrt(int x){
access(x);splay(x);
// cout<<" after splay "<<x<<endl;
pushdown(x);
while(t[x].ch[]) {
x=t[x].ch[],pushdown(x);
// cout<<"findrt xx "<<x<<endl;
}
splay(x);
return x;
}
int link(int x,int y,int d){
// cout<<" link "<<x<<" "<<y<<" : "<<d<<" cnt "<<cnt<<endl;
makert(x);
// cout<<" makert "<<endl;
++cnt;
t[cnt].d=d;
t[cnt].id=cnt;
t[cnt].mi=d;
if(findrt(y)!=x){
// cout<<" new "<<endl;
access(y);splay(y);
t[x].fa=cnt;
t[cnt].fa=y;
return ;
}
pushup(x);
splay(x);
int kil=t[x].id;
int lp=t[x].mi;
// cout<<" ---------------------kil "<<kil<<endl;
splay(kil);
// cout<<" kilfa "<<t[kil].fa<<endl;
t[t[kil].ch[]].fa=;
t[t[kil].ch[]].fa=;
makert(x);
splay(x);splay(y);
t[x].fa=cnt;
t[cnt].fa=y;
//cout<<" t[x].id "<<t[x].id<<" t[y].id "<<t[y].id<<endl;
return lp;
}
}
int main(){
lct::t[].id=-;
lct::t[].d=inf;
lct::t[].mi=inf;
rd(n);rd(m);rd(k);rd(typ);
for(reg i=;i<=n;++i) lct::t[i].d=inf,lct::t[i].id=-,lct::t[i].mi=inf;
lct::cnt=n;
int x,y;
for(reg i=;i<=m;++i){
rd(x);rd(y);
if(x!=y) ti[i]=lct::link(x,y,i);
else ti[i]=i;
//cout<<" ii "<<i<<" "<<ti[i]<<endl;
}
for(reg i=;i<=m;++i){
seg::add(i,ti[i]);
}
int l,r;
int las=;
while(k--){
rd(l);rd(r);
if(typ==) l^=las,r^=las;
int tmp=seg::query(seg::rt[r],seg::rt[l-],,m,,l-);
printf("%d\n",n-tmp);
las=n-tmp;
}
return ;
} }
signed main(){
//freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
Miracle::main();
return ;
} /*
Author: *Miracle*
Date: 2018/12/30 21:24:18
*/
思路和区间数颜色挺像的!都是把贡献放在第一个能贡献的位置统计上!
并且方法也是类似的:离线:排序扫描线+树状数组,在线:主席树
Codechef MARCH14 GERALD07加强版的更多相关文章
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
- BZOJ 3514: Codechef MARCH14 GERALD07加强版( LCT + 主席树 )
从左到右加边, 假如+的边e形成环, 那么记下这个环上最早加入的边_e, 当且仅当询问区间的左端点> _e加入的时间, e对答案有贡献(脑补一下). 然后一开始是N个连通块, 假如有x条边有贡献 ...
- 【LCT+主席树】BZOJ3514 Codechef MARCH14 GERALD07加强版
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 2023 Solved: 778 ...
- BZOJ 3514: Codechef MARCH14 GERALD07加强版 [LCT 主席树 kruskal]
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1312 Solved: 501 ...
- [BZOJ 3514]Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES)
[BZOJ3514] Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES) 题意 \(N\) 个点 \(M\) 条边的无向图,\(K\) 次询问保 ...
- [BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 2177 Solved: 834 ...
- bzoj3514 Codechef MARCH14 GERALD07加强版 lct预处理+主席树
Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1951 Solved: 746[Submi ...
- BZOJ3514 : Codechef MARCH14 GERALD07加强版
以边编号为权值 用Link-cut Tree维护最大生成树 对于新加的第i条边(u,v) a[i]表示当a[i]这条边加入后连通块个数会减少 若u==v则a[i]=m 若u与v不连通则连上,a[i]= ...
- BZOJ 3514 Codechef MARCH14 GERALD07加强版
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3514 题意:给出一个图m条边.每次询问只加入编号在区间[L,R]之内的边有多少连通 ...
- 【bzoj3514】Codechef MARCH14 GERALD07加强版
hzwer上少有的几道需要建一下模的 要不是有这么几道题 我都觉得lct只会考裸题了呢 题解看hzwer吧 http://hzwer.com/4358.html 唯一蛋疼的就是为了处理0这个呵呵的位置 ...
随机推荐
- ElasticSearch搜索引擎安装配置中文分词器IK插件
近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...
- Python字典操作大全
//2018.11.6 Python字典操作 1.对于python编程里面字典的定义有以下几种方法: >>> a = dict(one=1, two=2, three=3) > ...
- ajax 个人理解 学习笔记
W:Ajax Q:异步网络请求.无刷新请求数据. W:ajax的实现流程如下: Q: 创建XHR对象 调用open()方法,创建请求 调用send()方法,发送请求 捕获请求状态,判断请求结果 获取数 ...
- MySQL三方面优化
第一方面:30种mysql优化sql语句查询的方法1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用 ...
- lintcode166 链表倒数第n个节点
链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 思路:设置两个指针first,second指向head,first指针先向前走n,然后两个指针一起走,first指针走到末 ...
- 小球下落 (Dropping Balls,UVA 679)
题目描述: 题目思路: 1.直接用数组模拟二叉树下落过程 //超时 #include <iostream> #include <cstring> using namespace ...
- LeetCode 109——有序链表转化二叉搜索树
1. 题目 2. 解答 2.1. 方法一 在 LeetCode 108--将有序数组转化为二叉搜索树 中,我们已经实现了将有序数组转化为二叉搜索树.因此,这里,我们可以先遍历一遍链表,将节点的数据存入 ...
- [leetcode-753-Open the Lock]
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...
- 如何做好FAE工作及FAE职位发展
此文较长,是作者对于半导体FAE职业的一些总结,码字不容易,耐心的阅读,欢迎点赞. 曾经认识一位做电源研发的工程师,转行在一家代理商做FAE,做了一年半以后,就提出了离职请求,他老板问他是什么原因,他 ...
- Python函数的内省-Introspection
Python函数可以进行内省-Introspection,查看函数内部的细节,方式就是使用函数的__code__属性. def func(a, b = 2): return a + b >> ...