제출 #749373

#제출 시각아이디문제언어결과실행 시간메모리
749373amunduzbaev서열 (APIO23_sequence)C++17
100 / 100
1730 ms57004 KiB
#include "sequence.h" #include "bits/stdc++.h" using namespace std; #ifndef EVAL #include "grader.cpp" #endif #define ar array typedef long long ll; //~ #define int ll const int INF = 1e9; const int N = 5e5 + 5; int A_[N], B_[N]; struct ST{ int min[N << 2], max[N << 2], p[N << 2]; ST(){ memset(min, 0, sizeof min); memset(max, 0, sizeof max); memset(p, 0, sizeof p); } void push(int x, int lx, int rx){ if(lx == rx) return; min[x << 1] += p[x], max[x << 1] += p[x], p[x << 1] += p[x]; min[x << 1 | 1] += p[x], max[x << 1 | 1] += p[x], p[x << 1 | 1] += p[x]; p[x] = 0; } void add(int l, int r, int v, int lx, int rx, int x){ if(lx > r || rx < l) return; if(lx >= l && rx <= r){ min[x] += v, max[x] += v, p[x] += v; return; } int m = (lx + rx) >> 1; push(x, lx, rx); add(l, r, v, lx, m, x << 1), add(l, r, v, m + 1, rx, x << 1 | 1); min[x] = ::min(min[x << 1], min[x << 1 | 1]); max[x] = ::max(max[x << 1], max[x << 1 | 1]); } void add(int l, int r, int v){ add(l, r, v, 0, N, 1); } int Max(int l, int r, int lx, int rx, int x){ if(lx > r || rx < l) return -INF; if(lx >= l && rx <= r) return max[x]; int m = (lx + rx) >> 1; push(x, lx, rx); return ::max(Max(l, r, lx, m, x << 1), Max(l, r, m + 1, rx, x << 1 | 1)); } int Max(int l, int r){ return Max(l, r, 0, N, 1); } int Min(int l, int r, int lx, int rx, int x){ if(lx > r || rx < l) return INF; if(lx >= l && rx <= r) return min[x]; int m = (lx + rx) >> 1; push(x, lx, rx); return ::min(Min(l, r, lx, m, x << 1), Min(l, r, m + 1, rx, x << 1 | 1)); } int Min(int l, int r){ return Min(l, r, 0, N, 1); } }; int sequence(int n, vector<int> a){ a.insert(a.begin(), 0); ST A, B; vector<int> p(n); iota(p.begin(), p.end(), 1); sort(p.begin(), p.end(), [&](int i, int j){ if(a[i] != a[j]) return a[i] < a[j]; return i < j; }); for(int i=1;i<=n;i++){ B.add(i, i, i); A.add(i, i, -i); } int res = 0; for(int i=0,j=0;i<n;){ while(j < n && a[p[i]] == a[p[j]]){ A.add(p[j], n, 2); j++; } for(int k=0;i + k<j;k++){ A_[k] = A.Min(0, p[i + k] - 1); B_[k] = B.Min(0, p[i + k] - 1); int MxA = A.Max(p[i + k], n), MxB = B.Max(p[i + k], n); int l = 0, r = k; while(l < r){ int m = (l + r) >> 1; if(A_[m] <= MxA && B_[m] <= MxB) r = m; else l = m + 1; } res = max(res, k - l + 1); } while(i < j){ B.add(p[i], n, -2); i++; } } return res; }
#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...