#include "sequence.h"
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
vector<int> ct[500005];
int N;
vector<pii> v;
struct node {
int val;
node *left, *right;
node(int v) {
val = v;
left = right = NULL;
}
};
struct persistTree {
node *root;
void build(node *cur, int l, int r) {
if(l==r) return;
int m = (l+r)/2;
cur->left = new node(0);
cur->right = new node(0);
build(cur->left, l, m);
build(cur->right, m+1, r);
}
void build() {
root = new node(0);
build(root, 0, N-1);
}
void update(node *cur, node *prev, int l, int r, int x) {
if(l==r) {
cur->val++;
return;
}
int m = (l+r)/2;
if(x<=m) {
cur->left = new node(0);
cur->right = prev->right;
update(cur->left, prev->left, l, m, x);
} else {
cur->left = prev->left;
cur->right = new node(0);
update(cur->right, prev->right, m+1, r, x);
}
cur->val = cur->left->val+cur->right->val;
}
void update(node *prev, int x) {
root = new node(0);
update(root, prev, 0, N-1, x);
}
int query(node *add, node *sub, int l, int r, int wl, int wr) {
if(wl>r || wr<l) return 0;
if(wl<=l && wr>=r) return add->val-sub->val;
int m = (l+r)/2;
return query(add->left, sub->left, l, m, wl, wr) + query(add->right, sub->right, m+1, r, wl, wr);
}
int query(node *sub, int wl, int wr) {
if(wr<wl) return 0;
return query(root, sub, 0, N-1, wl, wr);
}
};
vector<persistTree> persist;
bool check(int x) {
for(int i=1;i<=N;i++) {
for(int j=0;j+x-1<ct[i].size();j++) {
int st = ct[i][j];
int en = ct[i][j+x-1];
int q1 = persist[lower_bound(v.begin(), v.end(), make_pair(i, -1))-v.begin()].query(persist[0].root, st+1, en-1);
int q2 = persist[N].query(persist[upper_bound(v.begin(), v.end(), make_pair(i, (int)1e9))-v.begin()].root, st+1, en-1);
if(abs(q1-q2)<=x) return true;
}
}
return false;
}
int sequence(int n, std::vector<int> A) {
N = n;
for(int i=0;i<N;i++) {
ct[A[i]].push_back(i);
v.push_back({A[i], i});
}
sort(v.begin(), v.end());
persist.resize(N+1);
persist[0].build();
for(int i=0;i<N;i++) {
persist[i+1].update(persist[i].root, v[i].second);
}
int l = 1, r = 1;
for(int i=1;i<=N;i++) {
r = max(r, (int)ct[i].size());
}
while(r>1) {
if(check(r)) return r;
r--;
}
return 1;
}
Compilation message
sequence.cpp:98:2: error: extended character is not valid in an identifier
98 | while(r>1) {
| ^
sequence.cpp:99:2: error: extended character is not valid in an identifier
99 | if(check(r)) return r;
| ^
sequence.cpp:99:5: error: extended character is not valid in an identifier
99 | if(check(r)) return r;
| ^
sequence.cpp:100:2: error: extended character is not valid in an identifier
100 | r--;
| ^
sequence.cpp:100:5: error: extended character is not valid in an identifier
100 | r--;
| ^
sequence.cpp:102:2: error: extended character is not valid in an identifier
102 |
| ^
sequence.cpp:103:2: error: extended character is not valid in an identifier
103 | return 1;
| ^
sequence.cpp: In function 'bool check(int)':
sequence.cpp:71:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int j=0;j+x-1<ct[i].size();j++) {
| ~~~~~^~~~~~~~~~~~~
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:98:2: error: '\U000000a0while' was not declared in this scope
98 | while(r>1) {
| ^~~~~~
sequence.cpp:102:2: error: '\U000000a0' was not declared in this scope
102 |
| ^
sequence.cpp:94:7: warning: unused variable 'l' [-Wunused-variable]
94 | int l = 1, r = 1;
| ^
sequence.cpp:104:1: warning: no return statement in function returning non-void [-Wreturn-type]
104 | }
| ^