Skip to content

Commit 0f2c336

Browse files
authored
Create 2019-B-C.cpp
segment tree
1 parent 78bd444 commit 0f2c336

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

kick-start/2019-B-C.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define ms(x,v) memset(x,(v), sizeof(x))
6+
#define msn(x,v,n) memset(x,(v),sizeof(x[0]) * n)
7+
#define INF 0x3f3f3f3f
8+
9+
typedef long long LL;
10+
11+
12+
const int MAXN = 100000 + 3;
13+
14+
int a[MAXN];
15+
int S,n;
16+
17+
struct SegTree{
18+
int tree[MAXN<<2],lazy[MAXN<<2];
19+
void clear(){
20+
ms(tree,0);
21+
ms(lazy,0);
22+
}
23+
void update(int val,int ul,int ur,int node=1,int l=1,int r=n){
24+
if(ul > r || ur < l)return;
25+
if(ul<=l && r <= ur){// contained
26+
lazy[node] += val;
27+
tree[node] += val;
28+
return;
29+
}
30+
int lc = node <<1, rc = node <<1 |1;
31+
int mid = (l+r) >>1;
32+
update(val,ul,ur,lc,l,mid);
33+
update(val,ul,ur,rc,mid+1,r);
34+
tree[node] = max(tree[lc],tree[rc]) + lazy[node];
35+
}
36+
37+
int Max(int ul,int ur,int node=1,int l=1,int r=n){
38+
39+
if(ul>r || ur <l)return -INF;
40+
if(ul <=l && r <=ur) return tree[node];
41+
int mid = (l+r) >>1;
42+
return max(Max(ul,ur,node<<1,l,mid),Max(ul,ur,node<<1|1,mid+1,r));
43+
}
44+
};
45+
46+
SegTree seg;
47+
48+
int main(int argc, char const *argv[])
49+
{
50+
ios :: sync_with_stdio(0);
51+
cin.tie(0);
52+
std::cout.precision(8);
53+
std::cout.setf( std::ios::fixed, std:: ios::floatfield );
54+
int T;
55+
cin >> T;
56+
57+
for(int t =1; t <= T ; ++t)
58+
{
59+
60+
cin >>n >> S;
61+
vector<int> pos[MAXN];
62+
int ptr[MAXN];
63+
64+
for(int i=1 ; i<=n ; ++i){
65+
cin >> a[i];
66+
pos[a[i]].push_back(i);
67+
}
68+
for(int i=1 ; i <MAXN ; ++i)ptr[i] = pos[i].size()-1; // current access pos
69+
70+
seg.clear();
71+
int ans =0;
72+
for(int l = n ; l >0 ; --l){
73+
seg.update(1,l,n);
74+
75+
int cur = a[l];
76+
if(ptr[cur] + S <= pos[cur].size()-1){
77+
int st = pos[cur][ptr[cur]+S];
78+
seg.update(-(S+1),st,n);
79+
}
80+
if(ptr[cur] + S < pos[cur].size() -1){
81+
int st = pos[cur][ptr[cur] +S +1];
82+
seg.update(S,st,n);
83+
}
84+
ptr[cur]--;
85+
ans = max(ans,seg.Max(1,n));
86+
}
87+
88+
cout << "Case #"<<t << ": " << ans << "\n";
89+
}
90+
91+
return 0;
92+
}

0 commit comments

Comments
 (0)