思路:

//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
const int N=;
int n,m,k,type,ch[N][],fa[N],minn[N],rev[N],q[N],top,pre[N];
int root[N],tree[N*],lson[N*],rson[N*],cnt,xx,yy,ans;
struct Road{int x,y,wei;}road[N];
bool isroot(int p){return ch[fa[p]][]!=p&&ch[fa[p]][]!=p;}
void push_up(int p){
minn[p]=p;
if(road[minn[ch[p][]]].wei<road[minn[p]].wei)minn[p]=minn[ch[p][]];
if(road[minn[ch[p][]]].wei<road[minn[p]].wei)minn[p]=minn[ch[p][]];
}
void push_down(int p){rev[ch[p][]]^=,rev[ch[p][]]^=,rev[p]=,swap(ch[p][],ch[p][]);}
void rotate(int p){
int q=fa[p],y=fa[q],x=(ch[q][]==p);
ch[q][x]=ch[p][!x],fa[ch[q][x]]=q;
ch[p][!x]=q,fa[p]=y;
if(!isroot(q)){
if(ch[y][]==q)ch[y][]=p;
if(ch[y][]==q)ch[y][]=p;
}fa[q]=p;push_up(q);
}
void splay(int x){
q[++top]=x;
for(int i=x;!isroot(i);i=fa[i])q[++top]=fa[i];
while(top){if(rev[q[top]])push_down(q[top]);top--;}
for(int y=fa[x];!isroot(x);rotate(x),y=fa[x])if(!isroot(y)){
if((ch[y][]==x)^(ch[fa[y]][]==y))rotate(x);
else rotate(y);
}push_up(x);
}
void access(int x){for(int t=;x;t=x,x=fa[x])splay(x),ch[x][]=t,push_up(x);}
void makeroot(int x){access(x),splay(x),rev[x]^=;}
bool connected(int x,int y){while(fa[x])x=fa[x];while(fa[y])y=fa[y];return x==y;}
void link(int x,int y){makeroot(x),fa[x]=y;}
void split(int x,int y){makeroot(x),access(y),splay(y);}
void cut(int x,int y){split(x,y),ch[y][]=fa[x]=;push_up(y);}
void insert(int l,int r,int &pos,int last,int wei){
pos=++cnt,tree[pos]=tree[last]+;
if(l==r)return;
int mid=(l+r)>>;
if(mid<wei)lson[pos]=lson[last],insert(mid+,r,rson[pos],rson[last],wei);
else rson[pos]=rson[last],insert(l,mid,lson[pos],lson[last],wei);
}
int query(int l,int r,int pos,int last,int L,int R){
if(l>=L&&r<=R)return tree[pos]-tree[last];
int mid=(l+r)>>;
if(mid<L)return query(mid+,r,rson[pos],rson[last],L,R);
else if(mid>=R)return query(l,mid,lson[pos],lson[last],L,R);
else return query(l,mid,lson[pos],lson[last],L,R)+query(mid+,r,rson[pos],rson[last],L,R);
}
int main(){
scanf("%d%d%d%d",&n,&m,&k,&type);
for(int i=;i<=n;i++)road[i].wei=0x3f3f3f3f;
for(int i=n+;i<=n+m;i++)scanf("%d%d",&road[i].x,&road[i].y),road[i].wei=i;
for(int i=n+;i<=n+m;i++){
if(connected(road[i].x,road[i].y)){
if(road[i].x==road[i].y){pre[i-n]=i-n;continue;}
split(road[i].x,road[i].y);
int t=minn[road[i].y];
pre[i-n]=road[t].wei-n;
cut(road[t].x,t),cut(road[t].y,t);
}
link(road[i].x,i),link(road[i].y,i);
}
for(int i=;i<=m;i++)insert(,m,root[i],root[i-],pre[i]);
while(k--){
scanf("%d%d",&xx,&yy);
if(type)xx^=ans,yy^=ans;
printf("%d\n",ans=n-query(,m,root[yy],root[xx-],,xx-));
}
}

最新文章

  1. html_随笔
  2. 数据库的修改和删除;比较标签代替&lt;,&gt;,=号;模板替换;session的用法
  3. mysql sql语句大全
  4. DataTable是否存在某个列的判断
  5. nginx常用命令
  6. VS2010调用Com组件
  7. CCPC总结
  8. HDU4289 Control 最大流
  9. 用C# sqlserver实现增删改查
  10. IOS学习之路二十(程序json转换数据的中文字符问题解决)
  11. C# 连接 Access 数据库
  12. c++ 定义宏常量
  13. 精益IT组织与分享式领导
  14. 【STL】c++ priority_queue的使用方法
  15. @ExceptionHandler异常统一处理
  16. Spring Boot程序的执行流程
  17. Node.js作web服务器总结
  18. [2017BUAA软工助教]个人项目准备工作
  19. Spring+thymeleaf
  20. LZW算法PHP实现方法 lzw_decompress php

热门文章

  1. CCF201503-2 数字排序 java(100分)
  2. python3 的 zip
  3. 解决CUDA程序的黑屏恢复问题
  4. Java Web学习总结(31)——全站HTTPS化SSL免费证书使用
  5. cmd界面中断一个程序快捷键 ctrl+c
  6. codevs2370 小机房的树
  7. CODEVS1222 信与信封问题 (匈牙利算法)
  8. BAT经典面试题,深入理解Java内存模型JMM
  9. SVM学习(续)核函数 &amp; 松弛变量和惩罚因子
  10. 【python】蛋疼的中文乱码解决方案