P8512 [Ynoi Easy Round 2021] TEST_152 Solution

Description

有一序列 c=(c1,c2,⋯ ,cm)c=(c_1,c_2,\cdots,c_m)c=(c1,c2,,cm)nnn 个三元组 (li,ri,vi)(l_i,r_i,v_i)(li,ri,vi).
回答 qqq 次形如 (L,R)(L,R)(L,R) 的询问,具体如下:

  • ccc 置为全 000.
  • 对每个 i∈[L,R]i\in[L,R]i[L,R],将 cli∼cric_{l_i}\sim c_{r_i}clicri 赋值viv_ivi.
  • 最后输出 ∑i=1mci\sum_{i=1}^m c_ii=1mci.

询问之间互相独立.

Limitations

1≤n,m,q≤5×1051\le n,m,q \le 5\times 10^51n,m,q5×105
1≤li≤ri≤m1 \le l_i\le r_i\le m1lirim
1≤L≤R≤n1 \le L \le R \le n1LRn

Solution

典题一道.
答案不好在线求,但可由前缀相减得到,考虑挂在 rrr 上做扫描线,并开 BIT 维护时间轴.
我们都知道,区间 [l,r][l,r][l,r] 覆盖 vvv 后的贡献为 v×(r−l+1)v\times (r-l+1)v×(rl+1).
然而覆盖后需要抹除之前的贡献,而这可以借助 ODT 完成.
具体地,在 assign 时将 [l,r][l,r][l,r] 中原有的颜色段的贡献在 BIT 上一段段减掉.
然后就做完了,代码很好写.
时间复杂度 O(nlog⁡n)O(n\log n)O(nlogn),具体请自证.

Code

2.65KB,10.01s,64.81MB  (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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值