QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#360539 | #3066. Intrinsic Interval | eastcloud | WA | 4ms | 24752kb | C++14 | 5.1kb | 2024-03-21 21:16:22 | 2024-03-21 21:16:23 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define N 300005
#define inf 0x3f3f3f3f
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=getchar();
while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x*f;
}
void write(int x){
if(x<0)x=-x,putchar('-');
if(x/10)write(x/10);
putchar(x%10+'0');
}
int n,m;
struct segmenttree{
int ls[N<<2],rs[N<<2],l[N<<2],r[N<<2],v[N<<2],tag[N<<2];
int tot;
void push_down(int x){
if(!tag[x])return;
tag[ls[x]]+=tag[x];tag[rs[x]]+=tag[x];
v[ls[x]]+=tag[x];v[rs[x]]+=tag[x];tag[x]=0;
}
void push_up(int x){
v[x]=min(v[ls[x]],v[rs[x]]);
}
void build(int &x,int L,int R){
x=++tot;l[x]=L;r[x]=R;if(L==R)return v[x]=0,void();
int mid=(L+R)>>1;build(ls[x],L,mid);build(rs[x],mid+1,R);
push_up(x);
}
void update(int x,int L,int R,int val){
//cerr<<l[x]<<' '<<r[x]<<' '<<L<<' '<<R<<' '<<val<<' '<<v[x]<<' '<<x<<endl;
if(L<=l[x] && r[x]<=R)return v[x]+=val,tag[x]+=val,void();
int mid=(l[x]+r[x])>>1;push_down(x);
if(L<=mid)update(ls[x],L,R,val);
if(R>mid)update(rs[x],L,R,val);
push_up(x);
}
int query(int x,int p){
if(l[x]==r[x])return l[x];
push_down(x);
if(r[ls[x]]<=p && v[ls[x]]==0)return query(ls[x],p);
if(r[ls[x]]>p)return query(ls[x],p);
else return query(rs[x],p);
}
}T;
int tot=0;
struct Node{
int l,r,val;
Node(int l=0,int r=0,int val=0):l(l),r(r),val(val){}
};
Node stamin[N],stamax[N],sta[N],info[N];
int tp1,tp2,a[N],tp,clo[N];
int consec(Node u,Node v){
if(u.r+1==v.l)return 1;
else if(v.r+1==u.l)return 2;
else return 0;
}
vector<int> e[N];
void add(int u,int v){
//cerr<<u<<' '<<v<<endl;
e[u].push_back(v);e[v].push_back(u);
}
int con[N];
void build(){
for(int i=1;i<=n;i++)if(abs(a[i]-a[i+1])==1)con[i]=1;
stamin[++tp1]=Node(1,1,a[1]);stamax[++tp2]=Node(1,1,a[1]);
T.update(1,1,n,-1);sta[++tp]=Node(a[1],a[1],1);tot=n;clo[1]=3;info[1]=Node(1,1,0);
T.update(1,1,1,1);
for(int i=2;i<=n;i++){
T.update(1,1,n,-1);int l=i;clo[i]=3;
T.update(1,i,i,i);
//cerr<<stamin[tp1].val<<' '<<a[i]<<endl;
while(tp1 && stamin[tp1].val>a[i]){
//cerr<<stamin[tp1].l<<' '<<l-1<<' '<<a[i]-stamin[tp1].val<<endl;
T.update(1,stamin[tp1].l,l-1,stamin[tp1].val-a[i]);
l=stamin[tp1].l;tp1--;
}
stamin[++tp1]=Node(l,i,a[i]);l=i;
while(tp2 && stamax[tp2].val<a[i]){
//cerr<<stamax[tp2].l<<' '<<l-1<<' '<<a[i]-stamax[tp2].val<<endl;
T.update(1,stamax[tp2].l,l-1,a[i]-stamax[tp2].val);
l=stamax[tp2].l;tp2--;
}
stamax[++tp2]=Node(l,i,a[i]);
sta[++tp]=Node(a[i],a[i],i);info[i]=Node(i,i,0);
while(tp!=1){
Node u=sta[tp],v=sta[tp-1];
int vali=consec(v,u);
if(clo[v.val] && e[v.val].size() && !con[info[v.val].r] && vali==clo[v.val]){
add(v.val,u.val);tp--;info[v.val].r=info[u.val].r;
}
else{
int pos=T.query(1,i);
//cerr<<pos<<endl;
if(pos==info[u.val].l)break;
else if(pos==info[v.val].l){
++tot;Node np=Node(min(v.l,u.l),max(v.r,u.r),tot);tp-=2;
add(tot,v.val);add(tot,u.val);
info[tot]=Node(info[v.val].l,info[u.val].r,0);
clo[tot]=consec(v,u);sta[++tp]=np;
}
else{
++tot;int r=0;
while(info[sta[tp].val].l!=pos)r=max(r,sta[tp].r),add(tot,sta[tp].val),tp--;
//cerr<<tp<<endl;
add(tot,sta[tp].val);
r=max(r,sta[tp].r);info[tot]=Node(pos,i,0);tp--;
sta[++tp]=Node(r-(i-pos-1)+1,r,tot);
}
}
}
//for(int i=1;i<=tp;i++)cerr<<info[sta[i].val].l<<' '<<info[sta[i].val].r<<' ';
//cerr<<"****"<<endl;
}
}
int cnt,dfn[N],mi[N][21];
void dfs(int x,int fa){
//cerr<<x<<' '<<fa<<endl;
dfn[x]=++cnt;mi[dfn[x]][0]=fa;
for(auto v:e[x])if(v!=fa)dfs(v,x);
}
int get(int x,int y){
return (dfn[x]<dfn[y]?x:y);
}
void pre_lca(){
for(int i=1;i<=__lg(tot);i++){
for(int j=1;j+(1<<i)-1<=tot;j++){
mi[j][i]=get(mi[j][i-1],mi[j+(1<<(i-1))][i-1]);
}
}
}
int lca(int x,int y){
if(x==y)return x;x=dfn[x];y=dfn[y];
if(x>y)swap(x,y);x++;
int d=__lg(y-x+1);
return get(mi[x][d],mi[y-(1<<d)+1][d]);
}
int main(){
#ifdef EAST_CLOUD
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();int rt;
for(int i=1;i<=n;i++)a[i]=read();
T.build(rt,1,n);build();dfs(tot,0);pre_lca();m=read();
for(int i=1;i<=m;i++){
int u=read(),v=read(),res=lca(u,v);
write(info[res].l);putchar(' ');write(info[res].r);putchar('\n');
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 24752kb
input:
7 3 1 7 5 6 4 2 3 3 6 7 7 1 3
output:
3 6 7 7 1 7
result:
ok 3 lines
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 24748kb
input:
10 2 1 4 3 5 6 7 10 8 9 5 2 3 3 7 4 7 4 8 7 8
output:
1 4 1 7 1 7 1 10 1 10
result:
wrong answer 2nd lines differ - expected: '3 7', found: '1 7'