voidmain(){ for (int i = 1; i <= q; i++) { scanf("%d%d%d", &qq[i].x, &qq[i].l, &qq[i].r); if (qq[i].x > a[1]) continue; qq[i].l += qq[i].x, qq[i].r += qq[i].x; qry[qq[i].r].emplace_back(qq[i].x + 1, i); qry[qq[i].l - 1].emplace_back(qq[i].x + 1, -i); } vector<int> stk; for (int i = 1; i <= n; i++) { while (!stk.empty() && p[stk.back()] >= p[i]) { int r = stk.back(); stk.pop_back(); int l = (!stk.empty() ? stk.back() : 0) + 1; // t in [i, n], k in [i - ll + 1, t - ll + 1] (tips: ll in [l, r]) vec[i].emplace_back(i - r + 1, i - l + 1, p[i] - p[r]); } // t in [i, n], k in [1, t - i + 1] vec[i].emplace_back(1, 1, p[i]); stk.push_back(i); } solve(0); for (int i = 1; i <= n; i++) vector<tuple<int, int, int>>().swap(vec[i]); vector<int>().swap(stk); for (int i = 1; i <= n; i++) { while (!stk.empty() && p[stk.back()] >= p[i]) { int r = stk.back(); stk.pop_back(); int l = (!stk.empty() ? stk.back() : 0) + 1; // t in [i, n], k in [i - ll + 1, t - ll + 1] (tips: ll in [l, r]) vec[i].emplace_back(n - r + 2, n - l + 2, p[r] - p[i]); } // t in [i, n], k in [1, t - i + 1] vec[i].emplace_back(n - i + 2, n - i + 2, -p[i]); stk.push_back(i); } solve(1); updatemin(a[1]); for (int i = 1; i <= n; i++) vector<pair<int, int>>().swap(qry[i]); for (int i = 1; i <= q; i++) { if (qq[i].x <= a[1]) continue; qq[i].x -= a[1]; qq[i].l += qq[i].x, qq[i].r += qq[i].x; qry[qq[i].r].emplace_back(qq[i].x + 1, i); qry[qq[i].l - 1].emplace_back(qq[i].x + 1, -i); } for (int i = 1; i <= n; i++) vector<tuple<int, int, int>>().swap(vec[i]); vector<int>().swap(stk); for (int i = 1; i <= n; i++) { while (!stk.empty() && p[stk.back()] <= p[i]) { int r = stk.back(); stk.pop_back(); int l = (!stk.empty() ? stk.back() : 0) + 1; // t in [i, n], k in [i - ll + 1, t - ll + 1] (tips: ll in [l, r]) vec[i].emplace_back(i - r + 1, i - l + 1, p[i] - p[r]); } // t in [i, n], k in [1, t - i + 1] vec[i].emplace_back(1, 1, p[i]); stk.push_back(i); } solve(0); for (int i = 1; i <= n; i++) vector<tuple<int, int, int>>().swap(vec[i]); vector<int>().swap(stk); for (int i = 1; i <= n; i++) { while (!stk.empty() && p[stk.back()] <= p[i]) { int r = stk.back(); stk.pop_back(); int l = (!stk.empty() ? stk.back() : 0) + 1; // t in [i, n], k in [i - ll + 1, t - ll + 1] (tips: ll in [l, r]) vec[i].emplace_back(n - r + 2, n - l + 2, p[r] - p[i]); } // t in [i, n], k in [1, t - i + 1] vec[i].emplace_back(n - i + 2, n - i + 2, -p[i]); stk.push_back(i); } solve(1); for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]); return ; } }
int n, m, q, p[150005], a[150005]; int tmp[150005];
voidupdatemin(int len){ deque<int> q; for (int i = n; i >= 1; i--) { while (!q.empty() && p[q.back()] > p[i]) q.pop_back(); q.push_back(i); if (q.front() > i + len) q.pop_front(); tmp[i] = p[q.front()]; } n -= len; for (int i = 1; i <= n; i++) p[i] = tmp[i]; return ; } voidupdatemax(int len){ deque<int> q; for (int i = n; i >= 1; i--) { while (!q.empty() && p[q.back()] < p[i]) q.pop_back(); q.push_back(i); if (q.front() > i + len) q.pop_front(); tmp[i] = p[q.front()]; } n -= len; for (int i = 1; i <= n; i++) p[i] = tmp[i]; return ; }
namespace Subtask7 { constint B = 387;
structdecompose { longlong w[150005], sum[B + 5];
voidclear(){memset(w, 0, sizeof(w)); memset(sum, 0, sizeof(sum)); return ;} voidupdate(int pos, longlong val){w[pos] += val, sum[(pos - 1) / B + 1] += val; return ;} longlongquery(int pos){ longlong res = 0; int limit = (pos - 1) / B; for (int i = 1; i <= limit; i++) res += sum[i]; for (int i = limit * B + 1; i <= pos; i++) res += w[i]; return res; } } dc1, dc2; int s[150005]; structquery {int x, l, r; longlong ans;} qry[150005]; vector<int> id[150005]; vector<pair<int, int>> qq[150005];
voidupdate(int l, int r, longlong val){ dc1.update(l, val), dc1.update(r + 1, -val); dc2.update(l, val * l), dc2.update(r + 1, -val * (r + 1)); return ; } longlongquery(int r){return dc1.query(r) * (r + 1) - dc2.query(r);} voidsolve(int op){ dc1.clear(), dc2.clear(); vector<int> stk; for (int i = 1; i <= n; i++) { while (!stk.empty() && (!op ? p[stk.back()] >= p[i] : p[stk.back()] <= p[i])) { int r = stk.back(); stk.pop_back(); int l = (!stk.empty() ? stk.back() : 0) + 1; // t in [i, n], k in [i - ll + 1, t - ll + 1] (tips: ll in [l, r]) update(i - r + 1, i - l + 1, p[i] - p[r]); } // t in [i, n], k in [1, t - i + 1] update(1, 1, p[i]); stk.push_back(i); for (auto j : qq[i]) if (j.second > 0) qry[j.second].ans += query(j.first); else qry[-j.second].ans -= query(j.first); } dc1.clear(), dc2.clear(); stk.clear(); for (int i = 1; i <= n; i++) { while (!stk.empty() && (!op ? p[stk.back()] >= p[i] : p[stk.back()] <= p[i])) { int r = stk.back(); stk.pop_back(); int l = (!stk.empty() ? stk.back() : 0) + 1; // t in [i, n], k in [i - ll + 1, t - ll + 1] (tips: ll in [l, r]) update(n - r + 2, n - l + 2, p[r] - p[i]); } // t in [i, n], k in [1, t - i + 1] update(n - i + 2, n - i + 2, -p[i]); stk.push_back(i); for (auto j : qq[i]) if (j.second > 0) qry[j.second].ans += query(j.first + n - i); else qry[-j.second].ans -= query(j.first + n - i); } return ; }
voidmain(){ for (int i = 1; i <= m; i++) s[i] = s[i - 1] + 2 * a[i]; for (int i = 1; i <= q; i++) { scanf("%d%d%d", &qry[i].x, &qry[i].l, &qry[i].r); int pos = lower_bound(s + 1, s + m + 1, qry[i].x) - s; id[pos].push_back(i); } for (int l = 1, r, lstsum = 0, lst = 0; l <= m; lst = l, l = r + 1) { r = l; while (r < m && a[r + 1] <= a[l]) r++; n -= 2 * (s[l - 1] - a[lst]); for (int i = 1; i <= n; i++) p[i] = p[i + s[l - 1] - a[lst]]; lstsum += s[l - 1]; for (auto i : id[l]) { qry[i].x -= lstsum * 2; if (qry[i].x <= a[l]) { int x = qry[i].x, ll = qry[i].l + x, rr = qry[i].r + x; qq[rr].emplace_back(x + 1, i); qq[ll - 1].emplace_back(x + 1, -i); } } solve(0); for (int i = 1; i <= n; i++) qq[i].clear(); updatemin(a[l]); for (int i : id[l]) if (qry[i].x > a[l]) { int x = qry[i].x - a[l], ll = qry[i].l + x, rr = qry[i].r + x; qq[rr].emplace_back(x + 1, i); qq[ll - 1].emplace_back(x + 1, -i); } solve(1); for (int i = 1; i <= n; i++) qq[i].clear(); updatemax(a[l]); s[l] = a[l]; for (int i = l + 1; i <= r; i++) { s[i] = s[i - 1] + a[i]; for (int j : id[i]) { qry[j].x -= lstsum * 2; int x, ll, rr; if (qry[j].x == 2 * s[i]) { x = 0; ll = qry[j].l + s[i] - a[l]; rr = qry[j].r + s[i] - a[l]; } elseif (qry[j].x <= 2 * s[i - 1] + a[i]) { x = qry[j].x - 2 * s[i - 1]; ll = qry[j].l + qry[j].x - s[i - 1] - a[l]; rr = qry[j].r + qry[j].x - s[i - 1] - a[l]; } else { x = 2 * s[i] - qry[j].x; ll = qry[j].l + s[i] - a[l]; rr = qry[j].r + s[i] - a[l]; } qq[rr].emplace_back(x + 1, j); qq[ll - 1].emplace_back(x + 1, -j); } } solve(0); for (int i = 1; i <= n; i++) qq[i].clear(); } for (int i = 1; i <= q; i++) printf("%lld\n", qry[i].ans); return ; } }
intmain(){ scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= n; i++) scanf("%d", &p[i]); for (int i = 1; i <= m; i++) scanf("%d", &a[i]); Subtask7::main(); return0; }