Submission #555069

# Submission time Handle Problem Language Result Execution time Memory
555069 2022-04-30T06:03:36 Z SmolBrain Birthday gift (IZhO18_treearray) C++17
100 / 100
1242 ms 73992 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<int> 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
    int n, m, q; cin >> n >> m >> q;
    rep1(i, n - 1) {
        int u, v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    lca_algo LCA(n);

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

    set<int> mp1[n + 5], mp2[n + 5];

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

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

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

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

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

            if (i - 1 >= 1) {
                int 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) {
                int lca = LCA.query(a[i], a[i + 1]);
                mp2[lca].insert(i);
            }

            if (i - 1 >= 1) {
                int lca = LCA.query(a[i], a[i - 1]);
                mp2[lca].insert(i - 1);
            }
        }
        else {
            int 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 4 ms 4948 KB n=100
5 Correct 3 ms 5012 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 4948 KB n=100
8 Correct 3 ms 5016 KB n=100
9 Correct 3 ms 4996 KB n=100
10 Correct 3 ms 5024 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 4 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 5020 KB n=100
17 Correct 3 ms 5016 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 5016 KB n=100
22 Correct 4 ms 4948 KB n=100
23 Correct 3 ms 4984 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 3 ms 4940 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 4 ms 4948 KB n=100
5 Correct 3 ms 5012 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 4948 KB n=100
8 Correct 3 ms 5016 KB n=100
9 Correct 3 ms 4996 KB n=100
10 Correct 3 ms 5024 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 4 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 5020 KB n=100
17 Correct 3 ms 5016 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 5016 KB n=100
22 Correct 4 ms 4948 KB n=100
23 Correct 3 ms 4984 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 3 ms 4948 KB n=100
28 Correct 4 ms 5156 KB n=500
29 Correct 3 ms 5160 KB n=500
30 Correct 4 ms 5076 KB n=500
31 Correct 4 ms 5076 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 3 ms 5156 KB n=500
36 Correct 3 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5076 KB n=500
39 Correct 4 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 5148 KB n=500
44 Correct 4 ms 5076 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 3 ms 5160 KB n=500
47 Correct 4 ms 5076 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 3 ms 5076 KB n=500
50 Correct 3 ms 5076 KB n=500
51 Correct 3 ms 5164 KB n=500
52 Correct 3 ms 5156 KB n=500
53 Correct 4 ms 5156 KB n=500
54 Correct 4 ms 5076 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5152 KB n=500
57 Correct 4 ms 5076 KB n=500
58 Correct 4 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 4 ms 4948 KB n=100
5 Correct 3 ms 5012 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 4948 KB n=100
8 Correct 3 ms 5016 KB n=100
9 Correct 3 ms 4996 KB n=100
10 Correct 3 ms 5024 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 4 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 5020 KB n=100
17 Correct 3 ms 5016 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 5016 KB n=100
22 Correct 4 ms 4948 KB n=100
23 Correct 3 ms 4984 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 3 ms 4948 KB n=100
28 Correct 4 ms 5156 KB n=500
29 Correct 3 ms 5160 KB n=500
30 Correct 4 ms 5076 KB n=500
31 Correct 4 ms 5076 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 3 ms 5156 KB n=500
36 Correct 3 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5076 KB n=500
39 Correct 4 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 5148 KB n=500
44 Correct 4 ms 5076 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 3 ms 5160 KB n=500
47 Correct 4 ms 5076 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 3 ms 5076 KB n=500
50 Correct 3 ms 5076 KB n=500
51 Correct 3 ms 5164 KB n=500
52 Correct 3 ms 5156 KB n=500
53 Correct 4 ms 5156 KB n=500
54 Correct 4 ms 5076 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5152 KB n=500
57 Correct 4 ms 5076 KB n=500
58 Correct 4 ms 5076 KB n=500
59 Correct 6 ms 5672 KB n=2000
60 Correct 8 ms 5708 KB n=2000
61 Correct 5 ms 5668 KB n=2000
62 Correct 6 ms 5628 KB n=2000
63 Correct 6 ms 5588 KB n=2000
64 Correct 6 ms 5676 KB n=2000
65 Correct 5 ms 5668 KB n=2000
66 Correct 5 ms 5588 KB n=2000
67 Correct 6 ms 5588 KB n=2000
68 Correct 6 ms 5588 KB n=2000
69 Correct 5 ms 5716 KB n=2000
70 Correct 6 ms 5668 KB n=2000
71 Correct 5 ms 5716 KB n=2000
72 Correct 5 ms 5668 KB n=2000
73 Correct 5 ms 5588 KB n=2000
74 Correct 5 ms 5548 KB n=1844
75 Correct 5 ms 5624 KB n=2000
76 Correct 6 ms 5716 KB n=2000
77 Correct 6 ms 5616 KB n=2000
78 Correct 6 ms 5588 KB n=2000
79 Correct 6 ms 5588 KB n=2000
80 Correct 6 ms 5668 KB n=2000
81 Correct 6 ms 5588 KB n=2000
82 Correct 6 ms 5672 KB n=2000
83 Correct 6 ms 5672 KB n=2000
84 Correct 6 ms 5672 KB n=2000
85 Correct 6 ms 5672 KB n=2000
86 Correct 6 ms 5672 KB n=2000
87 Correct 6 ms 5716 KB n=2000
88 Correct 6 ms 5588 KB n=2000
89 Correct 5 ms 5588 KB n=2000
90 Correct 6 ms 5588 KB n=2000
91 Correct 5 ms 5716 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 4 ms 4948 KB n=100
5 Correct 3 ms 5012 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 4948 KB n=100
8 Correct 3 ms 5016 KB n=100
9 Correct 3 ms 4996 KB n=100
10 Correct 3 ms 5024 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 4 ms 4948 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 5020 KB n=100
17 Correct 3 ms 5016 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 5016 KB n=100
22 Correct 4 ms 4948 KB n=100
23 Correct 3 ms 4984 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 3 ms 4948 KB n=100
28 Correct 4 ms 5156 KB n=500
29 Correct 3 ms 5160 KB n=500
30 Correct 4 ms 5076 KB n=500
31 Correct 4 ms 5076 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 3 ms 5156 KB n=500
36 Correct 3 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5076 KB n=500
39 Correct 4 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 5148 KB n=500
44 Correct 4 ms 5076 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 3 ms 5160 KB n=500
47 Correct 4 ms 5076 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 3 ms 5076 KB n=500
50 Correct 3 ms 5076 KB n=500
51 Correct 3 ms 5164 KB n=500
52 Correct 3 ms 5156 KB n=500
53 Correct 4 ms 5156 KB n=500
54 Correct 4 ms 5076 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5152 KB n=500
57 Correct 4 ms 5076 KB n=500
58 Correct 4 ms 5076 KB n=500
59 Correct 6 ms 5672 KB n=2000
60 Correct 8 ms 5708 KB n=2000
61 Correct 5 ms 5668 KB n=2000
62 Correct 6 ms 5628 KB n=2000
63 Correct 6 ms 5588 KB n=2000
64 Correct 6 ms 5676 KB n=2000
65 Correct 5 ms 5668 KB n=2000
66 Correct 5 ms 5588 KB n=2000
67 Correct 6 ms 5588 KB n=2000
68 Correct 6 ms 5588 KB n=2000
69 Correct 5 ms 5716 KB n=2000
70 Correct 6 ms 5668 KB n=2000
71 Correct 5 ms 5716 KB n=2000
72 Correct 5 ms 5668 KB n=2000
73 Correct 5 ms 5588 KB n=2000
74 Correct 5 ms 5548 KB n=1844
75 Correct 5 ms 5624 KB n=2000
76 Correct 6 ms 5716 KB n=2000
77 Correct 6 ms 5616 KB n=2000
78 Correct 6 ms 5588 KB n=2000
79 Correct 6 ms 5588 KB n=2000
80 Correct 6 ms 5668 KB n=2000
81 Correct 6 ms 5588 KB n=2000
82 Correct 6 ms 5672 KB n=2000
83 Correct 6 ms 5672 KB n=2000
84 Correct 6 ms 5672 KB n=2000
85 Correct 6 ms 5672 KB n=2000
86 Correct 6 ms 5672 KB n=2000
87 Correct 6 ms 5716 KB n=2000
88 Correct 6 ms 5588 KB n=2000
89 Correct 5 ms 5588 KB n=2000
90 Correct 6 ms 5588 KB n=2000
91 Correct 5 ms 5716 KB n=2000
92 Correct 751 ms 73108 KB n=200000
93 Correct 1205 ms 72124 KB n=200000
94 Correct 1182 ms 72132 KB n=200000
95 Correct 731 ms 72984 KB n=200000
96 Correct 711 ms 72972 KB n=200000
97 Correct 1212 ms 72204 KB n=200000
98 Correct 747 ms 73092 KB n=200000
99 Correct 915 ms 72440 KB n=200000
100 Correct 722 ms 73184 KB n=200000
101 Correct 1160 ms 72392 KB n=200000
102 Correct 463 ms 73936 KB n=200000
103 Correct 451 ms 73884 KB n=200000
104 Correct 451 ms 73992 KB n=200000
105 Correct 454 ms 73700 KB n=200000
106 Correct 446 ms 73656 KB n=200000
107 Correct 455 ms 73632 KB n=200000
108 Correct 854 ms 72852 KB n=200000
109 Correct 831 ms 72660 KB n=200000
110 Correct 839 ms 72632 KB n=200000
111 Correct 727 ms 72900 KB n=200000
112 Correct 1191 ms 72196 KB n=200000
113 Correct 1187 ms 72256 KB n=200000
114 Correct 702 ms 72972 KB n=200000
115 Correct 1242 ms 72200 KB n=200000
116 Correct 718 ms 72768 KB n=200000
117 Correct 1156 ms 71924 KB n=200000
118 Correct 1156 ms 72260 KB n=200000
119 Correct 691 ms 72364 KB n=200000
120 Correct 1106 ms 71424 KB n=200000
121 Correct 1098 ms 71328 KB n=200000
122 Correct 1081 ms 71764 KB n=200000
123 Correct 478 ms 72952 KB n=200000
124 Correct 224 ms 20008 KB n=25264