QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#722209 | #7904. Rainbow Subarray | futarian | TL | 450ms | 20468kb | C++11 | 3.3kb | 2024-11-07 18:11:08 | 2024-11-07 18:11:09 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
const int Len = 5e5 + 5;
mt19937 rnd(time(0));
uniform_int_distribution<int> op1(0 , 1e9 + 7);
inline int read()
{
int x = 0 , f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = getchar();
}
while('0' <= ch && ch <= '9')
{
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
void write(int x)
{
if(x < 0){putchar('-');x = -x;}
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
inline void writeN(int x){write(x) , putchar('\n');}
#define ll long long
int n,m,tot,rt,x,y,z,a[Len];
struct FHQ
{
int l,r,ff,ky,val,sz;
ll sum;
FHQ(){l = r = ff = ky = val = sz = sum = 0;}
FHQ(int L,int R,int FF,int KY,int VAL,int SZ,ll SUM){l = L , r = R , ff = FF , ky = KY , val = VAL , sz = SZ , sum = SUM;}
}fhq[Len];
#define ls(p) fhq[p].l
#define rs(p) fhq[p].r
inline void push_up(int p){fhq[p].sz = fhq[ls(p)].sz + fhq[rs(p)].sz + 1;fhq[p].sum = fhq[ls(p)].sum + fhq[rs(p)].sum + fhq[p].val;}
inline int clone(int v)
{
tot ++;
fhq[tot] = FHQ(0 , 0 , 0 , op1(rnd) , v , 1 , v);
return tot;
}
void split(int now,int v,int &x,int &y,int lstx,int lsty)
{
if(!now){x = y = 0;return;}
if(fhq[now].val <= v)
{
x = now;
fhq[x].ff = lstx;
split(rs(now) , v , rs(now) , y , x , lsty);
}
else
{
y = now;
fhq[y].ff = lsty;
split(ls(now) , v , x , ls(now) , lstx , y);
}
push_up(now);
}
int merge(int x,int y)
{
if(!x || !y) return x + y;
if(fhq[x].ky < fhq[y].ky)
{
rs(x) = merge(rs(x) , y);
fhq[rs(x)].ff = x;
push_up(x);
return x;
}
else
{
ls(y) = merge(x , ls(y));
fhq[ls(y)].ff = y;
push_up(y);
return y;
}
}
inline void ins(int v)
{
//printf("wowenwen%d\n",v);
split(rt , v - 1 , rt , x , 0 , 0);
rt = merge(rt , clone(v));
rt = merge(rt , x);
}
inline void del(int v)
{
split(rt , v - 1 , x , y , 0 , 0);
split(y , v , y , z , 0 , 0);
y = merge(ls(y) , rs(y));
y = merge(y , z);
rt = merge(x , y);
}
inline void quRak(int v)
{
split(rt , v - 1 , x , y , 0 , 0);
writeN(fhq[x].sz + 1);
rt = merge(x , y);
}
inline int Prknum(int rk)
{
int u = rt;
if(fhq[u].sz < rk) return -1;
while(u)
{
int lsize = fhq[ls(u)].sz;
if(lsize + 1 == rk) return fhq[u].val;
else if(lsize + 1 > rk) u = ls(u);
else rk -= lsize + 1 , u = rs(u);
}
return -1;
}
inline void clr()
{
for(int i = 1 ; i <= tot ; i ++) fhq[i] = FHQ(0 , 0 , 0 , 0 , 0 , 0 , 0);
tot = rt = x = y = z = 0;
}
ll k;
inline int check(int len)
{
if(len <= 1) return 1;
const int qrk = len / 2 + (len % 2);
int ql = 0 , nv = 0;ll sm = 0;
nv = Prknum(qrk);
split(rt , nv , x , y , 0 , 0);
sm = 1ll * nv * fhq[x].sz - fhq[x].sum + fhq[y].sum - 1ll * nv * fhq[y].sz;
rt = merge(x , y);
ql |= (sm <= k);
return ql;
}
signed main()
{
int T = read();
int ct = 0;
while(T --)
{
clr();
n = read() , k = read();
for(int i = 1 ; i <= n ; i ++) a[i] = read() - i;
int l = 1 , r = 0 , anss = 0;
while(l <= n)
{
while(1)
{
ct ++;
assert(ct <= 2000000);
if(r + 1 <= n)
{
r ++;
ins(a[r]);
if(check(r - l + 1))
{
anss = max(anss , r - l + 1);
continue;
}
del(a[r --]);
break;
}
else break;
}
del(a[l ++]);
}
writeN(anss);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 20468kb
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: 0
Accepted
time: 450ms
memory: 19680kb
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...
result:
ok 11102 lines
Test #3:
score: -100
Time Limit Exceeded
input:
1 500000 17244641009859 54748096 75475634 204928248 276927808 84875072 103158867 27937890 322595515 186026685 45468307 69240390 139887597 188586447 373764525 121365644 310156469 185188306 60350786 211308832 384695957 370562147 208427221 35937909 267590963 126478310 275357775 55361535 335993561 36696...