QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#275768 | #7904. Rainbow Subarray | Jacka1# | TL | 1ms | 3620kb | C++20 | 2.9kb | 2023-12-05 00:53:29 | 2023-12-05 00:53:29 |
Judging History
answer
// duziteng ^ ^
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+10;
const int M = 998244353;
const int mod = 998244353;
#define db double
#define int long long
int up(int a,int b){return a<0?a/b:(a+b-1)/b;}
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define PII pair<int,int>
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
struct node{
int l,r,v,cnt;
}tr[N<<2];
int n,m;
void pushup(int i){
auto &u=tr[i],&l=tr[i<<1],&r=tr[i<<1|1];
u.v=l.v+r.v;
u.cnt=l.cnt+r.cnt;
}
void build(int i,int l,int r){
tr[i].l=l,tr[i].r=r,tr[i].cnt=0,tr[i].v=0;
if(l==r){
return;
}
int mid=l+r>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
pushup(i);
}
void modify(int i,int x,int k,int k2){
if(tr[i].l>=x&&tr[i].r<=x){
auto &u=tr[i];
u.v+=k;
u.cnt+=k2;
return;
}
if(tr[i].l>x||tr[i].r<x)return;
if(tr[i<<1].r>=x)modify(i<<1,x,k,k2);
if(tr[i<<1|1].l<=x)modify(i<<1|1,x,k,k2);
pushup(i);
}
int query(int i,int f){
if(tr[i].l==tr[i].r){
return tr[i].l;
}
if(tr[i<<1].cnt>=f)return query(i<<1,f);
else{
return query(i<<1|1,f-tr[i<<1].cnt);
}
}
PII query(int i,int l,int r){
PII res={0,0};
if(tr[i].l>=l&&tr[i].r<=r){
auto &u=tr[i];
return {u.v,u.cnt};
}
if(tr[i].l>r||tr[i].r<l)return res;
if(tr[i<<1].r>=l){
PII now= query(i<<1,l,r);
res.first+=now.first;
res.second+=now.second;
}
if(tr[i<<1|1].l<=r){
PII now= query(i<<1|1,l,r);
res.first+=now.first;
res.second+=now.second;
}
return res;
}
void solve(){
cin>>n>>m;
vector<int>a(n+1);
map<int,int>mp;
vector<int>mpp(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
a[i]-=i;
mp[a[i]]++;
}
int idx=0;
for(auto [pos,w]:mp){
++idx;
mp[pos]=idx;
mpp[idx]=pos;
}
int l=1,r=n;
while(l<r){
int mid=l+r+1>>1;
build(1,1,idx+1);
for(int i=1;i<mid;i++)modify(1,mp[a[i]],a[i],1);
int flag=0;
// cout<<mid<<endl;
for(int i=mid;i<=n;i++){
modify(1,mp[a[i]],a[i],1);
int z=mpp[query(1,up(mid,2))];
// cout<<i<<' '<<z<<endl;
auto [lv,lcnt]=query(1,1,mp[z]);
auto [rv,rcnt]=query(1,mp[z]+1,idx);
// cout<<lcnt<<' '<<lv<<' '<<rcnt<<' '<<rv<<endl;
// cout<<lcnt*z-lv+rv-rcnt*z<<' '<<m<<endl;
if(lcnt*z-lv+rv-rcnt*z<=m)flag=1;
modify(1,mp[a[i-mid+1]],-a[i-mid+1],-1);
}
if(flag)l=mid;
else r=mid-1;
}
cout<<l<<endl;
}
signed main(){
fast
int t;t=1;cin>>t;
while(t--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3620kb
input:
5 7 5 7 2 5 5 4 11 7 6 0 100 3 4 5 99 100 5 6 1 1 1 1 1 5 50 100 200 300 400 500 1 100 3
output:
4 3 5 1 1
result:
ok 5 lines
Test #2:
score: -100
Time Limit Exceeded
input:
11102 2 167959139 336470888 134074578 5 642802746 273386884 79721198 396628655 3722503 471207868 6 202647942 268792718 46761498 443917727 16843338 125908043 191952768 2 717268783 150414369 193319712 6 519096230 356168102 262263554 174936674 407246545 274667941 279198849 9 527268921 421436316 3613460...
output:
1 4 3 2 6 5 7 2 4 1 4 1 1 3 2 2 7 8 7 7 1 7 6 2 4 3 1 6 7 7 3 4 3 9 3 8 6 6 3 1 6 3 1 2 4 6 4 6 4 1 4 7 1 6 3 5 6 6 1 7 5 3 1 6 4 5 3 2 2 6 2 3 10 1 4 3 2 4 5 1 7 5 5 5 8 5 3 6 3 5 5 8 5 4 5 2 1 5 2 3 3 4 8 1 3 1 2 2 8 3 1 6 8 1 8 4 5 6 6 8 4 8 3 2 8 4 5 6 2 6 2 4 1 5 4 5 3 2 4 1 2 1 4 5 8 3 7 3 3 3...