Submission #696460

#TimeUsernameProblemLanguageResultExecution timeMemory
696460Cross_RatioThrough Another Maze Darkly (CCO21_day1problem3)C++14
25 / 25
1083 ms293396 KiB
#include <bits/stdc++.h> #define int long long using namespace std; vector<int> adj[800005]; int num[800005]; struct SegTree { vector<int> seg; int MAX; SegTree(int N) { int i; for(i=1;i<2*N;i*=2) {} seg.resize(i); MAX = i; } void Edit(int n, int a) { n += MAX/2; seg[n] += a; n /= 2; while(n) { seg[n] = seg[2*n] + seg[2*n+1]; n /= 2; } } int kth(int k, int n, int ns, int ne) { if(ns+1==ne) return ns; int mid = ns + ne >> 1; if(k < seg[2*n]) return kth(k, 2*n, ns, mid); else return kth(k-seg[2*n], 2*n+1, mid, ne); } }; int ans[800005]; int dfs_cnt = 0; int A[1600005]; int lev[1600005]; vector<array<int, 2>> V; void dfs(int c, int p, int d) { if(p != -1) V.push_back({c, d}); int sz = adj[c].size(); int i, j; if(p == -1) { for(i=1;i<sz;i++) { dfs(adj[c][i], c, d); V.push_back({c, d}); } dfs(adj[c][0], c, d); V.push_back({c, d}); return; } int pt = -1; for(i=0;i<sz;i++) { if(adj[c][i] == p) pt = i; } assert(pt != -1); for(i=pt+1;i<sz;i++) { dfs(adj[c][i], c, d+(pt==0?0:1)); V.push_back({c, d+(pt==0?0:1)}); } if(pt != 0) { dfs(adj[c][0], c, d+1); V.push_back({c, d+1}); for(i=1;i<pt;i++) { dfs(adj[c][i], c, d); V.push_back({c, d}); } } } vector<int> B[800005]; vector<array<int, 2>> Query; signed main() { cin.sync_with_stdio(false); cin.tie(0); cout.tie(0); int N, Q; cin >> N >> Q; int i, j; for(i=0;i<N;i++) { int k; cin >> k; for(j=0;j<k;j++) { int c; cin >> c; adj[i].push_back(c-1); } num[i] = 1 % k; } dfs(0, -1, 1); //cout << "dfs done\n"; int sz = V.size(); SegTree tree(sz+3); int MAX = tree.MAX; int ma = 0; for(i=0;i<sz;i++) { A[i] = V[i][0]; ma = max(ma, V[i][1]); B[V[i][1]].push_back(i); } for(i=0;i<Q;i++) { int a; cin >> a; Query.push_back({a-1, i}); } sort(Query.begin(),Query.end()); int sum = 0; int cnt = 0; int qt = 0; //cout << "Start Finding\n"; //for(i=0;i<sz;i++) cout << A[i] << ' '; //cout << '\n'; for(i=1;i<=ma;i++) { for(int n : B[i]) { tree.Edit(n, 1); //cout << n << ' '; sum++; } //cout << '\n'; //cout<< i << ":\n"; while(qt<Query.size()&&Query[qt][0] < cnt + sum) { //cout << qt << " : "; int v = Query[qt][0] - cnt; //cout << v << ' '; //cout << tree.kth(v, 1, 0, MAX/2) << '\n'; ans[Query[qt][1]] = A[tree.kth(v, 1, 0, MAX/2)]; qt++; } //cout << '\n'; cnt += sum; } while(qt < Query.size()) { int v = (Query[qt][0] - cnt) % sum; ans[Query[qt][1]] = A[tree.kth(v, 1, 0, MAX/2)]; qt++; } for(i=0;i<Q;i++) cout << ans[i]+1 << '\n'; }

Compilation message (stderr)

Main.cpp: In member function 'long long int SegTree::kth(long long int, long long int, long long int, long long int)':
Main.cpp:26:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   26 |         int mid = ns + ne >> 1;
      |                   ~~~^~~~
Main.cpp: In function 'void dfs(long long int, long long int, long long int)':
Main.cpp:39:12: warning: unused variable 'j' [-Wunused-variable]
   39 |     int i, j;
      |            ^
Main.cpp: In function 'int main()':
Main.cpp:117:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |         while(qt<Query.size()&&Query[qt][0] < cnt + sum) {
      |               ~~^~~~~~~~~~~~~
Main.cpp:128:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |     while(qt < Query.size()) {
      |           ~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...