제출 #1324589

#제출 시각아이디문제언어결과실행 시간메모리
1324589adscodingBirthday gift (IZhO18_treearray)C++20
0 / 100
11 ms19276 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#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 FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __prine_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        cerr << "  ,  ";
        ++s;
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = {0, (__prine_one(s, args), 0)...};
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, const X &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, const X &b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

// --------------------------------------------------------------------------------------------

const int maxn = 2e5 + 3;
int n, m, q, h[maxn], lg[maxn << 1], tour[maxn << 1][20], tin[maxn], tdfs, a[maxn];
vector<int> g[maxn];

// --------------------------------------------------------------------------------------------

void dfs_pre(int u, int p)
{
    tin[u] = ++tdfs;
    tour[tdfs][0] = u;
    for (int v : g[u])
    {
        if (v == p) continue;
        h[v] = h[u] + 1;
        dfs_pre(v, u);
        tour[++tdfs][0] = u;
    }
}

#define MIN_H(x, y) (h[x] < h[y] ? x : y)

int lca(int u, int v)
{
    if (u > v) swap(u, v);
    int k = lg[v - u + 1];
    return MIN_H(tour[u][k], tour[v - (1 << k) + 1][k]);
}

set<int> active[maxn];
set<int> seg2[maxn]; 

void solve()
{
    cin >> n >> m >> q;
    FOR(i, 2, n)
    {
        int u, v; cin >> u >> v;
        if (u > v) swap(u, v);
        g[u].push_back(v);
        g[v].push_back(u);
    }

    dfs_pre(1, -1);

    FOR(j, 1, 19)
        FOR(i, 1, tdfs - (1 << j) + 1)
            tour[i][j] = MIN_H(tour[i][j - 1], tour[i + (1 << j - 1)][j - 1]);
    FOR(i, 2, tdfs)
        lg[i] = lg[i >> 1] + 1;

    FOR(i, 1, m)
    {
        cin >> a[i];
        active[a[i]].insert(i);
        if (i > 1)
        {
            seg2[lca(a[i], a[i - 1])].insert(i - 1);
        }
    }

    while (q--)
    {
        int op; cin >> op;
        if (op == 1)
        {
            int pos, val; cin >> pos >> val;
            active[a[pos]].erase(pos);
            active[val].insert(pos);
            if (pos > 1)
            {
                seg2[lca(a[pos - 1], a[pos])].erase(pos - 1);
                seg2[lca(a[pos - 1], val)].insert(pos - 1);
            }
            if (pos < m)
            {
                seg2[lca(a[pos], a[pos + 1])].erase(pos);
                seg2[lca(val, a[pos + 1])].insert(pos);
            }

            a[pos] = val;
        }
        else
        {
            int l, r, v; cin >> l >> r >> v;
            auto it = active[v].lower_bound(l);
            if (it != active[v].end() && (*it) <= r)
            {
                cout << (*it) << " " << (*it) << '\n';
                continue;
            }
            if (r > l)
            {
                it = seg2[v].lower_bound(l);
                if (it != seg2[v].end() && (*it) + 1 <= r)
                {
                    cout << (*it) << " " << (*it) + 1 << '\n';
                    continue;
                }
            }
            cout << "-1 -1\n";
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

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

treearray.cpp: In function 'int main()':
treearray.cpp:182:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  182 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:183:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  183 |         freopen(TASK".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...