Submission #865799

#TimeUsernameProblemLanguageResultExecution timeMemory
865799mychecksedadPrize (CEOI22_prize)C++17
100 / 100
2245 ms460724 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define MOD (1000000000+7) #define MOD1 (998244353) #define pb push_back #define all(x) x.begin(), x.end() #define en cout << '\n' const int N = 1e6+100, M = 1e5+10, K = 22, MX = 30; vector<array<int, 2>> Q; void ask(int i, int j){ cout << "? " << i << ' ' << j << '\n'; Q.pb({i, j}); } int n, k, q, t, root[2], par[N][2], dep[N][2], dp[N][K][2], up[N][K][2], tin[N][2], tout[N][2], timer = 0, pref[N][2]; vector<int> g[N][2], comp, r[N]; vector<array<int, 2>> E[N][2]; vector<array<bool, 2>> vis(N); void dfs(int v, int d, int rep){ if(comp.size() < k){ comp.pb(v); } tin[v][rep] = timer++; dep[v][rep] = d; for(int u: g[v][rep]) dfs(u, d+1, rep); tout[v][rep] = timer++; } bool is_parent(int u, int v, int rep){ return tin[u][rep] <= tin[v][rep] && tout[v][rep] <= tout[u][rep]; } int _lca(int u, int v, int rep){ if(is_parent(u, v, rep)) return u; if(is_parent(v, u, rep)) return v; for(int j = K - 1; j >= 0; --j) if(!is_parent(up[u][j][rep], v, rep)) u = up[u][j][rep]; return up[u][0][rep]; } void full_ask(){ cout << "!" << endl; for(auto f: Q){ int x, y, z, w; cin >> x >> y >> z >> w; int lca1 = _lca(f[0], f[1], 0); int lca2 = _lca(f[0], f[1], 1); // cout << f[0] << ' ' << f[1] << ' ' << lca1 << ' ' << lca2 << endl; comp.pb(lca2); E[lca1][0].pb({f[0], x}); E[f[0]][0].pb({lca1, -x}); E[lca1][0].pb({f[1], y}); E[f[1]][0].pb({lca1, -y}); E[lca2][1].pb({f[0], z}); E[f[0]][1].pb({lca2, -z}); E[lca2][1].pb({f[1], w}); E[f[1]][1].pb({lca2, -w}); } } void calc(int v, int rep){ vis[v][rep] = 1; for(auto U: E[v][rep]){ int u = U[0], w = U[1]; if(!vis[u][rep]){ pref[u][rep] = pref[v][rep] + w; calc(u, rep); } } } void solve(){ cin >> n >> k >> q >> t; for(int rep = 0 ; rep < 2; ++rep){ for(int i = 1; i <= n; ++i){ int p; cin >> p; up[i][0][rep] = p; if(p != -1) g[p][rep].pb(i); else root[rep] = i; } } timer = 0; dfs(root[0], 0, 0); timer = 0; dfs(root[1], 0, 1); // cout << comp.size() << '\n'; for(int v: comp) cout << v << ' '; cout << endl; sort(all(comp), [&](const int &x, const int&y){ return tin[x][1] < tin[y][1]; }); for(int i = 0; i < k - 1; ++i) ask(comp[i], comp[i + 1]); for(int rep = 0; rep < 2; ++rep){ up[root[rep]][0][rep] = root[rep]; for(int j = 1; j < K; ++j){ for(int i = 1; i <= n; ++i){ up[i][j][rep] = up[up[i][j - 1][rep]][j - 1][rep]; } } } vis.resize(n+1); full_ask(); for(int i = 1; i <= n; ++i) pref[i][0] = pref[i][1] = 0; calc(root[0], 0); calc(root[1], 1); for(int v: comp){ if(!vis[v][0]) calc(v, 0); if(!vis[v][1]) calc(v, 1); } vector<array<int, 2>> qq; for(;t--;){ int u, v; cin >> u >> v; qq.pb({u, v}); } for(auto [u, v]: qq){ for(int rep = 0; rep < 2; ++rep){ int lca = _lca(u, v, rep); cout << pref[v][rep] + pref[u][rep] - 2 * pref[lca][rep] << ' '; } en; } cout << endl; } int main(){ cin.tie(0); ios::sync_with_stdio(0); int tt = 1, aa; // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(tt--){ solve(); } // cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n"; return 0; }

Compilation message (stderr)

Main.cpp: In function 'void dfs(int, int, int)':
Main.cpp:24:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   24 |   if(comp.size() < k){
      |      ~~~~~~~~~~~~^~~
Main.cpp: In function 'int main()':
Main.cpp:137:15: warning: unused variable 'aa' [-Wunused-variable]
  137 |   int tt = 1, aa;
      |               ^~
#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...