CF407E k-d-sequence 题解
扫描线 区间子区间问题
显然 只有
分段 将连续且
分别解决每段的子问题 考虑平面表示区间 做法同CF997E
我的题解
然后呢还是傻傻的把所有玩意都表示成矩形加
有一个显然的性质是一个区间满足则其子区间满足 因此线段树上二分第一个小于
坑点 特判
时间空间稳稳倒数
#include <bits/stdc++.h>
#define int long long
#define oo 10000000000
using namespace std;
map<int,int>pre;
const int N=2e5+3;
int n,k,d,a[N],cntl,cntq;
struct _b
{
int l,r;
}block[N],lim_max[N],lim_min[N];
struct _ans
{
int l,r,len;
}ans,qwq_ans;
struct _stc
{
int id;
int key;
bool friend operator < (const _stc a,const _stc b)
{
return a.key<b.key;
}
bool friend operator > (const _stc a,const _stc b)
{
return a.key>b.key;
}
bool friend operator <= (const _stc a,const _stc b)
{
return a.key<=b.key;
}
bool friend operator >= (const _stc a,const _stc b)
{
return a.key>=b.key;
}
};
inline int read()
{
int x=0;
bool f=0;
char c=getchar();
while(c>'9'||c<'0')
{
if(c=='-')f=1;
c=getchar();
}
while(c<='9'&&c>='0')
{
x=(x<<1)+(x<<3)+c-'0';
c=getchar();
}
return f?-x:x;
}
inline void stc_max(int l,int r)
{
stack<_stc>stc;
stc.push({l-1,100000000});
for(int i=l;i<=r;i++)
{
_stc sa={i,a[i]};
if(stc.top()>sa)
{
lim_max[i].l=stc.top().id;
stc.push(sa);
}
else
{
while(stc.top()<=sa&&stc.top().id!=l-1)
{
lim_max[stc.top().id].r=i;
stc.pop();
}
lim_max[i].l=stc.top().id;
stc.push(sa);
}
}
while(stc.top().id!=l-1)
{
lim_max[stc.top().id].r=r+1;
stc.pop();
}
}
inline void stc_min(int l,int r)
{
stack<_stc>stc;
stc.push({l-1,-1});
for(int i=l;i<=r;i++)
{
_stc sa={i,a[i]};
if(stc.top()<sa)
{
lim_min[i].l=stc.top().id;
stc.push(sa);
}
else
{
while(stc.top()>=sa&&stc.top().id!=l-1)
{
lim_min[stc.top().id].r=i;
stc.pop();
}
lim_min[i].l=stc.top().id;
stc.push(sa);
}
}
while(stc.top().id!=l-1)
{
lim_min[stc.top().id].r=r+1;
stc.pop();
}
}
struct node
{
int l_l,l_r,r,v;
bool friend operator < (const node a,const node b)
{
return a.r<b.r;
}
}line[N<<4];
struct seg
{
int key;
int tag;
}
t[N<<2];
#define ls p<<1
#define rs p<<1|1
inline void update(int p)
{
t[p].key=min(t[ls].key,t[rs].key);
}
inline void pushdown(int p)
{
if(t[p].tag!=0)
{
t[ls].tag+=t[p].tag;
t[rs].tag+=t[p].tag;
t[ls].key+=t[p].tag;
t[rs].key+=t[p].tag;
t[p].tag=0;
}
}
void modify(int l,int r,int ql,int qr,int dx,int p)
{
if(ql<=l&&r<=qr)
{
t[p].key+=dx;
t[p].tag+=dx;
return;
}
int mid=l+r>>1;
pushdown(p);
if(ql<=mid)modify(l,mid,ql,qr,dx,ls);
if(qr>mid)modify(mid+1,r,ql,qr,dx,rs);
update(p);
}
int query(int l,int r,int ql,int qr,int p,bool &type)
{
if(type)
{
if(l==r)
return l;
pushdown(p);
int mid=l+r>>1;
if(t[ls].key<=0)return query(l,mid,ql,qr,ls,type);
return query(mid+1,r,ql,qr,rs,type);
}
if(ql<=l&&r<=qr)
{
if(t[p].key<=0)
{
type=1;
return query(l,r,ql,qr,p,type);
}
return oo;
}
int mid=l+r>>1;
pushdown(p);
int res=oo;
if(ql<=mid)res=query(l,mid,ql,qr,ls,type);
if(type)return res;
if(qr>mid)res=query(mid+1,r,ql,qr,rs,type);
return res;
}
inline void solve(int l,int r)
{
ans={0,0,0};
stc_max(l,r);
stc_min(l,r);
for(int i=l;i<=r;i++)
{
line[++cntl]={1,n,i,-1};
line[++cntl]={lim_max[i].l+1,i,i,a[i]};
line[++cntl]={lim_max[i].l+1,i,lim_max[i].r,-a[i]};
line[++cntl]={lim_min[i].l+1,i,i,-a[i]};
line[++cntl]={lim_min[i].l+1,i,lim_min[i].r,a[i]};
}
sort(line+cntq,line+cntl+1);
for(int i=l;i<=r;i++)
{
while(line[cntq].r<=i&&cntq<=cntl)
{
modify(1,n,line[cntq].l_l,line[cntq].l_r,line[cntq].v,1);
cntq++;
}
if(pre[a[i]]>=l)
modify(1,n,l,pre[a[i]],oo,1);
pre[a[i]]=i;
bool ttt=0;
ans.l=query(1,n,l,i,1,ttt);
ans.r=i;
ans.len=ans.r-ans.l+1;
if(qwq_ans.len<ans.len)
qwq_ans=ans;
}
}
int cntb;
signed main()
{
n=read();
k=read();
d=read();
if(d==0)
{
int cnt=1;
a[1]=read();
int l,r=1;
int tcnt=1;
l=1;
int tl;
for(int i=2;i<=n;i++)
{
a[i]=read();
if(a[i]==a[i-1])tcnt++;
else
{
if(cnt<tcnt)
{
cnt=tcnt;
l=tl;
r=i-1;
}
tcnt=1;
tl=i;
}
}
cnt=max(cnt,tcnt);
cout<<l<<" "<<r;
return 0;
}
a[1]=read();
cntb=1;
block[cntb].l=1;
int rest[2];
rest[0]=a[1]%d;
if(rest[0]<0)rest[0]+=d;
int w=1;
for(int i=2;i<=n;i++,w^=1)
{
a[i]=read();
rest[w]=a[i]%d;
if(rest[w]<0)rest[w]+=d;
if(rest[w]!=rest[w^1])
{
block[cntb].r=i-1;
block[++cntb].l=i;
}
a[i-1]=(a[i-1]-rest[w^1])/d;
}
for(int i=1;i<=n;i++)pre[a[i]]=0;
a[n]=(a[n]-rest[w^1])/d;
block[cntb].r=n;
cntq=1;
qwq_ans.len=-oo;
for(int i=1;i<=n;i++)line[++cntl]={i,i,1,i-k};
for(int i=1;i<=cntb;i++)
{
solve(block[i].l,block[i].r);
}
printf("%lld %lld",qwq_ans.l,qwq_ans.r);
return 0;
}