Submission #515655

#TimeUsernameProblemLanguageResultExecution timeMemory
515655Be_dosBirthday gift (IZhO18_treearray)C++17
100 / 100
906 ms92628 KiB
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <unordered_set>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <random>
#include <cfloat>
#include <chrono>
#include <bitset>
#include <complex>
#include <immintrin.h>
#include <cassert>

int32_t* tin, *tout;
int32_t timer_ = 0;
void dfs(int32_t v, int32_t parent, std::vector<int32_t>* tree) {
    tin[v] = timer_++;
    for(int32_t i = 0; i < tree[v].size(); i++)
        if(tree[v][i] != parent)
            dfs(tree[v][i], v, tree);
    tout[v] = timer_;
}

int32_t** ancestors;
void build_ancestors(int32_t v, int32_t parent, std::vector<int32_t>* tree) {
    ancestors[v] = new int32_t[20];
    ancestors[v][0] = v;
    ancestors[v][1] = parent;
    for(int32_t i = 2; i < 20; i++)
        ancestors[v][i] = ancestors[v][i - 1] == -1 ? -1 : ancestors[ancestors[v][i - 1]][i - 1];

    for(int32_t i = 0; i < tree[v].size(); i++)
        if(tree[v][i] != parent)
            build_ancestors(tree[v][i], v, tree);
}

bool is_ancestor(int32_t v1, int32_t v2) {
    return tin[v1] <= tin[v2] && tout[v1] >= tout[v2];
}

int32_t get_lca(int32_t v1, int32_t v2) {
    if(is_ancestor(v1, v2))
        return v1;
    if(is_ancestor(v2, v1))
        return v2;
    int32_t cur = v1;
    for(int32_t i = 19; i >= 0; i--)
        if(ancestors[cur][i] != -1 && !is_ancestor(ancestors[cur][i], v2))
            cur = ancestors[cur][i];
    return ancestors[cur][1];
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    
    int32_t n, m, num_q;
    std::cin >> n >> m >> num_q;

    std::vector<int32_t>* tree = new std::vector<int32_t>[n];
    for(int32_t i = 0; i < n - 1; i++) {
        int32_t src, dst;
        std::cin >> src >> dst;
        src--;
        dst--;

        tree[src].push_back(dst);
        tree[dst].push_back(src);
    }

    int32_t* arr = new int32_t[m];
    for(int32_t i = 0; i < m; i++) {
        std::cin >> arr[i];
        arr[i]--;
    }

    tin = new int32_t[n];
    tout = new int32_t[n];
    dfs(0, -1, tree);

    ancestors = new int32_t*[n];
    build_ancestors(0, -1, tree);

    std::set<int32_t>* ind_per_v = new std::set<int32_t>[n];
    for(int32_t i = 0; i < m; i++)
        ind_per_v[arr[i]].insert(i);
    std::set<int32_t>* ind_per_lca = new std::set<int32_t>[n];
    for(int32_t i = 0; i < m - 1; i++)
        ind_per_lca[get_lca(arr[i], arr[i + 1])].insert(i);
    for(int32_t q = 0; q < num_q; q++) {
        int32_t type;
        std::cin >> type;

        if(type == 1) {
            int32_t pos, v;
            std::cin >> pos >> v;
            pos--;
            v--;

            ind_per_v[arr[pos]].erase(pos);
            if(pos > 0)
                ind_per_lca[get_lca(arr[pos - 1], arr[pos])].erase(pos - 1);
            if(pos < m - 1)
                ind_per_lca[get_lca(arr[pos + 1], arr[pos])].erase(pos);

            arr[pos] = v;
            ind_per_v[arr[pos]].insert(pos);
            if(pos > 0)
                ind_per_lca[get_lca(arr[pos - 1], arr[pos])].insert(pos - 1);
            if(pos < m - 1)
                ind_per_lca[get_lca(arr[pos + 1], arr[pos])].insert(pos);
        } else {
            int32_t left, right, v;
            std::cin >> left >> right >> v;
            left--;
            right--;
            v--;

            auto it = ind_per_v[v].lower_bound(left);
            if(it != ind_per_v[v].end() && *it <= right) {
                std::cout << *it + 1 << " " << *it + 1 << "\n";
                continue;
            }

            auto it2 = ind_per_lca[v].lower_bound(left);
            if(it2 != ind_per_lca[v].end() && *it2 < right) {
                std::cout << *it2 + 1 << " " << *it2 + 2 << "\n";
                continue;
            }
            std::cout << -1 << " " << -1 << "\n";
        }
    }
    return 0;
}






Compilation message (stderr)

treearray.cpp: In function 'void dfs(int32_t, int32_t, std::vector<int>*)':
treearray.cpp:28:26: warning: comparison of integer expressions of different signedness: 'int32_t' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     for(int32_t i = 0; i < tree[v].size(); i++)
      |                        ~~^~~~~~~~~~~~~~~~
treearray.cpp: In function 'void build_ancestors(int32_t, int32_t, std::vector<int>*)':
treearray.cpp:42:26: warning: comparison of integer expressions of different signedness: 'int32_t' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for(int32_t i = 0; i < tree[v].size(); i++)
      |                        ~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...