Submission #1143650

#TimeUsernameProblemLanguageResultExecution timeMemory
1143650agrim_09Solar Storm (NOI20_solarstorm)C++20
36 / 100
449 ms254928 KiB
// #pragma GCC optimize("O3,unroll-loops") // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define endl '\n'; #define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define py cout << "YES" << endl; #define pn cout << "NO" << endl; #define pb push_back #define int long long typedef long double lld; #define double lld typedef long long ll; typedef unsigned long long ull; const ll inf = 1e18; const ll mod = 1e9+7; const int N = 2e5; vector<int>primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; // Check for queue, priorioty_queue, stack, ordered_set solutions // stack => LIFO (whatever goes in last comes out last) // queue => FIFO (whatever goes in first comes out first) // priority_queue => Dynamic queries of minimum/maximum // ordered_set => set in vector form //[order_of_key(k) gives number of elements less than k, while find_by_order(i) gives i^th element] // To comment multiple lines : ctrl + / // To find and replace : ctrl+H void judge(){ srand(time(NULL)); #ifndef ONLINE_JUDGE freopen("1.txt","r",stdin); freopen("2.txt","w",stdout); freopen("Error.txt", "w", stderr); #define debug(x...) cerr << #x <<" = "; _print(x); cerr << endl; #else #define debug(x...) #endif } void usaco(string s) { freopen((s + ".in").c_str(),"r",stdin); freopen((s + ".out").c_str(),"w",stdout); } int n,d,k; vector<int>a,vals; vector<int>adj,extra, prefix; int curr_sum = 0; int summation(int l, int r){ if(l>r){ return 0; } if(l==0){ return prefix[r]; } return prefix[r] - prefix[l-1]; } void build_adj(){ prefix.resize(n); prefix[0] = vals[0]; for(int i = 1;i<n;i++) prefix[i] = prefix[i-1] + vals[i]; adj.resize(n+1), extra.resize(n); vector<int>pref(n); pref[0] = 0; for(int i = 1;i<n;i++) pref[i] = pref[i-1] + a[i-1]; for(int i = 1;i<n;i++){ auto it = upper_bound(pref.begin(),pref.end(),pref[i-1]+k); adj[i-1] = it - pref.begin(); } extra[0] = 0; for(int i = 1;i<n;i++){ auto it = lower_bound(pref.begin(),pref.end(),pref[i]-k); int idx = it - pref.begin(); extra[i] += summation(idx,i-1); } adj[n-1] = n, adj[n] = n; } struct bin_node { int vertex, depth, jump, par, subSize; int dist1 = 0; // dist between node and node.par int dist2 = 0; // dist between node and node.jump }; struct ans_node{ int vertex, dist; }; ans_node unite(ans_node a, ans_node b){ ans_node ans; ans.vertex = b.vertex; ans.dist = a.dist + b.dist; return ans; } struct BinaryJump{ vector<bin_node> info; int root; BinaryJump(const vector<vector<int>>& adj, int r) { info.resize(adj.size()); root = r; build(root, root, adj); } void build(int node, int par, const vector<vector<int>>& adj) { if(node==root){ info[root].vertex = root; info[root].depth = 0; info[root].par = info[root].jump = root; for(auto child : adj[node]) { if(par==child){ continue; } build(child, node, adj); info[node].subSize += info[child].subSize; } return; } info[node].vertex = node; info[node].depth = info[par].depth + 1; info[node].par = par; info[node].subSize = 1; info[node].dist1 = summation(node,info[node].par-1); // Subject to change info[node].dist2 = info[node].dist1; // Subject to change int par2 = info[par].jump; if(info[par].depth - info[info[par].jump].depth==info[par2].depth - info[info[par2].jump].depth){ info[node].jump = info[par2].jump; info[node].dist2 += info[par].dist2; // Subject to change } else{ info[node].jump = par; } for(auto child : adj[node]) { if(par==child){ continue; } build(child, node, adj); info[node].subSize += info[child].subSize; } } ans_node kthPar(int node, int k) const { int sum = 0; // of the dist while (k > 0 && node >= 0 and node!=root) { if (info[node].depth - k <= info[info[node].jump].depth) { k -= info[node].depth - info[info[node].jump].depth; sum += info[node].dist2; // Subject to change node = info[node].jump; } else { k--; sum += info[node].dist1; // Subject to change node = info[node].par; } } ans_node a; a.vertex = node; a.dist = sum; if(k>0){ a.vertex = -1; } return a; } }; void final_answer(){ vector<vector<int>>list(n+1); for(int i = 0;i<n;i++){ list[i].pb(adj[i]); list[adj[i]].pb(i); } BinaryJump binjump(list,n); int vertex = binjump.kthPar(0,d).vertex; int best = 0, maxi = summation(0,vertex-1) + extra[0]; for(int i = 1;i<n;i++){ vertex = binjump.kthPar(i,d).vertex; int curr = summation(i,vertex-1) + extra[i]; if(curr>maxi){ maxi = curr, best = i; } } vector<int>answer; while(d--){ if(best==n){ break; } answer.pb(best); best = adj[best]; } cout << answer.size() << endl; for(auto x : answer){ cout << x + 1 << ' '; } } signed main(){ fastio; //judge(); cin >> n >> d >> k; a.resize(n-1), vals.resize(n+1); extra.resize(n); for(auto &x : a) cin >> x; for(auto &x : vals) cin >> x; build_adj(); final_answer(); }

Compilation message (stderr)

SolarStorm.cpp: In function 'void judge()':
SolarStorm.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen("1.txt","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     freopen("2.txt","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     freopen("Error.txt", "w", stderr);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp: In function 'void usaco(std::string)':
SolarStorm.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     freopen((s + ".in").c_str(),"r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     freopen((s + ".out").c_str(),"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...
#Verdict Execution timeMemoryGrader output
Fetching results...