QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#661751#9431. The Quest for El DoradoShwStoneRE 921ms105412kbC++144.0kb2024-10-20 17:55:472024-10-20 17:55:53

Judging History

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

  • [2024-10-20 17:55:53]
  • 评测
  • 测评结果:RE
  • 用时:921ms
  • 内存:105412kb
  • [2024-10-20 17:55:47]
  • 提交

answer

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

struct Bits {
	vector<unsigned long long> b;
	vector<int> sum;
	int len;

	Bits(int n) {
		len = n >> 6;
		b.resize(len + 1, 0);
		sum.resize(len + 1, 0);
	}

	// 第 k 位(从 1 开始编号)设为 1
	void set(int k) {
		k--;  // 内部从 0 开始存储
		b[k >> 6] |= (1ull << (k & 0x3f));
	}

	// 设定完毕后调用
	void prepare() {
		for (int i = 0; i < b.size(); i++) {
			if (i) sum[i] = sum[i - 1];
			sum[i] += __builtin_popcountll(b[i]);
		}
	}

	int rank1(int k) {
		int res = 0;
		int hi = (k >> 6), lo = (k & 0x3f);
		if (hi) res += sum[hi - 1];
		// (1 << lo) - 1 取了小于 lo 的位,正好符合从 0 开始存储
		res += __builtin_popcountll(b[hi] & ((1ull << lo) - 1ull));
		return res;
	}

	int rank0(int k) { return k - rank1(k); }
};

struct WaveletMatrix {
	vector<Bits> b;
	int n;

	// 注意 a 将被修改
	WaveletMatrix(int n, int *a) {
		this->n = n;
		b.resize(32, Bits(n));
		for (int j = 31; j >= 0; j--) {
			for (int i = 1; i <= n; i++)
				if ((a[i] >> j) & 1) b[j].set(i);
			b[j].prepare();
			stable_partition(a + 1, a + n + 1, [&](int x) { return (x >> j) & 1; });
		}
	}

	int id(int j, int c, int i) {
		return c ? b[j].rank1(i) : b[j].rank0(i) + b[j].rank1(n);
	}

	int kth(int l, int r, int k) {
		int res = 0;
		for (int j = 31; j >= 0; j--) {
			int cnt = b[j].rank0(r) - b[j].rank0(l - 1);
			if (cnt >= k) {
				l = id(j, 0, l - 1) + 1;
				r = id(j, 0, r);
			} else {
				res |= (1 << j);
				k -= cnt;
				l = id(j, 1, l - 1) + 1;
				r = id(j, 1, r);
			}
		}
		return res;
	}

	int ranking(int l, int r, int w) {
		int res = 0;
		for (int j = 31; j >= 0; j--) {
			int c = (w >> j) & 1;
			if (c) {
				res += b[j].rank0(r) - b[j].rank0(l - 1);
				l = id(j, 1, l - 1) + 1;
				r = id(j, 1, r);
			} else {
				l = id(j, 0, l - 1) + 1;
				r = id(j, 0, r);
			}
		}
		return res;
	}
};


const int MAXN(5e5 + 5);

struct Edge {
	int v, w, c;
	Edge(int v, int w, int c) : v(v), w(w), c(c) {}
};

struct Node {
	int u, w, c, k;
	Node(int u, int w, int c, int k) : u(u), w(w), c(c), k(k) {}
};

int n, m, k;
int b[MAXN], tmp[MAXN];
bool vis[MAXN];
vector<int> ts[MAXN];
vector<Edge> graph[MAXN];
vector<WaveletMatrix> wms;

