Submission #765384

#TimeUsernameProblemLanguageResultExecution timeMemory
765384vjudge1Roadside Advertisements (NOI17_roadsideadverts)C++17
100 / 100
67 ms22604 KiB
#include "bits/stdc++.h" using namespace std; // #define ORD_SET // #define ROPE #ifdef ORD_SET #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #endif #ifdef ROPE #include <ext/rope> using namespace __gnu_cxx; #endif // #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") using ll = long long; using ld = long double; #define pb push_back #define ff first #define ss second #define sz(x) (ll)(x).size() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); } void ioigold2024insh() { ios_base::sync_with_stdio(false); cin.tie(NULL); } void pre(ll a) { cout<<fixed<<setprecision(a); } ll bit(ll a) { return __builtin_popcountll(a); } ll binmul(ll a, ll b, ll c) { ll res = 0; while(b) { if(b&1) (res += a) %= c; (a += a) %= c; b >>= 1; } return res; } ll binpow(ll a, ll b, ll c) { ll res = 1; while(b) { if(b&1) (res *= a) %= c; (a *= a) %= c; b >>= 1; } return res; } template<typename T> T gcd(T a, T b) { if(b==0) return a; return gcd(b, a%b); } template<typename T> T lcm(T a, T b) { return a/gcd(a, b)*b; } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ld rnd() { return rng()%INT_MAX*1.0/INT_MAX; } struct sn { ll l, r, x; }; const ll mod = 1e9+7, inf = 1e18+7, MX = LLONG_MAX, MN = LLONG_MIN; const ll N = 3e5+5; vector<pair<ll, ll>> g[N]; ll up[N][20], pr[N], d[N], tin[N], tout[N], timer; void dfs(ll v, ll p) { up[v][0] = p; for(ll i=1; i<=16; i++) up[v][i] = up[up[v][i-1]][i-1]; tin[v] = ++timer; for(auto [to, w]: g[v]) { if(to==p) continue; pr[to] = pr[v]+w, d[to] = d[v]+1; dfs(to, v); } tout[v] = ++timer; } ll lca(ll x, ll y) { if(d[x]<d[y]) swap(x, y); ll k = d[x]-d[y]; for(ll i=16; i>=0; i--) { if(k&(1<<i)) x = up[x][i]; } if(x==y) return x; for(ll i=16; i>=0; i--) { if(up[x][i]!=up[y][i]) x = up[x][i], y = up[y][i]; } return up[x][0]; } void kigash() { ll n; cin>>n; for(ll i=1; i<n; i++) { ll u, v, w; cin>>u>>v>>w; g[++u].pb({++v, w}), g[v].pb({u, w}); } dfs(1, 0); ll q; cin>>q; for(ll t=1; t<=q; t++) { ll res = 0; vector<pair<ll, ll>> v; for(ll j=1; j<=5; j++) { ll x; cin>>x, x++; v.pb({tin[x], x}); } sort(all(v)); for(ll j=0; j<5; j++) { ll x = v[j].ss, y = v[(j+1)%5].ss; ll lc = lca(x, y); res += pr[x]+pr[y]-2*pr[lc]; } cout<<res/2<<"\n"; } return; } signed main(/*Kigash Amir*/) { // freopen(""); ioigold2024insh(); ll tt = 1; // cin>>tt; for(ll i=1; i<=tt; i++) kigash(); exit(0); }

Compilation message (stderr)

roadsideadverts.cpp: In function 'void freopen(std::string)':
roadsideadverts.cpp:31:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
      |                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp:31:73: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); 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...