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

题目大意: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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 有一个 $n$ 个点的棋盘,每个点上有一个数字 $a_i$,你需要从 $(1,1)$ 走到 $(n,n)$,每次只能往右或往下走,每个格子只能经过一次,路径上的数字和为 $S$。定义一个点 $(x,y)$ 的权值为 $a_x+a_y$,求所有满足条件的路径中,所有点的权值和的最小值。 输入格式 第一行一个整数 $n$。 接下来 $n$ 行,每行 $n$ 个整数,表示棋盘上每个点的数字。 输出格式 输出一个整数,表示所有满足条件的路径中,所有点的权值和的最小值。 数据范围 $1\leq n\leq 300$ 输入样例 3 1 2 3 4 5 6 7 8 9 输出样例 25 算法1 (树形dp) $O(n^3)$ 我们可以先将所有点的权值求出来,然后将其看作是一个有权值的图,问题就转化为了在这个图中求从 $(1,1)$ 到 $(n,n)$ 的所有路径中,所有点的权值和的最小值。 我们可以使用树形dp来解决这个问题,具体来说,我们可以将这个图看作是一棵树,每个点的父节点是它的前驱或者后继,然后我们从根节点开始,依次向下遍历,对于每个节点,我们可以考虑它的两个儿子,如果它的两个儿子都被遍历过了,那么我们就可以计算出从它的左儿子到它的右儿子的路径中,所有点的权值和的最小值,然后再将这个值加上当前节点的权值,就可以得到从根节点到当前节点的路径中,所有点的权值和的最小值。 时间复杂度 树形dp的时间复杂度是 $O(n^3)$。 C++ 代码 算法2 (动态规划) $O(n^3)$ 我们可以使用动态规划来解决这个问题,具体来说,我们可以定义 $f(i,j,s)$ 表示从 $(1,1)$ 到 $(i,j)$ 的所有路径中,所有点的权值和为 $s$ 的最小值,那么我们就可以得到如下的状态转移方程: $$ f(i,j,s)=\min\{f(i-1,j,s-a_{i,j}),f(i,j-1,s-a_{i,j})\} $$ 其中 $a_{i,j}$ 表示点 $(i,j)$ 的权值。 时间复杂度 动态规划的时间复杂度是 $O(n^3)$。 C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值