Submission #396856

#TimeUsernameProblemLanguageResultExecution timeMemory
396856abdzagGlobal Warming (CEOI18_glo)C++17
100 / 100
1216 ms60712 KiB
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=int(a);i<int(b);i++) #define rrep(i,a,b) for(int i=int(a);i>int(b); i--); #define all(v) v.begin(),v.end() #define trav(a,v) for(auto&a:v) using namespace std; const long long inf = 1e15; typedef long long ll; struct MaxTree { typedef ll T; static constexpr T unit = -inf; vector<T> s; T f(T a, T b) { return max(a, b); } ll n; MaxTree(ll n = 0, T def = unit) : n(n), s(2 * n, def) {} void update(ll pos, T val) { for (s[pos += n] = val; pos /= 2;) { s[pos] = f(s[2 * pos], s[2 * pos + 1]); } } T query(ll l, ll r) { T a = unit, b = unit; if (l == r)return 0; for (l += n, r += n; l < r; l /= 2, r /= 2) { if (l % 2)a = f(a, s[l++]); if (r % 2) b = f(b, s[--r]); } return f(a, b); } T getVal(ll pos) { return s[pos + n]; } }; int main() { cin.sync_with_stdio(false); ll n, x; cin >> n >> x; vector<vector<ll>> before(2 * n + 1); vector<ll> v(n); rep(i, 0, n) cin >> v[i]; ll ans = 0; rep(i, 0, n)v.push_back(v[i] - x); vector<ll> sorted = v; sort(all(sorted)); map<ll, ll> maping; rep(i, 0, 2 * n)maping[sorted[i]] = i; vector<ll> lis(n); MaxTree tree(2 * n); rep(i, 0, 2 * n) { tree.update(i, 0); } MaxTree tree2 = tree; vector<ll> v2; rep(i, n, 2 * n) v2.push_back(v[i]); rep(i, 0, n) { lis[i] = 1 + tree.query(0, maping[v2[i]]); if (tree.getVal(maping[v2[i]])) before[maping[v2[i]]].push_back(tree.getVal(maping[v2[i]])); tree.update(maping[v2[i]], lis[i]); } rep(i, 0, n) { ll curval = 1 + tree2.query(maping[v[n - 1 - i]] + 1, 2 * n); tree2.update(maping[v[n - 1 - i]], curval); if (before[maping[v2[n - 1 - i]]].empty())tree.update(maping[v2[n - 1 - i]], 0); else { tree.update(maping[v2[n - 1 - i]], before[maping[v2[n - 1 - i]]].back()); before[maping[v2[n - 1 - i]]].pop_back(); } ans = max(ans, curval + tree.query(0, maping[v[n - 1 - i]])); } cout << ans << endl; return 0; }

Compilation message (stderr)

glo.cpp: In constructor 'MaxTree::MaxTree(ll, MaxTree::T)':
glo.cpp:20:5: warning: 'MaxTree::n' will be initialized after [-Wreorder]
   20 |  ll n;
      |     ^
glo.cpp:16:12: warning:   'std::vector<long long int> MaxTree::s' [-Wreorder]
   16 |  vector<T> s;
      |            ^
glo.cpp:21:2: warning:   when initialized here [-Wreorder]
   21 |  MaxTree(ll n = 0, T def = unit) : n(n), s(2 * n, def) {}
      |  ^~~~~~~
#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...