1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #include<bits/stdc++.h>
using namespace std;
using ll = long long; using ull = unsigned long long; using i32 = int; using i64 = long long; using i128 = __int128;
template<typename T1,typename T2> ostream& operator<<(ostream& o,const tuple<T1,T2>& t){ return o<<"("<<get<0>(t)<<","<<get<1>(t)<<")"; }
template<typename T1,typename T2,typename T3> ostream& operator<<(ostream& o,const tuple<T1,T2,T3>& t){ return o<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")"; }
template<typename T,const size_t S> ostream& operator<<(ostream& o,const array<T,S>& arr){ for(int i=(o<<"[",0);i<S;++i)o<<arr[i]<<(i+1<S?",":""); return o<<"]\n"; }
template<typename T> ostream& operator<<(ostream& o,const vector<T>& vec){ for(int i=(o<<"[",0);i<vec.size();++i)o<<vec[i]<<(i+1<vec.size()?",":""); return o<<"]\n"; }
constexpr int M = 150005, mod = 998244353, inf = 1e9; constexpr double pi = acos(-1), eps = 1e-9;
ll v[M<<2],t[M<<2],tag1[M<<2],tag2[M<<2];
void build(int now,int l,int r){ tag1[now]=-inf; tag2[now]=0; v[now]=0; if(l==r)return; int mid=l+r>>1; build(now<<1,l,mid),build(now<<1|1,mid+1,r); return; }
void update1(int now,ll x){ v[now]=x; tag1[now]=x; tag2[now]=0; return; }
void update2(int now,ll x){ if(tag1[now]>-inf)return update1(now,tag1[now]+x); v[now]+=x; tag2[now]+=x; return; }
void push_down(int now){ if(tag1[now]>-inf)update1(now<<1,tag1[now]),update1(now<<1|1,tag1[now]); if(tag2[now])update2(now<<1,tag2[now]),update2(now<<1|1,tag2[now]); return tag1[now]=-inf,tag2[now]=0,void(); }
void change(int now,int l,int r,int L,int R,ll x){ if(l>R||L>r)return; if(L<=l&&r<=R)return update1(now,x); push_down(now); int mid=l+r>>1; change(now<<1,l,mid,L,R,x),change(now<<1|1,mid+1,r,L,R,x); v[now]=max(v[now<<1],v[now<<1|1]); return; }
void add(int now,int l,int r,int L,int R,ll x){ if(l>R||L>r)return; if(L<=l&&r<=R)return update2(now,x); push_down(now); int mid=l+r>>1; add(now<<1,l,mid,L,R,x),add(now<<1|1,mid+1,r,L,R,x); v[now]=max(v[now<<1],v[now<<1|1]); return; }
ll query(int now,int l,int r,int L,int R){ if(l>R||L>r)return -inf; if(L<=l&&r<=R)return v[now]; push_down(now); int mid=l+r>>1; return max(query(now<<1,l,mid,L,R),query(now<<1|1,mid+1,r,L,R)); }
void solve(){ int n; cin>>n; build(1,1,n); unordered_map<int,vector<int>> p; vector<int> a(n); for(auto& i:a)cin>>i; for(int i=n;i>=1;--i){ int x=a[i-1]; p[x].emplace_back(i); } int ans=0,ansl=0,ansr=0; unordered_set<int> vis; for(int i=1;i<=n;++i){ auto& cur=p[a[i-1]]; cur.pop_back(); if(!vis.contains(a[i-1])){ vis.insert(a[i-1]); if(cur.size()>=2){ int l=cur.back(),r=cur.front(); add(1,1,n,l+1,r,1); } } else{ if(cur.size()<=0){ continue; } int l=i,r=cur.back(); add(1,1,n,l+1,r,-1); } int s=query(1,1,n,1,n); if(s>ans){ ans=s; ansl=i+1; } } unordered_map<int,int> cnt[3]; for(int i=0;i<ansl-1;++i)cnt[0][a[i]]=1; for(int i=ansl-1;i<n;++i)cnt[1][a[i]]++; for(int t=0,i=ansl-1;i<n-1;++i){ cnt[2][a[i]]++,cnt[1][a[i]]--; if(!cnt[0].contains(a[i]))continue; if(cnt[1][a[i]]==0)--t; if(cnt[2][a[i]]==1)++t; if(t==ans){ ansr=i+2; break; } } if(!ans)ansl=2,ansr=3; cout<<ans<<"\n"<<ansl<<" "<<ansr<<"\n"; return; }
int main() { ios::sync_with_stdio(false); cin.tie(0); int t{1}; cin>>t; while(t--){ solve(); } return 0; }
|