Description
有一序列
c
=
(
c
1
,
c
2
,
⋯
,
c
m
)
c=(c_1,c_2,\cdots,c_m)
c=(c1,c2,⋯,cm) 和
n
n
n 个三元组
(
l
i
,
r
i
,
v
i
)
(l_i,r_i,v_i)
(li,ri,vi).
回答
q
q
q 次形如
(
L
,
R
)
(L,R)
(L,R) 的询问,具体如下:
- 将 c c c 置为全 0 0 0.
- 对每个 i ∈ [ L , R ] i\in[L,R] i∈[L,R],将 c l i ∼ c r i c_{l_i}\sim c_{r_i} cli∼cri 赋值为 v i v_i vi.
- 最后输出 ∑ i = 1 m c i \sum_{i=1}^m c_i ∑i=1mci.
询问之间互相独立.
Limitations
1
≤
n
,
m
,
q
≤
5
×
1
0
5
1\le n,m,q \le 5\times 10^5
1≤n,m,q≤5×105
1
≤
l
i
≤
r
i
≤
m
1 \le l_i\le r_i\le m
1≤li≤ri≤m
1
≤
L
≤
R
≤
n
1 \le L \le R \le n
1≤L≤R≤n
Solution
典题一道.
答案不好在线求,但可由前缀相减得到,考虑挂在
r
r
r 上做扫描线,并开 BIT
维护时间轴.
我们都知道,区间
[
l
,
r
]
[l,r]
[l,r] 覆盖
v
v
v 后的贡献为
v
×
(
r
−
l
+
1
)
v\times (r-l+1)
v×(r−l+1).
然而覆盖后需要抹除之前的贡献,而这可以借助 ODT
完成.
具体地,在 assign
时将
[
l
,
r
]
[l,r]
[l,r] 中原有的颜色段的贡献在 BIT
上一段段减掉.
然后就做完了,代码很好写.
时间复杂度
O
(
n
log
n
)
O(n\log n)
O(nlogn),具体请自证.
Code
2.65 KB , 10.01 s , 64.81 MB (in total, C++20 with O2) 2.65\text{KB},10.01\text{s},64.81\text{MB}\;\texttt{(in total, C++20 with O2)} 2.65KB,10.01s,64.81MB(in total, C++20 with O2)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;
template<class T>
bool chmax(T &a, const T &b){
if(a < b){ a = b; return true; }
return false;
}
template<class T>
bool chmin(T &a, const T &b){
if(a > b){ a = b; return true; }
return false;
}
struct Node {
int l, r;
mutable int v;
int t;
inline Node(int l, int r = 0, int v = 0, int t = 0) : l(l), r(r), v(v), t(t) {}
inline bool operator<(const Node& rhs) const { return l < rhs.l; }
};
struct ODT {
set<Node> s;
using Iter = set<Node>::iterator;
inline ODT() {}
inline ODT(int n) {
s.emplace(0, n - 1, 0, 0);
}
inline Iter split(int pos) {
auto it = s.lower_bound(Node(pos, 0, 0, 0));
if (it != s.end() && it->l == pos) return it;
--it;
if (it->r < pos) return s.end();
auto [l, r, val, tim] = *it;
s.erase(it), s.emplace(l, pos - 1, val, tim);
return s.emplace(pos, r, val, tim).first;
}
inline void assign(int l, int r, int v, int t, vector<Node>& opt) {
opt.clear();
auto itr = split(r + 1), itl = split(l);
for (auto it = itl; it != itr; it++) opt.push_back(*it);
s.erase(itl, itr);
s.emplace(l, r, v, t);
}
};
int lowbit(int x){
return x & -x;
}
template<class T>
struct fenwick{
int n;
vector<T> c;
fenwick() {}
fenwick(int _n): n(_n){
c.resize(n + 1);
}
fenwick(const vector<T> &a): n(a.size()){
c.resize(n + 1);
for(int i = 1; i <= n; i++){
c[i] = c[i] + a[i - 1];
int j = i + lowbit(i);
if(j <= n) c[j] = c[j] + c[i];
}
}
void add(int x, const T& v){
for(int i = x + 1; i <= n; i += lowbit(i)) c[i] = c[i] + v;
}
T ask(int x){
T ans{};
for(int i = x + 1; i; i -= lowbit(i)) ans = ans + c[i];
return ans;
}
T ask(int l, int r){
return ask(r) - ask(l - 1);
}
};
using pii = pair<int, int>;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
vector<int> L(n), R(n), X(n);
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &L[i], &R[i], &X[i]);
L[i]--, R[i]--;
}
vector<vector<pii>> adj(n);
for (int i = 0, l, r; i < q; i++) {
scanf("%d %d", &l, &r), l--, r--;
adj[r].emplace_back(l, i);
}
ODT odt(m);
fenwick<i64> fwk(n);
vector<Node> tmp;
vector<i64> ans(q);
for (int i = 0; i < n; i++) {
odt.assign(L[i], R[i], X[i], i, tmp);
for (auto [l, r, v, t] : tmp) fwk.add(t, -1LL * (r - l + 1) * v);
fwk.add(i, 1LL * (R[i] - L[i] + 1) * X[i]);
for (auto [j, id] : adj[i]) ans[id] = fwk.ask(j, i);
}
for (int i = 0; i < q; i++) printf("%lld\n", ans[i]);
return 0;
}