我的代码在别的OJ上已经过了,但是为啥luogu上过不了,而且第一个点错了,但是在本地跑的是一样的啊 code:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MX=4e5+5;
struct node{
int l,r,v,h,t,sh;
}s[2*MX];
vector <int > g[2*MX];
int tNode=0,n,m,root=0,root1,root2;
bool v[2*MX];
int to[MX];
int h[MX],next1[MX],next2[MX];
int grand[MX][20];
int asa2[MX][20];
int suma[MX][20];
void upd(int root){s[root].t=s[s[root].l].t+s[s[root].r].t+1;}
void dfs1(int p){
if(v[p]||p==0)return;
v[p]=true;
for(int i=0;i<g[p].size();i++){
int sp=g[p][i];
to[p]=sp;
dfs1(sp);
}
}
void update(int root){s[root].t=s[s[root].l].t+s[s[root].r].t+1;}
int new_node(int v,int i){
s[++tNode].l=s[tNode].r=0;
s[tNode].v=v;
s[tNode].h=rand();
s[tNode].t=1;
s[tNode].sh=i;
return tNode;
}
void split(int root,int k,int &x,int &y){
if(root==0)x=y=0;
else {
if(s[root].v<=k)
x=root,split(s[root].r,k,s[root].r,y);
else
y=root,split(s[root].l,k,x,s[root].l);
update(root);
}
}
int merge(int x,int y){
if(!x||!y)return x+y;
if(s[x].h<s[y].h){
s[x].r=merge(s[x].r,y);
update(x);
return x;
}
else{
s[y].l=merge(x,s[y].l);
update(y);
return y;
}
}
int kth(int root,int k){
if(root==0||k==0)return 0;
while(1){
if(k<=s[s[root].l].t)root=s[root].l;
else if(k==s[s[root].l].t+1)return root;
else k-=s[s[root].l].t+1,root=s[root].r;
}
}
int pre(int root,int x){
if(x==-1e10)return 0;
int a,b,ret=0;
split(root,x-1,a,b);
ret=kth(a,s[a].t);
root=merge(a,b);
return ret;
}
int nxt(int root,int x){
if(x==-1e10)return 0;
int a,b,ret=0;
split(root,x,a,b);
ret=kth(b,1);
root=merge(a,b);
return ret;
}
void jago1(){
for(int i=0;i<=2*n;i++)grand[i][0]=to[i];
for(int s=1;s<=19;s++)
for(int i=0;i<=2*n;i++)
grand[i][s]=grand[grand[i][s-1]][s-1];
}
void jago2(){
for(int i=0;i<=2*n;i++)asa2[i][0]=abs(h[to[i]]-h[i]);
for(int i=0;i<=n;i++)suma[i][0]=abs(h[to[i]]-h[i]);
for(int s=1;s<=19;s++)
for(int i=0;i<=2*n;i++){
asa2[i][s]=asa2[i][s-1]+asa2[grand[i][s-1]][s-1];
suma[i][s]=suma[i][s-1]+suma[grand[i][s-1]][s-1];
}
}
pair<int ,int > sx(int x,int i){
int pos=i;
int length=0;
int asum=0,bsum=0;
for(int j=19;j>=0;j--){
if(asa2[pos][j]<=x&&!grand[pos][j]==0){
x-=asa2[pos][j];
length+=asa2[pos][j];
asum+=suma[pos][j];
pos=grand[pos][j];
}
}
return make_pair(asum,length-asum);
}
signed main(){
//freopen("My.out","w",stdout);
memset(v,false,sizeof(v));
h[0]=0x3f3f3f3f;
s[0].v=-1e10;
int sd;
cin>>n;
for(int i=1;i<=n;i++)cin>>h[i];
for(int i=1;i<=n;i++)h[i+n]=h[i];
cin>>sd;
for(int i=n;i>=1;i--){
int x,y;
split(root,h[i]-1,x,y);
root=merge(x,merge(new_node(h[i],i),y));
if(i==n){
next1[i]=0,next2[i]=0;
continue;
}
if(i==n-1){
next1[i]=i+1,next2[i]=0;
continue;
}
int a=pre(root,h[i]),b=nxt(root,h[i]);
int c=pre(root,s[a].v);
int d=nxt(root,s[b].v);
int sa=s[a].v,sb=s[b].v,sc=s[c].v,sd=s[d].v;
//cout<<s[a].sh<<' '<<s[b].sh<<' '<<s[c].sh<<' '<<s[d].sh<<endl;
pair<int,int > temp[10];
temp[1].first=abs(sa-h[i]);
temp[2].first=abs(sb-h[i]);
temp[3].first=abs(sc-h[i]);
temp[4].first=abs(sd-h[i]);
temp[1].second=a;
temp[2].second=b;
temp[3].second=c;
temp[4].second=d;
sort(temp+1,temp+5);
if(temp[1].first==temp[2].first){
if(h[s[temp[1].second].sh]<h[s[temp[2].second].sh]){
next1[i]=s[temp[1].second].sh;
next2[i]=s[temp[2].second].sh;
}
else{
next2[i]=s[temp[1].second].sh;
next1[i]=s[temp[2].second].sh;
}
}
else{
next1[i]=s[temp[1].second].sh;
if(temp[2].first==temp[3].first){
if(h[s[temp[2].second].sh]<h[s[temp[3].second].sh]){
next2[i]=s[temp[2].second].sh;
}
else{
next2[i]=s[temp[3].second].sh;
}
}
else{
next2[i]=s[temp[2].second].sh;
}
}
}
for(int i=1;i<=n;i++){
if(next1[i]!=0)
to[i+n]=next1[i];
if(next2[i]!=0)
to[i]=next2[i]+n;
}
to[n]=0;
to[2*n]=0;
jago1();
jago2();
int res=-1,a=MX,b=1;
for(int i=1;i<=n;i++){
pair<int,int > state=sx(sd,i);
if(res==-1){
res=1;
a=state.first;
b=state.second;
}
else{
if(b==0){
res=i;
a=state.first;
b=state.second;
}
else if(state.second==0)continue;
//cout<<a*1.0/b<<' '<<state.first*1.0/state.second<<endl;
if(a*1.0/b>state.first*1.0/state.second){
res=i;
a=state.first;
b=state.second;
}
else if(state.second*a==state.first*b&&h[i]>h[res]){
res=i;
a=state.first;
b=state.second;
}
}
}
cout<<res<<endl;
cin>>m;
for(int i=1;i<=m;i++){
int st,dx;
cin>>st>>dx;
pair<int,int >state = sx(dx,st);
cout<<state.first<<' '<<state.second<<endl;
}
return 0;
}
点1数据:
输入:
10
4 5 6 1 2 3 7 8 9 10
7
10
1 7
2 7
3 7
4 7
5 7
6 7
7 7
8 7
9 7
10 7
输出:
2
3 2
2 4
2 1
2 4
5 1
5 1
2 1
2 0
0 0
0 0