This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "sequence.h"
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define f first
#define s second
vector<int> ct[500005];
int N;
struct segTree {
pii seg[4*500005];
int lazy[4*500005];
void laze(int i, int l, int r) {
if(lazy[i]) {
seg[i] = {seg[i].f+lazy[i], seg[i].s+lazy[i]};
if(l!=r) {
lazy[i*2+1] += lazy[i];
lazy[i*2+2] += lazy[i];
}
lazy[i] = 0;
}
}
void build(int i, int l, int r, int val) {
lazy[i] = 0;
if(l==r) {
seg[i].f = seg[i].s = val*(l);
return;
}
int m = (l+r)/2;
build(i*2+1, l, m, val);
build(i*2+2, m+1, r, val);
seg[i].f = min(seg[i*2+1].f, seg[i*2+2].f);
seg[i].s = max(seg[i*2+1].s, seg[i*2+2].s);
}
void build(int val) {
build(0, 0, N, val);
}
void update(int i, int l, int r, int wl, int wr, int val) {
if(wl<=l && wr>=r) lazy[i] += val;
laze(i, l, r);
if(wl>r || wr<l || (wl<=l && wr>=r)) return;
int m = (l+r)/2;
update(i*2+1, l, m, wl, wr, val);
update(i*2+2, m+1, r, wl, wr, val);
seg[i].f = min(seg[i*2+1].f, seg[i*2+2].f);
seg[i].s = max(seg[i*2+1].s, seg[i*2+2].s);
}
void update(int wl, int wr, int val) {
update(0, 0, N, wl+1, wr+1, val);
}
pii query(int i, int l, int r, int wl, int wr) {
laze(i, l, r);
if(wl>r || wr<l) return {(int)1e9, (int)-1e9};
if(wl<=l && wr>=r) return seg[i];
int m = (l+r)/2;
pii q1 = query(i*2+1, l, m, wl, wr);
pii q2 = query(i*2+2, m+1, r, wl, wr);
return {min(q1.f, q2.f), max(q1.s, q2.s)};
}
pii query(int wl, int wr) {
return query(0, 0, N, wl+1, wr+1);
}
} les, mor;
int sequence(int n, std::vector<int> A) {
N = n;
for(int i=0;i<N;i++) {
ct[A[i]].push_back(i);
}
int ans = 1;
les.build(-1);
mor.build(1);
for(int i=1;i<=N;i++) {
for(int j:ct[i]) {
les.update(j, N-1, 2);
}
for(int j=0;j+ans<ct[i].size();j++) {
int st = ct[i][j];
int en = ct[i][j+ans];
int qles = les.query(en, N-1).s - les.query(-1, st-1).f;
int qmor = mor.query(en, N-1).s - mor.query(-1, st-1).f;
if(qles>=0 && qmor>=0) {
ans++; j--;
}
}
for(int j:ct[i]) {
mor.update(j, N-1, -2);
}
}
return ans;
}
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:82:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int j=0;j+ans<ct[i].size();j++) {
| ~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |