QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#599149 | #8577. 평균 최대화 | Matutino | 0 | 0ms | 28336kb | C++14 | 4.1kb | 2024-09-29 01:50:44 | 2024-09-29 01:50:45 |
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(1,n)); //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: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 0ms
memory: 28336kb
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: 0
Wrong Answer
time: 0ms
memory: 13332kb
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 4104409 1 6788079 2 6788079 2 5849878 1 10304997 2 ...
result:
wrong answer Wrong Answer on query #25: 4104409/1 != 20782335/5
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
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:
0%