제출 #1278175

#제출 시각아이디문제언어결과실행 시간메모리
1278175phuocrucppTourism (JOI23_tourism)C++20
0 / 100
5091 ms21052 KiB
/*ㅤ∧_∧  ( ・∀・)  ( つ┳⊃ ε (_)へ⌒ヽフ (  ( ・ω・) ◎―◎ ⊃ ⊃ BePhuongSuperSuwi From TK4 - CHT ㅤㅤ/ ⌒\____   /・   )  \  /ノへ ノ    /| ノ   \\ |/_/_/*/ #include<bits/stdc++.h> #define task "main" //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define endl '\n' #define int long long #define pb push_back #define fi first #define se second #define ii pair<int,int> #define iii pair<int,ii> #define iv pair<ii, ii> #define base 341 #define MASK(i) (1ll << i) #define oo 1e18 #define isOn(x,i) ((x) & MASK(i)) #define bitOn(x,i) ((x) | MASK(i)) #define bitOff(x,i) ((x) & ~MASK(i)) #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> #define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b)) using namespace std; //using namespace __gnu_pbds; const int maxn = 1e5 + 5; const int lg = 18; int n, m, test, tin[maxn], timer, c[maxn], h[maxn], par[maxn][lg + 2], tour[maxn]; vector <int> g[maxn]; void dfs(int u, int p) { tin[u] = ++timer; tour[timer] = u; for (int x : g[u]) { if (x == p) continue; h[x] = h[u] + 1; par[x][0] = u; dfs(x, u); } } int lca(int u, int v) { if (h[u] < h[v]) swap(u,v); for (int i = lg; i >= 0; i--) { if ((h[u] - h[v]) >= MASK(i)) u = par[u][i]; } if (u == v) return u; for (int i = lg; i >= 0; i--) { if (par[u][i] != par[v][i]) { u = par[u][i]; v = par[v][i]; } } return par[u][0]; } void prelca() { dfs(1, -1); for (int j = 1; j <= lg; j++) { for (int i = 1; i <= n; i++) { par[i][j] = par[par[i][j-1]][j-1]; } } } int dist(int l, int r) { return h[l] + h[r] - 2 * h[lca(l, r)]; } int S; struct pt{ int l, r, id; }; bool ss(pt a, pt b) { if (a.l/S != b.l/S) return a.l < b.l; if(a.l/S&1) return a.r < b.r; else return a.r>b.r; } bool vs[maxn]; int sum, res[maxn]; set <int> st; void add(int x) { vs[x] ^= 1; if (vs[x]) { if (st.empty()) { st.insert(tin[x]); return; } auto it = st.lower_bound(tin[x]); int t1 = 0, t2 = 0; if (it != st.end()) t2 = tour[*it]; else t2 = tour[*st.begin()]; if (it != st.begin()) { it--; t1 = tour[*it]; } else t1 = tour[*st.rbegin()]; // cout << "+ " << t1 << " " << t2 << " " << x << endl; sum += dist(t1, x) + dist(t2, x) - dist(t1, t2); st.insert(tin[x]); } else { if (st.size() == 1) { st.erase(tin[x]); sum = 0; return; } auto it = st.lower_bound(tin[x]); int t1 = 0, t2 = 0; it++; if (it != st.end()) t2 = tour[*it]; else t2 = tour[*st.begin()]; it--; if (it != st.begin()) { it--; t1 = tour[*it]; } else t1 = tour[*st.rbegin()]; sum -= dist(t1, x) + dist(t2, x) - dist(t1, t2); st.erase(tin[x]); } } 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); } cin >> n >> m >> test; for (int i = 1; i < n; i++) { int l, r; cin >> l >> r; g[l].pb(r); g[r].pb(l); } prelca(); for (int i = 1; i <= m; i++) cin >> c[i]; vector <pt> query; S = sqrt(test); for (int i = 1; i <= test; i++) { int l, r; cin >> l >> r; query.pb({l, r, i}); } sort(query.begin(), query.end(), ss); int L = 1, R = 0; for (auto q : query) { // cout << q.l << " " << q.r << endl; while(L > q.l){ L--; add(c[L]); } while(R < q.r) { R++; add(c[R]); } while(L < q.l) { add(c[L]); L++; } while(R > q.r) { add(c[R]); R--; } res[q.id] = sum / 2 + 1; } for (int i = 1; i <= test; i++) cout << res[i] << endl; }

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

tourism.cpp:146:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  146 | main() {
      | ^~~~
tourism.cpp: In function 'int main()':
tourism.cpp:149:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |         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...