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+wai←ai+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=l∑r[ai≥c].
Limitations
1≤n≤1061 \le n \le 10^61≤n≤106
1≤q≤30001 \le q \le 30001≤q≤3000
1≤w≤10001 \le w \le 10001≤w≤1000
1≤ai,c≤1091 \le a_i,c \le 10^91≤ai,c≤109
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(qnlogn)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;
}
1580

被折叠的 条评论
为什么被折叠?



