제출 #760112

#제출 시각아이디문제언어결과실행 시간메모리
7601128e7서열 (APIO23_sequence)C++17
41 / 100
2086 ms56284 KiB
#include "sequence.h" //Challenge: Accepted #include <bits/stdc++.h> using namespace std; #ifdef zisk void debug(){cout << endl;} template<class T, class ... U> void debug(T a, U ...b){cout << a << " ", debug(b...);} template<class T> void pary(T l, T r) { while (l != r) cout << *l << " ", l++; cout << endl; } #else #define debug(...) 0 #define pary(...) 0 #endif #define ll long long #define maxn 500005 #define ff first #define ss second #define pii pair<int, int> const int inf = 1<<30; struct segtree{ int ma[4*maxn], mi[4*maxn], tag[4*maxn]; int type, N; void init(int cur, int l, int r) { if (r <= l) return; tag[cur] = 0; if (r - l == 1) { ma[cur] = mi[cur] = type ? N - l : l+1; return; } int m = (l + r) / 2; init(cur*2, l, m), init(cur*2+1, m, r); ma[cur] = max(ma[cur*2], ma[cur*2+1]); mi[cur] = min(mi[cur*2], mi[cur*2+1]); } void push(int cur, int l, int r) { if (!tag[cur]) return; ma[cur] += tag[cur]; mi[cur] += tag[cur]; if (r - l > 1) { tag[cur*2] += tag[cur]; tag[cur*2+1] += tag[cur]; } tag[cur] = 0; } void modify(int cur, int l, int r, int ql, int qr, int v) { if (r <= l || ql >= r || qr <= l) return; push(cur, l, r); if (ql <= l && qr >= r) { tag[cur] += v; return; } int m = (l + r) / 2; modify(cur*2, l, m, ql, qr, v); modify(cur*2+1, m, r, ql, qr, v); push(cur*2, l, m), push(cur*2+1, m, r); ma[cur] = max(ma[cur*2], ma[cur*2+1]); mi[cur] = min(mi[cur*2], mi[cur*2+1]); } pii query(int cur, int l, int r, int ql, int qr) { //(ma, mi) if (r <= l || ql >= r || qr <= l) return {-inf, inf}; push(cur, l, r); if (ql <= l && qr >= r) return make_pair(ma[cur], mi[cur]); int m = (l + r) / 2; pii pl = query(cur*2, l, m, ql, qr), pr = query(cur*2+1, m, r, ql, qr); pl.ff = max(pl.ff, pr.ff); pl.ss = min(pl.ss, pr.ss); return pl; } int get(int cur, int l, int r, int x) { if (r <= l) return 0; push(cur, l, r); if (r - l == 1) return ma[cur]; int m = (l + r) / 2; if (x < m) return get(cur*2, l, m, x); else return get(cur*2+1, m, r, x); } } pref, suf; vector<int> pos[maxn]; int sequence(int N, std::vector<int> A) { pref.type = 0, suf.type = 1; pref.N = suf.N = N; pref.init(1, 0, N), suf.init(1, 0, N); for (int i = 0;i < N;i++) { pos[A[i]].push_back(i); } //for (int j = 0;j < N;j++) cout << pref.get(1,0, N, j) << " "; //cout << endl; int ans = 1; for (int i = 1;i <= N;i++) { int siz = pos[i].size(); for (int j = 0;j < siz;j++) { pref.modify(1, 0, N, pos[i][j], N, -1); suf.modify(1, 0, N, 0, pos[i][j]+1, -1); } //for (int j = 0;j < N;j++) cout << pref.get(1,0, N, j) << " "; //cout << endl; for (int j = 0;j < siz;j++) { int r = pos[i][j]; int vj = pref.get(1, 0, N, r); auto check = [&] (int ind) { int l = pos[i][ind], cnt = j - ind + 1; //[l, r] int sum = vj - pref.get(1, 0, N, l), vind = suf.get(1, 0, N, l); pii pl = suf.query(1, 0, N, 0, l), pr = pref.query(1, 0, N, r+1, N); pii p = {sum + max(0, pl.ff-vind) + max(0, pr.ff-vj), sum + min(0, pl.ss-vind) + min(0, pr.ss-vj)}; //debug(ind, j, p.ss, p.ff); if (max(p.ss, -cnt) <= min(p.ff, cnt)) { ans = max(ans, j - ind + 1); return true; } else { return false; } }; for (int ind = 0;ind < j;ind++) check(ind); } for (int j = 0;j < siz;j++) { pref.modify(1, 0, N, pos[i][j], N, -1); suf.modify(1, 0, N, 0, pos[i][j]+1, -1); } } return ans; }
#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...