답안 #608706

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
608706 2022-07-27T09:28:26 Z messiuuuuu Birthday gift (IZhO18_treearray) C++14
0 / 100
21 ms 42712 KB
///https://codeforces.com/group/Chft8wRRWW/contest/391796/problem/D
///https://oj.uz/problem/submit/IZhO18_treearray
#include<bits/stdc++.h>
#define task "C"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 2e5 + 5;
const ll INF = 1e18 + 5;
const int LOGN = 19;

int n, m, q;
vector<int> Adj[MAXN];
int a[MAXN];

void Input()
{
    cin >> n >> m >> q;
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        Adj[u].pb(v);
        Adj[v].pb(u);
    }
    for (int i = 1; i <= m; i++)
    {
        cin >> a[i];
    }
}

int P[MAXN][LOGN], dep[MAXN];

void DFS(int u, int par)
{
    for (int v : Adj[u])
    {
        if (v == par)
        {
            continue;
        }
        dep[v] = dep[u] + 1;
        P[v][0] = u;
        DFS(v, u);
    }
}

int LCA(int u, int v)
{
    if (dep[u] > dep[v])
        swap(u, v);
    for (int i = LOGN - 1; i >= 0; i--)
    {
        if (dep[u] <= dep[P[v][i]])
        {
            v = P[v][i];
        }
    }
    for (int i = LOGN - 1; i >= 0; i--)
    {
        if (P[u][i] != P[v][i])
        {
            u = P[u][i];
            v = P[v][i];
        }
    }
    if (u != v)
    {
        u = P[u][0];
    }
    return u;
}

multiset<int> st[4 * MAXN];

void Update(int id, int l, int r, int p, int oldval, int nval)
{
    if (r < p || p < l)
        return;

    if (oldval != -1)
        st[id].erase(st[id].find(oldval));
    st[id].insert(nval);

    if (l == p && p == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    Update(2 * id, l, mid, p, oldval, nval);
    Update(2 * id + 1, mid + 1, r, p, oldval, nval);
}

int sta, lft, rght;

void GetAns(int id, int l, int r, int u, int v, int val)
{
    if (u > v || r < u || v < l)
        return;
    if (u <= l && r <= v)
    {
        if (st[id].count(val))
        {
            sta = id;
            lft = l;
            rght = r;
        }
        return;
    }
    int mid = (l + r) >> 1;
    GetAns(2 * id, l, mid, u, v, val);
    GetAns(2 * id + 1, mid + 1, r, u, v, val);
}

int Walk(int id, int l, int r, int val)
{
    if (l == r)
    {
        return l;
    }
    int mid = (l + r) >> 1;
    if (st[2 * id].count(val))
        return Walk(2 * id, l, mid, val);
    else
        return Walk(2 * id + 1, mid + 1, r, val);
}

void Solve()
{
    dep[1] = 1;
    DFS(1, 0);
    for (int i = 1; i < LOGN; i++)
        for (int j = 1; j <= n; j++)
            P[j][i] = P[j][P[j][i - 1]];

    for (int i = 1; i <= m; i++)
    {
        Update(1, 1, m, i, -1, a[i]);
        if (i < m)
            Update(1, 1, m, i, -1, LCA(a[i], a[i + 1]));
    }

    while (q-- > 0)
    {
        int t, l, r;
        cin >> t >> l >> r;
        if (t == 1)
        {
            if (l < m)
                Update(1, 1, m, l, LCA(a[l], a[l + 1]), LCA(r, a[l + 1]));
            if (l > 1)
                Update(1, 1, m, l - 1, LCA(a[l - 1], a[l]), LCA(a[l - 1], r));
            Update(1, 1, m, l, a[l], r);
            a[l] = r;
        } else
        {
            int k;
            cin >> k;
            sta = -1;
            GetAns(1, 1, m, l, r - 1, k);
            if (sta == -1)
            {
                if (a[r] == k)
                {
                    cout << r << ' ' << r << '\n';
                } else
                    cout << -1 << ' ' << -1 << '\n';
            } else
            {
                int res = Walk(sta, lft, rght, k);
                cout << res << ' ' << res + (res < m && LCA(a[res], a[res + 1]) == k) << '\n';
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(task".INP","r"))
    {
        freopen(task".INP","r",stdin);
        //freopen(task".OUT","w",stdout);
    }
    Input();
    Solve();
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:186:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  186 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 42580 KB n=5
2 Incorrect 21 ms 42712 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 42580 KB n=5
2 Incorrect 21 ms 42712 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 42580 KB n=5
2 Incorrect 21 ms 42712 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 42580 KB n=5
2 Incorrect 21 ms 42712 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -