QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#125580#6531. Base Station ConstructionzyoobnWA 104ms17240kbC++202.4kb2023-07-16 22:20:322023-07-16 22:20:36

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-16 22:20:36]
  • 评测
  • 测评结果:WA
  • 用时:104ms
  • 内存:17240kb
  • [2023-07-16 22:20:32]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

struct SegTree {
	struct Node {
		int l, r;
		i64 val, lazy;
	};

	int n;
	vector<Node> tr;
	SegTree() {}

	template <class T>
	SegTree(vector<T> &a) {
		init(a);
	}

	template <class T>
	void init(vector<T> &a) {
		n = a.size();
		tr.resize(n * 4);
		function<void(int, int, int)> build = [&] (int k, int l, int r) {
			tr[k] = {l, r};
			if (l == r) {
				tr[k].val = a[l - 1]; // 赋值要注意数组a下标从0开始用l - 1,从1开始用l 
				return;
			}
			int mid = (l + r) / 2;
			build(k * 2, l, mid);
			build(k * 2 + 1, mid + 1, r);
			pushup(k);
		};
		build(1, 1, n);
	}

	void pushup(int k) {
		int ls = k * 2, rs = k * 2 + 1;
		tr[k].val = min(tr[ls].val, tr[rs].val);
	}
	
	template<class Num>
	void update(int x, int y, Num v, int k = 1) {
		int l = tr[k].l, r = tr[k].r;
		if(x <= l && r <= y) {
			tr[k].val += v;
			tr[k].lazy += v;
			return;
		}
		int mid = (l + r) >> 1;
		if (x <= mid && y > mid) {
			update(x, y, v, k * 2); 
			update(x, y, v, k * 2 + 1);
		} else if (x <= mid) {
			update(x, y, v, k * 2); 
		} else if (y > mid) {
			update(x, y, v, k * 2 + 1);
		}
		pushup(k); 
	}

	Node query(int x, int y, int k = 1) {
		int l = tr[k].l, r = tr[k].r;
		if(x <= l && r <= y) {
			return tr[k];
		}
		int mid = (l + r) >> 1;
		Node res;
		if (x <= mid && y > mid) {
			Node L = query(x, y, k * 2);
			Node R = query(x, y, k * 2 + 1);
			res.val = min(L.val, R.val);
		} else if (x <= mid) {
			res = query(x, y, k * 2);
		} else if (y > mid) {
			res = query(x, y, k * 2 + 1);
		}
		return res;
	}	
};

void solve() {
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	int m;
	cin >> m;
	vector<vector<int>> g(n);
	for (int i = 0; i < m; ++i) {
		int l, r;
		cin >> l >> r;
		--l, --r;
		g[r].push_back(l);
	}



	vector<i64> dp(n);
	SegTree tr(dp);
	priority_queue<int> q;

	for (int i = 0; i < n; ++i) {
		if (!q.empty()) {
			dp[i] = tr.query(q.top() + 1, i - 1 + 1).val + a[i];
		} else {
			dp[i] = a[i];
		}
		tr.update(i + 1, i + 1, dp[i]);
		// cout << dp[i] << " \n"[i == n - 1];
		for (auto &l : g[i]) {
			q.push(l);
		}
	}
	cout << dp[n - 1] << '\n';

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3404kb

input:

2
5
3 2 4 1 100
3
1 3
2 4
5 5
5
7 3 4 2 2
3
1 4
2 3
4 5

output:

102
5

result:

ok 2 number(s): "102 5"

Test #2:

score: -100
Wrong Answer
time: 104ms
memory: 17240kb

input:

6201
12
88 78 46 95 84 98 55 3 68 42 6 18
19
6 9
10 11
12 12
8 11
8 12
2 3
2 3
1 5
9 9
7 8
6 11
2 4
12 12
2 4
2 9
7 10
8 8
1 7
6 11
5
76 27 48 66 61
2
1 4
3 5
8
64 81 20 6 86 9 4 55
1
7 7
9
1 43 18 81 11 22 9 61 83
14
5 6
2 6
5 8
1 4
9 9
9 9
7 7
2 5
8 9
5 6
4 8
5 8
9 9
6 6
10
66 41 84 7 80 31 22 96 ...

output:

141
88
59
126
303
141
45
170
159
139
222
194
217
147
230
93
287
89
124
130
148
125
100
193
228
111
239
303
236
150
177
57
59
38
90
128
83
160
108
62
35
122
156
81
115
229
110
242
126
28
210
86
311
244
262
162
302
177
93
238
30
129
75
109
96
179
81
224
142
172
119
111
215
247
301
121
120
56
213
118
3...

result:

wrong answer 2nd numbers differ - expected: '48', found: '88'