Submission #293524

#TimeUsernameProblemLanguageResultExecution timeMemory
293524crossing0verExercise Deadlines (CCO20_day1problem2)C++17
25 / 25
235 ms8340 KiB
#include<bits/stdc++.h> #define vi vector<int> #define pii pair<int,int> #define fi first #define se second #define pb push_back using namespace std; const int N = 2e5+5; int MX[4*N],sum[4*N]; vector<int> v,v1; void build(int v,int tl,int tr) { if (tl == tr) { MX[v] = v1[tl]; sum[v] = 1; return; } int tm = (tl + tr)/2; build(v*2,tl,tm); build(v*2+1,tm+1,tr); sum[v] = sum[v*2] + sum[v*2+1]; MX[v] = max(MX[v*2],MX[v*2+1]); } long long ans; int idx; void upd(int v,int tl,int tr,int val) { if (tl == tr) { sum[v] = 0; MX[v] = -1; idx = tl; return; } int tm = (tl + tr)/2; if (MX[v*2+1] >= val) { upd(v*2+1,tm+1,tr,val); }else upd(v*2,tl,tm,val); sum[v] = sum[v*2] + sum[v*2+1]; MX[v] = max(MX[v*2],MX[v*2+1]); } int get(int v,int tl,int tr,int pos) { if (pos > tr) return 0; if (pos <= tl) return sum[v]; int tm = (tl + tr)/2; return get(v*2,tl,tm,pos) + get(v*2+1,tm+1,tr,pos); } main() { int n; cin >> n; v.resize(n); for (int i = 0; i < n; i++) { cin >> v[i]; --v[i]; } vi g = v; v1 = v; sort(g.begin(),g.end()); // INVALID for (int i = 0; i < n; i++) if (g[i] < i) { cout <<-1; return 0; }// build(1,0,n-1); for (int i = n-1; i >= 0; i--) { upd(1,0,n-1,i); ans += get(1,0,n-1,idx); } cout << ans; return 0; }

Compilation message (stderr)

Main.cpp:49:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   49 | main() {
      |      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...