牛妹的苹果树【牛客练习赛67 F】【倍增+欧拉序+树的直径】

题目链接


  求区间[l, r]内的点对的最大直径。

  其实,我们可以发现直径有相对应的传递关系的,如果我们已知一棵树上的直径,(u, v),假设又知道另一棵树上的直径,(u', v'),那么新的直径只能通过这四个点两两组和来实现的。

  所以,我们可以用倍增的方法来维护这个连续关系对。

9 1
1 2 1
1 3 2
1 4 3
1 5 5
1 6 6
2 7 7
3 8 4
4 9 2
6 7
ans:14
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 3e5 + 7;
int N, Q;
namespace Graph
{
    int head[maxN], cnt;
    struct Eddge
    {
        int nex, to; ll val;
        Eddge(int a=-1, int b=0, ll c=0):nex(a), to(b), val(c) {}
    } edge[maxN << 1];
    inline void addEddge(int u, int v, ll w)
    {
        edge[cnt] = Eddge(head[u], v, w);
        head[u] = cnt++;
    }
    inline void _add(int u, int v, ll w) { addEddge(u, v, w); addEddge(v, u, w); }
    inline void init()
    {
        cnt = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
    }
};
using namespace Graph;
namespace Grand_Father
{
    int deep[maxN], euler[maxN << 1], Esiz, rid[maxN], LOG_2[maxN << 1];
    ll dis[maxN];
    void dfs(int u, int fa)
    {
        deep[u] = deep[fa] + 1; rid[u] = Esiz + 1;
        for(int i=head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(v == fa) continue;
            euler[++Esiz] = u;
            dis[v] = dis[u] + edge[i].val;
            dfs(v, u);
        }
        euler[++Esiz] = u;
    }
    int mn[maxN << 1][20];
    inline void RMQ_Init()
    {
        for(int i=2; i<=(N << 1); i++) LOG_2[i] = LOG_2[i >> 1] + 1;
        dis[1] = 0;
        dfs(1, 0);
        for(int i=1; i<=Esiz; i++) mn[i][0] = euler[i];
        for(int j=1; (1 << j) <= Esiz; j++)
        {
            for(int i=1; i + (1 << j) - 1 <= Esiz; i++)
            {
                if(deep[mn[i][j - 1]] < deep[mn[i + (1 << (j - 1))][j - 1]]) mn[i][j] = mn[i][j - 1];
                else mn[i][j] = mn[i + (1 << (j - 1))][j - 1];
            }
        }
    }
    inline int Rmq(int l, int r)
    {
        int det = r - l + 1, kk = LOG_2[det];
        if(deep[mn[l][kk]] <= deep[mn[r - (1 << kk) + 1][kk]]) return mn[l][kk];
        else return mn[r - (1 << kk) + 1][kk];
    }
    inline int _LCA(int u, int v)
    {
        int l = rid[u], r = rid[v];
        if(l > r) swap(l, r);
        return Rmq(l, r);
    }
    inline ll _Dis(int u, int v)
    {
        int lca = _LCA(u, v);
        return dis[u] + dis[v] - 2 * dis[lca];
    }
};
using namespace Grand_Father;
#define MP(a, b) make_pair(a, b)
#define Max_3(a, b, c) max(a, max(b, c))
#define pii pair
pair ans[maxN][20];
bool cmp (pii e1, pii e2) { return _Dis(e1.first, e1.second) < _Dis(e2.first, e2.second); }
inline pii Big_one(pii x, pii y)
{
    pii ans;
    ans = max(x, y, cmp);
    ans = max(ans, MP(x.first, y.first), cmp);
    ans = max(ans, MP(x.first, y.second), cmp);
    ans = max(ans, MP(x.second, y.first), cmp);
    ans = max(ans, MP(x.second, y.second), cmp);
    return ans;
}
inline void Solve()
{
    for(int i=1; i<=N; i++) ans[i][0] = MP(i, i);
    pii a1, a2;
    for(int j=1; (1 << j) <= N; j++)
    {
        for(int i=1; i + (1 << j) - 1 <= N; i++)
        {
            a1 = ans[i][j - 1];
            a2 = ans[i + (1 << (j - 1))][j - 1];
            ans[i][j] = Big_one(a1, a2);
        }
    }
}
inline ll query(int l, int r)
{
    int det = r - l + 1, kk = LOG_2[det];
    pii s = Big_one(ans[l][kk], ans[r - (1 << kk) + 1][kk]);
    return _Dis(s.first, s.second);
}
int main()
{
    scanf("%d%d", &N, &Q);
    init();
    for(int i=1, u, v, w; i

 

你可能感兴趣的:(欧拉序,树的直径,欧拉序,倍增)