Node calc(Node u, Edge e) {
	if (u.c == e.c && b[u.k] >= e.w + u.w) {
		return Node(e.v, e.w + u.w, e.c, u.k);
	}
	int l = 0, r = ts[e.c].size() - 1, mid;
	if (b[ts[e.c][l]] >= e.w) {
		while (l < r) {
			mid = (l + r + 1) >> 1;
			if (b[ts[e.c][mid]] >= e.w) l = mid;
			else r = mid - 1;
		}
		l++;
		int rk = wms[e.c].ranking(1, l, u.k + 1) + 1;
		if (rk <= l) {
			int k = wms[e.c].kth(1, l, rk);
			return Node(e.v, e.w, e.c, k);
		}
	}
	return Node(-1, -1, -1, -1);
}

bool operator < (Node a, Node b) {
	return a.k != b.k ? a.k > b.k : a.w > b.w;
}

void solve() {
	scanf("%d %d %d", &n, &m, &k);
	for (int i = 1; i <= n; i++) {
		graph[i].clear();
		vis[i] = false;
	}
	for (int i = 1; i <= m; i++) {
		ts[i].clear();
		int u, v, w, c;
		scanf("%d %d %d %d", &u, &v, &c, &w);
		graph[u].emplace_back(v, w, c);
		graph[v].emplace_back(u, w, c);
	}
	for (int i = 1; i <= k; i++) {
		int c;
		scanf("%d %d", &c, b + i);
		ts[c].emplace_back(i);
	}
	wms.clear();
	wms.emplace_back(0, tmp);
	for (int i = 1; i <= m; i++) {
		sort(ts[i].begin(), ts[i].end(), [&](int x, int y) { return b[x] > b[y]; });
		int tt = ts[i].size();
		for (int j = 1; j <= tt; j++) {
			tmp[j] = ts[i][j - 1];
		}
		wms.emplace_back(tt, tmp);
	}
	priority_queue<Node> pq;
	pq.emplace(1, 0, 0, 0);
	while (!pq.empty()) {
		Node u = pq.top();
		pq.pop();
		if (vis[u.u]) continue;
		vis[u.u] = true;
		for (Edge e : graph[u.u]) {
			if (vis[e.v]) continue;
			Node v = calc(u, e);
			if (v.u == -1) continue;
			pq.emplace(v);
		}
	}
	for (int i = 1; i <= n; i++) {
		putchar(vis[i] ? '1' : '0');
	}
	puts("");
}

int main() {
	int _;
	scanf("%d", &_);
	while (_--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 31440kb

input:

2
5 6 4
1 2 1 30
2 3 1 50
2 5 5 50
3 4 6 10
2 4 5 30
2 5 1 40
1 70
6 100
5 40
1 30
3 1 1
2 3 1 10
1 100

output:

11011
100

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 921ms
memory: 105412kb

input:

1110
46 80 30
44 23 5 46
10 28 1 64
32 34 3 40
9 36 1 26
15 14 5 95
38 19 2 34
2 17 4 183
10 38 2 81
5 15 2 83
31 38 3 100
40 30 1 53
41 10 1 193
29 20 5 18
14 41 3 78
8 16 5 74
46 13 3 78
44 28 3 45
1 40 3 133
5 32 1 108
22 26 2 83
10 38 1 77
11 40 1 11
17 21 2 66
41 46 3 98
9 36 2 25
40 18 1 19
27...

output:

1000110011110111110010100001010100100101000000
1100010010111011011011000000011000001100001000
1000000000000000000000000000000000000000000000
1011010000000010000100010011000100000000000010
1000000000000000000000101000010000001001000001
1001100010110000100001100000000011001110110
101010000000000000010...

result:

ok 1110 lines

Test #3:

score: -100
Runtime Error

input:

1110
41 95 66
37 16 1 93
8 38 13 61
41 25 7 10
40 26 13 90
18 34 12 84
29 21 7 22
32 41 10 52
20 17 18 273
41 31 2 163
17 11 20 316
24 14 7 35
1 5 7 39
26 38 13 48
10 15 14 154
4 7 12 8
13 6 20 139
18 9 10 90
16 33 7 54
9 35 5 39
36 31 17 9
10 20 17 74
37 34 13 6
7 9 9 153
12 7 18 173
5 7 3 194
21 3...

output:


result: