제출 #945450

#제출 시각아이디문제언어결과실행 시간메모리
945450VahanAbrahamBirthday gift (IZhO18_treearray)C++17
100 / 100
811 ms109528 KiB
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <math.h>
#include <bitset>
#include <cmath>
#include <vector>
#include <iomanip>
#include <random>
#include <chrono>
#include <cassert>
using namespace std;

#define ll long long
#define fr first
#define sc second
#define pb push_back
#define US freopen("cowmbat.in", "r", stdin); freopen("cowmbat.out", "w", stdout);

ll gcd(ll a, ll b)
{
    if (a == 0 || b == 0) {
        return  max(a, b);
    }
    if (a <= b) {
        return gcd(a, b % a);
    }
    else {
        return gcd(a % b, b);
    }
}
ll lcm(ll a, ll b) {
    return (a / gcd(a, b)) * b;
}

const int N = 300005;
const ll oo = 1000000000000000000, MOD = 1000000007;

set<int> s[N], s1[N];
int timer = 0, a[N], b[N], up[N][25], tin[N], tout[N];
vector<int> g[N];


void dfs(int u, int p) {
    ++timer;
    tin[u] = timer;
    up[u][0] = p;
    for (int i = 1; i <= 20; ++i) {
        up[u][i] = up[up[u][i - 1]][i - 1];
    }
    for (int i = 0; i < g[u].size(); ++i) {
        int h = g[u][i];
        if (p != h) {
            dfs(h, u);
        }
    }
    ++timer;
    tout[u] = timer;
}

bool upper(int a, int b) {
    return tin[a] <= tin[b] && tout[b] <= tout[a];
}

int lca(int a, int b) {
    if (upper(a, b)) {
        return a;
    }
    if (upper(b, a)) {
        return b;
    }
    for (int i = 20; i >= 0; --i) {
        if (upper(up[a][i], b) != 1) {
            a = up[a][i];
        }
    }
    return up[a][0];
}

void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 1; i <= n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1, 1);
    for (int i = 1; i <= m; ++i) {
        cin >> a[i];
        s1[a[i]].insert(i);
    }
    for (int i = 1; i <= m - 1; ++i) {
        b[i] = lca(a[i], a[i + 1]);
        s[b[i]].insert(i);
    }
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            int pos, v;
            cin >> pos >> v;
            s1[a[pos]].erase(pos);
            s1[v].insert(pos);
            int ind = pos - 1;
            if (ind >= 1) {
                s[b[ind]].erase(ind);
            }
            if (pos <= m - 1) {
                s[b[pos]].erase(pos);
            }
            a[pos] = v;
            if (pos > 1) {
                b[pos - 1] = lca(a[pos - 1], a[pos]);
                s[b[pos - 1]].insert(pos - 1);
            }
            if (pos <= m - 1) {
                b[pos] = lca(a[pos + 1], a[pos]);
                s[b[pos]].insert(pos);
            }
        }
        else {
            int l, r, v;
            cin >> l >> r >> v;
            auto it1 = s1[v].lower_bound(l);
            if (it1 != s1[v].end() && *it1 <= r) {
                cout << *it1 << " " << *it1 << endl;
            }
            else {
                auto it = s[v].lower_bound(l);
                if (it != s[v].end()) {
                    if (*it >= r) {
                        cout << -1 << " " << -1 << endl;
                    }
                    else {
                        cout << (*it) << " " << *(it)+1 << endl;
                    }
                }
                else {
                    cout << -1 << " " << -1 << endl;
                }
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    //US
    int tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
    }
}

컴파일 시 표준 에러 (stderr) 메시지

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i = 0; i < g[u].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...