樹鏈剖分 題目

SPOJ - QTREE3 Query on a tree again!

Description

You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N. In the start, the color of any node in the tree is white.node

We will ask you to perfrom some instructions of the following form:c++

  • 0 i : change the color of the i-th node (from white to black, or from black to white);
    or
  • 1 v : ask for the id of the first black node on the path from node 1 to node v. if it doesn't exist, you may return -1 as its result.

Input

In the first line there are two integers N and Q.數組

In the next N-1 lines describe the edges in the tree: a line with two integers a bdenotes an edge between a and b.測試

The next Q lines contain instructions "0 i" or "1 v" (1 ≤ i, v ≤ N).ui

Output

For each "1 v" operation, write one integer representing its result.spa

Example

Input:

9 8
1 2
1 3
2 4
2 9
5 9
7 9
8 9
6 8
1 3
0 8
1 6
1 7
0 2
1 9
0 2
1 9

Output:

-1
8
-1
2
-1

Constraints & Limits

There are 12 real input files.code

For 1/3 of the test cases, N=5000, Q=400000.orm

For 1/3 of the test cases, N=10000, Q=300000.ip

For 1/3 of the test cases, N=100000, Q=100000.input

題解

樹鏈剖分後,用線段樹或者樹狀數組維護區間和,找距離1最近的黑點即找區間和大於零的最小的左端點,對於線段樹查詢,咱們優先訪問左兒子便可,對於樹狀數組能夠套二分查詢深度最小的點

代碼

二分+樹狀數組

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
vector<int> G[N];
int n, m;
int fa[N];
int son[N];
int sze[N];
int dep[N];
void dfs1(int u, int f) {
    sze[u] = 1;
    fa[u] = f;
    son[u] = 0;
    dep[u] = dep[f] + 1;
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if (v == f) continue;
        dfs1(v, u);
        sze[u] += sze[v];
        if (sze[v] > sze[son[u]]) son[u] = v;
    }
}
int top[N];
int cnt;
int pos[N];
int mp[N];
void dfs2(int u, int f, int t) {
    top[u] = t;
    pos[u] = ++cnt;
    mp[cnt] = u;
    if (son[u]) dfs2(son[u], u, t);
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if (v == f || v == son[u]) continue;
        dfs2(v, u, v);
    }
}
int a[N];
int d[N];
void update(int x, int v) {
    for (int i = x; i <= n; i += i & (-i)) d[i] += v;
}
int query(int x) {
    int ans = 0;
    for (int i = x; i; i -= i & (-i)) ans += d[i];
    return ans;
}
int calcans(int u) {
    int ans = 0;
    int res = -1;
    while (top[u] != top[1]) {
        if (query(pos[u]) - query(pos[top[u]] - 1) > 0) {
            int l = pos[top[u]], r = pos[u];
            int tl = l;
            while (l <= r) {
                int mid = (l + r) >> 1;
                if (query(mid) - query(tl - 1) > 0) {
                    r = mid - 1;
                    ans = mid;
                }
                else l = mid + 1;
            }
            if (ans) res = mp[ans];
        }
        u = fa[top[u]];
    }
    if (query(pos[u]) - query(pos[1] - 1) > 0) {
        int l = pos[1], r = pos[u];
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (query(mid) > 0) {
                r = mid - 1;
                ans = mid;
            }
            else l = mid + 1;
        }
        if (ans) res = mp[ans];
    }
    return res;
}
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            G[i].clear();
        }
        memset(d, 0, sizeof(d));
        memset(sze, 0, sizeof(sze));
        memset(a, 0, sizeof(a));
        for (int i = 1; i < n; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }

        dep[0] = 0;
        dfs1(1, 0);
        cnt = 0;
        dfs2(1, 0, 1);

        int ch, k;
        for (int i = 1; i <= m; i++) {
            scanf("%d%d", &ch, &k);
            switch(ch) {
                case 1: printf("%d\n", calcans(k)); break;
                case 0: {
                    if (a[k] == 0) {
                        update(pos[k], 1);
                        a[k] = 1;
                    }
                    else {
                        update(pos[k], -1);
                        a[k] = 0;
                    }
                    break;
                }

            }
        }

    return 0;
}

線段樹

#include <bits/stdc++.h>
#define lson (o << 1)
#define rson (o << 1 | 1)
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
vector<int> G[N];
int n, m;
int fa[N];
int son[N];
int sze[N];
int dep[N];
void dfs1(int u, int f) {
    sze[u] = 1;
    fa[u] = f;
    son[u] = 0;
    dep[u] = dep[f] + 1;
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if (v == f) continue;
        dfs1(v, u);
        sze[u] += sze[v];
        if (sze[v] > sze[son[u]]) son[u] = v;
    }
}
int top[N];
int cnt;
int pos[N];
int mp[N];
void dfs2(int u, int f, int t) {
    top[u] = t;
    pos[u] = ++cnt;
    mp[cnt] = u;
    if (son[u]) dfs2(son[u], u, t);
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if (v == f || v == son[u]) continue;
        dfs2(v, u, v);
    }
}
int a[N];
int sumv[N << 2];
void pushup(int o) {
    sumv[o] = sumv[lson] + sumv[rson];
}
void update(int o, int l, int r, int pos) {
    if (l == r) {
        sumv[o] = !sumv[o];
        return;
    }
    int mid = (l + r) >> 1;
    if (pos <= mid) update(lson, l, mid, pos);
    else update(rson, mid + 1, r, pos);
    pushup(o);
}
int query(int o, int l, int r, int ql, int qr) {
    if (sumv[o] == 0) return 0;
    if (l == r) return l;
    int mid = (l + r) >> 1;
    int ans = 0;
    if (ql <= mid) ans = query(lson, l, mid, ql, qr);
    if (ans) return ans;
    if (qr > mid) ans = query(rson, mid + 1, r, ql, qr);
    return ans;
}
int calcans(int u) {
    int ans;
    int res = -1;
    while (1) {
        ans = query(1, 1, n, pos[top[u]], pos[u]);
        if (ans) res = mp[ans];
        if (u == 1 || top[u] == 1) break;
        u = fa[top[u]];
    }
    return res;
}
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            G[i].clear();
        }
        memset(sumv, 0, sizeof(sumv));
        memset(sze, 0, sizeof(sze));
        memset(a, 0, sizeof(a));
        for (int i = 1; i < n; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }

        dep[0] = 0;
        dfs1(1, 0);
        cnt = 0;
        dfs2(1, 0, 1);

        int ch, k;
        for (int i = 1; i <= m; i++) {
            scanf("%d%d", &ch, &k);
            switch(ch) {
                case 1: printf("%d\n", calcans(k)); break;
                case 0: {
                    update(1, 1, n, pos[k]);
                    break;
                }

            }
        }

    return 0;
}

BZOJ-2243 染色

Description

給定一棵有n個節點的無根樹和m個操做,操做有2類:

一、將節點a到節點b路徑上全部點都染成顏色c;

二、詢問節點a到節點b路徑上的顏色段數量(連續相同顏色被認爲是同一段),

如「112221」由3段組成:「11」、「222」和「1」。

請你寫一個程序依次完成這m個操做。

Input

第一行包含2個整數n和m,分別表示節點數和操做數;

第二行包含n個正整數表示n個節點的初始顏色

下面 行每行包含兩個整數x和y,表示x和y之間有一條無向邊。

下面 行每行描述一個操做:

「C a b c」表示這是一個染色操做,把節點a到節點b路徑上全部點(包括a和b)都染成顏色c;

「Q a b」表示這是一個詢問操做,詢問節點a到節點b(包括a和b)路徑上的顏色段數量。

Output

對於每一個詢問操做,輸出一行答案。

Sample Input

6 5
2 2 1 2 1 1
1 2
1 3
2 4
2 5
2 6
Q 3 5
C 2 1 1
Q 3 5
C 5 1 2
Q 3 5

Sample Output

3
1
2

Hint

數N<=10^5,操做數M<=10^5,全部的顏色C爲整數且在[0, 10^9]之間。

題解

樹剖後用線段樹維護每一個區間的顏色段數,和左端點右端點的顏色是什麼,合併時判斷顏色是否同樣便可

代碼

