Submission #65934811
Source Code Expand
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define all(x) x.begin(), x.end()
#define ca(x) for(auto i:x) cout<<i<<" ";
#define nl cout<<"\n";
#define f first
#define s second
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
int l[n], r[n]; ll ar[n];
for(int i=0; i<n; i++) cin>>ar[i];
for(int i=0; i<n; i++) cin>>l[i]>>r[i], l[i]--, r[i]--;
int q; cin>>q;
auto does_intersect = [&](int i, int j) -> bool {
if(l[i] > r[j] || l[j] > r[i]) return 0;
return 1;
};
vector<ll> best_end(2*n,1e16);
vector<ll> best_start(2*n,1e16);
for(int i=0; i<n; i++) {
best_end[r[i]] = min(best_end[r[i]], ar[i]);
best_start[l[i]] = min(best_start[l[i]], ar[i]);
}
for(int i=1; i<2*n; i++) {
best_end[i] = min(best_end[i], best_end[i-1]);
best_start[2*n-i-1] = min(best_start[2*n-i-1], best_start[2*n-i]);
}
auto get_best_start = [&](int i) -> ll {
if(i+1 >= 2*n) return 1e16;
return best_start[i+1];
};
auto get_best_end = [&](int i) -> ll {
if(i-1 < 0) return 1e16;
return best_end[i-1];
};
while(q--){
int a, b;
cin>>a>>b; a--; b--;
if(!does_intersect(a,b)) {
cout<<ar[a]+ar[b]<<"\n";
} else {
ll res = 1e16;
res = min(res, ar[a]+ar[b]+get_best_start(r[a])+get_best_end(l[b]));
res = min(res, ar[a]+ar[b]+get_best_start(r[b])+get_best_end(l[a]));
res = min(res, ar[a]+ar[b]+get_best_start(max(r[b],r[a])));
res = min(res, ar[a]+ar[b]+get_best_end(min(l[b],l[a])));
if(res == 1e16) cout<<-1<<"\n";
else cout<<res<<"\n";
}
}
}
Submission Info
| Submission Time | |
|---|---|
| Task | A - Complement Interval Graph |
| User | keepers |
| Language | C++ 20 (gcc 12.2) |
| Score | 700 |
| Code Size | 1856 Byte |
| Status | AC |
| Exec Time | 74 ms |
| Memory | 12628 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 700 / 700 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt, example1.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, example0.txt, example1.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 68 ms | 12536 KiB |
| 001.txt | AC | 59 ms | 12568 KiB |
| 002.txt | AC | 62 ms | 12488 KiB |
| 003.txt | AC | 71 ms | 12492 KiB |
| 004.txt | AC | 74 ms | 12544 KiB |
| 005.txt | AC | 71 ms | 12568 KiB |
| 006.txt | AC | 71 ms | 12476 KiB |
| 007.txt | AC | 71 ms | 12476 KiB |
| 008.txt | AC | 62 ms | 12504 KiB |
| 009.txt | AC | 43 ms | 6740 KiB |
| 010.txt | AC | 39 ms | 8868 KiB |
| 011.txt | AC | 41 ms | 10028 KiB |
| 012.txt | AC | 21 ms | 3780 KiB |
| 013.txt | AC | 34 ms | 9044 KiB |
| 014.txt | AC | 74 ms | 12580 KiB |
| 015.txt | AC | 74 ms | 12544 KiB |
| 016.txt | AC | 74 ms | 12520 KiB |
| 017.txt | AC | 74 ms | 12544 KiB |
| 018.txt | AC | 73 ms | 12476 KiB |
| 019.txt | AC | 74 ms | 12540 KiB |
| 020.txt | AC | 74 ms | 12500 KiB |
| 021.txt | AC | 73 ms | 12628 KiB |
| 022.txt | AC | 73 ms | 12488 KiB |
| 023.txt | AC | 74 ms | 12488 KiB |
| example0.txt | AC | 1 ms | 3596 KiB |
| example1.txt | AC | 1 ms | 3384 KiB |