Submission #750596

#TimeUsernameProblemLanguageResultExecution timeMemory
750596arnold518Sequence (APIO23_sequence)C++17
100 / 100
852 ms65292 KiB
#include "sequence.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 5e5; int N, A[MAXN+10]; vector<int> V[MAXN+10]; struct Node { int sum, minv, maxv; Node() : sum(0), minv(0), maxv(0) {} Node(int x) : sum(x), minv(min(x, 0)), maxv(max(x, 0)) {} }; Node operator + (const Node &p, const Node &q) { Node ret; ret.sum=p.sum+q.sum; ret.minv=min(p.minv, p.sum+q.minv); ret.maxv=max(p.maxv, p.sum+q.maxv); return ret; } struct SEG { Node tree[MAXN*4+10]; void update(int node, int tl, int tr, int p, int q) { if(p<tl || tr<p) return; if(tl==tr) { tree[node]=Node(q); return; } int mid=tl+tr>>1; if(p<=mid) update(node*2, tl, mid, p, q); else update(node*2+1, mid+1, tr, p, q); tree[node]=tree[node*2]+tree[node*2+1]; } Node query(int node, int tl, int tr, int l, int r) { if(l<=tl && tr<=r) return tree[node]; if(r<tl || tr<l) return Node(); int mid=tl+tr>>1; return query(node*2, tl, mid, l, r)+query(node*2+1, mid+1, tr, l, r); } }seg; int sequence(int _N, std::vector<int> _A) { N=_N; for(int i=1; i<=N; i++) A[i]=_A[i-1]; for(int i=1; i<=N; i++) V[i].push_back(0); for(int i=1; i<=N; i++) V[A[i]].push_back(i); for(int i=1; i<=N; i++) V[i].push_back(N+1); for(int i=1; i<=N; i++) seg.update(1, 1, N, i, 1); int ans=0; for(int i=1; i<=N; i++) { for(auto it : V[i]) seg.update(1, 1, N, it, 0); for(auto it : V[i-1]) seg.update(1, 1, N, it, -1); vector<pii> VV; int t=0; for(int j=1; j<V[i].size(); j++) { Node nd=seg.query(1, 1, N, V[i][j-1]+1, V[i][j]-1); VV.push_back({t+nd.minv, t+nd.maxv}); t+=nd.sum; } //for(auto it : VV) printf("%d %d\n", it.first, it.second); for(int j=0; j<VV.size(); j++) { int lo=-1, hi=j; while(lo+1<hi) { int mid=lo+hi>>1; if(VV[j].second+j<VV[mid].first || VV[j].first-j>VV[mid].second) lo=mid; else hi=mid; } ans=max(ans, j-hi); VV[j].first+=j; VV[j].second-=j; if(j) { VV[j].first=min(VV[j].first, VV[j-1].first); VV[j].second=max(VV[j].second, VV[j-1].second); } } //printf("-> %d\n", i); } return ans; }

Compilation message (stderr)

sequence.cpp: In member function 'void SEG::update(int, int, int, int, int)':
sequence.cpp:42:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   42 |         int mid=tl+tr>>1;
      |                 ~~^~~
sequence.cpp: In member function 'Node SEG::query(int, int, int, int, int)':
sequence.cpp:51:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   51 |         int mid=tl+tr>>1;
      |                 ~~^~~
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:73:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |         for(int j=1; j<V[i].size(); j++)
      |                      ~^~~~~~~~~~~~
sequence.cpp:80:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |         for(int j=0; j<VV.size(); j++)
      |                      ~^~~~~~~~~~
sequence.cpp:85:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   85 |                 int mid=lo+hi>>1;
      |                         ~~^~~
#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...