求调,闭关,10pts
查看原帖
求调,闭关,10pts
167697
BartAllen楼主2024/11/29 21:45
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5;
const int M = 1e3 + 5;

int n, q, a[N], b[N];
int len, tot, L[M], R[M], pos[N], add[M];

void init() {
	len = sqrt(n), tot = n / len;
	if (n % len) tot++;
	for (int i = 1; i <= tot; i++) {
		L[i] = (i - 1) * len + 1;
		R[i] = i * len;
	}
	R[tot] = n;
	for (int i = 1; i <= tot; i++)
		for (int j = L[i]; j <= R[i]; j++) pos[j] = i, b[j] = a[j];
	for (int i = 1; i <= tot; i++) sort(b + L[i], b + R[i] + 1);
}

void change(int x, int y, int k) {
	if (pos[x] == pos[y]) {
		for (int i = x; i <= y; i++) a[i] += k, b[i] = a[i];
		sort(b + L[pos[x]], b + R[pos[x]] + 1);
	}
	else {
		for (int i = x; i <= R[pos[x]]; i++) a[i] += k, b[i] = a[i];
		sort(b + L[pos[x]], b + R[pos[x]] + 1);
		
		for (int i = pos[x] + 1; i <= pos[y] - 1; i++) add[i] += k;
		
		for (int i = L[pos[y]]; i <= y; i++) a[i] += k, b[i] = a[i];
		sort(b + L[pos[y]], b + R[pos[y]] + 1);
	}
}

int ask(int x, int y, int k) {
	int ans = 0;
	if (pos[x] == pos[y]) {
		for (int i = x; i <= y; i++)
			if (add[pos[x]] + a[i] >= k) ans++;
	}
	else {
		for (int i = x; i <= R[pos[x]]; i++)
			if (add[pos[i]] + a[i] >= k) ans++;

		for (int i = pos[x] + 1; i <= pos[y] - 1; i++)
			ans = R[i] - (lower_bound(b + L[i], b + R[i] + 1, k - add[i]) - b) + 1;

		for (int i = L[pos[y]]; i <= y; i++)
			if (add[pos[i]] + a[i] >= k) ans++;
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> q;
	for (int i = 1; i <= n; i++) cin >> a[i];
	init();
	while (q--) {
		char ch; int x, y, k; cin >> ch >> x >> y >> k;
		switch (ch) {
			case 'M': change(x, y, k); break;
			case 'A': cout << ask(x, y, k) << endl;
		}
	}
	return 0;
}
2024/11/29 21:45
加载中...