悬关,简单分块裸题。样例正确,提交全WA
查看原帖
悬关,简单分块裸题。样例正确,提交全WA
429491
xin20110426楼主2024/7/18 14:52
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, cnt, a[200010], add[200010], num[1000], l[1000], r[1000], B, sz[1000], belong[200010];
signed main() {
	cin >> n >> m;
	B = sqrt(n);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (i % B == 1) l[i / B] = i, cnt++; 		
		if (i % B == 0) r[i / B - 1] = i; 	
		belong[i] = (i - 1) / B;
	}
	r[cnt - 1] = n; 	
	for (int i = 0; i < cnt; i++) sz[i] = r[i] - l[i] + 1;
	for (int i = 1; i <= m; i++) {
		int type, x, y, k;
		cin >> type;
		if (type == 1) {
			cin >> x >> y >> k;
			int p = belong[x], q = belong[y];
			for (int i = x; i <= r[p]; i++) a[i] += k, num[p] += k;
			for (int i = p + 1; i < q; i++) num[i] += sz[i] * k, add[i] += k;
			for (int i = l[q]; i <= y; i++) a[i] += k, num[q] += k;
		}
		if (type == 2) {
			cin >> k;
			a[1] += k;
			num[belong[1]] += k;
		}
		if (type == 3) { 			
			cin >> k; 			
			a[1] -= k; 			
			num[belong[1]] -= k; 		
		}
		if (type == 4) {
			cin >> x >> y;
			int p = belong[x], q = belong[y];
			int ans = 0;
			for (int i = x; i <= r[p]; i++) ans += a[i] + add[p];		
			for (int i = p + 1; i < q; i++) ans += num[i];
			for (int i = l[q]; i <= y; i++) ans += a[i] + add[q];
			cout << ans << endl;
		}
		if (type == 5) {
			cout << a[1] + add[belong[1]] << endl;
		}
	}
	return 0;
} 

2024/7/18 14:52
加载中...