Submission #63099329
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e9 + 1;
struct SegTree {
vector<int> t;
int n;
SegTree (vector<int> a) {
n = a.size();
t.resize(2 * n);
for (int i = 0; i < n; i++) t[i + n] = a[i];
for (int i = n - 1; i > 0; i--) t[i] = min(t[i << 1], t[i << 1 | 1]);
}
int query(int l, int r) {
int res = MAX;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = min(res, t[l++]);
if (r & 1) res = min(res, t[--r]);
}
return res;
}
};
int main() {
int n;
cin >> n;
vector<int> w(n);
for (int i = 0; i < n; i++) cin >> w[i];
vector<int> l(n), r(n);
for (int i = 0; i < n; i++) cin >> l[i] >> r[i];
vector<int> ir(n), il(n);
iota(ir.begin(), ir.end(), 0);
iota(il.begin(), il.end(), 0);
sort(ir.begin(), ir.end(), [&](int i, int j) { return r[i] < r[j]; });
sort(il.begin(), il.end(), [&](int i, int j) { return l[i] < l[j]; });
vector<int> wr(n), wl(n);
for (int i = 0; i < n; i++) wr[i] = w[ir[i]];
for (int i = 0; i < n; i++) wl[i] = w[il[i]];
SegTree tr(wr), tl(wl);
int q;
cin >> q;
while (q--) {
int s, t;
cin >> s >> t;
s--; t--;
if (r[s] - l[s] < r[t] - l[t]) swap(s, t);
bool pos = false;
long long res = 0;
res += w[t];
res += w[s];
if (r[s] < l[t] || r[t] < l[s]) {
pos = true;
} else {
int mn = MAX;
mn = min(mn, tr.query(0, lower_bound(ir.begin(), ir.end(), min(l[s], l[t]), [&](int i, int v) {
return r[i] < v;
}) - ir.begin()));
mn = min(mn, tl.query(lower_bound(il.begin(), il.end(), max(r[s], r[t]), [&](int i, int v) {
return l[i] <= v;
}) - il.begin(), n));
if (l[t] <= r[s] && r[s] <= r[t]) swap(s, t);
if (l[t] <= l[s] && l[s] <= r[t]) {
int mn1 = tr.query(0, lower_bound(ir.begin(), ir.end(), l[s], [&](int i, int v) {
return r[i] < v;
}) - ir.begin());
int mn2 = tl.query(lower_bound(il.begin(), il.end(), r[t], [&](int i, int v) {
return l[i] <= v;
}) - il.begin(), n);
if (mn1 != MAX && mn2 != MAX) {
mn = min(mn, mn1 + mn2);
}
}
if (mn != MAX) {
pos = true;
res += mn;
}
}
if (pos) cout << res << "\n";
else cout << -1 << "\n";
}
return 0;
}
Submission Info
| Submission Time |
|
| Task |
A - Complement Interval Graph |
| User |
Phi001 |
| Language |
C++ 20 (gcc 12.2) |
| Score |
700 |
| Code Size |
2773 Byte |
| Status |
AC |
| Exec Time |
532 ms |
| Memory |
12492 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 |
422 ms |
12404 KiB |
| 001.txt |
AC |
385 ms |
12368 KiB |
| 002.txt |
AC |
386 ms |
12464 KiB |
| 003.txt |
AC |
429 ms |
12488 KiB |
| 004.txt |
AC |
473 ms |
12420 KiB |
| 005.txt |
AC |
530 ms |
12340 KiB |
| 006.txt |
AC |
426 ms |
12404 KiB |
| 007.txt |
AC |
428 ms |
12460 KiB |
| 008.txt |
AC |
380 ms |
12408 KiB |
| 009.txt |
AC |
346 ms |
6736 KiB |
| 010.txt |
AC |
273 ms |
8996 KiB |
| 011.txt |
AC |
262 ms |
10208 KiB |
| 012.txt |
AC |
204 ms |
4072 KiB |
| 013.txt |
AC |
223 ms |
8936 KiB |
| 014.txt |
AC |
526 ms |
12324 KiB |
| 015.txt |
AC |
530 ms |
12416 KiB |
| 016.txt |
AC |
532 ms |
12492 KiB |
| 017.txt |
AC |
529 ms |
12456 KiB |
| 018.txt |
AC |
530 ms |
12412 KiB |
| 019.txt |
AC |
529 ms |
12420 KiB |
| 020.txt |
AC |
530 ms |
12440 KiB |
| 021.txt |
AC |
529 ms |
12468 KiB |
| 022.txt |
AC |
528 ms |
12356 KiB |
| 023.txt |
AC |
527 ms |
12448 KiB |
| example0.txt |
AC |
1 ms |
3372 KiB |
| example1.txt |
AC |
1 ms |
3372 KiB |