# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
878140 | vjudge1 | Fish 2 (JOI22_fish2) | C++17 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
#include "sequence.h"
using namespace std;
const int N = 6e5 + 1;
typedef long long ll;
int n;
struct segtree{
int mn[N * 4],mx[N * 4],add[N * 4];
void build(int v = 1,int tl = 1,int tr = n){
add[v] = mn[v] = mx[v] = 0;
if(tl == tr) return;
int tm = (tl + tr) >> 1;
build(v + v,tl,tm);build(v + v + 1,tm + 1,tr);
}
void inc(int v,int val){
add[v] += val;
mn[v] += val;
mx[v] += val;
}
void push(int v){
if(add[v]){
inc(v + v,add[v]);
inc(v + v +1,add[v]);
add[v] = 0;
}
}
void upd(int l,int r,int val,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr){
return;
}
if(tl >= l && tr <= r){
inc(v,val);
}else{
push(v);
int tm = (tl + tr) >> 1;
upd(l,r,val,v+v,tl,tm);
upd(l,r,val,v+v+1,tm+1,tr);
mn[v] = min(mn[v + v],mn[v + v + 1]);
mx[v] = max(mx[v + v],mx[v + v + 1]);
}
}
int get_min(int l,int r,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr) return 1e9;
if(tl >= l && tr <= r) return mn[v];
push(v);
int tm = (tl + tr) >> 1;
mn[v] = min(mn[v + v],mn[v + v + 1]);
mx[v] = max(mx[v + v],mx[v + v + 1]);
return min(get_min(l,r,v+v,tl,tm),get_min(l,r,v+v+1,tm+1,tr));
}
int get_max(int l,int r,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr) return -1e9;
if(tl >= l && tr <= r) return mx[v];
push(v);
int tm = (tl + tr) >> 1;
mn[v] = min(mn[v + v],mn[v + v + 1]);
mx[v] = max(mx[v + v],mx[v + v + 1]);
return max(get_max(l,r,v+v,tl,tm),get_max(l,r,v+v+1,tm+1,tr));
}
}T,T1;
vector<int> pos[N];
bool inter(int l,int r,int l1,int r1){
return (min(r,r1) - max(l,l1) >= 0);
}
int sequence(int nn, std::vector<int> a) {
n = nn;
int mn = 1e9;
for(int i = 0;i < nn;i++){
mn = min(mn,a[i]);
pos[a[i]].push_back(i + 1);
}
T.build();
T1.build();
for(int i = 1;i <= n;i++){
T.upd(i,n,1);
T1.upd(i,n,1);
}
int res = 1;
for(int i = 1;i <= n;i++){
for(int j:pos[i - 1]){
T.upd(j,n,-2);
}
for(int j:pos[i]){
T1.upd(j,n,-2);
}
int it = 0;
for(int j = 0;j < (int)pos[i].size();j++){
while(it < (int)pos[i].size()){
ll mn_sum = T1.get_min(pos[i][it],n) - max(0,T1.get_max(1,pos[i][j] - 1));
ll mx_sum = T.get_max(pos[i][it],n) - min(0,T.get_min(1,pos[i][j] - 1));
int len = (it - j + 1);
assert(mx_sum >= mn_sum);
if(mn_sum * mx_sum <=0 ){
it++;
}else{
break;
}
}
res = max(res,it - j);
}
}
return res;
}
// int main() {
// int N;
// assert(1 == scanf("%d", &N));
// std::vector<int> A(N);
// for (int i = 0; i < N; ++i) {
// assert(1 == scanf("%d", &A[i]));
// }
// int result = sequence(N, A);
// printf("%d\n", result);
// return 0;
// }