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 <bits/stdc++.h>
using namespace std;
constexpr size_t N = 100000, LGN = 18;
size_t tree[1 << (LGN + 1)][2];
void build()
{
for (size_t i = (1 << LGN) - 1; i; --i)
{
tree[i][0] = min(tree[i << 1][0], tree[i << 1 | 1][0]);
tree[i][1] = max(tree[i << 1][1], tree[i << 1 | 1][1]);
}
}
pair<size_t, size_t> range_min_max(size_t i, size_t j)
{
i += 1 << LGN, j += 1 << LGN;
size_t x = SIZE_MAX, y = 0;
while (i <= j)
{
if (i & 1)
{
x = min(x, tree[i][0]);
y = max(y, tree[i++][1]);
}
if (!(j & 1))
{
x = min(x, tree[j][0]);
y = max(y, tree[j--][1]);
}
i >>= 1;
j >>= 1;
}
return {x, y};
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, m, q;
cin >> n >> m >> q;
for (size_t i = 0; i < n - 1; ++i)
{
size_t u, v;
cin >> u >> v;
}
for (size_t i = 0; i < m; ++i)
cin >> tree[(1 << LGN) + i][0], tree[(1 << LGN) + i][1] = tree[(1 << LGN) + i][0];
build();
while (q--)
{
size_t l, r;
cin >> l >> r, --l, --r;
auto [a, b] = range_min_max(l, r);
cout << b - a + 1 << '\n';
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |