hzwer上少有的几道需要建一下模的 要不是有这么几道题 我都觉得lct只会考裸题了呢

题解看hzwer吧 http://hzwer.com/4358.html

唯一蛋疼的就是为了处理0这个呵呵的位置,和严格小于,我把ntr数组全部+2,然后l+1,这样建树的时候就要写m+2了= =好蛋疼

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cctype> using namespace std; const int Maxn=;
const int logn=; int a[Maxn],b[Maxn],ntr[Maxn];
int n,m,k,type; template<typename Q> void gt(Q&x) {
static char c;
static bool f;
for(f=;c=getchar(),!isdigit(c);) if(c=='-') f=;
for(x=;isdigit(c);c=getchar()) x=x*+c-'';
f && (x=-x);
} struct PST {
int seq[Maxn]; struct Node{
Node *l,*r;
int sz;
}pool[Maxn*logn],*pis,*root[Maxn],*null; void build(Node*&o,Node *p,int l,int r,int rk) {
o=pis++;
*o=*p;
o->sz++;
if(l==r) return;
int mid=(l+r)>>;
if(rk<=mid) build(o->l,p->l,l,mid,rk);
else build(o->r,p->r,mid+,r,rk);
} void init() {
pis=pool;
memcpy(seq,ntr,sizeof seq);
sort(seq+,seq+m+);
null=pis++;
null->l=null->r=null;
null->sz=;
root[]=null;
for(int i=;i<=m;i++) {
build(root[i],root[i-],,m+,ntr[i]);
}
} int query(Node*L,Node*R,int l,int r,int lft,int rgt) {
if(lft<=l && r<=rgt) return R->sz - L->sz;
int mid=(l+r)>>;
int ans=;
if(lft<=mid) ans+=query(L->l,R->l,l,mid,lft,rgt);
if(mid<rgt) ans+=query(L->r,R->r,mid+,r,lft,rgt);
return ans;
} int query(int l,int r,int x) {
return query(root[l-],root[r],,m+,,x);
}
}pst; int ch[Maxn*][],w[Maxn*],mn[Maxn*],p[Maxn*],flip[Maxn*]; #define l ch[x][0]
#define r ch[x][1]
void update(int x) {
mn[x]=x;
if(w[mn[l]]<w[mn[x]]) mn[x]=mn[l];
if(w[mn[r]]<w[mn[x]]) mn[x]=mn[r];
} void down(int x) {
if(flip[x]) {
swap(l,r);
flip[l]^=;
flip[r]^=;
flip[x]^=;
}
}
#undef l
#undef r bool isroot(int x) {
return ch[p[x]][]!=x && ch[p[x]][]!=x;
} void rotate(int x) {
int y=p[x],z=p[y];
if(!isroot(y)) ch[z][ch[z][]==y]=x;
int l=ch[y][]==x,r=l^;
p[x]=z;
p[y]=x;
p[ch[x][r]]=y; ch[y][l]=ch[x][r];
ch[x][r]=y; update(y);
// update(x);
} void splay(int x) {
static int stk[Maxn],top;
stk[top=]=x;
for(int t=x;!isroot(t);t=p[t]) stk[++top]=p[t];
while(top) down(stk[top--]); for(;!isroot(x);) {
int y=p[x],z=p[y];
if(!isroot(y)) {
if( (ch[y][]==x)^(ch[z][]==y) )rotate(x);
rotate(y);
}
rotate(x);
}
update(x);
} void access(int x) {
for(int t=;x;x=p[t=x]) {
splay(x);
ch[x][]=t;
update(x);
}
}
void newroot(int x) {
access(x);
splay(x);
flip[x]^=;
} void Link(int x,int y) {
newroot(x);
p[x]=y;
} void Cut(int x,int y) {
newroot(y);
access(x);
splay(x);
if(ch[x][]==y) {
ch[x][]=;
p[y]=;
update(x);
}
} int getroot(int x) {
for(access(x),splay(x);ch[x][];x=ch[x][]);
return x;
} const int INF=0x3f3f3f3f; void init() {
scanf("%d%d%d%d",&n,&m,&k,&type);
for(int i=;i<=n;i++) {
w[i]=INF;
mn[i]=i;
}
for(int x,y,i=;i<=m;i++) {
// scanf("%d%d",a+i,b+i);
gt(a[i]),gt(b[i]);
x=a[i],y=b[i];
if(x==y) {
ntr[i]=i;
continue;
}
if(getroot(x)==getroot(y)) {
newroot(y);
access(x);
splay(x);
int t=mn[x];
ntr[i]=w[t];
Cut(t,a[w[t]]);
// Cut(t,b[w[t]]);
}
mn[i+n]=i+n;
w[i+n]=i;
Link(x,i+n);
Link(y,i+n);
}
for(int i=;i<=m;i++) ntr[i]+=;
pst.init();
} void work() {
for(int l,r,last_ans=;k--;) {
// scanf("%d%d",&l,&r);
gt(l),gt(r);
if(type) l^=last_ans,r^=last_ans; last_ans=n-pst.query(l,r,l+);
printf("%d\n",last_ans);
}
} void PST_test() {
scanf("%d",&m);
for(int i=;i<=m;i++) {
scanf("%d",ntr+i);
}
for(int i=;i<=m;i++) ntr[i]+=;
pst.init();
for(scanf("%d",&k);k--;) {
int x,y,w;
scanf("%d%d%d",&x,&y,&w);w++;
printf("%d\n",pst.query(x,y,w));
}
} int main() {
#ifdef DEBUG
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif // PST_test();return 0; init();
work(); return ;
}

【bzoj3514】Codechef MARCH14 GERALD07加强版的更多相关文章

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

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

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

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

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

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

  4. BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3514 题意概括 N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. N ...

  5. BZOJ3514 : Codechef MARCH14 GERALD07加强版

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

  6. 沉迷Link-Cut tree无法自拔之:[BZOJ3514] Codechef MARCH14 GERALD07 加强版

    来自蒟蒻 \(Hero \_of \_Someone\) 的 \(LCT\) 学习笔记 $ $ 又是一道骚题...... 先讲一个结论: 假设我们用 \(LCT\) 来做这道题, 在插入边 \(i\) ...

  7. BZOJ3514: Codechef MARCH14 GERALD07加强版【LCT】【主席树】【思维】

    Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密. 接下来 ...

  8. BZOJ3514: Codechef MARCH14 GERALD07加强版(LCT,主席树)

    Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密.接下来M ...

  9. BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT+可持久化线段树

    自己独自想出来并切掉还是很开心的~ Code: #include <bits/stdc++.h> #define N 400005 #define inf 1000000000 #defi ...

  10. BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT维护最大生成树 主席树

    题面 考虑没有询问,直接给你一个图问联通块怎么做. 并查集是吧. 现在想要动态地做,那么应该要用LCT. 考虑新加进来一条边,想要让它能够减少一个联通块的条件就是现在边的两个端点还没有联通. 如果联通 ...

随机推荐

  1. SpringMVC4+thymeleaf3的一个简单实例(篇一:基本环境)

    首语:用SpringMVC和thymeleaf实现一个简单的应用,包括基本环境搭建,SpringMVC4和thymeleaf3的整合,页面参数的获取,页面参数验证,以及用MySQL保存数据.我会把步骤 ...

  2. Visual Studio vs2010 去掉中文注释红色下划线;去掉代码红色下划线;

    vs去掉下挂线也分两种: 1.去掉中文注释红色下划线,需要去掉VisualAssist下划线鸡肋功能: 1.选择Visual AssistX Options: 2.把如图所示的勾去掉,解决. 以后再次 ...

  3. 【BZOJ3673】【可持久化并查集】可持久化并查集 by zky

    Description n个集合 m个操作操作:1 a b 合并a,b所在集合2 k 回到第k次操作之后的状态(查询算作操作)3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0<n ...

  4. visualSVN server库迁移(转)

    转自:http://blog.csdn.net/yuhuijun_1/article/details/9762683 首先,VisualSVN Server Manager,包含两个路径,一个是安装路 ...

  5. C# 控制台程序设置字体颜色

    这几天做了个程序,程序本身很简单.大体功能是输入查询条件,从数据库里取出结果计算并显示.但是用户的要求是使用控制台(console)来实现功能.由于功能简单,程序很快就做完了,在面向用户演示程序时,突 ...

  6. for语句嵌套使用 实现9*9乘法表

         这个实例主要考察对for循环语句的使用,出现递增规律的乘法表. 开发环境      开发工具:Microsoft Visual Studio2010 旗舰版 具体步骤      先是制作一个 ...

  7. js 的其它运算符和优先级

    三元运算符: 语法为 exp1? exp2:exp3 判断 exp1是true 和 false  如果true,则返回exp2  ,如果false ,则返回exp3 <script> if ...

  8. JQUERY1.9学习笔记 之可见性过滤器(一) 隐藏选择器

    描述:选择所有隐藏的元素. jQuery( ":hidden" ) 例:显示出所有隐藏的div元素,并对隐藏的input元素计数. <!doctype html>< ...

  9. 更换Python默认软件镜像源

    限于一些众所周知的原因,在我们pip安装软件的时候出现类似报错: data = self.read(amt=amt, decode_content=decode_content) File " ...

  10. ETL控件学习之一从数据库导出数据到平面

    今天主要进行ETL控件的学习.主要是使用微软的SSDT工具.使用DataFlowTask 将数据源导出到目标文件的方式. 1.打开SSDT新建一个SSIS的project,如下图所示: 2.在SSIS ...