이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wall.h"
#include <iostream>
#include <climits>
#include <set>
using namespace std;
const int nmax=2*1000*1000+5;
struct aint
{
int mn,mx,whmn,whmx;
}arb[4*nmax],ans;
void mrg(aint &A,aint B,aint C)
{
A=B;
if(C.mn<A.mn) A.mn=C.mn,A.whmn=C.whmn;
if(C.mx>A.mx) A.mx=C.mx,A.whmx=C.whmx;
}
set<int> s;
set<int>::iterator it,it1;
int v[nmax];
int n,poz,i;
void upd(int nod,int l,int r,int poz)
{
if(l==r)
{
arb[nod].mx=arb[nod].mn=v[poz];
arb[nod].whmx=arb[nod].whmn=poz;
if(v[poz]==-1)
{
arb[nod].mx=-1;arb[nod].mn=INT_MAX;
}
return;
}
int m=(l+r)/2;
if(poz<=m) upd(2*nod,l,m,poz);
else upd(2*nod+1,m+1,r,poz);
mrg(arb[nod],arb[2*nod],arb[2*nod+1]);
}
void query(int nod,int l,int r,int st,int dr)
{
if(st<=l&&r<=dr)
{
mrg(ans,ans,arb[nod]);
return;
}
int m=(l+r)/2;
if(st<=m) query(2*nod,l,m,st,dr);
if(m<dr) query(2*nod+1,m+1,r,st,dr);
}
int getmx(int st,int dr)
{
ans.mx=-1,ans.mn=INT_MAX;
query(1,1,n,st,dr);
poz=ans.whmx;
return ans.mx;
}
int getmn(int st,int dr)
{
ans.mx=-1,ans.mn=INT_MAX;
query(1,1,n,st,dr);
poz=ans.whmn;
return ans.mn;
}
void rs(int wh,int llim,int H)
{
it=s.lower_bound(wh);
if(it!=s.begin())
{
it--;
if((*it)<llim-1)
{
v[llim-1]=v[wh];
s.insert(llim-1);
upd(1,1,n,llim-1);
}
}
else
{
if(llim-1)
{
v[llim-1]=v[wh];
s.insert(llim-1);
upd(1,1,n,llim-1);
}
}
s.insert(wh);
v[wh]=H;
upd(1,1,n,wh);
}
void comp(int poz)
{
it=s.lower_bound(poz);
bool brk=0;
if(it!=s.begin())
{
it--;
while(it!=s.begin()&&(!brk))
{
if(v[(*it)]!=v[poz]) brk=1;
else
{
v[(*it)]=-1;
upd(1,1,n,(*it));
it1=it;
it1--;
s.erase(it);
it=it1;
}
}
}
if(s.upper_bound(poz)!=s.end()&&v[(*s.upper_bound(poz))]==v[poz])
{
v[poz]=-1;upd(1,1,n,poz);
s.erase(poz);
}
}
bool finds(int x)
{
return (s.find(x)!=s.end());
}
void build(int nod,int l,int r)
{
if(l==r)
{
arb[nod].whmx=arb[nod].whmn=l;
arb[nod].mx=-1;arb[nod].mn=INT_MAX;
return;
}
int m=(l+r)/2;
build(2*nod,l,m);
build(2*nod+1,m+1,r);
mrg(arb[nod],arb[2*nod],arb[2*nod+1]);
}
void buildWall(int N, int k, int op[], int left[], int right[], int height[], int finalHeight[]){
n=N;
build(1,1,n);
int h,pp;
s.insert(n);
v[n]=0;
upd(1,1,n,n);
for(int i=0;i<k;i++)
{
left[i]++;right[i]++;
h=height[i];
if(op[i]==1)
{
while(getmn(left[i],right[i])<h)
{
if(finds(poz))
{
rs(poz,left[i],h);
if(finds(left[i]-1))comp(left[i]-1);
if(finds(poz)) comp(poz);
}
}
pp=(*s.lower_bound(right[i]));
if(getmn(pp,pp)<h)
{
rs(pp,left[i],v[pp]);
rs(right[i],left[i],h);
if(finds(left[i]-1))comp(left[i]-1);
if(finds(right[i])) comp(right[i]);
}
}
else
{
while(getmx(left[i],right[i])>h)
{
if(finds(poz))
rs(poz,left[i],h);
}
pp=(*s.lower_bound(right[i]));
if(getmx(pp,pp)>h)
{
rs(pp,left[i],v[pp]);
rs(right[i],left[i],h);
upd(1,1,n,right[i]);
if(finds(left[i]-1))comp(left[i]-1);
if(finds(right[i])) comp(right[i]);
}
}
}
for(i=1;i<=n;i++)
if(finds(i)) finalHeight[i-1]=v[i];
for(i=n-1;i>=0;i--)
if(!finds(i+1))
finalHeight[i]=finalHeight[i+1];
return;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |