QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#599190 | #8577. 평균 최대화 | Matutino | 5 | 824ms | 28676kb | C++14 | 4.1kb | 2024-09-29 02:03:13 | 2024-09-29 02:03:13 |
Judging History
answer
// #include"average.h"
#include<bits/stdc++.h>
#define reg register
#define int long long
const int N=2e5+10;
int n,a[N],st[20][N],idx,qwq,Rt[N];
std::mt19937 rnd(233);
std::vector<int> vc[N],G[N];
std::map<std::pair<int,int>,int> id;
std::pair<int,int> ID[N];
struct Node{int x,y,sz,sx,sy,val,ls,rs;}t[N];
struct Fac{
int x,y;
inline bool operator<(const Fac &A)const{return x*A.y<y*A.x;}
inline bool operator>(const Fac &A)const{return x*A.y>y*A.x;}
};
std::map<int,Fac> ans;
inline int chk(reg int x,reg int y){return a[x]==a[y]?std::min(x,y):a[x]<a[y]?x:y;}
inline int qry(reg int l,reg int r){reg int L=std::__lg(r-l+1); return chk(st[L][l],st[L][r-(1<<L)+1]);}
int build(reg int l,reg int r){
if (r==l+1){
++qwq,id[ID[qwq]={l,r}]=qwq;
ans[qwq]={a[l]+a[r],2};
// return ++qwq;
return 0;
}
reg int p=l+1,rt=++qwq,lst=0; id[{l,r}]=qwq;
ID[qwq]={l,r};
std::cerr<<l<<" "<<r<<" "<<qwq<<"\n";
vc[rt].push_back(l);
while (p<r){
reg int now=qry(p,r-1); if (lst&&a[now]!=a[lst]) break;
vc[rt].push_back(lst=now),p=lst+1;
}
vc[rt].push_back(r);
for (reg int i=0;i+1<vc[rt].size();i++){
reg int v=build(vc[rt][i],vc[rt][i+1]);
if (v) G[rt].push_back(v);
}
return rt;
}
inline int newnode(reg int x,reg int y){return t[++idx]={x,y,1,x,y,rnd(),0,0},idx;}
inline void pushup(reg int o){
t[o].sx=t[t[o].ls].sx+t[o].x+t[t[o].rs].sx,
t[o].sy=t[t[o].ls].sy+t[o].y+t[t[o].rs].sy,
t[o].sz=t[t[o].ls].sz+1+t[t[o].rs].sz;
}
void split(reg int o,reg int dx,reg int dy,reg int &x,reg int &y){
if (!o) return x=y=0,void();
// std::cerr<<o<<"\n";
assert(t[o].ls!=o&&t[o].rs!=o);
if (dy*t[o].x>=t[o].y*dx) y=o,split(t[o].ls,dx,dy,x,t[y].ls),pushup(y);
else x=o,split(t[o].rs,dx,dy,t[x].rs,y),pushup(x);
}
void Split(reg int o,reg int dx,reg int dy,reg int &x,reg int &y){
if (!o) return x=y=0,void();
// std::cerr<<o<<"\n";
assert(t[o].ls!=o&&t[o].rs!=o);
if (dy*t[o].x>=t[o].y*dx) y=o,Split(t[o].ls,dx,dy,x,t[y].ls),pushup(y);
else x=o,Split(t[o].rs,dx+t[t[o].ls].sx+t[o].x,dy+t[t[o].ls].sy+t[o].y,t[x].rs,y),pushup(x);
}
int merge(reg int x,reg int y){
if (!x||!y) return x+y;
// std::cerr<<x<<" "<<y<<"\n";
if (t[x].val>t[y].val) return t[x].rs=merge(t[x].rs,y),pushup(x),x;
return t[y].ls=merge(x,t[y].ls),pushup(y),y;
}
int nd[N],tot;
void dfs(reg int o){if (!o) return; dfs(t[o].ls),nd[++tot]=o,dfs(t[o].rs);}
void Dfs(reg int u){
// std::cerr<<"<< "<<u<<"\n";
reg int sx=0,sy=0;
for (reg int i=1;i+1<vc[u].size();i++) sx++,sy+=a[vc[u][i]];
for (auto v:G[u]){
Dfs(v);
if (t[Rt[u]].sz<t[Rt[v]].sz) std::swap(Rt[u],Rt[v]);
// std::cerr<<"edge "<<u<<" "<<v<<"\n";
tot=0,dfs(Rt[v]);
for (reg int i=1,a,b;i<=tot;i++){
split(Rt[u],t[nd[i]].x,t[nd[i]].y,a,b);
// std::cerr<<"qwq "<<t[2].ls<<" "<<t[2].rs<<"\n";
// std::cerr<<"<<< "<<"\n";
Rt[u]=merge(merge(a,nd[i]),b);
}
}
tot=0,dfs(Rt[u]);
sy+=a[ID[u].first]+a[ID[u].second],sx+=2; reg int A,B;
Split(Rt[u],sx,sy,A,B);
reg Fac &now=ans[u]; now={sy+t[A].sy,sx+t[A].sx};
Rt[u]=merge(A,B);
std::cerr<<"ans "<<now.x<<" "<<now.y<<"\n";
sy-=a[ID[u].first]+a[ID[u].second],sx-=2;
Split(Rt[u],sx,sy,A,B);
sx+=t[A].sx,sy+=t[A].sy;
Rt[u]=merge(newnode(sx,sy),B);
std::cerr<<"merge "<<sx<<" "<<sy<<"\n";
}
#undef int
void initialize(std::vector<int> A){
n=A.size(); for (reg int i=1;i<=n;i++) a[i]=A[i-1],st[0][i]=i;
for (reg int j=1;1<<j<=n;j++) for (reg int i=1;i+(1<<j)-1<=n;i++) st[j][i]=chk(st[j-1][i],st[j-1][i+(1<<j-1)]);
Dfs(build(0,n+1)); //exit(0);
// for (reg int i=1;i<=qwq;i++) for (auto j:G[i]) std::cerr<<"edge "<<i<<" "<<j<<"\n";
}
std::array<long long, 2> maximum_average(int i, int j){
auto [x,y]=ans[id[{i+1,j+1}]];
std::cerr<<"query "<<x<<" "<<y<<"\n";
// x+=a[i+1]+a[j+1],y+=2;
reg long long g=std::__gcd(x,y);
return {x/g,y/g};
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 0ms
memory: 24600kb
input:
10 2 4 3 9 9 9 9 9 9 1 2 0 2 0 9
output:
3 1 20 3
result:
ok correct!
Test #2:
score: 5
Accepted
time: 3ms
memory: 28676kb
input:
15 4596730 8340349 4612555 5692442 3914918 5213545 5248236 1276073 3844119 2943960 9231647 5091649 2239006 9139001 4735414 100 7 8 5 6 2 4 0 4 8 9 10 11 3 4 0 1 10 11 10 11 3 4 4 5 12 13 0 2 2 4 11 12 12 14 2 3 7 8 12 14 6 7 4 5 11 12 10 11 7 12 8 9 8 9 0 2 2 3 12 14 7 9 7 9 12 13 10 11 9 11 13 14 8...
output:
2560096 1 10461781 2 14219915 3 27156994 5 6788079 2 7161648 1 4803680 1 12937079 2 7161648 1 7161648 1 4803680 1 9128463 2 11378007 2 5849878 1 14219915 3 7330655 2 16113421 3 10304997 2 2560096 1 16113421 3 6524309 2 9128463 2 7330655 2 7161648 1 4156467 1 6788079 2 6788079 2 5849878 1 10304997 2 ...
result:
ok correct!
Test #3:
score: 5
Accepted
time: 824ms
memory: 24360kb
input:
15 962724 8815662 7612372 5708998 125165 5107756 9366378 9514244 2381600 4299006 9423670 8225791 7458292 2315903 7210561 600000 7 8 0 4 6 8 9 10 11 12 4 5 13 14 8 13 9 11 2 3 7 8 9 12 6 8 0 4 0 2 12 13 1 2 8 13 0 3 9 10 4 5 6 7 6 8 6 7 0 2 4 13 3 4 6 7 8 9 6 7 11 12 5 8 0 3 9 13 4 8 4 8 8 9 8 13 4 1...
output:
5947922 1 23224921 5 21262222 3 6861338 1 15684083 2 5232921 2 4763232 1 17052131 3 21948467 3 6660685 1 5947922 1 29406759 4 21262222 3 23224921 5 17390758 3 9774195 2 8214017 1 17052131 3 5774939 1 6861338 1 5232921 2 9440311 1 21262222 3 9440311 1 17390758 3 11643561 2 5834163 2 9440311 1 3340303...
result:
ok correct!
Test #4:
score: 5
Accepted
time: 2ms
memory: 26328kb
input:
15 1 8446287 2 999999 3000000 5533975 3000000 3816891 3000000 7671276 3000000 999999 5836790 8574548 1 23 0 14 1 2 0 1 2 14 0 2 3 11 2 3 4 6 3 4 5 6 4 5 6 8 7 8 6 7 8 10 9 10 8 9 10 11 11 14 12 14 11 12 13 14 12 13
output:
17959923 5 8446289 2 4223144 1 45433481 13 2815430 1 31022140 9 1000001 2 11533975 3 3999999 2 8533975 2 8533975 2 3272297 1 6816891 2 6816891 2 4557092 1 5335638 1 5335638 1 3999999 2 7705669 2 14411339 3 6836789 2 8574549 2 7205669 1
result:
ok correct!
Test #5:
score: 5
Accepted
time: 5ms
memory: 24560kb
input:
15 1 15 16 14 18 13 20 12 22 11 24 10 26 9 1 27 0 14 1 3 0 1 2 3 1 2 3 5 0 3 4 5 3 4 5 7 0 5 6 7 5 6 7 9 0 7 8 9 7 8 9 11 0 9 10 11 9 10 11 13 0 11 12 13 11 12 13 14 0 13
output:
212 15 15 1 8 1 15 1 31 2 15 1 23 2 31 2 16 1 15 1 77 6 16 1 33 2 15 1 109 8 33 2 17 1 15 1 71 5 17 1 35 2 15 1 44 3 35 2 18 1 5 1 211 14
result:
ok correct!
Subtask #2:
score: 0
Memory Limit Exceeded
Dependency #1:
100%
Accepted
Test #6:
score: 0
Memory Limit Exceeded
input:
48 225555 4046145 5839635 7194994 4703765 1253415 2526352 3198926 6313532 2368195 5024833 9436074 1792945 7650559 3393464 2402026 7697170 5205463 9830460 5392966 1687150 9984223 3014343 8856776 1412298 9773499 6469768 5802450 758943 2748325 7110370 4498454 2674137 8596714 8823659 9855644 6654297 367...
output:
Unauthorized output
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Runtime Error
Test #15:
score: 0
Runtime Error
input:
300000 1 2 4 4 4 4 3 2 4 4 3 4 4 4 4 4 4 4 4 4 3 4 3 4 4 4 4 4 4 4 4 3 3 4 4 4 3 4 3 4 4 4 4 4 4 4 4 4 4 3 3 4 4 4 3 4 4 3 4 4 4 4 4 4 4 3 2 4 4 4 4 4 4 4 4 4 4 4 4 4 3 4 4 4 4 4 4 4 4 4 4 4 2 4 4 2 4 4 3 4 4 4 2 3 4 4 4 4 4 4 3 2 4 4 4 2 4 4 4 4 4 4 4 4 4 4 4 2 4 4 4 4 4 4 3 4 4 3 4 4 4 4 4 4 4 4 4...
output:
Unauthorized output
result:
Subtask #5:
score: 0
Skipped
Dependency #3:
0%
Subtask #6:
score: 0
Runtime Error
Test #28:
score: 0
Runtime Error
input:
300000 1 300000 300001 299999 300003 299998 300005 299997 300007 299996 300009 299995 300011 299994 300013 299993 300015 299992 300017 299991 300019 299990 300021 299989 300023 299988 300025 299987 300027 299986 300029 299985 300031 299984 300033 299983 300035 299982 300037 299981 300039 299980 3000...
output:
Unauthorized output
result:
Subtask #7:
score: 0
Skipped
Dependency #4:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%