QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#226108 | #1443. Potato Shuffle | Crysfly | WA | 3ms | 15712kb | C++17 | 3.4kb | 2023-10-25 16:02:37 | 2023-10-25 16:02:38 |
Judging History
answer
// what is matter? never mind.
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
//#define int long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define mod 1000000007
bool mbe;
int n,k,id[maxn],a[maxn],pos[maxn];
pii mn[maxn<<2],mx[maxn<<2];
int tc[maxn<<2];
void up(int p){
mn[p]=min(mn[p<<1],mn[p<<1|1]);
mx[p]=max(mx[p<<1],mx[p<<1|1]);
tc[p]=tc[p<<1]+tc[p<<1|1];
}
void build(int p,int l,int r){
if(l==r)return mn[p]=mx[p]=mkp(a[l],l),tc[p]=1,void();
int mid=l+r>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r),up(p);
}
void del(int p,int l,int r,int x){
if(l==r)return mn[p]=mkp(inf,l),mx[p]=mkp(-inf,l),tc[p]=0,void();
int mid=l+r>>1;
if(x<=mid)del(p<<1,l,mid,x);
else del(p<<1|1,mid+1,r,x);
up(p);
}
pii resmn,resmx; int cc;
void ask(int p,int l,int r,int ql,int qr){
if(ql>qr)return;
if(l>=ql&&r<=qr){
resmn=min(resmn,mn[p]);
resmx=max(resmx,mx[p]);
cc+=tc[p];
return;
}
int mid=l+r>>1;
if(ql<=mid)ask(p<<1,l,mid,ql,qr);
if(qr>mid)ask(p<<1|1,mid+1,r,ql,qr);
}
void ask(int ql,int qr){
resmn=mkp(inf,0);
resmx=mkp(-inf,0);
cc=0;
ask(1,1,n,ql,qr);
}
unsigned int pri[maxn];
mt19937 rnd(64);
int tr[maxn],ls[maxn],rs[maxn];
void upd(int p){
tr[p]=max(p,max(tr[ls[p]],tr[rs[p]]));
}
void dfs(int p){
if(ls[p])dfs(ls[p]);
cout<<p<<" ";
if(rs[p])dfs(rs[p]);
}
void split(int p,int k,int&x,int&y){
if(!p)return x=y=0,void();
if(max(tr[ls[p]],p)<k)x=p,split(rs[p],k,rs[p],y);
else y=p,split(ls[p],k,x,ls[p]);
upd(p);
}
int merge(int x,int y){
if(!x||!y)return x|y;
if(pri[x]<pri[y])return rs[x]=merge(rs[x],y),upd(x),x;
else return ls[y]=merge(x,ls[y]),upd(y),y;
}
pair<int,int>solve(int l,int r){
ask(l,r);
int cc=::cc;
// cout<<"l,r "<<l<<" "<<r<<"\n";
if(resmn.fi==inf)return {0,1};
if(resmn.se==resmx.se)return {id[resmn.se],1};
if(resmn.fi+resmx.fi>k){
int u=resmx.se;//cout<<"max "<<u<<"\n";
del(1,1,n,u);
pii fl=solve(l,u-1),fr=solve(u+1,r);
return {merge(fl.fi,merge(id[u],fr.fi)),1ll*fl.se*fr.se%mod};
}else{
int u=resmn.se;
// cout<<"min "<<u<<"\n";
del(1,1,n,u);
pii tmp=solve(l,r);
tmp.se=1ll*tmp.se*cc%mod;
// cout<<"L R "<<l<<" "<<r<<" "<<tmp.se<<"\n";
// return tmp;
// ins.
int rt=tmp.fi;
if(1){
int x,y;
split(rt,id[u],x,y);
rt=merge(merge(x,id[u]),y);
}
return {rt,tmp.se};
}
}
bool med;
signed main()
{
// freopen("data.in","r",stdin);
// cout<<((1.0*(&mbe-&med))/1024576.0)<<"\n";
n=read(),k=read();
For(i,1,n)a[i]=read();
// For(i,1,n)cout<<a[i]<<" ";cout<<"\n";
For(i,1,n)pri[i]=rnd(),tr[i]=i;
build(1,1,n);
pii res=solve(1,n);
cout<<res.se<<"\n";
return 0;
}
/*
10 100
6 2 8 10 4 1 9 5 3 7
98 89 77 93 91 15 10 57 89 27
4
0 0
2 0
1 1
0 1
1 5 9 10 15 18
1 5 9 13 15 18
4 4 4 2 3
8,14.
6 6 6 2
1 5 9 10 15 18
2 2
1 3 3 -3
2 2
1 3 4 3
2 3
2 4
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 15668kb
input:
3 7 5 2 4
output:
3
result:
ok "3"
Test #2:
score: -100
Wrong Answer
time: 3ms
memory: 15712kb
input:
5 4 1 2 3 2 1
output:
20
result:
wrong answer 1st words differ - expected: '10', found: '20'