我是傻逼。。
首先义眼盯这,离线扫描线,考虑贡献。
发现固定右端点贡献是单调的。
于是插入 就直接弹丹钓战,转成区间覆盖形式。
维护 和历史和,额外维护 即可。
上线段树。
覆盖 :
覆盖 :
历史和累加:
冲:
#ifdef N2
#define LOG(...) fprintf(stderr, __VA_ARGS__), void(0)
//#define _GLIBCXX_DEBUG
#else
#define LOG(...) void(0)
#endif
#include<bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
const int maxn = 2.5e5 + 10;
class Mat {
public:
ull d00, d02, d03, d11, d12, d13, d22, d23, d40, d41, d42, d43;
Mat operator*(const Mat& __rhs) const {
Mat res;
res.d00 = d00 * __rhs.d00;
res.d02 = d00 * __rhs.d02 + d02 * __rhs.d22;
res.d03 = d00 * __rhs.d03 + d02 * __rhs.d23 + d03;
res.d11 = d11 * __rhs.d11;
res.d12 = d11 * __rhs.d12 + d12 * __rhs.d22;
res.d13 = d11 * __rhs.d13 + d12 * __rhs.d23 + d13;
res.d22 = d22 * __rhs.d22;
res.d23 = d22 * __rhs.d23 + d23;
res.d40 = d40 * __rhs.d00 + __rhs.d40;
res.d41 = d41 * __rhs.d11 + __rhs.d41;
res.d42 = d40 * __rhs.d02 + d41 * __rhs.d12 + d42 * __rhs.d22 + __rhs.d42;
res.d43 = d40 * __rhs.d03 + d41 * __rhs.d13 + d42 * __rhs.d23 + d43 + __rhs.d43;
return res;
}
void clear() {
d00 = d11 = d22 = 1;
d02 = d03 = d12 = d13 = d23 = d40 = d41 = d42 = d43 = 0;
}
bool empty() {
return (d00 == 1) && (d11 == 1) && (d22 == 1) &&
!d02 && !d03 && !d12 && !d13 && !d23 && !d40 && !d41 && !d42 && !d43;
}
};
class Vec {
public:
ull d[5];
ull& operator[](const size_t& __n) {
return d[__n];
}
const ull& operator[](const size_t& __n) const {
return d[__n];
}
Vec operator*(const Mat& __rhs) const {
Vec res;
res.d[0] = d[0] * __rhs.d00 + d[4] * __rhs.d40;
res.d[1] = d[1] * __rhs.d11 + d[4] * __rhs.d41;
res.d[2] = d[0] * __rhs.d02 + d[1] * __rhs.d12 + d[2] * __rhs.d22 + d[4] * __rhs.d42;
res.d[3] = d[0] * __rhs.d03 + d[1] * __rhs.d13 + d[2] * __rhs.d23 + d[3] + d[4] * __rhs.d43;
res.d[4] = d[4];
return res;
}
Vec operator+(const Vec& __rhs) const {
Vec res;
res.d[0] = d[0] + __rhs.d[0];
res.d[1] = d[1] + __rhs.d[1];
res.d[2] = d[2] + __rhs.d[2];
res.d[3] = d[3] + __rhs.d[3];
res.d[4] = d[4] + __rhs.d[4];
//@d[4] is the len, calc when build
return res;
}
};
class SegTree {
public:
Vec tr[maxn << 2];
Mat tag[maxn << 2];
void __update(int k) {
tr[k] = tr[k << 1] + tr[k << 1 | 1];
}
void __push(int k) {
auto& x = tag[k];
if(x.empty()) return;
tr[k << 1] = tr[k << 1] * x;
tr[k << 1 | 1] = tr[k << 1 | 1] * x;
tag[k << 1] = tag[k << 1] * x;
tag[k << 1 | 1] = tag[k << 1 | 1] * x;
x.clear();
}
void build(int k, int l, int r) {
tr[k][4] = r - l + 1;
tag[k].clear();
if(l == r) return;
int mid = (l + r) >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
__update(k);
}
void modify(int k, int l, int r, int L, int R, Mat val) {
if(L <= l && r <= R) {
tr[k] = tr[k] * val;
tag[k] = tag[k] * val;
return;
}
__push(k);
int mid = (l + r) >> 1;
if(L <= mid) modify(k << 1, l, mid, L, R, val);
if(R > mid) modify(k << 1 | 1, mid + 1, r, L, R, val);
__update(k);
}
ull query(int k, int l, int r, int L, int R) {
if(L <= l && r <= R) return tr[k][3];
__push(k);
int mid = (l + r) >> 1;
ull res = 0;
if(L <= mid) res += query(k << 1, l, mid, L, R);
if(R > mid) res += query(k << 1 | 1, mid + 1, r, L, R);
return res;
}
}sgt;
deque<pair<int, int>> qa, qb;
int a[maxn], b[maxn];
int T, n, m;
vector<pair<int, int>> q[maxn];
ull ans[maxn];
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> T >> n;
sgt.build(1, 1, n);
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) cin >> b[i];
cin >> m;
for(int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
q[r].emplace_back(l, i);
}
qa.emplace_back(INT32_MAX, 0);
qb.emplace_back(INT32_MAX, 0);
for(int i = 1; i <= n; i++) {
while(qa.back().first < a[i]) qa.pop_back();
while(qb.back().first < b[i]) qb.pop_back();
sgt.modify(1, 1, n, qa.back().second + 1, i, Mat({0, 0, 0, 1, a[i], 0, 0, 0, a[i], 0, 0, 0}));
sgt.modify(1, 1, n, qb.back().second + 1, i, Mat({1, b[i], 0, 0, 0, 0, 0, 0, 0, b[i], 0, 0}));
sgt.modify(1, 1, n, 1, i, Mat({1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0}));
for(auto cur : q[i]) {
ans[cur.second] = sgt.query(1, 1, n, cur.first, i);
}
qa.emplace_back(a[i], i);
qb.emplace_back(b[i], i);
}
for(int i = 1; i <= m; i++) cout << ans[i] << '\n';
}