Submission #555041

# Submission time Handle Problem Language Result Execution time Memory
555041 2022-04-30T03:15:52 Z SmolBrain Birthday gift (IZhO18_treearray) C++17
56 / 100
119 ms 856 KB
// Om Namah Shivaya
// GM in 94 days

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define endl '\n'
#define pb push_back
#define conts continue
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define ff first
#define ss second
#define ceil2(x,y) ((x+y-1) / (y))
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#ifndef ONLINE_JUDGE
#define debug(x) cout << #x <<" = "; print(x); cout << endl
#else
#define debug(x)
#endif

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

bool iseven(ll n) {if ((n & 1) == 0) return true; return false;}

void print(ll t) {cout << t;}
void print(int t) {cout << t;}
void print(string t) {cout << t;}
void print(char t) {cout << t;}
void print(double t) {cout << t;}
void print(ld t) {cout << t;}

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(pair <T, V> p) {cout << "{"; print(p.ff); cout << ","; print(p.ss); cout << "}";}
template <class T> void print(vector <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T> void print(set <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T> void print(multiset <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T, class V> void print(map <T, V> v) {cout << "[ "; for (auto i : v) {print(i); cout << " ";} cout << "]";}
template<typename T> void amin(T &a, T b) { a = min(a, b); }
template<typename T> void amax(T &a, T b) { a = max(a, b); }

void usaco(string filename) {
    freopen((filename + ".in").c_str(), "r", stdin);
    freopen((filename + ".out").c_str(), "w", stdout);
}

const int MOD = 1e9 + 7;
const int maxn = 2000 + 5;
const int inf1 = 1e9 + 5;
const ll inf2 = ll(1e18) + 5;

vector<ll> adj[maxn];

struct lca_algo {
    // LCA template (for graphs with 1-based indexing)

    int LOG = 1;
    vector<int> depth;
    vector<vector<int>> up;

    lca_algo() {

    }

    lca_algo(int n) {
        lca_init(n);
    }

    void lca_init(int n) {
        while ((1 << LOG) < n) LOG++;
        up = vector<vector<int>>(n + 1, vector<int>(LOG, 1));
        depth = vector<int>(n + 1);

        lca_dfs(1, -1);
    }

    void lca_dfs(int node, int par) {
        trav(child, adj[node]) {
            if (child == par) conts;

            up[child][0] = node;
            rep1(j, LOG - 1) {
                up[child][j] = up[up[child][j - 1]][j - 1];
            }

            depth[child] = depth[node] + 1;

            lca_dfs(child, node);
        }
    }

    int lift(int u, int k) {
        rep(j, LOG) {
            if (k & (1 << j)) {
                u = up[u][j];
            }
        }

        return u;
    }

    int query(int u, int v) {
        if (depth[u] < depth[v]) swap(u, v);
        int k = depth[u] - depth[v];
        u = lift(u, k);

        if (u == v) return u;

        rev(j, LOG - 1, 0) {
            if (up[u][j] != up[v][j]) {
                u = up[u][j];
                v = up[v][j];
            }
        }

        u = up[u][0];
        return u;
    }
};

vector<ll> tin(maxn), tout(maxn);
ll timer = 1;

void dfs(ll node, ll par) {
    tin[node] = timer++;

    trav(child, adj[node]) {
        if (child == par) conts;
        dfs(child, node);
    }

    tout[node] = timer++;
}

void solve(int test_case)
{
    ll n, m, q; cin >> n >> m >> q;
    rep1(i, n - 1) {
        ll u, v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    lca_algo LCA(n);
    dfs(1, -1);

    vector<ll> a(m + 5);
    rep1(i, m) cin >> a[i];

    while (q--) {
        ll op; cin >> op;
        if (op == 1) {
            ll i, v; cin >> i >> v;
            a[i] = v;
        }
        else {
            ll l, r, v; cin >> l >> r >> v;
            ll start = -1;
            ll lca = -1;
            bool flag = false;

            for (ll i = l; i <= r; ++i) {
                if (tin[v] <= tin[a[i]] and tout[v] >= tout[a[i]]) {
                    // v = ancestor of a[i]
                    if (lca == -1) {
                        lca = a[i];
                        start = i;
                    }

                    lca = LCA.query(lca, a[i]);
                    if (lca == v) {
                        cout << start << " " << i << endl;
                        flag = true;
                        break;
                    }
                }
                else {
                    start = -1, lca = -1;
                }
            }

            if (!flag) cout << -1 << " " << -1 << endl;
        }
    }
}

int main()
{
    chrono::steady_clock::time_point begin = chrono::steady_clock::now();

    fastio;

    int t = 1;
    // cin >> t;
    rep1(i, t) {
        solve(i);
    }

    chrono::steady_clock::time_point end = chrono::steady_clock::now();
    cerr << "[Finished in " << chrono::duration_cast<chrono::microseconds>(end - begin).count() / 1000 << "ms]" << endl;

    return 0;
}

Compilation message

treearray.cpp: In function 'void usaco(std::string)':
treearray.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     freopen((filename + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     freopen((filename + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 400 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 400 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 468 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 396 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 400 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 1 ms 340 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 400 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 400 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 468 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 396 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 400 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 2 ms 468 KB n=500
30 Correct 2 ms 468 KB n=500
31 Correct 2 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 2 ms 468 KB n=500
34 Correct 2 ms 468 KB n=500
35 Correct 2 ms 468 KB n=500
36 Correct 3 ms 404 KB n=500
37 Correct 2 ms 480 KB n=500
38 Correct 3 ms 408 KB n=500
39 Correct 2 ms 468 KB n=500
40 Correct 1 ms 468 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 2 ms 468 KB n=500
43 Correct 2 ms 368 KB n=500
44 Correct 2 ms 468 KB n=500
45 Correct 1 ms 412 KB n=500
46 Correct 2 ms 468 KB n=500
47 Correct 1 ms 408 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 2 ms 468 KB n=500
52 Correct 2 ms 468 KB n=500
53 Correct 2 ms 468 KB n=500
54 Correct 2 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 5 ms 468 KB n=500
57 Correct 6 ms 412 KB n=500
58 Correct 2 ms 468 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 400 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 400 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 468 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 396 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 400 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 2 ms 468 KB n=500
30 Correct 2 ms 468 KB n=500
31 Correct 2 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 2 ms 468 KB n=500
34 Correct 2 ms 468 KB n=500
35 Correct 2 ms 468 KB n=500
36 Correct 3 ms 404 KB n=500
37 Correct 2 ms 480 KB n=500
38 Correct 3 ms 408 KB n=500
39 Correct 2 ms 468 KB n=500
40 Correct 1 ms 468 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 2 ms 468 KB n=500
43 Correct 2 ms 368 KB n=500
44 Correct 2 ms 468 KB n=500
45 Correct 1 ms 412 KB n=500
46 Correct 2 ms 468 KB n=500
47 Correct 1 ms 408 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 2 ms 468 KB n=500
52 Correct 2 ms 468 KB n=500
53 Correct 2 ms 468 KB n=500
54 Correct 2 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 5 ms 468 KB n=500
57 Correct 6 ms 412 KB n=500
58 Correct 2 ms 468 KB n=500
59 Correct 2 ms 724 KB n=2000
60 Correct 15 ms 748 KB n=2000
61 Correct 14 ms 724 KB n=2000
62 Correct 13 ms 732 KB n=2000
63 Correct 2 ms 672 KB n=2000
64 Correct 14 ms 668 KB n=2000
65 Correct 3 ms 724 KB n=2000
66 Correct 18 ms 724 KB n=2000
67 Correct 3 ms 704 KB n=2000
68 Correct 15 ms 672 KB n=2000
69 Correct 15 ms 724 KB n=2000
70 Correct 14 ms 724 KB n=2000
71 Correct 17 ms 748 KB n=2000
72 Correct 8 ms 728 KB n=2000
73 Correct 9 ms 660 KB n=2000
74 Correct 2 ms 596 KB n=1844
75 Correct 9 ms 724 KB n=2000
76 Correct 14 ms 724 KB n=2000
77 Correct 12 ms 724 KB n=2000
78 Correct 14 ms 664 KB n=2000
79 Correct 2 ms 624 KB n=2000
80 Correct 14 ms 724 KB n=2000
81 Correct 12 ms 740 KB n=2000
82 Correct 2 ms 660 KB n=2000
83 Correct 14 ms 784 KB n=2000
84 Correct 5 ms 668 KB n=2000
85 Correct 14 ms 856 KB n=2000
86 Correct 10 ms 748 KB n=2000
87 Correct 5 ms 668 KB n=2000
88 Correct 118 ms 796 KB n=2000
89 Correct 119 ms 772 KB n=2000
90 Correct 56 ms 800 KB n=2000
91 Correct 13 ms 724 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 400 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 400 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 468 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 396 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 400 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 2 ms 468 KB n=500
30 Correct 2 ms 468 KB n=500
31 Correct 2 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 2 ms 468 KB n=500
34 Correct 2 ms 468 KB n=500
35 Correct 2 ms 468 KB n=500
36 Correct 3 ms 404 KB n=500
37 Correct 2 ms 480 KB n=500
38 Correct 3 ms 408 KB n=500
39 Correct 2 ms 468 KB n=500
40 Correct 1 ms 468 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 2 ms 468 KB n=500
43 Correct 2 ms 368 KB n=500
44 Correct 2 ms 468 KB n=500
45 Correct 1 ms 412 KB n=500
46 Correct 2 ms 468 KB n=500
47 Correct 1 ms 408 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 2 ms 468 KB n=500
52 Correct 2 ms 468 KB n=500
53 Correct 2 ms 468 KB n=500
54 Correct 2 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 5 ms 468 KB n=500
57 Correct 6 ms 412 KB n=500
58 Correct 2 ms 468 KB n=500
59 Correct 2 ms 724 KB n=2000
60 Correct 15 ms 748 KB n=2000
61 Correct 14 ms 724 KB n=2000
62 Correct 13 ms 732 KB n=2000
63 Correct 2 ms 672 KB n=2000
64 Correct 14 ms 668 KB n=2000
65 Correct 3 ms 724 KB n=2000
66 Correct 18 ms 724 KB n=2000
67 Correct 3 ms 704 KB n=2000
68 Correct 15 ms 672 KB n=2000
69 Correct 15 ms 724 KB n=2000
70 Correct 14 ms 724 KB n=2000
71 Correct 17 ms 748 KB n=2000
72 Correct 8 ms 728 KB n=2000
73 Correct 9 ms 660 KB n=2000
74 Correct 2 ms 596 KB n=1844
75 Correct 9 ms 724 KB n=2000
76 Correct 14 ms 724 KB n=2000
77 Correct 12 ms 724 KB n=2000
78 Correct 14 ms 664 KB n=2000
79 Correct 2 ms 624 KB n=2000
80 Correct 14 ms 724 KB n=2000
81 Correct 12 ms 740 KB n=2000
82 Correct 2 ms 660 KB n=2000
83 Correct 14 ms 784 KB n=2000
84 Correct 5 ms 668 KB n=2000
85 Correct 14 ms 856 KB n=2000
86 Correct 10 ms 748 KB n=2000
87 Correct 5 ms 668 KB n=2000
88 Correct 118 ms 796 KB n=2000
89 Correct 119 ms 772 KB n=2000
90 Correct 56 ms 800 KB n=2000
91 Correct 13 ms 724 KB n=2000
92 Runtime error 1 ms 596 KB Execution killed with signal 11
93 Halted 0 ms 0 KB -