QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#392722 | #8578. 과일 게임 | Naganohara_Yoimiya | 0 | 1205ms | 34292kb | C++14 | 6.2kb | 2024-04-17 19:53:18 | 2024-04-17 19:53:19 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define fi first
#define se second
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
return x*f;
}
const int mod=998244353;
int ksm(int x,ll y,int p=mod){
int ans=1;y%=(p-1);
for(int i=y;i;i>>=1,x=1ll*x*x%p)if(i&1)ans=1ll*ans*x%p;
return ans%p;
}
int inv(int x,int p=mod){return ksm(x,p-2,p)%p;}
mt19937 rnd(time(0));
int randint(int l,int r){return rnd()%(r-l+1)+l;}
void add(int &x,int v){x+=v;if(x>=mod)x-=mod;}
void Mod(int &x){if(x>=mod)x-=mod;}
int cmod(int x){if(x>=mod)x-=mod;return x;}
template<typename T>void cmax(T &x,T v){x=max(x,v);}
template<typename T>void cmin(T &x,T v){x=min(x,v);}
const int N=1e5+5;
struct Node{
vector<pair<int,int> >L,R;
int mx;bool sep;
};
void Rec(vector<pair<int,int> >&A){
vector<pair<int,int> >B;
for(int i=0;i<A.size();i++){
int j=i;int cnt=A[i].se;
while(j+1<A.size()&&A[j+1].fi==A[i].fi)cnt+=A[j+1].se,j++;
B.emplace_back(mk(A[i].fi,cnt)),i=j;
}
A=B;
}
int Lg[N];
int getans(int x,int y){
return x+Lg[y];
}
int calc(vector<pair<int,int> >vals){
if(vals.empty())return 0;
Rec(vals);
while(1){
int mx=0,mn=1e9;
// cout<<"vals = ";for(auto [v,c]:vals)cout<<"("<<v<<","<<c<<") ";puts("");
for(auto [v,c]:vals)if(v!=-1)cmax(mx,v),cmin(mn,v);
// cout<<"mx = "<<mx<<" mn = "<<mn<<endl;
if(mx==mn)break;
vector<pair<int,int> >to;
for(auto [v,c]:vals){
if(v!=mn){
to.emplace_back(mk(v,c));
continue;
}
if(c%2==0)to.emplace_back(mk(v+1,c/2));
else{
if(c-1>0)to.emplace_back(mk(v+1,(c-1)/2));
to.emplace_back(mk(-1,1));
if(c-1>0)to.emplace_back(mk(v+1,(c-1)/2));
}
}
// cout<<"to = ";for(auto [v,c]:to)cout<<"("<<v<<","<<c<<") ";puts("");
Rec(to);
// cout<<" -> to = ";for(auto [v,c]:to)cout<<"("<<v<<","<<c<<") ";puts("");
vals=to;
}
int cc=0,vv=0;
for(auto [v,c]:vals)if(v!=-1)cmax(cc,c),vv=v;
return getans(vv,cc);
}
const int INF=1e9;
int del_sep(vector<pair<int,int> >&A){
int pl=INF,pr=-1;
for(int i=0;i<A.size();i++)if(A[i].fi==-1)cmin(pl,i),cmax(pr,i);
if(pr==-1||pl==pr)return 0;
vector<pair<int,int> >B,C;
for(int i=0;i<=pl;i++)C.emplace_back(A[i]);
for(int i=pl+1;i<pr;i++)B.emplace_back(A[i]);
for(int i=pr+1;i<A.size();i++)C.emplace_back(A[i]);
A=C;
return calc(B);
}
int vc(int x){return x==-1?INF:x;}
int Simp(vector<pair<int,int> >&A){
Rec(A);
int res=0;
while(1){
int p=-1;
for(int i=1;i+1<A.size();i++)if(A[i].fi!=-1){
if(A[i].fi<min(vc(A[i-1].fi),vc(A[i+1].fi))){p=i;break;}
}
if(p==-1)break;
if(A[p].se%2==0)A[p].fi++,A[p].se/=2,Rec(A);
else{
auto [v,c]=A[p];v++,c=(c-1)/2;
A[p].fi=-1,A[p].se=1;
if(c>=1){
auto it=A.begin()+p;
A.insert(it,mk(v,c));
it=A.begin()+p+2;
A.insert(it,mk(v,c));
}
cmax(res,del_sep(A));
Rec(A);
}
}
return res;
}
void print(Node A){
cout<<"mx = "<<A.mx<<" sep = "<<A.sep<<endl;
cout<<"L = ";for(auto [v,c]:A.L)cout<<"("<<v<<","<<c<<") ";puts("");
cout<<"R = ";for(auto [v,c]:A.R)cout<<"("<<v<<","<<c<<") ";puts("");
}
const double DBG=0;
Node op(Node lc,Node rc){
Node res;
if(DBG){
cout<<"=========== Merge ===========\n";
puts("lc : ");
print(lc);
puts("rc : ");
print(rc);
}
res.mx=max(lc.mx,rc.mx);
if(lc.sep&&rc.sep){
vector<pair<int,int> >nw;
for(auto A:lc.R)nw.emplace_back(A);
for(auto A:rc.L)nw.emplace_back(A);
cmax(res.mx,calc(nw));
res.L=lc.L,res.R=rc.R;
}
else if(lc.sep){
res.L=lc.L;
vector<pair<int,int> >W;
W.emplace_back(mk(-1,1));
for(auto A:lc.R)W.emplace_back(A);
for(auto A:rc.L)W.emplace_back(A);
cmax(res.mx,Simp(W));
if(W[0].fi==-1)W.erase(W.begin());
res.R=W;
}
else if(rc.sep){
res.R=rc.R;
vector<pair<int,int> >W;
W=lc.L;
for(auto A:rc.L)W.emplace_back(A);
W.emplace_back(mk(-1,1));
cmax(res.mx,Simp(W));
if(W.back().fi==-1)W.pop_back();
res.L=W;
}
else{
vector<pair<int,int> >W;
W=lc.L;
for(auto A:lc.R)W.emplace_back(A);
for(auto A:rc.L)W.emplace_back(A);
for(auto A:rc.R)W.emplace_back(A);
cmax(res.mx,Simp(W));int p=-1;
for(int i=0;i<W.size();i++)if(W[i].fi==-1){p=i;break;}
if(p==-1)res.sep=0,res.L=W;
else{
res.sep=1;
for(int i=0;i<p;i++)res.L.emplace_back(W[i]);
for(int i=p+1;i<W.size();i++)res.R.emplace_back(W[i]);
}
}
cmax(res.mx,calc(res.L)),cmax(res.mx,calc(res.R));
for(auto [v,c]:res.L)cmax(res.mx,getans(v,c));
for(auto [v,c]:res.R)cmax(res.mx,getans(v,c));
if(DBG){
cout<<"res : \n";
print(res);
}
return res;
}
int a[N],n;
struct sgt{
Node d[N<<2];
#define ls(p) (p<<1)
#define rs(p) (p<<1|1)
void pushup(int p){d[p]=op(d[ls(p)],d[rs(p)]);}
void build(int l,int r,int p){
if(l==r){d[p].sep=0,d[p].L.emplace_back(mk(a[l],1)),d[p].mx=a[l];return ;}
int mid=(l+r)>>1;build(l,mid,ls(p)),build(mid+1,r,rs(p)),pushup(p);
}
void modify(int x,int v,int ql,int qr,int p){
if(ql==qr)return d[p].sep=0,d[p].L[0]=mk(v,1),d[p].mx=v,a[x]=v,void();
int mid=(ql+qr)>>1;
if(x<=mid)modify(x,v,ql,mid,ls(p));
else modify(x,v,mid+1,qr,rs(p));
pushup(p);
}
Node query(int l,int r,int ql,int qr,int p){
if(l<=ql&&qr<=r)return d[p];
int mid=(ql+qr)>>1;
if(l>mid)return query(l,r,mid+1,qr,rs(p));
if(r<=mid)return query(l,r,ql,mid,ls(p));
return op(query(l,r,ql,mid,ls(p)),query(l,r,mid+1,qr,rs(p)));
}
#undef ls
#undef rs
}T;
void prepare_game(vector<int>A){
n=A.size();
for(int i=1;i<=n;i++)a[i]=A[i-1];
for(int i=2;i<=n;i++)Lg[i]=Lg[i>>1]+1;
T.build(1,n,1);
}
int play_game(int l,int r){
l++,r++;
return T.query(l,r,1,n,1).mx;
}
void update_game(int p,int v){
p++;
T.modify(p,v,1,n,1);
}
#ifndef ONLINE_JUDGE
signed main(void){
// freopen("in.txt","r",stdin);
// freopen("data_8578/8578/2.in","r",stdin);
n=read();
for(int i=1;i<=n;i++)a[i]=read();
for(int i=2;i<=n;i++)Lg[i]=Lg[i>>1]+1;
T.build(1,n,1);
int q=read();
for(int i=1;i<=q;i++){
int op=read(),x=read(),y=read();
if(op==1)cout<<play_game(x,y)<<'\n';
else update_game(x,y);
}
return 0;
}
#endif
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 6ms
memory: 26480kb
input:
10 2 2 1 2 2 2 2 1 2 2 10 1 0 2 1 0 9 1 0 5 1 2 4 1 0 9 1 2 7 1 3 7 1 7 9 1 1 3 1 0 2
output:
3 4 3 3 4 4 4 3 2 3
result:
ok 10 lines
Test #2:
score: -5
Wrong Answer
time: 3ms
memory: 25784kb
input:
10 1 1 2 1 2 2 1 2 1 1 10 1 3 4 1 2 6 1 0 2 1 0 2 1 4 5 1 3 9 1 0 6 1 5 8 1 4 9 1 2 7
output:
2 3 3 3 3 4 3 2 4 3
result:
wrong answer 6th lines differ - expected: '3', found: '4'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #31:
score: 0
Wrong Answer
time: 23ms
memory: 26408kb
input:
4000 1 1 1 1 1 1 1 1 2 2 2 2 1 1 1 1 2 2 2 2 1 1 1 1 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 1 1 1 1 2 2 2 2...
output:
11 12 13 11 11 12 11 12 13 10 9 11 9 9 10 13 11 10 11 9 12 8 9 10 11 13 11 12 12 8 9 12 10 12 10 13 13 13 11 8 5 12 10 12 12 7 12 13 8 11 11 12 12 11 9 12 13 12 6 6 12 12 12 12 10 11 8 10 12 12 11 12 13 11 9 11 11 12 13 9 10 12 8 10 10 11 7 9 12 12 8 10 12 12 9 12 11 6 7 9 10 11 11 7 12 11 10 12 12 ...
result:
wrong answer 3rd lines differ - expected: '12', found: '13'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #3:
0%
Subtask #6:
score: 0
Wrong Answer
Test #69:
score: 0
Wrong Answer
time: 1205ms
memory: 34292kb
input:
100000 2 10 6 3 5 4 2 6 9 3 8 3 9 6 9 8 8 9 4 6 5 10 7 1 2 5 5 2 7 3 5 10 5 6 7 5 9 10 6 10 7 3 2 1 7 8 4 4 3 10 1 6 9 9 6 9 6 1 6 4 8 5 5 6 8 3 3 7 6 6 3 5 5 9 5 5 7 10 7 3 10 1 4 2 3 6 9 2 7 2 8 10 4 5 2 6 7 1 8 2 8 3 3 10 9 8 6 6 9 6 4 5 8 4 10 10 4 1 6 4 4 3 9 4 7 7 2 8 8 7 10 6 8 2 1 4 2 2 5 2 ...
output:
12 11 11 12 12 12 11 12 12 12 12 12 11 12 12 12 12 12 12 12 12 12 12 12 12 11 12 12 11 11 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 11 12 12 12 12 12 12 12 12 12 12 12 11 12 12 12 12 ...
result:
wrong answer 30th lines differ - expected: '10', found: '11'
Subtask #7:
score: 0
Skipped
Dependency #1:
0%