比赛 4043级2023省选练习赛5 评测结果 WWWEETEEEE
题目名称 市场 最终得分 0
用户昵称 HeSn 运行时间 3.889 s
代码语言 C++ 内存使用 7.22 MiB
提交时间 2023-03-13 20:22:18
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 100010;
int n, m, s, a[MAXN], maxn[MAXN], minn[MAXN], sum[MAXN], tag1[MAXN], tag2[MAXN];
void update1(int x, int y, int w) {
	int ka = x / s, kb = y / s;
	if(ka == kb) {
		maxn[ka] = 0;
		minn[ka] = 1e12;
		sum[ka] = 0;
		for(int i = ka * s; i < (ka + 1) * s; i ++) {
			a[i] /= tag2[ka];
		}
		tag2[ka] = 1;
		for(int i = x; i <= y; i ++) {
			a[i] += w;
		}
		for(int i = ka * s; i < (ka + 1) * s; i ++) {
			sum[ka] += a[i];
			maxn[ka] = max(maxn[ka], a[i]);
			minn[ka] = min(minn[ka], a[i]);
		}
		return ;
	}
	update1(x, (ka + 1) * s - 1, w);
	update1(kb * s, y, w);
	for(int i = ka + 1; i < kb; i ++) {
		if(maxn[i] / tag2[i] == minn[i] / tag2[i]) {
			maxn[i] = minn[i] = maxn[i] / tag2[i] + w;
			sum[i] = (maxn[i] + w) * s;
			tag1[i] += w;
		}
		else {
			maxn[i] = 0;
			minn[i] = 1e12;
			sum[i] = 0;
			for(int j = i * s; j < (i + 1) * s; j ++) {
				a[j] /= tag2[i];
				a[j] += w;
				sum[i] += a[j];
				maxn[i] = max(maxn[i], a[j]);
				minn[i] = min(minn[i], a[j]);
			}
			tag2[i] = 1;
		}
	}
}
void update2(int x, int y, int w) {
	int ka = x / s, kb = y / s;
	if(ka == kb) {
		maxn[ka] = 0;
		minn[ka] = 1e12;
		sum[ka] = 0;
		for(int i = ka * s; i < (ka + 1) * s; i ++) {
			a[i] += tag1[ka];
		}
		tag1[ka] = 0;
		for(int i = x; i <= y; i ++) {
			a[i] /= w;
		}
		for(int i = ka * s; i < (ka + 1) * s; i ++) {
			sum[ka] += a[i];
			maxn[ka] = max(maxn[ka], a[i]);
			minn[ka] = min(minn[ka], a[i]);
		}
		return ;
	}
	update2(x, (ka + 1) * s - 1, w);
	update2(kb * s, y, w);
	for(int i = ka + 1; i < kb; i ++) {
		tag2[i] *= w;
		if(maxn[i] / w == minn[i] / w) {
			maxn[i] = minn[i] = maxn[i] / w;
			sum[i] = maxn[i] * s;
		}
		else {
			maxn[i] = 0;
			minn[i] = 1e12;
			sum[i] = 0;
			for(int j = i * s; j < (i + 1) * s; j ++) {
				a[j] += tag1[i];
				a[j] /= tag2[i];
				sum[i] += a[j];
				maxn[i] = max(maxn[i], a[j]);
				minn[i] = min(minn[i], a[j]);
			}
			tag1[i] = 0;
			tag2[i] = 1;
		}
	}
}
int query1(int x, int y) {
	int ka = x / s, kb = y / s, minx = 1e12;
	if(ka == kb) {
		for(int i = x; i <= y; i ++) {
			minx = min(a[i], minx);
		}
		return minx;
	}
	minx = min(minx, query1(x, (ka + 1) * s - 1));
	minx = min(minx, query1(kb * s, y));
	for(int i = ka + 1; i < kb; i ++) {
		minx = min(minx, minn[i]);
	}
	return minx;
}
int query2(int x, int y) {
	int ka = x / s, kb = y / s, res = 0;
	if(ka == kb) {
		for(int i = x; i <= y; i ++) {
			res += a[i];
		}
		return res;
	}
	res += query2(x, (ka + 1) * s - 1);
	res += query2(kb * s, y);
	for(int i = ka + 1; i < kb; i ++) {
		res += sum[i];
	}
	return res;
}
signed main() {
	freopen("2017market.in", "r", stdin);
	freopen("2017market.out", "w", stdout);
	cin >> n >> m;
	s = sqrt(n);
	for(int i = 0; i <= s; i ++) {
		maxn[i] = 0;
		minn[i] = 1e12;
	}
	for(int i = 0; i < n; i ++) {
		cin >> a[i];
		maxn[i / s] = max(maxn[i / s], a[i]);
		minn[i / s] = min(minn[i / s], a[i]);
		tag2[i / s] = 1;
		sum[i / s] += a[i];
	}
	for(int i = 1; i <= m; i ++) {
		int op, x, y, w;
		cin >> op >> x >> y;
		if(op == 1) {
			cin >> w;
			update1(x, y, w);
		}
		if(op == 2) {
			cin >> w;
			update2(x, y, w);
		}
		if(op == 3) {
			cout << query1(x, y) << endl;
		}
		if(op == 4) {
			cout << query2(x, y) << endl;
		}
	} 
	return 0;
}