强制在线不代表不能预处理!

考虑暴力怎么干?

开始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加强版的更多相关文章

  1. 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1288  Solved: 490 ...

  2. BZOJ 3514: Codechef MARCH14 GERALD07加强版( LCT + 主席树 )

    从左到右加边, 假如+的边e形成环, 那么记下这个环上最早加入的边_e, 当且仅当询问区间的左端点> _e加入的时间, e对答案有贡献(脑补一下). 然后一开始是N个连通块, 假如有x条边有贡献 ...

  3. 【LCT+主席树】BZOJ3514 Codechef MARCH14 GERALD07加强版

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 2023  Solved: 778 ...

  4. BZOJ 3514: Codechef MARCH14 GERALD07加强版 [LCT 主席树 kruskal]

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1312  Solved: 501 ...

  5. [BZOJ 3514]Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES)

    [BZOJ3514] Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES) 题意 \(N\) 个点 \(M\) 条边的无向图,\(K\) 次询问保 ...

  6. [BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 2177  Solved: 834 ...

  7. bzoj3514 Codechef MARCH14 GERALD07加强版 lct预处理+主席树

    Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1951  Solved: 746[Submi ...

  8. BZOJ3514 : Codechef MARCH14 GERALD07加强版

    以边编号为权值 用Link-cut Tree维护最大生成树 对于新加的第i条边(u,v) a[i]表示当a[i]这条边加入后连通块个数会减少 若u==v则a[i]=m 若u与v不连通则连上,a[i]= ...

  9. BZOJ 3514 Codechef MARCH14 GERALD07加强版

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3514 题意:给出一个图m条边.每次询问只加入编号在区间[L,R]之内的边有多少连通 ...

  10. 【bzoj3514】Codechef MARCH14 GERALD07加强版

    hzwer上少有的几道需要建一下模的 要不是有这么几道题 我都觉得lct只会考裸题了呢 题解看hzwer吧 http://hzwer.com/4358.html 唯一蛋疼的就是为了处理0这个呵呵的位置 ...

随机推荐

  1. hdu1969Pie(根据体积二分,分馅饼)

    Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  2. QT 标题栏隐藏可拖拽

    这个也是我网上找到了 为了方便,记录一下 void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e); void ...

  3. Linux命令应用大词典-第26章 模块和内核管理

    26.1 lsmod:显示内核中模块的状态 26.2 get_module:查看内核模块详细信息 26.3 modinfo:显示内核模块信息

  4. Python全栈 进阶(进阶内容都在这了)

    原文地址 https://yq.aliyun.com/articles/632754?spm=a2c4e.11155435.0.0.23eb3312feB6dG ................... ...

  5. Redis 数据结构服务器

    Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久 ...

  6. AVL树和伸展树 -数据结构(C语言实现)

    读数据结构与算法分析 AVL树 带有平衡条件的二叉树,通常要求每颗树的左右子树深度差<=1 可以将破坏平衡的插入操作分为四种,最后通过旋转恢复平衡 破坏平衡的插入方式 描述 恢复平衡旋转方式 L ...

  7. java学习笔记-8.对象的容纳

    1.Iterator(迭代器)和Enumeration(枚举类),都是用来遍历集合的,他们都是接口.区别是Enumeration只能读取集合的数据,而Iterator可以对数据进行删除,Iterato ...

  8. 【shell 练习5】编写简单的多级菜单

    一.简单的多级菜单 [root@web129 ~]# cat menu.sh #!/bin/bash #shell菜单演示 function menu() { echo -e `date` cat & ...

  9. springMVC对jsp页面的数据进行校验

    一. 使用注解校验 a) 引入校验依赖包 <dependency> <groupId>javax.validation</groupId> <artifact ...

  10. HDU 4169 Wealthy Family(树形DP)

    Problem Description While studying the history of royal families, you want to know how wealthy each ...