Submission #338238

# Submission time Handle Problem Language Result Execution time Memory
338238 2020-12-22T19:52:21 Z aryan12 Birthday gift (IZhO18_treearray) C++17
0 / 100
4 ms 5228 KB
#include <bits/stdc++.h>
using namespace std;

//for 56 points

const long long N = 2e5 + 5;
vector<long long> g[N];
//long long tim = 0;
//long long Start[N], End[N];
long long DP[18][N], depth[N];

void dfs(long long node, long long par) {
    DP[0][node] = par;
   // Start[node] = ++tim;
    for(long long i = 0; i < g[node].size(); i++) {
        if(g[node][i] != par) {
            depth[g[node][i]] = depth[node] + 1;
            dfs(g[node][i], node);
        }
    }
    //End[node] = ++tim;
}

void DPTable(long long n) {
    for(long long j = 1; j < 18; j++) {
        for(long long i = 1; i <= n; i++) {
            if(DP[j - 1][i] == -1)
                DP[j][i] = -1;
            else
                DP[j][i] = DP[j - 1][DP[j - 1][i]];
            //cout << "DP[" << j << "][" << i << "] = " << DP[j][i] << endl;
        }
    }
}

long long lca(long long a, long long b) {
    if(depth[a] < depth[b])
        swap(a, b);
    long long diff = depth[a] - depth[b];
    for(long long i = 17; i >= 0; i--) {
        if(diff >= (1 << i)) {
            diff -= (1 << i);
            a = DP[i][a];
        }
    }
    if(a == b)
        return a;
    for(long long i = 17; i >= 0; i--) {
        if(DP[i][a] != DP[i][b]) {
            a = DP[i][a];
            b = DP[i][b];
        }
    }
    return DP[0][a];
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long n, m, q;
    cin >> n >> m >> q;
    for(long long i = 0; i < n - 1; i++) {
        long long v, u;
        cin >> v >> u;
        g[v].push_back(u);
        g[u].push_back(v);
    }
    depth[1] = 0;
    dfs(1, -1);
    DPTable(n);
    long long a[m + 1];
    for(long long i = 1; i <= m; i++) {
        cin >> a[i];
    }
    for(long long i = 1; i <= q; i++) {
        long long command;
        cin >> command;
        if(command == 1) {
            long long idx, val;
            cin >> idx >> val;
            a[idx] = val;
        }
        else {
            long long l, r, v;
            cin >> l >> r >> v;
            long long f = 0;
            for(long long j = l; j < r; j++) {
                if(lca(a[j], a[j + 1]) == v) {
                    cout << j << " " << j + 1 << "\n";
                    f = 1;
                    break;
                }
                if(a[j] == v) {
                    cout << j << " " << j << "\n";
                    f = 1;
                    break;
                }
            }
            if(a[r] == v) {
                cout << r << " " << r << "\n";
                f = 1;
            }
            if(f == 0) {
                cout << "-1 -1" << "\n";
            }
        }
    }
}

Compilation message

treearray.cpp: In function 'void dfs(long long int, long long int)':
treearray.cpp:15:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     for(long long i = 0; i < g[node].size(); i++) {
      |                          ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB n=5
2 Correct 4 ms 5228 KB n=100
3 Correct 4 ms 5228 KB n=100
4 Incorrect 4 ms 5228 KB Wrong range.
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB n=5
2 Correct 4 ms 5228 KB n=100
3 Correct 4 ms 5228 KB n=100
4 Incorrect 4 ms 5228 KB Wrong range.
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB n=5
2 Correct 4 ms 5228 KB n=100
3 Correct 4 ms 5228 KB n=100
4 Incorrect 4 ms 5228 KB Wrong range.
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB n=5
2 Correct 4 ms 5228 KB n=100
3 Correct 4 ms 5228 KB n=100
4 Incorrect 4 ms 5228 KB Wrong range.
5 Halted 0 ms 0 KB -