Submission #1359113

#TimeUsernameProblemLanguageResultExecution timeMemory
1359113retardeTourism (JOI23_tourism)C++20
28 / 100
5094 ms33184 KiB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define int long long
#define all(x) (x).begin(), (x).end()

typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;

const int mod = 1e9 + 7;
const int inf = INTMAX_MAX;
const bool tc = false;

const int mxn = 1e5 + 5;
vi adj[mxn]; int tin[mxn], tout[mxn], dep[mxn], c[mxn], up[mxn][20];

int timer = -1;
void dfs(int node, int prev, int dp) {
    tin[node] = ++timer;
    dep[node] = dp;
    up[node][0] = prev;
    for (auto &neighbour : adj[node]) {
        if (neighbour == prev) continue;
        dfs(neighbour, node, dp + 1);
    }
    tout[node] = timer;
}

int lca(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    if (tin[v] <= tin[u] && tout[u] <= tout[v]) return v;
    for (int k = 19; k >= 0; k--) {
        if (!up[u][k]) continue;
        if (dep[up[u][k]] >= dep[v]) u = up[u][k];
    }
    for (int k = 19; k >= 0; k--) {
        if (up[u][k] == up[v][k]) continue;
        u = up[u][k]; v = up[v][k];
    }
    return up[u][0];
}
int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }

const int sq = 325;
struct qry {
    int l, r, b, i;
    bool operator<(const qry &other) const {
        if (b != other.b) return b < other.b;
        else {
            if (b % 2) {
                return r < other.r;
            } else {
                return r > other.r;
            }
        }
    }
};

inline void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        adj[u].pb(v); adj[v].pb(u);
    }
    dfs(1, 0, 0);
    for (int k = 1; k < 20; k++) {
        for (int i = 1; i <= n; i++) {
            up[i][k] = up[up[i][k - 1]][k - 1];
        }
    }
    for (int i = 0; i < m; i++) cin >> c[i];

    vector<qry> queries(q); vi ans(q); for (int i = 0; i < q; i++) {
        int l, r; cin >> l >> r;
        l--; r--;
        queries[i] = {l, r, l/sq, i};
    }
    sort(all(queries));

    int L = 0, R = -1;
    set<pii> act; mii cnt; int curans = 0;
    for (auto q : queries) {
        while (L > q.l) {
            L--;
            int node = c[L];
            cnt[node]++;
            if (cnt[node] == 1) {
                pii cur = {tin[node], node};
                act.insert(cur);
                if (!(act.size() == 1)) {
                    auto it1 = act.lower_bound(cur);
                    auto it0 = it1; it0--;
                    auto it2 = it1; it2++;
                    if (it1 == act.begin()) {
                        it0 = act.end();
                        it0--;
                    }
                    if (it2 == act.end()) {
                        it2 = act.begin();
                    }
                    int prev = (*it0).se; int next = (*it2).se; int curn = cur.se;
                    curans -= dist(prev, next);
                    curans += dist(prev, curn) + dist(curn, next);
                }
            }
        }
        while (R < q.r) {
            R++;
            int node = c[R];
            cnt[node]++;
            if (cnt[node] == 1) {
                pii cur = {tin[node], node};
                act.insert(cur);
                if (!(act.size() == 1)) {
                    auto it1 = act.lower_bound(cur);
                    auto it0 = it1; it0--;
                    auto it2 = it1; it2++;
                    if (it1 == act.begin()) {
                        it0 = act.end();
                        it0--;
                    }
                    if (it2 == act.end()) {
                        it2 = act.begin();
                    }
                    int prev = (*it0).se; int next = (*it2).se; int curn = cur.se;
                    curans -= dist(prev, next);
                    curans += dist(prev, curn) + dist(curn, next);
                }
            }
        }
        while (L < q.l) {
            int node = c[L];
            cnt[node]--;
            if (cnt[node] == 0) {
                pii cur = {tin[node], node};
                if (!(act.size() == 1)) {
                    auto it1 = act.lower_bound(cur);
                    auto it0 = it1; it0--;
                    auto it2 = it1; it2++;
                    if (it1 == act.begin()) {
                        it0 = act.end();
                        it0--;
                    }
                    if (it2 == act.end()) {
                        it2 = act.begin();
                    }
                    int prev = (*it0).se; int next = (*it2).se; int curn = cur.se;
                    curans += dist(prev, next);
                    curans -= dist(prev, curn) + dist(curn, next);
                }
                act.erase(cur);
            }
            L++;
        }
        while (R > q.r) {
            int node = c[R];
            cnt[node]--;
            if (cnt[node] == 0) {
                pii cur = {tin[node], node};
                if (!(act.size() == 1)) {
                    auto it1 = act.lower_bound(cur);
                    auto it0 = it1; it0--;
                    auto it2 = it1; it2++;
                    if (it1 == act.begin()) {
                        it0 = act.end();
                        it0--;
                    }
                    if (it2 == act.end()) {
                        it2 = act.begin();
                    }
                    int prev = (*it0).se; int next = (*it2).se; int curn = cur.se;
                    curans += dist(prev, next);
                    curans -= dist(prev, curn) + dist(curn, next);
                }
                act.erase(cur);
            }
            R--;
        }
        ans[q.i] = curans/2 + 1;
    }

    for (auto &x : ans) cout << x << '\n';
}

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

signed main() {
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    //setIO();

    int t = 1;
    if (tc) {
        cin >> t;
    }

    while (t--) {
        solve();
    }
}

/*
set<pii> act; act.insert({tin[c[l]], c[l]});
int ans = 0;
for (int i = l + 1; i <= r; i++) {
    pii cur = mp(tin[c[i]], c[i]);
    if (act.find(cur) != act.end()) continue;
    act.insert(cur);
    auto it1 = act.lower_bound(cur);
    auto it0 = it1; it0--;
    auto it2 = it1; it2++;
    if (it1 == act.begin()) {
        it0 = act.end();
        it0--;
    }
    if (it2 == act.end()) {
        it2 = act.begin();
    }
    int prev = (*it0).se; int next = (*it2).se; int curn = cur.se;
    ans -= dist(prev, next);
    ans += dist(prev, curn) + dist(curn, next);
}
cout << ans/2 + 1 << '\n';
*/

Compilation message (stderr)

tourism.cpp: In function 'void setIO(std::string)':
tourism.cpp:206:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  206 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:207:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  207 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...