QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#97772 | #365. Railway Trip | eyiigjkn | 5 | 129ms | 97000kb | C++14 | 2.3kb | 2023-04-18 12:26:51 | 2023-04-18 12:26:54 |
Judging History
answer
# include <bits/stdc++.h>
using namespace std;
constexpr int INF=1e9;
int a[100010],nd[100010],id[200010],dep[200010],maxn[100010][20],fa[200010][20],tot=0;
vector<int> G[200010];
inline void chkmin(int &x,const int &y){x=min(x,y);}
struct Matrix
{
int a[2][2];
Matrix(){a[0][0]=a[0][1]=a[1][0]=a[1][1]=INF;}
Matrix(const initializer_list<int> &L)
{
auto it=L.begin();
for(int i:{0,1})
for(int j:{0,1})
a[i][j]=*it++;
}
Matrix operator*(const Matrix &t)const
{
Matrix ans;
for(int i:{0,1})
for(int j:{0,1})
for(int k:{0,1})
chkmin(ans.a[i][k],a[i][j]+t.a[j][k]);
return ans;
}
}I{0,INF,INF,0},f[200010][20];
inline int getmax(int i,int j){return a[i]!=a[j]?(a[i]>a[j]?i:j):min(i,j);}
int query(int l,int r)
{
if(l>r) return 0;
int k=__lg(r-l+1);
return getmax(maxn[l][k],maxn[r-(1<<k)+1][k]);
}
int build(int l,int r)
{
if(r-l<=1) return nd[l]=++tot;
int u=++tot,mx=query(l+1,r-1),le=l;
for(int i=mx;a[i]==a[mx];le=i,i=query(i+1,r-1)) G[u].push_back(build(le,i));
G[u].push_back(build(le,r));
return u;
}
void dfs(int u)
{
int sz=G[u].size();
for(int i=1;i<=16 && fa[u][i-1];i++)
{
fa[u][i]=fa[fa[u][i-1]][i-1];
f[u][i]=f[fa[u][i-1]][i-1]*f[u][i-1];
}
for(int i=0;i<sz;i++)
{
int v=G[u][i];
id[v]=i;
f[v][0]=Matrix{i,sz-i,i+1,sz-i-1};
dep[v]=dep[u]+1;
fa[v][0]=u;dfs(v);
}
}
int main()
{
int n,m,x,y;
scanf("%d%*d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) maxn[i][0]=i;
for(int i=n;i;i--)
for(int j=1;i+(1<<j)-1<=n;j++)
maxn[i][j]=getmax(maxn[i][j-1],maxn[i+(1<<j-1)][j-1]);
build(0,n+1);dfs(1);
while(m--)
{
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
if(y-x<=1) puts("0");
else
{
x=nd[x];y=nd[y-1];
Matrix L=I,R=I;
if(dep[x]>dep[y])
for(;dep[x]>dep[y];x=fa[x][__lg(dep[x]-dep[y])])
L=f[x][__lg(dep[x]-dep[y])]*L;
else
for(;dep[y]>dep[x];y=fa[y][__lg(dep[y]-dep[x])])
R=f[y][__lg(dep[y]-dep[x])]*R;
for(int i=16;i>=0;i--)
if(fa[x][i]!=fa[y][i])
{
L=f[x][i]*L;R=f[y][i]*R;
x=fa[x][i];y=fa[y][i];
}
int sz=G[fa[x][0]].size();
printf("%d\n",min(min(L.a[1][0],L.a[1][1]+1)+min(R.a[0][0]+1,R.a[0][1])+id[y]-id[x]-1,min(L.a[0][0],L.a[0][1]+1)+min(R.a[1][0]+1,R.a[1][1])+sz+id[x]-id[y])-1);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 12ms
memory: 71188kb
input:
100 100 50 100 86 39 28 49 22 79 14 83 100 15 26 37 51 53 18 74 15 96 72 47 80 10 46 62 88 20 36 46 29 40 28 37 88 91 41 24 63 14 92 24 31 99 61 62 96 94 51 51 21 72 97 59 96 97 94 66 88 32 96 58 26 53 1 100 31 85 30 42 69 40 62 54 94 49 62 13 20 82 74 20 44 54 69 65 34 78 64 48 69 19 35 8 92 100 87...
output:
3 1 3 5 3 3 3 0 2 5 2 6 1 3 1 3 5 5 3 5 4 7 3 6 3 4 4 4 7 0 2 3 2 4 5 5 6 5 5 6 4 3 2 4 5 2 5 4 2 2
result:
ok 50 lines
Test #2:
score: 0
Accepted
time: 15ms
memory: 71912kb
input:
100 100 50 100 85 82 7 50 49 51 45 30 3 29 99 71 93 5 68 70 52 12 44 1 35 99 80 76 34 23 28 62 91 80 77 59 57 30 15 23 13 16 21 58 23 38 49 44 73 7 47 24 53 97 83 14 71 16 75 61 24 17 96 51 41 74 53 25 2 42 36 73 57 53 45 10 12 11 79 68 2 78 44 47 67 21 99 25 68 60 71 23 92 9 2 97 37 43 64 32 28 7 1...
output:
2 0 5 4 2 4 2 4 3 5 4 3 3 6 4 4 3 3 3 4 2 3 3 3 4 5 5 2 3 3 5 4 3 4 2 2 3 5 3 6 2 5 4 2 2 4 4 3 4 7
result:
ok 50 lines
Test #3:
score: 0
Accepted
time: 8ms
memory: 71128kb
input:
100 100 50 100 56 83 81 2 73 24 77 19 11 79 100 36 32 62 4 41 50 51 62 68 6 11 36 28 21 61 82 72 86 35 93 94 87 50 14 77 83 14 49 95 32 5 20 59 55 77 31 52 70 23 81 4 10 34 100 4 67 60 1 23 26 65 1 30 96 43 49 70 81 18 82 97 80 62 28 93 38 91 39 67 6 17 78 60 60 55 97 45 58 44 80 24 91 14 5 35 93 25...
output:
4 4 4 5 3 3 3 4 2 4 2 5 4 3 4 3 5 4 5 3 3 2 4 1 3 4 3 3 5 2 5 5 5 0 3 3 3 2 4 1 5 2 4 3 0 5 4 5 4 3
result:
ok 50 lines
Test #4:
score: 0
Accepted
time: 8ms
memory: 72292kb
input:
100 100 50 100 50 72 67 84 3 28 84 40 70 52 28 37 16 66 92 47 27 30 49 33 7 69 22 33 85 1 98 4 97 89 27 99 21 33 76 89 26 74 10 80 23 70 10 63 1 78 38 28 30 95 11 17 99 10 52 5 30 38 95 4 71 50 2 40 28 17 21 10 13 23 98 92 84 8 3 37 38 71 78 57 87 22 79 59 26 13 50 33 87 9 6 78 85 19 68 79 9 62 100 ...
output:
5 4 1 1 3 5 3 7 1 5 1 4 4 3 7 3 1 1 5 3 4 5 3 2 3 4 0 5 1 6 3 6 4 5 3 3 5 4 6 2 4 4 3 4 4 5 4 6 3 5
result:
ok 50 lines
Test #5:
score: 0
Accepted
time: 8ms
memory: 71340kb
input:
100 100 50 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 100 91 65 31 33 13 98 45 91 54 50 94 66 78 5 28 13 100 4 15 63 55 2 72 49 97 18 57 59 40 ...
output:
25 1 14 45 3 27 26 14 3 47 46 22 20 1 6 1 22 27 15 39 25 36 35 24 14 12 35 32 48 5 9 2 26 4 27 17 36 35 10 15 38 7 16 2 48 3 31 32 48 6
result:
ok 50 lines
Test #6:
score: 0
Accepted
time: 20ms
memory: 71516kb
input:
100 100 50 100 99 99 99 97 97 97 95 95 95 93 93 93 91 91 91 89 89 89 87 87 87 85 85 85 83 83 83 81 81 81 79 79 79 77 77 77 75 75 75 73 73 73 71 71 71 69 69 69 67 67 67 68 68 70 70 70 72 72 72 74 74 74 76 76 76 78 78 78 80 80 80 82 82 82 84 84 84 86 86 86 88 88 88 90 90 90 92 92 92 94 94 94 96 96 96 ...
output:
9 6 23 6 1 21 5 18 5 15 2 3 16 3 15 26 2 3 12 1 1 0 20 27 26 3 3 3 30 22 30 1 1 0 26 11 13 13 16 21 2 12 7 22 7 13 2 9 8 15
result:
ok 50 lines
Test #7:
score: 0
Accepted
time: 12ms
memory: 71620kb
input:
100 100 50 100 99 99 99 97 97 97 95 95 95 93 93 93 91 91 91 89 89 89 87 87 87 85 85 85 83 83 83 81 81 81 79 79 79 77 77 77 75 75 75 73 73 73 71 71 71 69 69 69 67 67 67 68 68 70 70 70 72 72 72 74 74 74 76 76 76 78 78 78 80 80 80 82 82 82 84 84 84 86 86 86 88 88 88 90 90 90 92 92 92 94 94 94 96 96 96 ...
output:
9 20 22 8 30 3 22 11 2 8 10 5 1 26 10 20 19 21 1 6 17 2 6 9 13 9 20 25 24 1 25 19 4 14 15 23 10 19 19 3 2 3 9 24 9 10 17 10 7 14
result:
ok 50 lines
Test #8:
score: 0
Accepted
time: 3ms
memory: 71300kb
input:
100 30 50 30 29 29 29 27 27 27 25 25 25 23 23 23 21 21 21 19 19 19 17 17 17 15 15 15 13 13 13 11 11 11 9 9 9 7 7 7 5 5 5 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 4 4 4 6 6 6 8 8 8 10 10 10 12 12 12 14 14 14 16 16 16 18 18 18 20 20 20 22 22 22 24 24 24 26 26 26 28 28 28 30 29 60 12 67 26 10 71 29 70 5...
output:
9 15 10 3 14 21 20 17 4 8 18 0 2 6 16 3 11 12 7 14 2 8 17 3 13 7 3 3 7 21 6 6 10 2 9 11 5 0 18 9 20 14 2 2 7 26 3 3 9 13
result:
ok 50 lines
Test #9:
score: 0
Accepted
time: 14ms
memory: 72192kb
input:
10 10 50 10 7 7 4 1 9 3 8 1 10 8 9 2 7 7 6 4 2 7 4 5 8 6 7 10 2 1 9 7 2 9 1 2 7 5 8 6 9 1 3 5 2 9 5 5 3 5 8 4 8 7 5 10 7 3 7 6 1 9 1 2 8 3 1 9 7 4 1 6 9 10 7 3 9 5 4 3 1 8 2 2 3 5 10 4 6 10 5 6 4 5 8 10 7 2 6 5 8 1 6 6 4 2 6 6 8 10 8 7 9
output:
0 2 0 1 1 1 0 1 1 2 1 2 1 1 1 2 2 1 1 1 1 1 1 0 1 2 1 1 1 1 1 2 0 1 2 0 1 0 1 0 1 1 1 1 0 0 1 0 0 1
result:
ok 50 lines
Subtask #2:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Test #10:
score: 15
Accepted
time: 2ms
memory: 72636kb
input:
1000 1000 50 1000 922 228 50 969 778 800 874 487 278 681 989 234 951 889 87 926 534 311 876 955 989 810 841 423 580 204 360 127 808 441 249 754 777 831 192 797 272 163 832 471 669 837 129 774 425 435 315 515 626 725 883 415 932 9 891 34 146 288 321 980 972 776 449 458 188 75 412 81 523 705 137 564 7...
output:
10 5 8 4 6 3 5 3 5 5 3 5 7 6 6 6 10 5 2 5 9 4 10 6 7 7 10 4 5 5 2 7 7 7 6 6 5 7 5 6 10 6 9 2 3 9 6 6 5 7
result:
ok 50 lines
Test #11:
score: -15
Wrong Answer
time: 32ms
memory: 96232kb
input:
100000 10 50 10 6 4 3 1 7 3 5 3 4 10 3 8 2 10 4 6 7 3 10 10 10 10 1 5 2 1 4 8 8 2 2 8 8 5 9 6 6 7 9 1 3 10 2 8 3 3 3 9 10 2 7 3 10 2 3 5 9 6 9 10 4 7 4 6 9 4 8 7 2 5 5 9 6 5 9 4 7 6 9 1 5 8 5 2 10 2 6 3 3 2 9 9 3 1 10 10 1 5 9 5 6 9 5 9 3 9 9 6 8 8 9 4 8 3 7 4 10 6 5 4 7 4 5 4 1 2 7 3 6 9 7 4 7 9 4 ...
output:
1359 1428 3720 1112 1488 1359 2215 713 4226 3135 1169 3371 2015 4999 274 3670 3926 666 1474 2294 4789 3933 3816 3997 4619 3537 626 303 305 4196 4481 2949 2371 2426 4223 3585 4260 2204 4708 3962 4045 5028 576 2432 21 3687 1164 2279 1975 2912
result:
wrong answer 3rd lines differ - expected: '6356', found: '3720'
Subtask #3:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Test #29:
score: 0
Wrong Answer
time: 129ms
memory: 97000kb
input:
100000 14 100000 14 13 9 6 8 12 6 8 14 12 6 5 6 4 10 8 11 1 14 12 4 3 3 13 10 4 3 1 1 1 2 10 1 3 6 3 8 13 14 4 9 13 13 12 13 8 4 7 5 1 14 3 4 6 2 4 7 9 4 10 7 8 7 13 6 2 7 14 2 13 10 1 14 5 1 14 9 14 11 4 7 12 5 13 13 7 3 9 6 4 3 9 7 7 5 10 9 6 1 14 11 14 10 8 7 5 14 12 12 4 2 13 11 1 6 6 12 1 2 8 4...
output:
1523 3536 1392 500 2340 3295 2904 1305 798 2809 2164 2555 1588 1133 1415 3375 1397 148 2542 2485 1360 2752 201 2264 2746 342 1017 2912 2519 41 218 2929 3277 298 3506 792 2629 3260 1577 485 2109 906 1284 782 1568 1128 2103 2405 672 148 3125 145 768 1091 1110 963 876 2521 2573 802 79 2320 851 2633 248...
result:
wrong answer 6th lines differ - expected: '3799', found: '3295'
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%