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 <ext/pb_ds/assoc_container.hpp>
#include <utility>
#define V vector
#define P pair
#define S second
#define F first
#define pb push_back
#define all(v) v.begin(),v.end()
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
static int called = 0;const long long MOD = 1e9+7; // 1e9 + 7
void file(){
freopen("input.txt.txt","r",stdin);
freopen("output.txt.txt","w",stdout);
}
void file(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct segtree{
private:
struct node{
int maximum;
int minimum;
};
node neutral={-1,INT_MAX};
int no_operation=-1;
static node single(int v){
return {v,v};
}
static node merge( node a,node b){
return {max(a.maximum,b.maximum),min(a.minimum,b.minimum)};
}
static node modification(node a,int op){
if(op==-1)
return a;
else
return {op,op};
}
static int operation_modifier(int a,int b){
if(b==-1)
return a;
return b;
}
public:
int size;
V<node>query;
V<int>operation;
void init(int n){
size=1;
while(size<n)size*=2;
query.assign(2*size,neutral);
operation.assign(2*size,no_operation);
}
void build(V<int>&a,int x,int lx,int rx){
if(rx-lx==1){
if(lx<a.size())
query[x]=single(a[lx]);
return;
}
int m=(lx+rx)/2;
build(a,2*x+1,lx,m);
build(a,2*x+2,m,rx);
query[x]=merge(query[2*x+1],query[2*x+2]);
}
void build(V<int>&a){
build(a,0,0,size);
}
void propagate(int x,int lx,int rx){
if(rx-lx==1)
return;
query[2*x+1]=modification(query[2*x+1],operation[x]);
query[2*x+2]=modification(query[2*x+2],operation[x]);
operation[2*x+1]=operation_modifier(operation[2*x+1],operation[x]);
operation[2*x+2]= operation_modifier(operation[2*x+2],operation[x]);
operation[x]=no_operation;
}
void set(int l,int r,int v,int x,int lx,int rx){
propagate(x,lx,rx);
if(lx>=r || rx<=l)
return;
if(l<=lx && rx<=r){
query[x]=modification(query[x],v);
operation[x]= operation_modifier(operation[x],v);
return;
}
int m=(lx+rx)/2;
set(l,r,v,2*x+1,lx,m);
set(l,r,v,2*x+2,m,rx);
query[x]=modification(merge(query[2*x+1],query[2*x+2]),operation[x]);
}
void set(int l,int r,int v){
set(l,r,v,0,0,size);
}
node calc(int l,int r,int x,int lx,int rx){
if(lx>=r || rx<=l)
return neutral;
if(l<=lx && rx<=r)
return query[x];
int m=(lx+rx)/2;
node a=calc(l,r,2*x+1,lx,m);
node b=calc(l,r,2*x+2,m,rx);
return modification(merge(a,b),operation[x]);
}
int calc(int l,int r){
return calc(l,r,0,0,size).maximum;
}
int calc1(int l,int r){
return calc(l,r,0,0,size).minimum;
}
};
segtree tree_val;
int find (int val,V<int>&vp,V<int>&vp1){
int l=0,r=(int)vp.size()-1;
int ans=-1;
while(l<=r){
int m=l+(r-l)/2;
if(vp[m]==val){
if(tree_val.calc(m,m+1)==0){
ans=m;
r=m-1;
}
else{
l=m+1;
}
}
if(vp[m]>val)
r=m-1;
if(vp[m]<val)
l=m+1;
}
return ans;
}
void solve() {
int n;
cin>>n;
int d;
cin>>d;
V<int> A(n);
V<int>vp;
for(int i=0;i<n;i++) {
cin >> A[i];
vp.pb(A[i]);
}
V<int>val(n,0);
sort(all(vp));
int dp[n];
tree_val.init(n);
tree_val.build(val);
segtree tree_A;
tree_A.init(n);
tree_A.build(A);
int id=find(A[0],vp,val);
val[id]=1;
tree_val.set(id,id+1,1);
dp[0]=1;
int maxd=INT_MIN;
for(int i=1;i<n;i++){
dp[i]=1;
maxd=tree_A.calc1(max(0,i-d),i);
int l=0,r=n-1;
int ans=-1;
while(l<=r){
int m=l+(r-l)/2;
if(vp[m]>=maxd){
ans=m;
r=m-1;
}
else
l=m+1;
}
if(ans!=-1)
tree_val.set(0,ans,0);
l=0,r=n-1,ans=-1;
while(l<=r){
int m=l+(r-l)/2;
if(vp[m]<A[i]){
ans=m;
l=m+1;
}
else
r=m-1;
}
if(ans!=-1){
dp[i]=max(dp[i],tree_val.calc(0,ans+1)+1);
}
id=find(A[i],vp,val);
tree_val.set(id,id+1,dp[i]);
val[id]=dp[i];
}
int c=1;
for(int i=0;i<n;i++){
c=max(c,dp[i]);
}
cout<<c<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//file();
solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In member function 'void segtree::build(std::vector<int>&, int, int, int)':
Main.cpp:62:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if(lx<a.size())
| ~~^~~~~~~~~
Main.cpp: In function 'void file()':
Main.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen("input.txt.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen("output.txt.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void file(std::string)':
Main.cpp:22:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:23:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: At global scope:
Main.cpp:15:12: warning: 'called' defined but not used [-Wunused-variable]
15 | static int called = 0;const long long MOD = 1e9+7; // 1e9 + 7
| ^~~~~~
# | 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... |