Submission #992479

#TimeUsernameProblemLanguageResultExecution timeMemory
992479PedroBigManIslands (IOI08_islands)C++14
70 / 100
340 ms131072 KiB
#pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #pragma GCC optimize("Ofast") #include <iostream> #include <vector> #include <cmath> #include <algorithm> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <list> #include <iomanip> #include <stdlib.h> #include <time.h> #include <cstring> using namespace std; typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; #define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++) #define pb push_back #define mp make_pair #define pl pair<ll,ll> #define ff first #define ss second #define whole(x) x.begin(),x.end() #define DEBUG(i) cout<<"Pedro "<<i<<endl #define INF 1000000000000000000LL #define EPS ((ld)0.00000000001) #define pi ((ld)3.141592653589793) #define VV(vvvv,NNNN,xxxx); REP(iiiii,0,NNNN) {vvvv.pb(xxxx);} ll mod=1000000007; template<class A=ll> void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;} template<class A=ll> void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}} vector<ll> f, w; vector<bool> visited; vector<vector<ll> > adj; ll nei; ll bestpath; vector<ll> Floyd(ll s) //Floyd's Algorithm, O(N) time, O(1) memory, return <element of cycle,length od cycle> { ll a=f[s]; ll b=f[f[s]]; while(a!=b) {a=f[a]; b=f[f[b]];} vector<ll> ans; ans.pb(a); b=f[a]; while(b!=a) {ans.pb(b); b=f[b];} return ans; } ll DFS(ll s) { visited[s]=true; ll ans = 0; ll thisval; REP(i,0,adj[s].size()) { nei = adj[s][i]; if(visited[nei]) {continue;} thisval = w[nei]+DFS(nei); bestpath = max(bestpath,ans+thisval); ans = max(ans,thisval); } return ans; } class ST { public: ll N; class SV //seg value { public: ll a; SV() {a=0LL;} SV(ll x) {a=x;} SV operator & (SV X) {SV ANS(max(a,X.a)); return ANS;} }; class LV //lazy value { public: ll a; LV() {a=0LL;} LV(ll x) {a=x;} LV operator & (LV X) {LV ANS(a+X.a); return ANS;} }; SV upval(ll c) //how lazy values modify a seg value inside a node, c=current node { SV X(p[c].a+lazy[c].a); return X; } SV neuts; LV neutl; vector<SV> p; vector<LV> lazy; vector<pl> range; ST() {N=0LL;} ST(vector<ll> arr) { N = (ll) 1<<(ll) ceil(log2(arr.size())); REP(i,0,2*N) {range.pb(mp(0LL,0LL));} REP(i,0,N) {p.pb(neuts);} REP(i,0,arr.size()) {SV X(arr[i]); p.pb(X); range[i+N]=mp(i,i);} REP(i,arr.size(),N) {p.pb(neuts); range[i+N]=mp(i,i);} ll cur = N-1; while(cur>0) { p[cur]=p[2*cur]&p[2*cur+1]; range[cur]=mp(range[2*cur].ff,range[2*cur+1].ss); cur--; } REP(i,0,2*N) {lazy.pb(neutl);} } void prop(ll c) //how lazy values propagate { p[c] = upval(c); lazy[2*c]=lazy[c]&lazy[2*c]; lazy[2*c+1]=lazy[c]&lazy[2*c+1]; lazy[c]=neutl; } SV query(ll a,ll b, ll c=1LL) //range [a,b], current node. initially: query(a,b) { if(a>b) {return neuts;} ll x=range[c].ff; ll y=range[c].ss; if(y<a || x>b) {return neuts;} if(x>=a && y<=b) {return upval(c);} prop(c); SV ans = query(a,b,2*c)&query(a,b,2*c+1); return ans; } void update(LV s, ll a, ll b, ll c=1LL) //update LV, range [a,b], current node, current range. initially: update(s,a,b) { if(a>b) {return;} ll x=range[c].ff; ll y=range[c].ss; if(y<a || x>b) {return ;} if(x>=a && y<=b) { lazy[c]=s&lazy[c]; return; } prop(c); update(s,a,b,2*c); update(s,a,b,2*c+1); p[c]=upval(2*c)&upval(2*c+1); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(20); ll N; cin>>N; f = vector<ll>(N,-1); w = vector<ll>(N,-1); visited = vector<bool>(N,false); adj=vector<vector<ll> >(N,vector<ll>()); REP(i,0,N) {cin>>f[i]>>w[i]; f[i]--; adj[i].pb(f[i]); adj[f[i]].pb(i);} ll ans = 0LL; ll node; ll C; REP(s,0,N) { if(visited[s]) {continue;} ll bestval = 0; ll thisval; vector<ll> cycle = Floyd(s); C = cycle.size(); REP(i,0,cycle.size()) {node=cycle[i]; visited[node]=true;} vector<ll> val(C,0); REP(i,0,cycle.size()) { bestpath = 0; val[i]=DFS(cycle[i]); bestval = max(bestval,bestpath); } vector<ll> ps; ll cursum=0; REP(i,0,2*C) {node = cycle[i%C]; ps.pb(cursum+val[i%C]); cursum+=w[node];} ST S(ps); REP(i,0,C) { thisval=S.query(i+1,i+C-1).a-(ps[i]-val[i])+val[i]; bestval = max(thisval,bestval); } ans+=bestval; } cout<<ans<<endl; return 0; }

Compilation message (stderr)

islands.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    1 | #pragma GCC optimization ("O3")
      | 
islands.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("unroll-loops")
      |
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...