1. 程式人生 > >[Luogu] 天天愛跑步

[Luogu] 天天愛跑步

sin clu clas mem onclick AS work tchar ide

https://www.luogu.org/problemnew/show/P1600

亂寫的暴力,這道題暴力寫個60還是比較簡單的

技術分享圖片
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <cstring>

using namespace std;
const int N = 600001;
const int oo = 99999999
; #define lson jd << 1 #define rson jd << 1 | 1 #define yxy getchar() #define one_ n <= 993 #define two_ n == 99994 #define three_ n == 99995 int head[N], pre[N], dis[N], tim[N], Answer[N], Askl[N], Askr[N]; bool vis[N]; int n, m, now = 1; struct Node { int u, v, w, nxt; } G[N]; queue
<int> Q; vector <int> Vt[N]; int L[N << 2], R[N << 2], W[N << 2], F[N << 2]; inline int read() { int x = 0; char c = yxy; while(c < 0 || c > 9) c = yxy; while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = yxy; return
x; } inline void add(int u, int v) { G[now].v = v; G[now].nxt = head[u]; head[u] = now ++; } inline void spfa(int start,int endd) { for(int i = 1; i <= n; i ++) dis[i] = oo, vis[i] = 0; dis[start] = 0; Q.push(start); while(!Q.empty()) { int topp = Q.front(); Q.pop(); vis[topp] = 0; for(int i = head[topp]; ~ i; i = G[i].nxt) { if(dis[G[i].v] > dis[topp] + 1) { dis[G[i].v] = dis[topp] + 1; pre[G[i].v] = topp; if(!vis[G[i].v]) { vis[G[i].v] = 1; Q.push(G[i].v); } } } } } void calc(int start, int endd, int diss) { int js = -1; pre[start] = 0; while(endd) { js ++; if(tim[endd] == diss - js) Answer[endd] ++; endd = pre[endd]; } } void work_1() { for(int i = 1; i <= m; i ++) { spfa(Askl[i], Askr[i]); calc(Askl[i], Askr[i], dis[Askr[i]]); } } void work_2() { for(int i = 1; i <= m; i ++) Vt[Askl[i]].push_back(Askr[i]); for(int i = 1; i <= n; i ++) { int L_ = i - tim[i], R_ = i + tim[i]; int siz_ = Vt[L_].size(); if(L_ > 0) for(int j = 0; j < siz_; j ++) if(Vt[L_][j] >= i) Answer[i] ++; siz_ = Vt[R_].size(); if(R_ <= n) for(int j = 0; j < siz_; j ++) if(Vt[R_][j] <= i) Answer[i] ++; } } int bef[N], top[N], deep[N], size[N], fa[N], son[N], tree[N], spjs; int cnt[N]; void Dfs_3(int u, int f_, int dep) { fa[u] = f_, deep[u] = dep; size[u] = 1; for(int i = head[u]; ~ i; i = G[i].nxt) { int v = G[i].v; if(v != f_) { dis[v] = dis[u] + 1; Dfs_3(v, u, dep + 1); size[u] += size[v]; cnt[u] += cnt[v]; } } } inline void work_3() { memset(dis, 0, sizeof dis); for(int i = 1; i <= m; i ++) cnt[Askr[i]] ++; Dfs_3(1, 0, 0); for(int i = 1; i <= n; i ++) if(deep[i] == tim[i]) Answer[i] += cnt[i]; } int main() { n = read(); m = read(); for(int i = 1; i <= n; i ++) head[i] = -1; for(int i = 1; i <= n - 1; i ++) { int u = read(); int v = read(); add(u, v); add(v, u); } for(int i = 1; i <= n; i ++) tim[i] = read(); for(int i = 1; i <= m; i ++) Askl[i] = read(), Askr[i] = read(); if(one_) work_1(); else if(two_) work_2(); else if(three_) work_3(); //起點 == 1 for(int i = 1; i <= n; i ++) printf("%d ", Answer[i]); return 0; }
View Code

前置知識

Lca + 線段樹 + 差分 + 樹剖

考慮把一條路徑拆成兩段(這是非常常見的解決樹上問題的方法)

分別拆成 S - L 和 L - T (起點 - Lca, Lca - 終點)

這樣就可以得到當滿足

deep[s] - deep[i] = wat[i] => deep[s] = wat[i] + deep[i];

deep[s] + deep[i] - 2 * deep[Lca(s, i)] = wat[i] => deep[s] - 2 * deep[Lca(s, i)] = wat[i] - deep[i];

時玩家才會被 i 觀察員看到

發現 上面兩個式子滿足等式右邊都是定值

因此我們可以 以深度建立線段樹(動態開節點)

查詢時就應該查詢該節點所對應的深度的線段樹的區間lst[] 和 rst[] 之間的總的權值

lst[i] 表示以該節點為子樹的根中樹上編號的下界, 同理rst[]為上界(涉及到DFS序 && 樹剖的知識).

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int N = 3e5 + 10;

#define gc getchar()

int n, m, wat[N];
int Askl[N], Askr[N], Lca[N];

int now = 1, head[N];
struct Node {int v, nxt;} G[N << 1];

inline int read() {
    int x = 0; char c = gc;
    while(c < 0 || c > 9) c = gc;
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = gc;
    return x;
}

inline void Add(int u, int v) {G[now].v = v; G[now].nxt = head[u]; head[u] = now ++;}

int fa[N], deep[N], top[N], size[N], son[N], lst[N], rst[N], tree[N], Spjs;

void Dfs_1(int u, int f_, int dep) {
    fa[u] = f_; deep[u] = dep; size[u] = 1;
    for(int i = head[u]; ~ i; i = G[i].nxt) {
        int v = G[i].v;
        if(v != f_) {
            Dfs_1(v, u, dep + 1);
            size[u] += size[v];
            if(size[son[u]] < size[v]) son[u] = v;
        }
    }
}

void Dfs_2(int u, int tp) {
    top[u] = tp;
    lst[u] = ++ Spjs;
    tree[u] = Spjs;
    if(! son[u]) {rst[u] = Spjs; return ;}
    Dfs_2(son[u], tp);
    for(int i = head[u]; ~ i; i = G[i].nxt) {
        int v = G[i].v;
        if(v != fa[u] && v != son[u]) Dfs_2(v, v);
    }
    rst[u] = Spjs;
}

inline int Ask_Lca(int x, int y) {
    int tp1 = top[x], tp2 = top[y];
    while(tp1 != tp2) {
        if(deep[tp1] < deep[tp2]) swap(x, y), swap(tp1, tp2);
        x = fa[tp1];
        tp1 = top[x];
    }
    return deep[x] < deep[y] ? x : y;
}

int root[N * 3], lson[N * 25], rson[N * 25], W[N * 25], tot, cnt;

void Build_G(int l, int r, int & jd, int x, int yj) {
    if(!x) return ;
    if(!jd) jd = ++ tot;
    W[jd] += yj;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    if(x <= mid) Build_G(l, mid, lson[jd], x, yj);
    else Build_G(mid + 1, r, rson[jd], x, yj);
}

int Sec_A(int jd, int l, int r, int x, int y) {
    if(! jd) return 0;
    if(x <= l && r <= y) return W[jd];
    int mid = (l + r) >> 1;
    if(y <= mid) return Sec_A(lson[jd], l, mid, x, y);
    else if(x > mid) return Sec_A(rson[jd], mid + 1, r, x, y);
    else return Sec_A(lson[jd], l, mid, x, y) + Sec_A(rson[jd], mid + 1, r, x, y);
}

void Clear() {
    tot = 0;
    memset(lson, 0, sizeof lson);
    memset(rson, 0, sizeof rson);
    memset(W, 0, sizeof W);
    memset(root, 0, sizeof root);
}

int Answer[N];

int main() {
    n = read(); m = read();
    for(int i = 1; i <= n; i ++) head[i] = -1;
    for(int i = 1; i <= n - 1; i ++) {
        int u = read(), v = read();
        Add(u, v); Add(v, u);
    }
    for(int i = 1; i <= n; i ++) wat[i] = read();
    for(int i = 1; i <= m; i ++) Askl[i] = read(), Askr[i] = read(); 
    Dfs_1(1, 0, 0);
    Dfs_2(1, 1);
    for(int i = 1; i <= m; i ++) Lca[i] = Ask_Lca(Askl[i], Askr[i]);
    int dep;
    for(int i = 1; i <= m; i ++) {
        dep = deep[Askl[i]];
        Build_G(1, n, root[dep], tree[Askl[i]], 1);
        Build_G(1, n, root[dep], tree[fa[Lca[i]]], -1);
    }
    for(int i = 1; i <= n; i ++) 
        Answer[i] = Sec_A(root[deep[i] + wat[i]], 1, n, lst[i], rst[i]);
    Clear();
    for(int i = 1; i <= m; i ++) {
        dep = deep[Askl[i]] - deep[Lca[i]] * 2 + n * 2;
        Build_G(1, n, root[dep], tree[Askr[i]], 1);
        Build_G(1, n, root[dep], tree[Lca[i]], -1);
    }
    for(int i = 1; i <= n; i ++)
        Answer[i] += Sec_A(root[wat[i] - deep[i] + n * 2], 1, n, lst[i], rst[i]);
    for(int i = 1; i <= n; i ++) 
        cout << Answer[i] << " ";
    return 0;
}

[Luogu] 天天愛跑步