提出 #39858019


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int maxlog = 17;
int n, b[maxn], dep[maxn], fa[maxn][maxlog + 1], sz[maxn], dfn[maxn], dfntimes;
ll tree[maxn];
vector<pair<int, int>> e[maxn];
pair<int, int> eg[maxn];
void dfs_pre(int u, int from) {
    dfn[u] = ++dfntimes;
    sz[u] = 1;
    fa[u][0] = from;
    dep[u] = dep[from] + 1;
    for(int i = 1; i <= maxlog; i++) {
        fa[u][i] = fa[fa[u][i - 1]][i - 1];
    }
    for(auto v : e[u]) {
        if(v.first == from) {
            continue;
        }
        b[v.first] = v.second;
        dfs_pre(v.first, u);
        sz[u] += sz[v.first];
    }
}
int LCA(int x, int y) {
    if(dep[x] < dep[y]) {
        swap(x, y);
    }
    for(int i = maxlog; i >= 0; i--) {
        if(dep[fa[x][i]] >= dep[y]) {
            x = fa[x][i];
        }
    }
    if(x == y) {
        return x;
    }
    for(int i = maxlog; i >= 0;i --) {
        if(fa[x][i] != fa[y][i]) {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}
void update(int x, ll v) {
    for(; x <= n; x += x & (-x)) {
        tree[x] += v;
    }
}
ll query(int x) {
    ll res = 0;
    for(; x >= 1; x -= x & (-x)) {
        res += tree[x];
    }
    return res;
}
void modify(int x, int v) {
    update(dfn[x], v - b[x]);
    update(dfn[x] + sz[x], b[x] - v);
    b[x] = v;
}
ll getdis(int x) {
    return query(dfn[x]);
}
int main() {
#ifdef DEBUG
    freopen("1.in", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 1, u, v, w; i < n; i++) {
        cin >> u >> v >> w;
        e[u].emplace_back(v, w);
        e[v].emplace_back(u, w);
        eg[i] = make_pair(u, v);
    }
    dfs_pre(1, 0);
    for(int i = 2; i <= n; i++) {
        update(dfn[i], b[i]);
        update(dfn[i] + sz[i], -b[i]);
    }
    int q;
    cin >> q;
    while(q--) {
        int o, x, y;
        cin >> o >> x >> y;
        if(o == 1) {
            int u = eg[x].first, v = eg[x].second;
            if(dep[u] < dep[v]) {
                modify(v, y);
            }
            else {
                modify(u, y);
            }
        }
        else {
            cout << getdis(x) + getdis(y) - getdis(LCA(x, y)) * 2 << '\n';
        }
    }
    return 0;
}

提出情報

提出日時
問題 G - Distance Queries on a Tree
ユーザ yanchengzhi
言語 C++ (GCC 9.2.1)
得点 600
コード長 2399 Byte
結果 AC
実行時間 391 ms
メモリ 50192 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 4
AC × 73
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 01_handmade_10.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt, 02_random_26.txt, 02_random_27.txt, 02_random_28.txt, 02_random_29.txt, 02_random_30.txt, 02_random_31.txt, 02_random_32.txt, 02_random_33.txt, 02_random_34.txt, 02_random_35.txt, 02_random_36.txt, 02_random_37.txt, 02_random_38.txt, 02_random_39.txt, 02_random_40.txt, 02_random_41.txt, 02_random_42.txt, 02_random_43.txt, 02_random_44.txt, 02_random_45.txt, 02_random_46.txt, 02_random_47.txt, 02_random_48.txt, 02_random_49.txt, 02_random_50.txt, 02_random_51.txt, 02_random_52.txt, 02_random_53.txt, 02_random_54.txt, 02_random_55.txt, 02_random_56.txt, 02_random_57.txt, 02_random_58.txt, 02_random_59.txt, 02_random_60.txt, 02_random_61.txt, 02_random_62.txt, 02_random_63.txt, 02_random_64.txt, 02_random_65.txt, 02_random_66.txt, 02_random_67.txt, 02_random_68.txt, 02_random_69.txt, 02_random_70.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 13 ms 8224 KiB
00_sample_01.txt AC 9 ms 8288 KiB
00_sample_02.txt AC 8 ms 8196 KiB
00_sample_03.txt AC 8 ms 8336 KiB
01_handmade_03.txt AC 261 ms 37164 KiB
01_handmade_04.txt AC 253 ms 37108 KiB
01_handmade_05.txt AC 248 ms 36348 KiB
01_handmade_06.txt AC 316 ms 50192 KiB
01_handmade_07.txt AC 332 ms 41144 KiB
01_handmade_08.txt AC 202 ms 35956 KiB
01_handmade_09.txt AC 221 ms 35700 KiB
01_handmade_10.txt AC 61 ms 8256 KiB
02_random_10.txt AC 75 ms 17916 KiB
02_random_11.txt AC 77 ms 17836 KiB
02_random_12.txt AC 118 ms 26908 KiB
02_random_13.txt AC 206 ms 35312 KiB
02_random_14.txt AC 191 ms 34948 KiB
02_random_15.txt AC 180 ms 28956 KiB
02_random_16.txt AC 66 ms 11996 KiB
02_random_17.txt AC 129 ms 22944 KiB
02_random_18.txt AC 132 ms 22264 KiB
02_random_19.txt AC 162 ms 33964 KiB
02_random_20.txt AC 175 ms 25784 KiB
02_random_21.txt AC 259 ms 48756 KiB
02_random_22.txt AC 134 ms 23260 KiB
02_random_23.txt AC 93 ms 13248 KiB
02_random_24.txt AC 89 ms 17540 KiB
02_random_25.txt AC 188 ms 40576 KiB
02_random_26.txt AC 54 ms 12224 KiB
02_random_27.txt AC 176 ms 31840 KiB
02_random_28.txt AC 85 ms 26100 KiB
02_random_29.txt AC 98 ms 19532 KiB
02_random_30.txt AC 115 ms 30440 KiB
02_random_31.txt AC 152 ms 24280 KiB
02_random_32.txt AC 113 ms 22256 KiB
02_random_33.txt AC 265 ms 33828 KiB
02_random_34.txt AC 133 ms 25188 KiB
02_random_35.txt AC 188 ms 28236 KiB
02_random_36.txt AC 177 ms 27968 KiB
02_random_37.txt AC 184 ms 33928 KiB
02_random_38.txt AC 170 ms 29400 KiB
02_random_39.txt AC 172 ms 35268 KiB
02_random_40.txt AC 139 ms 24392 KiB
02_random_41.txt AC 204 ms 31228 KiB
02_random_42.txt AC 225 ms 41060 KiB
02_random_43.txt AC 215 ms 35280 KiB
02_random_44.txt AC 205 ms 42952 KiB
02_random_45.txt AC 277 ms 32512 KiB
02_random_46.txt AC 106 ms 22648 KiB
02_random_47.txt AC 175 ms 33372 KiB
02_random_48.txt AC 170 ms 30164 KiB
02_random_49.txt AC 110 ms 22232 KiB
02_random_50.txt AC 186 ms 30072 KiB
02_random_51.txt AC 246 ms 35992 KiB
02_random_52.txt AC 228 ms 36052 KiB
02_random_53.txt AC 261 ms 35936 KiB
02_random_54.txt AC 209 ms 36028 KiB
02_random_55.txt AC 289 ms 35984 KiB
02_random_56.txt AC 260 ms 37084 KiB
02_random_57.txt AC 243 ms 36800 KiB
02_random_58.txt AC 275 ms 37064 KiB
02_random_59.txt AC 223 ms 36908 KiB
02_random_60.txt AC 275 ms 36992 KiB
02_random_61.txt AC 333 ms 44608 KiB
02_random_62.txt AC 284 ms 42428 KiB
02_random_63.txt AC 377 ms 42644 KiB
02_random_64.txt AC 241 ms 44228 KiB
02_random_65.txt AC 391 ms 49708 KiB
02_random_66.txt AC 208 ms 35984 KiB
02_random_67.txt AC 200 ms 35984 KiB
02_random_68.txt AC 215 ms 35712 KiB
02_random_69.txt AC 183 ms 35708 KiB
02_random_70.txt AC 212 ms 35912 KiB