This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<stdio.h>
int tree[500001][4]={0}; //1: left, 2: right. 3: gap 0: rightgae
int sn[1000001]={0};
int sv[500000][2]={0};
int n;
long long int p;
int tot(int here,int gap,int wichi){
int cnt=0,pp=-1,i,prev=-1,prevn=-1;
if(wichi>1){
do{
// printf("%d",here);
prevn=here;
if(tree[here][3]>gap){
cnt+=tree[here][0];
here=tree[here][1];
prev=1;
}else{
tree[here][0]++;
here=tree[here][2];
prev=2;
}
}while(here>0);
}
tree[wichi][3]=gap;
tree[wichi][0]=1;
if(prev>-1) tree[prevn][prev]=wichi;
return cnt;
}
int main(){
int im,i;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&im);
sn[im]=i;
}
for(i=0;i<n;i++){
scanf("%d",&im);
// printf("!");
p+=tot(1,sn[im],i+1);
}
printf("%lld",p);
// fclose(stdin);
// fclose(stdout);
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |