제출 #1277936

#제출 시각아이디문제언어결과실행 시간메모리
1277936icebearTourism (JOI23_tourism)C++20
100 / 100
4639 ms36060 KiB
// ~~ icebear ~~ #include <bits/stdc++.h> using namespace std; //#define int long long typedef long long ll; typedef pair<int, int> ii; typedef pair<int, ii> iii; template<class T> bool minimize(T &a, const T &b) { if (a > b) return a = b, true; return false; } template<class T> bool maximize(T &a, const T &b) { if (a < b) return a = b, true; return false; } #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORR(i,a,b) for(int i=(a); i>=(b); --i) #define REP(i, n) for(int i=0; i<(n); ++i) #define RED(i, n) for(int i=(n)-1; i>=0; --i) #define MASK(i) (1LL << (i)) #define BIT(S, i) (((S) >> (i)) & 1) #define mp make_pair #define pb push_back #define fi first #define se second #define all(x) x.begin(), x.end() #define task "kingdom" const int MOD = 1e9 + 7; const int inf = 1e9 + 27092008; const ll INF = 1e18 + 27092008; const int N = 50 * 50 * 50 + 5; const int S = 555; struct Query { int l, r, id; bool operator < (const Query &other) const { if (l / S != other.l / S) return l < other.l; return ((l / S) & 1 ? r > other.r : r < other.r); } } Q[N]; int n, m, q, c[N]; vector<int> G[N]; int h[N], minH[N << 1][20], tour[N << 1], pos[N], tin[N], tout[N], counter = 0, timer = 0; void dfs(int u, int par) { pos[u] = ++timer; tin[u] = ++counter; tour[timer] = u; for(int v : G[u]) if (v != par) { h[v] = h[u] + 1; dfs(v, u); tour[++timer] = u; } tout[u] = counter; } #define MIN_HIGH(x, y) (h[x] < h[y] ? x : y) int LCA(int u, int v) { int l = pos[u]; int r = pos[v]; if (l > r) swap(l, r); int k = __lg(r - l + 1); return MIN_HIGH(minH[l][k], minH[r - MASK(k) + 1][k]); } int dist(int u, int v) { int p = LCA(u, v); return h[u] + h[v] - 2 * h[p]; } bool inSubtree(int u, int v) { return tin[u] <= tin[v] && tout[v] <= tout[u]; } int L, R, res = 1, ans[N]; set<int> nodes; int cnt[N]; void add(int u) { u = c[u]; cnt[u]++; if (cnt[u] > 1) return; auto it = nodes.insert(pos[u]).first; int prv = (it == nodes.begin() ? *prev(nodes.end()) : *prev(it)); int nxt = (next(it) == nodes.end() ? *nodes.begin() : *next(it)); prv = tour[prv]; nxt = tour[nxt]; res -= -h[LCA(prv, nxt)] + h[LCA(u, prv)] + h[LCA(u, nxt)]; res += h[u]; } void del(int u) { u = c[u]; cnt[u]--; if (cnt[u] > 0) return; auto it = nodes.find(pos[u]); int prv = (it == nodes.begin() ? *prev(nodes.end()) : *prev(it)); int nxt = (next(it) == nodes.end() ? *nodes.begin() : *next(it)); prv = tour[prv]; nxt = tour[nxt]; res += -h[LCA(prv, nxt)] + h[LCA(u, prv)] + h[LCA(u, nxt)]; res -= h[u]; nodes.erase(pos[u]); } void init(void) { cin >> n >> m >> q; FOR(i, 2, n) { int u, v; cin >> u >> v; G[u].pb(v); G[v].pb(u); } FOR(i, 1, m) cin >> c[i]; FOR(i, 1, q) cin >> Q[i].l >> Q[i].r, Q[i].id = i; } void process(void) { dfs(1, -1); FOR(i, 1, timer) minH[i][0] = tour[i]; FOR(j, 1, 19) FOR(i, 1, timer - MASK(j) + 1) minH[i][j] = MIN_HIGH(minH[i][j - 1], minH[i + MASK(j - 1)][j - 1]); sort(Q + 1, Q + q + 1); L = Q[1].l, R = Q[1].r; FOR(i, L, R) add(i); ans[Q[1].id] = res; FOR(i, 2, q) { int l = Q[i].l, r = Q[i].r; while(L > l) add(--L); while(R < r) add(++R); while(L < l) del(L++); while(R > r) del(R--); ans[Q[i].id] = res; } FOR(i, 1, q) cout << ans[i] << '\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } int tc = 1; // cin >> tc; while(tc--) { init(); process(); } return 0; }

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

tourism.cpp: In function 'int main()':
tourism.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |         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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...