码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 3514 Codechef MARCH14 GERALD07加强版 LCT+主席树

时间:2014-12-03 12:29:52      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:bzoj   codechef   link-cut-tree   主席树   可持久化线段树   

题目大意:N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数。


思路:看到了wulala的题解,这里就直接粘过来了。

葱娘说这是一个很巧妙的题。。
有一个比较猎奇的做法:首先把边依次加到图中,若当前这条边与图中的边形成了环,那么把这个环中最早加进来的边弹出去
并将每条边把哪条边弹了出去记录下来:ntr[i] = j,特别地,要是没有弹出边,ntr[i] = 0;
这个显然是可以用LCT来弄的对吧。
然后对于每个询问,我们的答案就是对l~r中ntr小于l的边求和,并用n减去这个值
正确性可以YY一下:
如果一条边的ntr >= l,那么显然他可以与从l ~ r中的边形成环,那么它对答案没有贡献
反之如果一条边的ntr < l那么它与从l ~ r中的边是不能形成环的,那么他对答案的贡献为-1
对于查询从l ~ r中有多少边的ntr小于l,我反正是用的函数式线段树

看了题解才会做的人伤不起啊。。。

做法已经很详细了,就直接贴代码了。


CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAX 200010
#define RANGE 7000010
using namespace std;
 
struct Edge{
    int x,y;
}edge[MAX];
 
struct SplayTree{
    SplayTree *son[2],*father;
    int val,_min;
    bool reverse;
     
    SplayTree(int _);
    bool Check() {
        return father->son[1] == this;
    }
    void Reverse() {
        reverse ^= 1;
        swap(son[0],son[1]);
    }
    void PushDown() {
        if(father->son[0] == this || father->son[1] == this)
            father->PushDown();
        if(reverse) {
            son[0]->Reverse();
            son[1]->Reverse();
            reverse = false;
        }
    }
    void PushUp() {
        _min = min(son[0]->_min,son[1]->_min);
        _min = min(_min,val);
    }
}none(INF),*nil = &none,*tree[MAX << 1];
SplayTree:: SplayTree(int _) {
    val = _min = _;
    son[0] = son[1] = father = nil;
    reverse = false;
}
 
int points,edges,asks;
int type,last_ans;
int ntr[MAX];
 
inline void Rotate(SplayTree *a,bool dir)
{
    SplayTree *f = a->father;
    f->PushDown(),a->PushDown();
    f->son[!dir] = a->son[dir];
    f->son[!dir]->father = f;
    a->son[dir] = f;
    if(f->father->son[0] == f || f->father->son[1] == f)
        f->father->son[f->Check()] = a;
    a->father = f->father;
    f->father = a;
    f->PushUp();
}
 
inline void Splay(SplayTree *a)
{
    a->PushDown();
    while(a->father->son[0] == a || a->father->son[1] == a) {
        SplayTree *f = a->father;
        if(f->father->son[0] != f && f->father->son[1] != f)
            Rotate(a,!a->Check());
        else if(!f->Check()) {
            if(!a->Check())  
                Rotate(a->father,true),Rotate(a,true);
            else    Rotate(a,false),Rotate(a,true);
        }
        else {
            if(a->Check())
                Rotate(a->father,false),Rotate(a,false);
            else    Rotate(a,true),Rotate(a,false);
        }
    }
    a->PushUp();
}
 
inline void Access(SplayTree *a)
{
    SplayTree *temp = nil;
    while(a != nil) {
        Splay(a);
        a->son[1] = temp;
        a->PushUp();
        temp = a;
        a = a->father;
    }
}
 
inline SplayTree *FindRoot(SplayTree *a)
{
    while(a->father != nil)
        a = a->father;
    return a;
}
 
inline void ToRoot(SplayTree *a)
{
    Access(a);
    Splay(a);
    a->Reverse();
}
 
inline void Link(SplayTree *x,SplayTree *y)
{
    ToRoot(y);
    y->father = x;
}
 
inline void Cut(SplayTree *x,SplayTree *y)
{
    ToRoot(x);
    Access(y);
    Splay(y);
    x->father = nil;
    y->son[0] = nil;
    y->PushUp();
}
 
inline void Insert(const Edge &e,int p)
{
    if(e.x == e.y) {
        ntr[p] = p;
        return ;
    }
    SplayTree *fx = FindRoot(tree[e.x]),*fy = FindRoot(tree[e.y]);
    if(fx == fy) {
        ToRoot(tree[e.x]);
        Access(tree[e.y]);
        Splay(tree[e.y]);
        int _min = tree[e.y]->_min;
        ntr[p] = _min;
        Cut(tree[edge[_min].x],tree[points + _min]);
        Cut(tree[edge[_min].y],tree[points + _min]);
    }
    tree[points + p] = new SplayTree(p);
    Link(tree[points + p],tree[e.x]);
    Link(tree[points + p],tree[e.y]);
}
 
struct SegTree{
    SegTree *son[2];
    int cnt;
     
    void *operator new(size_t,SegTree *_,SegTree *__,int ___);
}*seg_tree[MAX],mempool[RANGE],*C = mempool;
void *SegTree:: operator new(size_t,SegTree *_,SegTree *__,int ___) {
    C->son[0] = _;
    C->son[1] = __;
    C->cnt = ___;
    return C++;
}
 
SegTree *Modify(SegTree *last,int l,int r,int x)
{
    if(l == r)
        return new(NULL,NULL,last->cnt + 1)SegTree;
    int mid = (l + r) >> 1;
    if(x <= mid) return new(Modify(last->son[0],l,mid,x),last->son[1],last->cnt + 1)SegTree;
    return new(last->son[0],Modify(last->son[1],mid + 1,r,x),last->cnt + 1)SegTree;
}
 
int Ask(SegTree *contrast,SegTree *now,int l,int r,int x,int y)
{
    if(l == x && y == r)
        return now->cnt - contrast->cnt;
    int mid = (l + r) >> 1;
    if(y <= mid) return Ask(contrast->son[0],now->son[0],l,mid,x,y);
    if(x > mid)      return Ask(contrast->son[1],now->son[1],mid + 1,r,x,y);
    int left = Ask(contrast->son[0],now->son[0],l,mid,x,mid);
    int right = Ask(contrast->son[1],now->son[1],mid + 1,r,mid + 1,y);
    return left + right;
}
 
int main()
{
    cin >> points >> edges >> asks >> type;
    for(int i = 1; i <= points; ++i)
        tree[i] = new SplayTree(INF);
    for(int i = 1; i <= edges; ++i) {
        scanf("%d%d",&edge[i].x,&edge[i].y);
        Insert(edge[i],i);
    }
    seg_tree[0] = new(C,C,0)SegTree;
    for(int i = 1; i <= edges; ++i)
        seg_tree[i] = Modify(seg_tree[i - 1],0,edges,ntr[i]);
    for(int x,y,i = 1; i <= asks; ++i) {
        scanf("%d%d",&x,&y);
        if(type)    x ^= last_ans,y ^= last_ans;
        if(x > y)    swap(x,y);
        last_ans = points - Ask(seg_tree[x - 1],seg_tree[y],0,edges,0,x - 1);
        printf("%d\n",last_ans);
    }
    return 0;
}


BZOJ 3514 Codechef MARCH14 GERALD07加强版 LCT+主席树

标签:bzoj   codechef   link-cut-tree   主席树   可持久化线段树   

原文地址:http://blog.csdn.net/jiangyuze831/article/details/41694643

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!