Submission #555066

# Submission time Handle Problem Language Result Execution time Memory
555066 2022-04-30T06:01:19 Z SmolBrain Birthday gift (IZhO18_treearray) C++17
100 / 100
2377 ms 95108 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 = 2e5 + 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;
    }
};

void solve(int test_case)
{
    // https://github.com/DrSwad/CompetitiveProgramming/blob/master/Olympiad/IZhO/IZhO%2018-treearray.cpp
    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);

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

    map<ll, set<ll>> mp1, mp2;

    rep1(i, m) {
        mp1[a[i]].insert(i);

        if (i + 1 <= m) {
            ll lca = LCA.query(a[i], a[i + 1]);
            mp2[lca].insert(i);
        }
    }

    while (q--) {
        ll op; cin >> op;
        if (op == 1) {
            ll i, v; cin >> i >> v;

            mp1[a[i]].erase(i);

            if (i + 1 <= m) {
                ll lca = LCA.query(a[i], a[i + 1]);
                mp2[lca].erase(i);
            }

            if (i - 1 >= 1) {
                ll lca = LCA.query(a[i], a[i - 1]);
                mp2[lca].erase(i - 1);
            }

            a[i] = v;

            mp1[a[i]].insert(i);

            if (i + 1 <= m) {
                ll lca = LCA.query(a[i], a[i + 1]);
                mp2[lca].insert(i);
            }

            if (i - 1 >= 1) {
                ll lca = LCA.query(a[i], a[i - 1]);
                mp2[lca].insert(i - 1);
            }
        }
        else {
            ll l, r, v; cin >> l >> r >> v;
            auto &st1 = mp1[v], &st2 = mp2[v];

            auto it1 = st1.lower_bound(l);
            if (it1 != st1.end() and (*it1) <= r) {
                cout << *it1 << " " << *it1 << endl;
                conts;
            }

            auto it2 = st2.lower_bound(l);
            if (it2 != st2.end() and (*it2) + 1 <= r) {
                cout << *it2 << " " << *it2 + 1 << endl;
                conts;
            }

            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 3 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 3 ms 4996 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 4948 KB n=100
8 Correct 3 ms 4948 KB n=100
9 Correct 3 ms 4948 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4936 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 4948 KB n=100
17 Correct 3 ms 4948 KB n=100
18 Correct 3 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 4948 KB n=100
23 Correct 3 ms 4992 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 3 ms 4948 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 3 ms 4996 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 4948 KB n=100
8 Correct 3 ms 4948 KB n=100
9 Correct 3 ms 4948 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4936 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 4948 KB n=100
17 Correct 3 ms 4948 KB n=100
18 Correct 3 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 4948 KB n=100
23 Correct 3 ms 4992 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 3 ms 4948 KB n=100
28 Correct 4 ms 5076 KB n=500
29 Correct 3 ms 5204 KB n=500
30 Correct 3 ms 5076 KB n=500
31 Correct 4 ms 5204 KB n=500
32 Correct 4 ms 5076 KB n=500
33 Correct 4 ms 5076 KB n=500
34 Correct 4 ms 5076 KB n=500
35 Correct 4 ms 5204 KB n=500
36 Correct 4 ms 5076 KB n=500
37 Correct 3 ms 5120 KB n=500
38 Correct 3 ms 5076 KB n=500
39 Correct 3 ms 5076 KB n=500
40 Correct 3 ms 5076 KB n=500
41 Correct 3 ms 5076 KB n=500
42 Correct 4 ms 5076 KB n=500
43 Correct 4 ms 5076 KB n=500
44 Correct 4 ms 5128 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 3 ms 5204 KB n=500
47 Correct 3 ms 5204 KB n=500
48 Correct 4 ms 5160 KB n=500
49 Correct 3 ms 5204 KB n=500
50 Correct 4 ms 5076 KB n=500
51 Correct 4 ms 5204 KB n=500
52 Correct 4 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5204 KB n=500
57 Correct 4 ms 5188 KB n=500
58 Correct 3 ms 5076 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 3 ms 4996 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 4948 KB n=100
8 Correct 3 ms 4948 KB n=100
9 Correct 3 ms 4948 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4936 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 4948 KB n=100
17 Correct 3 ms 4948 KB n=100
18 Correct 3 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 4948 KB n=100
23 Correct 3 ms 4992 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 3 ms 4948 KB n=100
28 Correct 4 ms 5076 KB n=500
29 Correct 3 ms 5204 KB n=500
30 Correct 3 ms 5076 KB n=500
31 Correct 4 ms 5204 KB n=500
32 Correct 4 ms 5076 KB n=500
33 Correct 4 ms 5076 KB n=500
34 Correct 4 ms 5076 KB n=500
35 Correct 4 ms 5204 KB n=500
36 Correct 4 ms 5076 KB n=500
37 Correct 3 ms 5120 KB n=500
38 Correct 3 ms 5076 KB n=500
39 Correct 3 ms 5076 KB n=500
40 Correct 3 ms 5076 KB n=500
41 Correct 3 ms 5076 KB n=500
42 Correct 4 ms 5076 KB n=500
43 Correct 4 ms 5076 KB n=500
44 Correct 4 ms 5128 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 3 ms 5204 KB n=500
47 Correct 3 ms 5204 KB n=500
48 Correct 4 ms 5160 KB n=500
49 Correct 3 ms 5204 KB n=500
50 Correct 4 ms 5076 KB n=500
51 Correct 4 ms 5204 KB n=500
52 Correct 4 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5204 KB n=500
57 Correct 4 ms 5188 KB n=500
58 Correct 3 ms 5076 KB n=500
59 Correct 6 ms 5588 KB n=2000
60 Correct 7 ms 5796 KB n=2000
61 Correct 7 ms 5716 KB n=2000
62 Correct 7 ms 5588 KB n=2000
63 Correct 6 ms 5588 KB n=2000
64 Correct 7 ms 5680 KB n=2000
65 Correct 6 ms 5580 KB n=2000
66 Correct 10 ms 5728 KB n=2000
67 Correct 7 ms 5524 KB n=2000
68 Correct 8 ms 5704 KB n=2000
69 Correct 7 ms 5588 KB n=2000
70 Correct 5 ms 5588 KB n=2000
71 Correct 5 ms 5588 KB n=2000
72 Correct 5 ms 5588 KB n=2000
73 Correct 5 ms 5588 KB n=2000
74 Correct 6 ms 5588 KB n=1844
75 Correct 5 ms 5588 KB n=2000
76 Correct 8 ms 5580 KB n=2000
77 Correct 6 ms 5588 KB n=2000
78 Correct 6 ms 5588 KB n=2000
79 Correct 5 ms 5588 KB n=2000
80 Correct 8 ms 5716 KB n=2000
81 Correct 7 ms 5716 KB n=2000
82 Correct 6 ms 5608 KB n=2000
83 Correct 7 ms 5704 KB n=2000
84 Correct 6 ms 5716 KB n=2000
85 Correct 7 ms 5716 KB n=2000
86 Correct 7 ms 5716 KB n=2000
87 Correct 6 ms 5716 KB n=2000
88 Correct 6 ms 5716 KB n=2000
89 Correct 7 ms 5716 KB n=2000
90 Correct 8 ms 5804 KB n=2000
91 Correct 6 ms 5588 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 3 ms 4996 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 4948 KB n=100
8 Correct 3 ms 4948 KB n=100
9 Correct 3 ms 4948 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4936 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 4948 KB n=100
17 Correct 3 ms 4948 KB n=100
18 Correct 3 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 4948 KB n=100
23 Correct 3 ms 4992 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 3 ms 4948 KB n=100
28 Correct 4 ms 5076 KB n=500
29 Correct 3 ms 5204 KB n=500
30 Correct 3 ms 5076 KB n=500
31 Correct 4 ms 5204 KB n=500
32 Correct 4 ms 5076 KB n=500
33 Correct 4 ms 5076 KB n=500
34 Correct 4 ms 5076 KB n=500
35 Correct 4 ms 5204 KB n=500
36 Correct 4 ms 5076 KB n=500
37 Correct 3 ms 5120 KB n=500
38 Correct 3 ms 5076 KB n=500
39 Correct 3 ms 5076 KB n=500
40 Correct 3 ms 5076 KB n=500
41 Correct 3 ms 5076 KB n=500
42 Correct 4 ms 5076 KB n=500
43 Correct 4 ms 5076 KB n=500
44 Correct 4 ms 5128 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 3 ms 5204 KB n=500
47 Correct 3 ms 5204 KB n=500
48 Correct 4 ms 5160 KB n=500
49 Correct 3 ms 5204 KB n=500
50 Correct 4 ms 5076 KB n=500
51 Correct 4 ms 5204 KB n=500
52 Correct 4 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5204 KB n=500
57 Correct 4 ms 5188 KB n=500
58 Correct 3 ms 5076 KB n=500
59 Correct 6 ms 5588 KB n=2000
60 Correct 7 ms 5796 KB n=2000
61 Correct 7 ms 5716 KB n=2000
62 Correct 7 ms 5588 KB n=2000
63 Correct 6 ms 5588 KB n=2000
64 Correct 7 ms 5680 KB n=2000
65 Correct 6 ms 5580 KB n=2000
66 Correct 10 ms 5728 KB n=2000
67 Correct 7 ms 5524 KB n=2000
68 Correct 8 ms 5704 KB n=2000
69 Correct 7 ms 5588 KB n=2000
70 Correct 5 ms 5588 KB n=2000
71 Correct 5 ms 5588 KB n=2000
72 Correct 5 ms 5588 KB n=2000
73 Correct 5 ms 5588 KB n=2000
74 Correct 6 ms 5588 KB n=1844
75 Correct 5 ms 5588 KB n=2000
76 Correct 8 ms 5580 KB n=2000
77 Correct 6 ms 5588 KB n=2000
78 Correct 6 ms 5588 KB n=2000
79 Correct 5 ms 5588 KB n=2000
80 Correct 8 ms 5716 KB n=2000
81 Correct 7 ms 5716 KB n=2000
82 Correct 6 ms 5608 KB n=2000
83 Correct 7 ms 5704 KB n=2000
84 Correct 6 ms 5716 KB n=2000
85 Correct 7 ms 5716 KB n=2000
86 Correct 7 ms 5716 KB n=2000
87 Correct 6 ms 5716 KB n=2000
88 Correct 6 ms 5716 KB n=2000
89 Correct 7 ms 5716 KB n=2000
90 Correct 8 ms 5804 KB n=2000
91 Correct 6 ms 5588 KB n=2000
92 Correct 1094 ms 73392 KB n=200000
93 Correct 1981 ms 83988 KB n=200000
94 Correct 1989 ms 89928 KB n=200000
95 Correct 1056 ms 73300 KB n=200000
96 Correct 1070 ms 73364 KB n=200000
97 Correct 1997 ms 82164 KB n=200000
98 Correct 1059 ms 73348 KB n=200000
99 Correct 1280 ms 73036 KB n=200000
100 Correct 1061 ms 73552 KB n=200000
101 Correct 1970 ms 91256 KB n=200000
102 Correct 571 ms 71692 KB n=200000
103 Correct 576 ms 71896 KB n=200000
104 Correct 571 ms 71884 KB n=200000
105 Correct 591 ms 71524 KB n=200000
106 Correct 594 ms 71592 KB n=200000
107 Correct 584 ms 71552 KB n=200000
108 Correct 1152 ms 73452 KB n=200000
109 Correct 1187 ms 73276 KB n=200000
110 Correct 1157 ms 73292 KB n=200000
111 Correct 983 ms 70480 KB n=200000
112 Correct 1978 ms 90244 KB n=200000
113 Correct 1961 ms 82252 KB n=200000
114 Correct 971 ms 70600 KB n=200000
115 Correct 1913 ms 76988 KB n=200000
116 Correct 1234 ms 82232 KB n=200000
117 Correct 2201 ms 95108 KB n=200000
118 Correct 2377 ms 86656 KB n=200000
119 Correct 1570 ms 82092 KB n=200000
120 Correct 2360 ms 91096 KB n=200000
121 Correct 2359 ms 91180 KB n=200000
122 Correct 2025 ms 91724 KB n=200000
123 Correct 759 ms 71436 KB n=200000
124 Correct 427 ms 24992 KB n=25264