求求大佬帮调 QWQ
查看原帖
求求大佬帮调 QWQ
525056
I_love_LPN_Forever楼主2022/7/16 16:29

大佬求调 20分 WA

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e5 + 5;
int c[maxn], a[maxn], T;
int n, m, k, u, ans, res, cnt;
inline int read() {
	int x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
	return x * f;
}
struct node {
	int l, r;
	bool operator<(const node p) const { return r < p.r; }
} p[maxn], t;
bool cmp(node x, node y) { return x.l < y.l; }
int lowbit(int x) { return x & (-x); }
void update(int x, int y) {
	for (int i = x; i <= n; i += lowbit(x)) 
		c[i] += y;
}
int getsum(int x) {
	int res = 0;
	for (int i = x; i; i -= lowbit(i))
		res += c[i];
	return res;
}
bool check(int x) {
	priority_queue<node> q;
	while (!q.empty())
		q.pop();
	memset(c, 0, sizeof(c));
	res = 1, cnt = 0;
	for (int i = 1; i <= n; i++) {
		update(i, a[i] - a[i - 1]);
	}
	for (int i = 1; i <= n; i++) {
		while (res <= m && p[res].l <= i)
			q.push(p[res++]);
		while (getsum(i) < x && q.size()) {
			do {
				t = q.top();
				q.pop();
			} while (t.r < i && (!q.empty()));
			if (t.r < i || ++cnt > k) return false;
			update(t.l, u);
			update(t.r + 1, -u);
		}
		if (getsum(i) < x)
			return false;
	}
	return true;
}
signed main() {
	cin >> T;
	while (T--) {
		n = read(), m = read(), k = read(), u = read();
		int l, r, mid;
		for (int i = 1; i <= n; i++) {
			a[i] = read();
			l = min(l, a[i]), r = max(r, a[i]);
		}	
		for (int i = 1; i <= m; i++) 
			p[i].l = read(), p[i].r = read();
		sort(p + 1, p + n + 1, cmp);
		while (l <= r) {
			mid = l + r >> 1;
			if (check(mid)) {
				ans = mid;
				l = mid + 1;
			}
			else
				r = mid - 1;
		}
		printf("%d\n", ans);
	}
	return 0;
}
2022/7/16 16:29
加载中...