QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#618388 | #2559. Endless Road | Others | WA | 3ms | 75196kb | C++14 | 6.0kb | 2024-10-06 21:40:31 | 2024-10-06 21:40:32 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define wr(x,ch) write(x),putchar(ch)
using namespace std;
typedef pair<int,int> pai;
#define x first
#define y second
ll read() {
ll x=0;bool f=0;char c=getchar();
while(!isdigit(c)) f|=c=='-',c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
return f?-x:x;
}
void write(ll x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) write(x/10);
putchar(x%10^48);
return ;
}
const int N=5e5,inf=0x7f7f7f7f;
int flag[N],bit[N],top,n,lst[N],nxt[N];
set<int> st;
struct rg {
int l,r,i;
bool operator<(const rg &p) const {
return r<p.r||(r==p.r&&(l>p.l||(l==p.l&&i<p.i)));
}
}a[N];
struct node {
int Maxl,tag;
pai Minl,Mins;
int Mr,Ml;
}tr[N<<2];
pai operator+(const pai &x,const pai &y) {
if(x.x==y.x) return a[x.y].i<a[y.y].i?x:y;
return min(x,y);
}
pai operator*(const pai &x,const pai &y) {
if(x.x==y.x) return a[x.y].i<a[y.y].i?x:y;
return max(x,y);
}
void update(int p,int t) {
tr[p].Mins.x+=t;
tr[p].tag+=t;
return ;
}
void pushdown(int p) {
if(tr[p].tag) update(p<<1,tr[p].tag),update(p<<1|1,tr[p].tag),tr[p].tag=0;
return ;
}
void pushup(int p) {
tr[p].Maxl=max(tr[p<<1].Maxl,tr[p<<1|1].Maxl);
tr[p].Minl=tr[p<<1].Minl*tr[p<<1|1].Minl;
tr[p].Mins=tr[p<<1].Mins+tr[p<<1|1].Mins;
tr[p].Mr=max(tr[p<<1].Mr,tr[p<<1|1].Mr);
tr[p].Ml=min(tr[p<<1].Ml,tr[p<<1|1].Ml);
return ;
}
void build(int l,int r,int p,int lst) {
if(l==r) {
if(a[l].l>=lst) {
// printf("%d ",l);
tr[p].Mins=pai(flag[a[l].r]-flag[a[l].l],l);
tr[p].Maxl=a[l].l;
tr[p].Minl=pai(-1,0);
tr[p].Mr=a[l].r-1;
tr[p].Ml=a[l].l;
}else {
tr[p].Mins=pai(inf,0);
tr[p].Minl=pai(a[l].l,l);
tr[p].Maxl=0;
tr[p].Mr=0;
tr[p].Ml=inf;
}
return ;
}
int mid=l+r>>1;
build(l,mid,p<<1,lst),build(mid+1,r,p<<1|1,max(lst,tr[p<<1].Maxl));
return pushup(p);
}
void add(int x,int y) {
while(x<=top) bit[x]+=y,x+=(x&-x);
return ;
}
int ask(int x) {
int tot=0;
while(x) tot+=bit[x],x-=(x&-x);
return tot;
}
int askl(int l,int r,int p,int lim) {
if(tr[p].Mr<lim) return -1;
if(l==r) return l;
int mid=l+r>>1,ans;
if((ans=askl(l,mid,p<<1,lim))!=-1) return ans;
return askl(mid+1,r,p<<1|1,lim);
}
int askr(int l,int r,int p,int lim) {
if(tr[p].Ml>lim) return -1;
if(l==r) return l;
int mid=l+r>>1,ans;
if((ans=askr(mid+1,r,p<<1|1,lim))!=-1) return ans;
return askr(l,mid,p<<1,lim);
}
void add(int s,int t,int l,int r,int p,int d) {
if(s>t) return ;
if(s<=l&&r<=t) return update(p,-d);
int mid=l+r>>1;pushdown(p);
if(s<=mid) add(s,t,l,mid,p<<1,d);
if(t>mid) add(s,t,mid+1,r,p<<1|1,d);
return pushup(p);
}
void deal(int x) {
int l=askl(1,n,1,x),r=askr(1,n,1,x);
add(l,r,1,n,1,flag[x+1]-flag[x]);
add(x,flag[x+1]-flag[x]);
return ;
}
int ask1(int s,int t,int l,int r,int p) {
if(s>t) return 0;
if(s<=l&&r<=t) return tr[p].Maxl;
int mid=l+r>>1,ans=0;
if(s<=mid) ans=max(ans,ask1(s,t,l,mid,p<<1));
if(t>mid) ans=max(ans,ask1(s,t,mid+1,r,p<<1|1));
return ans;
}
int ask3(int s,int t,int l,int r,int p) {
if(s>t||!tr[p].Mins.y) return t+1;
if(s<=l&&r<=t) {
if(l==r) return l;
int mid=l+r>>1,tmp=ask3(s,t,l,mid,p<<1);
if(tmp!=t+1) return tmp;
return ask3(s,t,mid+1,r,p<<1|1);
}
int mid=l+r>>1,tmp;
if(s<=mid) {
tmp=ask3(s,t,l,mid,p<<1);
if(tmp!=t+1) return tmp;
}
return ask3(s,t,mid+1,r,p<<1|1);
}
pai ask2(int s,int t,int l,int r,int p) {
if(s>t) return pai(-1,0);
if(s<=l&&r<=t) return tr[p].Minl;
int mid=l+r>>1;
pai ans=pai(-1,0);
if(s<=mid) ans=ans*ask2(s,t,l,mid,p<<1);
if(t>mid) ans=ans*ask2(s,t,mid+1,r,p<<1|1);
return ans;
}
void del(int x,int l,int r,int p) {
if(l==r) {
tr[p].Mins=pai(inf,0);
tr[p].Minl=pai(-1,0);
tr[p].tag=0;
tr[p].Maxl=0;
tr[p].Mr=0;
tr[p].Ml=inf;
return ;
}
int mid=l+r>>1;
pushdown(p);
if(x<=mid) del(x,l,mid,p<<1);
else del(x,mid+1,r,p<<1|1);
return pushup(p);
}
void add(int x,int l,int r,int p) {
if(l==r) {
tr[p].Maxl=0,tr[p].tag=0;
tr[p].Minl=pai(-1,0),tr[p].Mins=pai(flag[a[x].r]-flag[a[x].l]-(ask(a[x].r-1)-ask(a[x].l-1)),x);
tr[p].Mr=a[x].r-1,tr[p].Ml=a[x].l;
return ;
}
int mid=l+r>>1;pushdown(p);
if(x<=mid) add(x,l,mid,p<<1);
else add(x,mid+1,r,p<<1|1);
return pushup(p);
}
void ins(int s,int t,int l,int r,int p,int &lim) {
if(s<=l&&r<=t) {
int mid=l+r>>1;
if(tr[p].Minl.x<lim) return ;
if(l==r) {
add(l,1,n,1);
lim=max(lim,a[l].l);
return ;
}
ins(s,t,l,mid,p<<1,lim);
ins(s,t,mid+1,r,p<<1|1,lim);
return pushup(p);
}
int mid=l+r>>1;pushdown(p);
if(s<=mid) ins(s,t,l,mid,p<<1,lim);
if(t>mid) ins(s,t,mid+1,r,p<<1|1,lim);
return pushup(p);
}
int Ask(int x,int l,int r,int p) {
if(l==r) return tr[p].Mins.x;
int mid=l+r>>1;pushdown(p);
if(x<=mid) return Ask(x,l,mid,p<<1);
return Ask(x,mid+1,r,p<<1|1);
}
signed main() {
// system("fc ex_ds1.out ds.out");
// return 0;
// freopen("ex_ds2.in","r",stdin);
// freopen("ds.out","w",stdout);
// freopen("ds.in","r",stdin);
// freopen("ds.out","w",stdout);
n=read();
for(int i=1;i<=n;i++) a[i].l=read()+1,a[i].r=read()+1,flag[++top]=a[i].l,flag[++top]=a[i].r,a[i].i=i;
sort(flag+1,flag+top+1),top=unique(flag+1,flag+top+1)-flag-1;
for(int i=1;i<=n;i++) a[i].l=lower_bound(flag+1,flag+top+1,a[i].l)-flag,a[i].r=lower_bound(flag+1,flag+top+1,a[i].r)-flag;
sort(a+1,a+n+1);
build(1,n,1,0);
// puts("");
for(int i=1;i<top;i++) st.insert(i),lst[i]=i-1,nxt[i]=i+1;
nxt[0]=1,nxt[top-1]=0,lst[0]=top-1;
for(int i=1;i<=n;i++) {
int t=tr[1].Mins.y;
if(tr[1].Mins.x) {
for(int j=*st.lower_bound(a[t].l);j<a[t].r&&j;j=nxt[j])
deal(j),lst[nxt[j]]=lst[j],nxt[lst[j]]=nxt[j];
auto L=st.lower_bound(a[t].l),R=st.lower_bound(a[t].r);
st.erase(L,R);
}
del(t,1,n,1);
int ml=ask1(1,t-1,1,n,1),tlim=ask3(t+1,n,1,n,1);
pai tmp;
ins(t+1,tlim-1,1,n,1,ml);
// printf("%d : ",a[t].i);
// while((tmp=ask2(t+1,tlim-1,1,n,1)).x>=ml)
// add(tmp.y,1,n,1),ml=max(ml,a[tmp.y].l),printf("%d ",tmp.y);
// puts("");
wr(a[t].i,' ');
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 75196kb
input:
6 1 2 2 3 3 4 4 5 1 3 3 5
output:
1 2 5 3 4 6
result:
ok 6 tokens
Test #2:
score: -100
Wrong Answer
time: 3ms
memory: 75112kb
input:
4 3 7 10 14 1 6 6 11
output:
1 3 2 0
result:
wrong answer 4th words differ - expected: '4', found: '0'