Submission #1116419

#TimeUsernameProblemLanguageResultExecution timeMemory
1116419ntdaccodeCat Exercise (JOI23_ho_t4)C++17
41 / 100
91 ms53428 KiB
#include<bits/stdc++.h> #define fori(i,a,b) for(int i = a;i <= b; i++) #define int long long #define pb push_back using namespace std; typedef pair<int,int> ii; typedef tuple<int,int,int> tp; const int M = 1e6 + 10; const int N = 1e3 + 10; const int mod = 1e9 + 7; int n,p[M]; int ma[M][20],lg[M]; int getmax(int l,int r) { int k = lg[r - l + 1]; int u = ma[l][k]; int v = ma[r - (1<<k) +1][k]; if(p[u] > p[v]) return u; return v; } int f[M],pos[M],cnt = 0; int solve(int l,int r) { //cout << l << " " << r << "\n"; if(l > r) return -1; cnt++; int cur = cnt; pos[cur] = getmax(l,r); //cout << l << " " << r << " " << pos[cur] << "\n"; int pre = solve(l,pos[cur] - 1); int nxt = solve(pos[cur] + 1,r); if(pre != -1) f[cur] = max(f[cur],f[pre] + abs(pos[cur] - pos[pre])); if(nxt != -1) f[cur] = max(f[cur],f[nxt] + abs(pos[cur] - pos[nxt])); //cout << l << " " << r << " " << f[cur] << "sa\n"; return cur; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen("1.inp","r")) { freopen("1.inp","r",stdin); freopen("1.out","w",stdout); } #define task "" if(fopen(task".inp","r")) { freopen(task".inp","r",stdin); freopen(task".out","w",stdout); } cin >> n ; for(int i = 1;i <= n; i++) cin >> p[i],ma[i][0] = i; for(int i = 1;i <= n - 1; i++) { int u,v; cin >> u >> v; } for(int i = 1;i <= 18; i++) { for(int j = 1;j + (1<<i) - 1 <= n; j++) { int u = ma[j][i - 1]; int v = ma[j + (1 << i-1)][i - 1]; if(p[u] > p[v]) ma[j][i] = u; else ma[j][i] = v; } } for(int i = 1;i <= n; i++) lg[i]= __lg(i); solve(1,n); cout << f[1] ; }

Compilation message (stderr)

Main.cpp: In function 'int32_t main()':
Main.cpp:69:35: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   69 |             int v = ma[j + (1 << i-1)][i - 1];
      |                                  ~^~
Main.cpp:51:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     freopen("1.inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:52:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |     freopen("1.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:57:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:58:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |     freopen(task".out","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...