QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#416110 | #6334. Passport | snpmrnhlol | 0 | 522ms | 83244kb | C++14 | 3.4kb | 2024-05-21 16:01:30 | 2024-05-21 16:01:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5;
const int inf = 2e9;
vector <pair<int,int>> e[4*N];
deque <int> q;
priority_queue <pair<int,int>> pq;
int dist[4*N][3];
bool vis[4*N];
int tr[N];
int n;
void buildtree(int l,int r,int node){
if(l == r){
tr[l] = node;
}else{
int mij = (l + r)/2;
buildtree(l,mij,node*2 + 1);
buildtree(mij + 1,r,node*2 + 2);
}
if(node != 0){
e[node].push_back({(node - 1)/2,0});
}
}
void updtree(int ql,int qr,int x,int l = 0,int r = n - 1,int node = 0){
if(r < ql || qr < l)return;
if(ql <= l && r <= qr){
e[node].push_back({tr[x],1});
//cout<<"edge:"<<node<<' '<<tr[x]<<'\n';
return;
}else{
int mij = (l + r)/2;
updtree(ql,qr,x,l,mij,node*2 + 1);
updtree(ql,qr,x,mij + 1,r,node*2 + 2);
}
}
int main(){
cin>>n;
buildtree(0,n - 1,0);
for(int i = 0;i < n;i++){
int l1,r1;
cin>>l1>>r1;
l1--;r1--;
updtree(l1,r1,i);
}
for(int i = 0;i < 4*n;i++){
dist[i][0] = -1;
dist[i][1] = -1;
dist[i][2] = -1;
}
dist[tr[0]][0] = 0;
q.push_back(tr[0]);
while(!q.empty()){
int x = q.front();
q.pop_front();
if(vis[x])continue;
vis[x] = 1;
for(auto i:e[x]){
if(dist[i.first][0] == -1 || dist[i.first][0] > dist[x][0] + i.second){
dist[i.first][0] = dist[x][0] + i.second;
if(i.second)q.push_back(i.first);
else q.push_front(i.first);
}
}
}
for(int i = 0;i < 4*n;i++){
vis[i] = 0;
}
dist[tr[n - 1]][1] = 0;
q.push_back(tr[n - 1]);
while(!q.empty()){
int x = q.front();
q.pop_front();
if(vis[x])continue;
vis[x] = 1;
for(auto i:e[x]){
if(dist[i.first][1] == -1 || dist[i.first][1] >= dist[x][1] + i.second){
dist[i.first][1] = dist[x][1] + i.second;
if(i.second)q.push_back(i.first);
else q.push_front(i.first);
}
}
}
for(int i = 0;i < 4*n;i++){
dist[i][2] = inf;
}
for(int i = 0;i < n;i++){
if(dist[tr[i]][0] == -1 || dist[tr[i]][1] == -1)continue;
if(i == 0 || i == n - 1){
pq.push({-dist[tr[i]][0] - dist[tr[i]][1],tr[i]});
dist[tr[i]][2] = dist[tr[i]][0] + dist[tr[i]][1];
}else{
pq.push({-dist[tr[i]][0] - dist[tr[i]][1] + 1,tr[i]});
dist[tr[i]][2] = dist[tr[i]][0] + dist[tr[i]][1] - 1;
}
}
while(!pq.empty()){
int x = -pq.top().first;
int id = pq.top().second;
pq.pop();
//cout<<x<<' '<<id<<'\n';
if(dist[id][2] != x)continue;
//cout<<x<<' '<<id<<'\n';
for(auto i:e[id]){
if(dist[i.first][2] > dist[id][2] + i.second){
dist[i.first][2] = dist[id][2] + i.second;
pq.push({-dist[i.first][2],i.first});
}
}
}
int q;
cin>>q;
for(int i = 0;i < q;i++){
int x;
cin>>x;
x--;
if(dist[tr[x]][2] == -1){
cout<<-1<<'\n';
}else{
cout<<dist[tr[x]][2]<<'\n';
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 23284kb
input:
2 1 1 1 2 1 1
output:
2000000000
result:
wrong answer 1st lines differ - expected: '-1', found: '2000000000'
Subtask #2:
score: 0
Wrong Answer
Test #8:
score: 16
Accepted
time: 3ms
memory: 24872kb
input:
2 1 1 1 2 1 2
output:
1
result:
ok single line: '1'
Test #9:
score: 0
Wrong Answer
time: 0ms
memory: 24916kb
input:
2 1 2 2 2 1 2
output:
2000000000
result:
wrong answer 1st lines differ - expected: '-1', found: '2000000000'
Subtask #3:
score: 0
Wrong Answer
Test #19:
score: 24
Accepted
time: 0ms
memory: 26568kb
input:
2337 1 3 2 77 1 1397 2 222 3 62 6 1896 7 10 6 950 9 9 10 16 11 455 9 588 13 16 7 1245 9 1342 8 1727 7 122 11 653 9 1094 2 57 11 81 19 1290 6 1584 16 79 14 215 21 61 27 27 16 1458 16 198 26 180 31 31 11 240 33 36 11 121 34 1542 9 1752 14 456 36 43 36 2244 40 40 4 517 42 662 31 1350 33 162 30 843 28 1...
output:
4
result:
ok single line: '4'
Test #20:
score: 24
Accepted
time: 4ms
memory: 26104kb
input:
2486 1 12 2 8 1 7 3 12 2 11 3 15 1 8 4 11 9 15 3 21 11 13 4 15 9 21 9 19 5 15 8 20 8 25 16 24 11 29 11 23 18 23 14 32 17 24 13 27 15 30 21 34 16 29 20 35 19 32 29 34 20 39 21 43 29 40 28 43 26 44 31 45 28 43 35 38 30 40 37 46 40 43 42 42 42 45 43 54 39 51 40 51 45 54 46 57 39 53 47 53 47 54 41 59 49...
output:
314
result:
ok single line: '314'
Test #21:
score: 24
Accepted
time: 6ms
memory: 26092kb
input:
2500 1 2500 1 2500 1 2500 2 2500 2 2499 3 2498 5 2496 6 2495 7 2495 8 2493 8 2492 6 2492 11 2491 12 2489 12 2490 12 2491 15 2486 15 2485 17 2484 18 2483 15 2482 20 2483 21 2481 19 2479 23 2481 23 2477 21 2477 25 2476 27 2474 28 2473 29 2472 30 2475 31 2470 30 2469 33 2468 32 2467 33 2466 33 2466 33 ...
output:
60
result:
ok single line: '60'
Test #22:
score: 24
Accepted
time: 4ms
memory: 25356kb
input:
2433 1 2433 1 2432 1 2431 2 2432 3 2429 1 2428 1 2428 6 2426 3 2425 1 2424 8 2423 1 2422 11 2421 12 2420 1 2420 4 2418 15 2417 16 2416 12 2415 16 2415 15 2414 13 2412 19 2412 21 2415 19 2410 23 2408 25 2407 26 2408 27 2409 28 2404 28 2403 11 2402 28 2409 31 2400 33 2418 34 2399 35 2397 36 2396 37 23...
output:
20
result:
ok single line: '20'
Test #23:
score: 24
Accepted
time: 4ms
memory: 26272kb
input:
2500 1 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 12 14 13 15 14 16 15 17 16 18 17 19 18 20 19 21 20 22 21 23 22 24 23 25 24 26 25 27 26 28 27 29 28 30 29 31 30 32 31 33 32 34 33 35 34 36 35 37 36 38 37 39 38 40 39 41 40 42 41 43 42 44 43 45 44 46 45 47 46 48 47 49 48 50 49 51 50 52 51 53 5...
output:
2498
result:
ok single line: '2498'
Test #24:
score: 24
Accepted
time: 3ms
memory: 26128kb
input:
2500 1 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52...
output:
2498
result:
ok single line: '2498'
Test #25:
score: 0
Wrong Answer
time: 0ms
memory: 25944kb
input:
2500 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1 1250 1...
output:
2000000000
result:
wrong answer 1st lines differ - expected: '-1', found: '2000000000'
Subtask #4:
score: 0
Wrong Answer
Test #28:
score: 0
Wrong Answer
time: 4ms
memory: 25304kb
input:
2419 1 883 1 29 3 41 4 2201 1 808 6 45 7 1456 6 134 6 1372 1 1776 4 441 7 208 5 28 4 604 7 56 9 617 8 2115 15 60 13 456 10 2071 18 23 18 39 5 39 21 35 4 75 25 44 24 640 21 30 4 860 30 31 18 78 32 779 1 927 33 34 19 59 34 181 21 502 23 155 39 39 2 254 30 641 42 50 10 2000 41 2220 18 632 35 48 27 905 ...
output:
3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 4 4 3 4 4 3 4 3 4 4 4 3 5 4 4 3 4 4 4 4 4 2000000000 3 4 4 3 3 4 4 4 3 3 4 4 3 2000000000 3 4 4 4 3 2000000000 4 4 4 3 4 4 4 4 4 4 3 3 4 5 4 3 3 4 4 3 4 4 3 4 3 4 4 2000000000 2000000000 3 4 4 3 4 4 3 4 3 4 5 4 4 4 4 3 4 4 4 3 4 5 4 4 4 4 4 3 4 4 4 4 4 4 4 3 4 4 4 2...
result:
wrong answer 39th lines differ - expected: '-1', found: '2000000000'
Subtask #5:
score: 0
Wrong Answer
Test #33:
score: 0
Wrong Answer
time: 522ms
memory: 83244kb
input:
196830 1 67357 2 183304 3 23390 4 54 1 145887 3 27807 3 12376 1 1013 3 113274 3 191874 6 23342 9 2113 13 94245 3 141449 10 1727 3 51 17 99028 6 193803 8 7452 6 121537 9 23658 18 611 6 4756 4 5141 8 8547 8 66922 13 7021 9 72 3 53043 16 167381 2 15530 5 192 33 33 9 92655 10 36182 20 19992 36 24454 1 6...
output:
3 3 3 4 3 3 3 3 3 3 3 3 3 3 3 4 3 3 3 3 3 3 3 3 3 3 3 4 3 3 3 4 2000000000 3 3 3 3 3 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 3 3 4 3 4 3 3 3 3 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 3 3 3 3 3 4 3 3 3 3 3 3 3 3 4 3 3 2000000000 3 3 3 3 3 3 3 3 3 4 3 3 3 3 3 3 4 2000000000 3 4 3 4 3 3 3 3 3 3 3 3 3 3 3 3 3...
result:
wrong answer 33rd lines differ - expected: '-1', found: '2000000000'