Submission #1270227

#TimeUsernameProblemLanguageResultExecution timeMemory
1270227vuquangsangBirthday gift (IZhO18_treearray)C++20
56 / 100
633 ms80448 KiB
#include <bits/stdc++.h>
using namespace std;

#define     el "\n"
#define     FOR(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define     FORD(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define     pb push_back
#define     fi first
#define     se second
#define     all(x) x.begin(),x.end()
#define     lg(x) __lg(x)
#define     alla(a,n) a+1,a+n+1
#define     ll long long


template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}


const int N = 2e5 + 2;

int n, q, a[N], m;
vector<int> adj[N];
void inp()
{
    cin >> n >> m >> q;
    FOR(i, 1, n - 1) {
        int x, y; cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    FOR(i, 1, m) cin >> a[i];
}

/* Try your best
    No regrets */

namespace subtask_1
{
    int h[N], up[N][21];
    void dfs(int u, int p)
    {
        up[u][0] = p;
        FOR(j, 1, lg(n)) up[u][j] = up[up[u][j - 1]][j - 1];
        for(int v : adj[u]) if(v != p) {
            h[v] = h[u] + 1;
            dfs(v, u);
        }
    }
    int LCA(int u, int v)
    {
        if(h[u] < h[v]) swap(u, v);
        FORD(j, lg(n), 0) if(h[up[u][j]] >= h[v]) u = up[u][j];
        if(u == v) return u;
        FORD(j, lg(n), 0) if(up[u][j] != up[v][j]) {
            u = up[u][j];
            v = up[v][j];
        }
        return up[u][0];
    }

    set<int> S[N];
    set<int> isValid[N];
    void slv()
    {
        dfs(1, 1);

        FOR(i, 1, m - 1) {
            S[LCA(a[i], a[i + 1])].insert(i);
        }

        FOR(i, 1, m) {
            isValid[a[i]].insert(i);
        }

        while(q--) {
            int type; cin >> type;
            if(type == 1) {
                int pos, v; cin >> pos >> v;

                if(pos <= n - 1) S[LCA(a[pos], a[pos + 1])].erase(pos);
                if(pos >= 2) S[LCA(a[pos - 1], a[pos])].erase(pos - 1);

                isValid[a[pos]].erase(pos);

                a[pos] = v;

                if(pos <= n - 1) S[LCA(a[pos], a[pos + 1])].insert(pos);
                if(pos >= 2) S[LCA(a[pos - 1], a[pos])].insert(pos - 1);

                isValid[a[pos]].insert(pos);

//                FOR(i, 1, n) {
//                    cout << "node" << " " << i << el;
//                    for(int x : S[i]) {
//                        cout << x << " ";
//                    } cout << el;
//                }
            }
            else {
                int l, r, v; cin >> l >> r >> v;

                auto pref = isValid[v].lower_bound(l);
                if(pref != isValid[v].end() && *pref <= r) {
                    int pos = *pref;
                    cout << pos << " " << pos << el;
                    continue;
                }

                auto it = S[v].lower_bound(l);
                int nxt = *it;

                if(it == S[v].end() || nxt > r - 1) {
                    cout << -1 << " " << -1 << el;
                }
                else {
                    cout << nxt << " " << nxt + 1 << el;
                }
            }
        }
    }
}

/* Code slowly, think carefully */

main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    #define __Azul__ "IZhO18_treearray"
    if(fopen(__Azul__".inp", "r")) {
        freopen(__Azul__".inp", "r", stdin);
        freopen(__Azul__".out", "w", stdout);
    }

    bool qs = 0;

    int T = 1;
    if(qs) cin >> T;
    while(T--) {
        inp();
        subtask_1::slv();
    }

    cerr << "\nTime" << 0.001 * clock() << "s "; return 0;


}





Compilation message (stderr)

treearray.cpp:126:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  126 | main()
      | ^~~~
treearray.cpp: In function 'int main()':
treearray.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |         freopen(__Azul__".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |         freopen(__Azul__".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...