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>
using namespace std;
#define int long long
#define pii pair<int, int>
const int INF = (1LL<<61LL) -1LL;
int n, q, d;
struct MaxTree{
vector<int> tree;
MaxTree(vector<int>&v){
int m =1;
while(m<v.size()){
m*=2;
}
m*=2;
tree.resize(m);
build(v, 0, v.size()-1, 1);
}
void build(vector<int>& v, int lt, int rt, int t){
if(rt ==lt){
tree[t] = v[lt];
}
else{
int mid = (lt+rt)/2;
build(v, lt, mid, t*2);
build(v, mid+1, rt, t*2+1);
tree[t] = max(tree[t*2], tree[t*2+1]);
}
}
void update(int id, int v, int lt, int rt, int t){
if(lt==id && rt ==id){
tree[t] = v;
}
else if(id<lt || rt<id){
return;
}
else{
int mid = (lt+rt)/2;
update(id, v, lt, mid, t*2);
update(id, v, mid+1, rt, t*2+1);
tree[t] = max(tree[t*2], tree[t*2+1]);
}
}
int get(int l, int r, int lt, int rt, int t){
//cout<<lt<<" "<<rt<<" "<<tree[t]<<endl;
if(l<=lt && rt<=r){
return tree[t];
}
else if(rt<l|| r<lt){
return -INF;
}
else{
int mid = (lt+rt)/2;
return max(get(l, r, lt, mid, t*2), get(l, r, mid+1, rt, t*2+1));
}
}
};
vector<pii> oc;
struct SetTree{
vector<int> tree;
vector<int> d;
vector<bool> act;
SetTree(vector<int>&v){
int m =1;
while(m<v.size()){
m*=2;
}
m*=2;
tree.resize(m);
d.resize(m);
act.resize(m, false);
build(v, 0, v.size()-1, 1);
}
void push(int t, int lt, int rt){
if(lt!= rt && act[t]){
d[t*2] = d[t];
d[t*2+1] = d[t];
act[t] = false;
act[t*2] = true;
act[t*2+1] = true;
tree[t*2] = d[t];
tree[t*2+1] = d[t];
}
}
void build(vector<int>& v, int lt, int rt, int t){
if(rt ==lt){
tree[t] = v[lt];
d[t] =v[lt];
act[t] = false;
}
else{
int mid = (lt+rt)/2;
build(v, lt, mid, t*2);
build(v, mid+1, rt, t*2+1);
tree[t] = min(tree[t*2], tree[t*2+1]);
}
}
void set_val(int l, int r, int v, int lt, int rt, int t){
if(l<=lt && rt<=r){
d[t] = v;
tree[t] = v;
act[t] = true;
}
else if(r<lt|| rt<l){
return;
}
else{
int mid = (lt+rt)/2;
push(t, lt, rt);
set_val(l, r, v, lt, mid, t*2);
set_val(l, r, v, mid+1, rt, t*2+1);
tree[t] = min(tree[t*2], tree[t*2+1]);
}
}
int get(int id, int lt, int rt, int t){
if(id==lt && id==rt){
return tree[t];
}
else{
int mid =(lt+rt)/2;
push(t, lt, rt);
if(id<=mid){
return get(id, lt, mid, t*2);
}
else{
return get(id, mid+1,rt, t*2+1);
}
}
}
void rem_min(int lt, int rt, int t){
if(lt == rt){
tree[t] = INF;
d[t] = INF;
oc.push_back(pii(lt, lt));
return;
}
else if(act[t]){
tree[t] = INF;
d[t] = INF;
oc.push_back(pii(lt, rt));
return;
}
else{
int mid = (lt+rt)/2;
if(tree[t*2] == tree[t]){
rem_min(lt, mid, t*2);
}
if(tree[t*2+1] == tree[t]){
rem_min(mid+1, rt, t*2+1);
}
tree[t] = min(tree[t*2], tree[t*2+1]);
return;
}
}
};
signed main(){
cin>>n>>d;
vector<int> v(n);
for(int i = 0; i<n; i++){
cin>>v[i];
}
vector<int> big(n);
map<int, int> small;
for(int i = 0; i<n; i++){
cin>>v[i];
big[i] = v[i];
}
set<int> big2(big.begin(), big.end());
big.assign(big2.begin(), big2.end());
for(int i = 0; i<big.size(); i++){
small.insert(pii(big[i], i));
//cout<<big[i]<<endl;
}
auto ref = vector<int>(big.size(), -1);
MaxTree max_tr = MaxTree(ref);
ref = vector<int>(big.size(), INF);
SetTree set_tr = SetTree(ref);
int bs =big.size();
set<int> rem;
for(int i = 0; i<n; i++){
oc.clear();
if(set_tr.tree[1]<i-d){
//cout<<"removing date "<<set_tr.tree[1]<<endl;
if(set_tr.tree[1]>=0){
set_tr.rem_min(0, bs-1, 1);
for(auto e: oc){
//cout<<"inter "<<e.first<<" "<<e.second<<endl;
for(auto it = rem.lower_bound(e.first); it!=rem.upper_bound(e.second);){
max_tr.update(*it, -1, 0, bs-1, 1);
//cout<<big[*it]<<endl;
rem.erase(it++);
}
}
}
}
//cout<<"i: "<<i+1<<" "<<max(1LL, max_tr.get(0, small[v[i]]-1, 0, bs-1, 1)+1)<<endl;
max_tr.update(small[v[i]], max(1LL, max_tr.get(0, small[v[i]]-1, 0, bs-1, 1)+1), 0, bs-1, 1);
set_tr.set_val(small[v[i]], bs-1,i, 0, bs-1, 1);
rem.insert(small[v[i]]);
}
cout<<max_tr.get(0, bs-1, 0, bs-1, 1)<<endl;
/*SetTree set_tr = SetTree(v);
for(int i = 0; i<d; i++){
int a, b, c;
cin>>a>>b>>c;
set_tr.set_val(a, b, c, 0, n-1, 1);
set_tr.rem_min(0, n-1, 1);
for(auto e: oc){
cout<<e.first<<" "<<e.second<<endl;
}
oc.clear();
cout<<"--"<<endl;
}*/
}
Compilation message (stderr)
Main.cpp: In constructor 'MaxTree::MaxTree(std::vector<long long int>&)':
Main.cpp:16:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | while(m<v.size()){
| ~^~~~~~~~~
Main.cpp: In constructor 'SetTree::SetTree(std::vector<long long int>&)':
Main.cpp:74:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | while(m<v.size()){
| ~^~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:194:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
194 | for(int i = 0; i<big.size(); i++){
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |