Submission #1053883

#TimeUsernameProblemLanguageResultExecution timeMemory
1053883underwaterkillerwhaleSequence (APIO23_sequence)C++17
100 / 100
985 ms63312 KiB
#include <bits/stdc++.h> #define ll long long #define rep(i,m,n) for(int i=(m); i<=(n); i++) #define reb(i,m,n) for(int i=(m); i>=(n); i--) #define pii pair<int,int> #define pll pair<ll,ll> #define MP make_pair #define fs first #define se second #define bit(msk, i) ((msk >> i) & 1) #define iter(id, v) for(auto id : v) #define pb push_back #define SZ(v) (int)v.size() #define ALL(v) v.begin(),v.end() using namespace std; mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count()); ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); } const int N = 5e5 + 7; const int Mod = 1e9 + 7; const int INF = 1e9 + 7; const ll BASE = 137; int n; int a[N]; vector<int> pos[N]; pii pre[N], suf[N]; struct Node { int premn, sufmn, premx, sufmx, sum; }; struct Segment_Tree { int m; Node st[N << 2] ; void init (int n) { m = n; } Node mer (Node lf, Node rt) { Node cur; cur.sum = lf.sum + rt.sum; cur.premn = min(lf.premn, lf.sum + rt.premn); cur.premx = max(lf.premx, lf.sum + rt.premx); cur.sufmn = min(rt.sufmn, rt.sum + lf.sufmn); cur.sufmx = max(rt.sufmx, rt.sum + lf.sufmx); return cur; } void update (int id, int l, int r, int pos, int val) { if (l > pos || r < pos) return; if (l == r) { st[id].sum = val; st[id].premn = st[id].sufmn = min(val, 0); st[id].premx = st[id].sufmx = max(val, 0); return; } int mid = l + r >> 1; update (id << 1, l, mid, pos, val); update (id << 1 | 1, mid + 1, r, pos, val); st[id] = mer(st[id << 1], st[id << 1 | 1]); } Node get (int id, int l, int r, int u, int v) { if (l > v || r < u || u > v) return {0, 0, 0, 0, 0}; if (l >= u && r <= v) return st[id]; int mid = l + r >> 1; return mer (get (id << 1, l, mid, u, v), get (id << 1 | 1, mid + 1, r, u, v)); } void update (int pos, int val) { update (1, 1, m, pos, val); } Node get (int u, int v) { return get (1, 1, m, u, v); } }ST; bool check (pii cur, ll K) { if (cur.se < 0) cur = MP(-cur.se, -cur.fs); if (cur.se <= K) return 1; if (cur.fs <= K) return 1; return 0; } pii mer (pii A, pii B) { return MP(min(A.fs, B.fs), max(A.se, B.se)); } int sequence (int _n, vector<int> _a) { //void solution () { // cin >> n; // rep (i, 1, n) { // cin >> a[i]; // } n = _n; rep (i, 1, n) a[i] = _a[i - 1]; rep (i, 1, n) { pos[a[i]].pb(i); } ST.init(n); rep (i, 1, n) ST.update (i, 1); int res = 1; rep (x, 1, n) { vector<int> &curp = pos[x]; iter (&id, curp) ST.update(id, 0); // cout << x<<":\n"; pii seg2 = MP(0, 0); int m = SZ(curp); reb (i, m - 1, 0) { int nxt = i < m - 1 ? curp[i + 1] - 1 : n; int added = ST.get(curp[i] + 1, nxt).sum; int delta = i == m - 1 ? 0 : 1; seg2 = MP(seg2.fs + added - delta, seg2.se + added + delta); Node get = ST.get(curp[i] + 1, nxt); pii cur = MP(get.premn, get.premx); seg2 = mer(seg2, cur); suf[i] = seg2; } pii seg1 = MP(0, 0); rep (i, 0, m - 1) { int prv = i > 0 ? curp[i - 1] + 1 : 1; int added = ST.get (prv, curp[i] - 1).sum; int delta = i == 0 ? 0 : 1; seg1 = MP(seg1.fs + added - delta, seg1.se + added + delta); Node get = ST.get (prv, curp[i] - 1); // if (x == 1 && i == 0) cout << get.premn<<",<"<<get.premx<<"\n"; pii cur = MP(get.sufmn, get.sufmx); seg1 = mer(seg1, cur); pre[i] = seg1; } rep (i, 0, m - 1) { while (i + res < m) { int added = ST.get(curp[i] + 1, curp[i + res] - 1).sum; pii cur = MP(pre[i].fs + added + suf[i + res].fs, pre[i].se + added + suf[i + res].se); if (check(cur, res + 1)) ++res; else { // cout << i <<" "<<res<<" "<<cur.fs<<" "<<cur.se<<"\n"; break; } } } iter (&id, curp) ST.update(id, -1); } // cout << res <<"\n"; return res; } // //#define file(name) freopen(name".inp","r",stdin); \ //freopen(name".out","w",stdout); //int main () { // file("c"); // ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0); // int num_Test = 1; //// cin >> num_Test; // while (num_Test--) // solution(); //} /* no bug challenge +2 2 + 8 * 2 - 9 */

Compilation message (stderr)

sequence.cpp:153:1: warning: multi-line comment [-Wcomment]
  153 | //#define file(name) freopen(name".inp","r",stdin); \
      | ^
sequence.cpp: In member function 'void Segment_Tree::update(int, int, int, int, int)':
sequence.cpp:61:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   61 |         int mid = l + r >> 1;
      |                   ~~^~~
sequence.cpp: In member function 'Node Segment_Tree::get(int, int, int, int, int)':
sequence.cpp:70:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   70 |         int mid = l + r >> 1;
      |                   ~~^~~
#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...