// Programmer: Shadow1
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using str = string; // yay python!
#define i64 int64_t
#define show(x) cerr << (#x) << " = " << (x) << '\n';
#define output_vector(v) for(auto &x : v){cout << x << ' ';}cout << '\n';
#define output_pairvector(v) for(auto &x : v){cout << x.first << " " << x.second << '\n';}
#define read_vector(v) for(auto &x : v){cin >> x;}
#define vt vector
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define fir first
#define sec second
#define sz(x) ll(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int ll
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
const int INF = 1e15;
void solve() {
int n, m, q;
cin >> n >> m >> q;
vector<int> a(m+1);
for(int i=1; i<=m; ++i) {
int l;
cin >> l >> a[i];
}
vector<pii> qry(q);
for(int i=0; i<q; ++i) {
cin >> qry[i].fir;
qry[i].sec = i;
}
sort(all(qry));
vector<int> ans(q);
stack<int> st;
int base = 0;
for(int i=1; i<=m; ++i) {
int s = 0, r = 0, sum = 0, mx = 0;
while(!st.empty()) {
if(a[i] <= st.top())
r = max(0ll, a[i] - mx);
else
r = max(0ll, st.top() - mx);
mx += r;
s = st.top() + sum - 1;
sum += st.top();
base += r;
pii k = make_pair(s, INF);
auto it = upper_bound(all(qry), k);
if(it != qry.end()) {
int x = it - qry.begin();
// if(x == 8) {
// show(i);
// show(r);
// }
ans[qry[x].sec] -= r;
}
if(st.top() >= a[i]) break;
st.pop();
}
while(!st.empty() && a[i] == st.top())
st.pop();
st.push(a[i]);
}
ans[qry[0].sec] += base;
for(int i=1; i<q; ++i) {
ans[qry[i].sec] += ans[qry[i-1].sec];
}
output_vector(ans);
}
// CHECK YOUR OVERFLOWS!!!!
signed main() {
// freopen("output.txt", "w", stdout);
// freopen("input.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
// cin >> T;
for(int tc = 1; tc <= T; ++tc) {
// cout << "Case #" << tc << ": ";
solve();
}
return 0;
}
/* CHECK :
1. COMPARATOR FUNCTION MUST RETURN FALSE WHEN ARGUMENTS ARE EQUAL!!!
2. Overflow! Typecase int64_t on operations if varaibles are int
3. Check array bounds!!!
4. Check array indexing!!!
5. Edge cases. (N==1)!!!
*/
# | 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... |