#include <bits/stdc++.h>
#define lson (o << 1)
#define rson (o << 1 | 1)
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
vector<int> G[N];
const ll inf = 1e9;
int n;
ll val[N];
int fa[N];
int son[N];
int sze[N];
int dep[N];
void dfs1(int u, int f) {
    sze[u] = 1;
    fa[u] = f;
    son[u] = 0;
    dep[u] = dep[f] + 1;
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if (v == f) continue;
        dfs1(v, u);
        sze[u] += sze[v];
        if (sze[v] > sze[son[u]]) son[u] = v;
    }
}
int top[N];
int cnt;
int pos[N];
int a[N];
void dfs2(int u, int f, int t) {
    top[u] = t;
    pos[u] = ++cnt;
    a[cnt] = val[u];
    if (son[u]) dfs2(son[u], u, t);
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if (v == f || v == son[u]) continue;
        dfs2(v, u, v);
    }
}
ll sumv[N << 2];
ll cov[N << 2];
ll L[N << 2];
ll R[N << 2];
void pushup(int o) {
    L[o] = L[lson];
    R[o] = R[rson];
    if (R[lson] == L[rson]) {
        sumv[o] = sumv[lson] + sumv[rson] - 1;
    }
    else sumv[o] = sumv[lson] + sumv[rson];
}
void pushdown(int o, int l, int r) {
    if (cov[o]) {
        cov[lson] = cov[rson] = cov[o];
        sumv[lson] = sumv[rson] = 1;
        L[lson] = R[lson] = cov[o];
        L[rson] = R[rson] = cov[o];
        cov[o] = 0;
    }
}
void build(int o, int l, int r) {
    if (l == r) {
        sumv[o] = 1;
        cov[o] = 0;
        L[o] = R[o] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(lson, l, mid); build(rson, mid + 1, r);
    pushup(o);
}
void update(int o, int l, int r, int ql, int qr, ll v) {
    if (ql <= l && r <= qr) {
        sumv[o] = 1;
        cov[o] = v;
        L[o] = R[o] = v;
        return;
    }
    int mid = (l + r) >> 1;
    pushdown(o, l, r);
    if (ql <= mid) update(lson, l, mid, ql, qr, v);
    if (qr > mid) update(rson, mid + 1, r, ql, qr, v);
    pushup(o);
}
ll query(int o, int l, int r, int ql, int qr) {
    if (ql == l && r == qr) {
        return sumv[o];
    }
    int mid = (l + r) >> 1;
    pushdown(o, l, r);
    if (qr <= mid) return query(lson, l, mid, ql, qr);
    else if (ql > mid) return query(rson, mid + 1, r, ql, qr);
    else {
        ll res = query(lson, l, mid, ql, mid) + query(rson, mid + 1, r, mid + 1, qr);
        if (R[lson] == L[rson]) return res - 1;
        else return res;
    }
}
ll querycor(int o, int l, int r, int pos) {
    if (l == r) {
        return L[o];
    }
    int mid = (l + r) >> 1;
    pushdown(o, l, r);
    if (pos <= mid) return querycor(lson, l, mid, pos);
    else return querycor(rson, mid + 1, r, pos);
}
ll calcsum(int u, int v) {
    ll ans = 0;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        ans += query(1, 1, n, pos[top[u]], pos[u]);
        int tmp = top[u];
        u = fa[top[u]];
        if (querycor(1, 1, n, pos[tmp]) == querycor(1, 1, n, pos[u])) ans--;
    }
    if (dep[u] < dep[v]) swap(u, v);
    ans += query(1, 1, n, pos[v], pos[u]);
    return ans;
}
void update1(int u, int v, ll val) {
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        update(1, 1, n, pos[top[u]], pos[u], val);
        u = fa[top[u]];
    }
    if (dep[u] < dep[v]) swap(u, v);
    update(1, 1, n, pos[v], pos[u], val);
}
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &n);
    int m; scanf("%d", &m);
    for (int i = 1; i <= n; i++) scanf("%lld", &val[i]);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }

    dep[0] = 0;
    dfs1(1, 0);
    cnt = 0;
    dfs2(1, 0, 1);
    build(1, 1, n);

    char ch[10];
    for (int i = 1; i <= m; i++) {
        scanf("%s", ch);
        int l, r, k;
        ll v;
        switch(ch[0]) {
            case 'Q': scanf("%d%d", &l, &r); printf("%lld\n", calcsum(l, r)); break;
            case 'C': {
                scanf("%d%d%lld", &l, &r, &v);
                update1(l, r, v);
                break;
            }

        }
    }
    return 0;
}

BZOJ-2157 旅遊

Description

Ray 樂忠於旅遊,此次他來到了T 城。T 城是一個水上城市,一共有 N 個景點,有些景點之間會用一座橋鏈接。爲了方便遊客到達每一個景點但又爲了節約成本,T 城的任意兩個景點之間有且只有一條路徑。換句話說, T 城中只有N − 1 座橋。Ray 發現,有些橋上能夠看到美麗的景色,讓人心情愉悅,但有些橋狹窄泥濘,使人煩躁。因而,他給每座橋定義一個愉悅度w,也就是說,Ray 通過這座橋會增長w 的愉悅度,這或許是正的也多是負的。有時,Ray 看待同一座橋的心情也會發生改變。如今,Ray 想讓你幫他計算從u 景點到v 景點能得到的總愉悅度。有時,他還想知道某段路上最美麗的橋所提供的最大愉悅度,或是某段路上最糟糕的一座橋提供的最低愉悅度。

Input

輸入的第一行包含一個整數N,表示T 城中的景點個數。景點編號爲 0...N − 1。接下來N − 1 行,每行三個整數u、v 和w,表示有一條u 到v,使 Ray 愉悅度增長w 的橋。橋的編號爲1...N − 1。|w| <= 1000。輸入的第N + 1 行包含一個整數M,表示Ray 的操做數目。接下來有M 行,每行描述了一個操做,操做有以下五種形式: C i w,表示Ray 對於通過第i 座橋的愉悅度變成了w。 N u v,表示Ray 對於通過景點u 到v 的路徑上的每一座橋的愉悅度都變成原來的相反數。 SUM u v,表示詢問從景點u 到v 所得到的總愉悅度。 MAX u v,表示詢問從景點u 到v 的路徑上的全部橋中某一座橋所提供的最大愉悅度。 MIN u v,表示詢問從景點u 到v 的路徑上的全部橋中某一座橋所提供的最小愉悅度。測試數據保證,任意時刻,Ray 對於通過每一座橋的愉悅度的絕對值小於等於1000。

Output

對於每個詢問(操做S、MAX 和MIN),輸出答案。

Sample Input

3
0 1 1
1 2 2
8
SUM 0 2
MAX 0 2
N 0 1
SUM 0 2
MIN 0 2
C 1 3
SUM 0 2
MAX 0 2

Sample Output

3
2
1
-1
5
3

Hint

一共有10 個數據,對於第i (1 <= i <= 10) 個數據, N = M = i * 2000。

題解

本題給的是邊權,對於邊權咱們不太方便樹鏈剖分,咱們將每條邊的邊權給子節點,這樣根節點點權爲0,而後就能夠正常樹剖了,對於取區間相反數的操做,咱們就將最大值和最小值交換再取負數就能夠了,區間和直接取負數.

#include <bits/stdc++.h>
#define lson (o << 1)
#define rson (o << 1 | 1)
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
struct node {
    int v, w;
    node(int v = 0, int w = 0): v(v), w(w) {}
};
vector<node> G[N];
const int inf = 1e9;
int n;
int val[N];
int fa[N];
int son[N];
int sze[N];
int dep[N];
void dfs1(int u, int f) {
    sze[u] = 1;
    fa[u] = f;
    son[u] = 0;
    dep[u] = dep[f] + 1;
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i].v;
        if (v == f) continue;
        val[v] = G[u][i].w;
        dfs1(v, u);
        sze[u] += sze[v];
        if (sze[v] > sze[son[u]]) {
            son[u] = v;
        }
    }
}
int top[N];
int cnt;
int pos[N];
int a[N];
void dfs2(int u, int f, int t) {
    top[u] = t;
    pos[u] = ++cnt;
    a[cnt] = val[u];
    if (son[u]) dfs2(son[u], u, t);
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i].v;
        if (v == f || v == son[u]) continue;
        dfs2(v, u, v);
    }
}
int sumv[N << 2];
int maxv[N << 2];
int minv[N << 2];
void pushup(int o) {
    sumv[o] = sumv[lson] + sumv[rson];
    maxv[o] = max(maxv[lson], maxv[rson]);
    minv[o] = min(minv[lson], minv[rson]);
}
int negv[N << 2];
void myswap(int x) {
    negv[x] ^= 1;
    swap(maxv[x], minv[x]);
    maxv[x] = -maxv[x];
    minv[x] = -minv[x];
    sumv[x] = -sumv[x];
}
void pushdown(int o, int l, int r) {
    if (negv[o]) {
        myswap(lson); myswap(rson);
        negv[o] = 0;
    }
}
void build(int o, int l, int r) {
    if (l == r) {
        sumv[o] = maxv[o] = minv[o] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(lson, l, mid); build(rson, mid + 1, r);
    pushup(o);
}
void update(int o, int l, int r, int pos, int v) {
    if (l == r) {
        sumv[o] = maxv[o] = minv[o] = v;
        return;
    }
    int mid = (l + r) >> 1;
    pushdown(o, l, r);
    if (pos <= mid) update(lson, l, mid, pos, v);
    else update(rson, mid + 1, r, pos, v);
    pushup(o);
}
void neg(int o, int l, int r, int ql, int qr) {
    if (ql > r || qr < l) return;
    if (ql <= l && r <= qr) {
        myswap(o);
        return;
    }
    int mid = (l + r) >> 1;
    pushdown(o, l, r);
    if (ql <= mid) neg(lson, l, mid, ql, qr);
    if (qr > mid) neg(rson, mid + 1, r, ql, qr);
    pushup(o);
}
int querysum(int o, int l, int r, int ql, int qr) {
    if (ql > r || qr < l) return 0;
    if (ql <= l && r <= qr) {
        return sumv[o];
    }
    int mid = (l + r) >> 1;
    int ans = 0;
    pushdown(o, l, r);
    if (ql <= mid) ans += querysum(lson, l, mid, ql, qr);
    if (qr > mid) ans += querysum(rson, mid + 1, r, ql, qr);
    return ans;
}
int querymax(int o, int l, int r, int ql, int qr) {
    if (ql > r || qr < l) return -inf;
    if (ql <= l && r <= qr) {
        return maxv[o];
    }
    int mid = (l + r) >> 1;
    int ans = -inf;
    pushdown(o, l, r);
    if (ql <= mid) ans = max(ans, querymax(lson, l, mid, ql, qr));
    if (qr > mid) ans = max(ans, querymax(rson, mid + 1, r, ql, qr));
    return ans;
}
int querymin(int o, int l, int r, int ql, int qr) {
    if (ql > r || qr < l) return inf;
    if (ql <= l && r <= qr) {
        return minv[o];
    }
    int mid = (l + r) >> 1;
    pushdown(o, l, r);
    int ans = inf;
    if (ql <= mid) ans = min(ans, querymin(lson, l, mid, ql, qr));
    if (qr > mid) ans = min(ans, querymin(rson, mid + 1, r, ql, qr));
    return ans;
}
int csum(int u, int v) {
    int ans = 0;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        ans += querysum(1, 1, n, pos[top[u]], pos[u]);
        u = fa[top[u]];
    }
    if (dep[u] < dep[v]) swap(u, v);
    ans += querysum(1, 1, n, pos[v] + 1, pos[u]);
    return ans;
}
int cmax(int u, int v) {
    int ans = -inf;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        ans = max(ans, querymax(1, 1, n, pos[top[u]], pos[u]));
        u = fa[top[u]];
    }
    if (dep[u] < dep[v]) swap(u, v);
    ans = max(ans, querymax(1, 1, n, pos[v] + 1, pos[u]));
    return ans;
}
int cmin(int u, int v) {
    int ans = inf;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        ans = min(ans, querymin(1, 1, n, pos[top[u]], pos[u]));
        u = fa[top[u]];
    }
    if (dep[u] < dep[v]) swap(u, v);
    ans = min(ans, querymin(1, 1, n, pos[v] + 1, pos[u]));
    return ans;
}
void update1(int u, int v) {
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        neg(1, 1, n, pos[top[u]], pos[u]);
        u = fa[top[u]];
    }
    if (dep[u] < dep[v]) swap(u, v);
    neg(1, 1, n, pos[v] + 1, pos[u]);
}
struct edge {
    int u, v;
} edge[N];
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &n);
    val[1] = 0;
    for (int i = 1; i < n; i++) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        u++; v++;
        G[u].push_back(node(v, w));
        G[v].push_back(node(u, w));
        edge[i].u = u;
        edge[i].v = v;
    }
    dep[0] = 0;
    dfs1(1, 0);
    cnt = 0;
    dfs2(1, 0, 1);
    build(1, 1, n);
    int m; scanf("%d", &m);
    char ch[10];
    for (int i = 1; i <= m; i++) {
        scanf("%s", ch);
        int x, y;
        scanf("%d%d", &x, &y);
        if (ch[0] == 'S') {
            x++, y++;
            printf("%d\n", csum(x, y));
        }
        else if (ch[0] == 'C') {
            int v = dep[edge[x].u] < dep[edge[x].v] ? edge[x].v : edge[x].u;
            update(1, 1, n, pos[v], y);
        }
        else if (ch[0] == 'N') {
            x++; y++;
            update1(x, y);
        }
        else if (ch[1] == 'A') {
            x++; y++;
            printf("%d\n", cmax(x, y));
        }
        else {
            x++; y++;
            printf("%d\n", cmin(x, y));
        }
    }
    return 0;
}

continue

相關文章
相關標籤/搜索