边和询问排序。。。。然后用LCT维护图。。。。
#include <bits/stdc++.h> using namespace std; #define lowbit(x) (x&(-x)) #define mp(x, y) make_pair(x, y) const int maxn = 200005; const int maxm = 500005; const int INF = 0x3f3f3f3f; struct node *null; struct node { int size, rev; pair<int, int> Min, cMin; node *fa, *ch[2]; inline bool d() { return fa->ch[1] == this; } inline bool isroot() { return fa == null || fa->ch[0] != this && fa->ch[1] != this; } inline void setc(node *p, int d) { ch[d] = p; p->fa = this; } inline void pushup() { size = ch[0]->size + ch[1]->size + 1; Min = cMin; if(ch[0] != null) Min = min(Min, ch[0]->Min); if(ch[1] != null) Min = min(Min, ch[1]->Min); } inline void flip() { if(this == null) return; swap(ch[0], ch[1]); rev ^= 1; } inline void pushdown() { if(rev) { ch[0]->flip(); ch[1]->flip(); rev = 0; } } inline void go() { if(!isroot()) fa->go(); pushdown(); } void rot() { node *f = fa, *ff = fa->fa; int c = d(), cc = fa->d(); f->setc(ch[!c], c); this->setc(f, !c); if(ff->ch[cc] == f) ff->setc(this, cc); else this->fa = ff; f->pushup(); } node* splay() { go(); while(!isroot()) { if(!fa->isroot()) d() == fa->d() ? fa->rot() : rot(); rot(); } pushup(); return this; } node* access() { for(node *p = this, *q = null; p != null; q = p, p = p->fa) { p->splay()->setc(q, 1); p->pushup(); } return splay(); } inline void makeroot() { access()->flip(); } node* getroot() { node *x; for(x = access(); x->pushdown(), x->ch[0] != null; x = x->ch[0]); return x; } void cut() { access(); ch[0]->fa = null; ch[0] = null; pushup(); } void cut(node *o) { makeroot(); o->cut(); } void link(node *p) { makeroot(); fa = p; } }; struct qu { int L, R, id; qu(int L = 0, int R = 0, int id = 0) : L(L), R(R), id(id) {} }q[maxn]; node pool[maxm]; node *Node[maxn]; node *E[maxn]; int tree[maxn]; int res[maxn]; int f[maxn]; pair<int, int> e[maxn]; node *tail; int n, m, m2; int cmp(qu a, qu b) { return a.R < b.R; } void init() { memset(tree, 0, sizeof tree); tail = pool; null = tail++; null->fa = null->ch[0] = null->ch[1] = null; null->size = null->rev = 0, null->Min = null->cMin = mp(INF, INF); for(int i = 1; i <= n; i++) { tail->size = 1; tail->rev = 0; tail->ch[0] = tail->ch[1] = tail->fa = null; tail->Min = tail->cMin = mp(INF, INF); Node[i] = tail++; } for(int i = 1; i <= m; i++) { tail->size = 1; tail->rev = 0; tail->ch[0] = tail->ch[1] = tail->fa = null; tail->Min = tail->cMin = mp(INF, INF); E[i] = tail++; } } pair<int, int> query(node *a, node *b) { a->access(); for(a = null; b != null; a = b, b = b->fa) { b->splay(); if(b->fa == null) return min(b->ch[1]->Min, a->Min); b->setc(a, 1); b->pushup(); } } int find(int u) { return f[u] = f[u] == u ? f[u] : find(f[u]); } void merge(int a, int b) { int aa = f[a], bb = f[b]; f[aa] = bb; } void add(int x, int v) { for(int i = x; i > 0; i -= lowbit(i)) tree[i] += v; } int sum(int x) { int res = 0; for(int i = x; i <= n; i += lowbit(i)) res += tree[i]; return res; } void work() { int u, v; for(int i = 1; i <= m; i++) { scanf("%d%d", &u, &v); if(u < v) swap(u, v); e[i] = mp(u, v); } sort(e+1, e+m+1); for(int i = 1; i <= m2; i++) { scanf("%d%d", &u, &v); if(u > v) swap(u, v); q[i] = qu(u, v, i); } sort(q+1, q+m2+1, cmp); for(int i = 1; i <= n; i++) f[i] = i; for(int i = 1, j = 1; j <= m2; j++) { while(i <= m && e[i].first <= q[j].R) { if(e[i].first == e[i].second) { i++; continue; } if(find(e[i].first) == find(e[i].second)) { pair<int, int> tpair = query(Node[e[i].first], Node[e[i].second]); int t = tpair.second; if(tpair.first <= e[i].second) { E[t]->cut(Node[e[t].first]); E[t]->cut(Node[e[t].second]); add(e[t].second, -1); E[i]->cMin = mp(e[i].second, i); E[i]->link(Node[e[i].first]); E[i]->link(Node[e[i].second]); add(e[i].second, 1); } } else { E[i]->cMin = mp(e[i].second, i); E[i]->link(Node[e[i].first]); E[i]->link(Node[e[i].second]); add(e[i].second, 1); merge(e[i].first, e[i].second); } i++; } res[q[j].id] = n - sum(q[j].L); } for(int i = 1; i <= m2; i++) printf("%d\n", res[i]); } int main() { // freopen("input", "r", stdin); // freopen("output", "w", stdout); while(scanf("%d%d%d", &n, &m, &m2) != EOF) { init(); work(); } return 0; }