P2801 教主的魔法 Solution

Description

给定序列 a=(a1,a2,⋯ ,an)a=(a_1,a_2,\cdots,a_n)a=(a1,a2,,an),有 qqq 次操作分两种:

  • add⁡(l,r,w)\operatorname{add}(l,r,w)add(l,r,w):对每个 i∈[l,r]i \in [l,r]i[l,r] 执行 ai←ai+wa_i \gets a_i+waiai+w.
  • query⁡(l,r,c)\operatorname{query}(l,r,c)query(l,r,c):求 ∑i=lr[ai≥c]\sum\limits_{i=l}^r [a_i \ge c]i=lr[aic].

Limitations

1≤n≤1061 \le n \le 10^61n106
1≤q≤30001 \le q \le 30001q3000
1≤w≤10001 \le w \le 10001w1000
1≤ai,c≤1091 \le a_i,c \le 10^91ai,c109
1s,128MB1 \text{s},128 \text{MB}1s,128MB

Solution

线段树不好维护,考虑分块.
由于要快速查询,所以维护 pip_ipi 表示块内元素升序排序后的结果,查询时整块直接 lower_bound,散块暴力.
至于区间加,就在每个块上维护懒标记 tag\textit{tag}tag,整块直接加在 tag\textit{tag}tag 上,散块直接暴力加然后重构块.
B=nB=\sqrt{n}B=n,时间复杂度 O(qnlog⁡n)O(q\sqrt{n\log n})O(qnlogn)

Code

2.31KB,536ms,15.72MB  (in total, C++20 with O2)2.31\text{KB},536\text{ms},15.72\text{MB}\;\texttt{(in total, C++20 with O2)}2.31KB,536ms,15.72MB(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 Block {
	int n, size, blocks;
	vector<int> A, P, belong, L, R, tag;
	
	inline Block() {}
	inline Block(const vector<int>& a): A(a) {
		n = a.size();
		size = (int)sqrt(n);
		blocks = (n + size - 1) / size;
		
		P.resize(n);
		belong.resize(n);
		L.resize(blocks);
		R.resize(blocks);
		tag.resize(blocks);
		
		for (int i = 0; i < blocks; i++) {
			L[i] = i * size;
			R[i] = min(L[i] + size - 1, n - 1);
			for (int j = L[i]; j <= R[i]; j++) belong[j] = i;
		}
		for (int i = 0; i < blocks; i++) build(i);
	}
	
	inline void build(int b) {
		for (int i = L[b]; i <= R[b]; i++) {
			A[i] += tag[b];
			P[i] = A[i];
		}
		sort(P.begin() + L[b], P.begin() + R[b] + 1);
		tag[b] = 0;
	}
	
	inline void add(int l, int r, int x) {
		const int bl = belong[l], br = belong[r];
		if (bl == br) {
			for (int i = l; i <= r; i++) A[i] += x;
			return build(bl);
		}
		for (int i = l; i <= R[bl]; i++) A[i] += x;
		for (int i = L[br]; i <= r; i++) A[i] += x;
		build(bl), build(br);
		for (int i = bl + 1; i < br; i++) tag[i] += x;
	}
	
	inline int query(int l, int r, int x) {
		const int bl = belong[l], br = belong[r];
		if (bl == br) {
			int ans = 0;
			for (int i = l; i <= r; i++) ans += (A[i] + tag[bl] >= x);
			return ans;
		}
		
		int ans = 0;
		for (int i = l; i <= R[bl]; i++) ans += (A[i] + tag[bl] >= x);
		for (int i = L[br]; i <= r; i++) ans += (A[i] + tag[br] >= x);
		for (int i = bl + 1; i < br; i++) {
			ans += R[i] + 1 - (lower_bound(P.begin() + L[i], P.begin() + R[i] + 1, 
			                               x - tag[i]) - P.begin());
		}
		return ans;
	}
};

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n, m;
	cin >> n >> m;
	
	vector<int> a(n);
	for (int i = 0; i < n; i++) cin >> a[i];
	
	Block blk(a);
	for (int i = 0; i < m; i++) {
		char op; int l, r, x;
		cin >> op >> l >> r >> x;
		l--, r--;
		if (op == 'M') blk.add(l, r, x);
		else cout << blk.query(l, r, x) << endl;
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值