QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#171826 | #7182. Very Sparse Table | ucup-team228# | AC ✓ | 773ms | 10608kb | C++20 | 5.2kb | 2023-09-09 17:39:22 | 2023-09-09 17:39:22 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (1 << 16) + 10;
int sq[N];
vector<tuple<int, int, int>> edges;
void precalc() {
for (int i = 1; i * i <= N - 1; i++) {
for (int j = 0; j <= 2 * i && i * i + j <= N - 1; j++) {
sq[i * i + j] = i;
}
}
}
void add_edge(int a, int b, int c) {
edges.emplace_back(a, b, c);
}
void build(int l, int r) {
int len = r - l + 1;
if (len <= 4) {
return;
} else {
int b = sq[len];
vector<int> vec = {l - 1};
for (int i = l + b - 1; i <= r; i += b) {
vec.push_back(i);
}
vec.push_back(r + 1);
int k = vec.size() - 2;
for (int ii = 1; ii <= k; ii++) {
int x = vec[ii];
int lef = vec[ii - 1];
int rig = vec[ii + 1];
for (int i = x + 2; i <= rig - 1; i++) {
add_edge(x, i - 1, i);
}
for (int i = x - 2; i >= lef + 1; i--) {
add_edge(i, i + 1, x);
}
if (rig <= r) {
add_edge(x, rig - 1, rig);
}
}
for (int i = 1; i <= k; i++) {
for (int j = i + 2; j <= k; j++) {
add_edge(vec[i], vec[j - 1], vec[j]);
}
}
for (int i = 0; i <= k; i++) {
build(vec[i] + 1, vec[i + 1] - 1);
}
}
}
void print_path(vector<int> path) {
path.erase(unique(path.begin(), path.end()), path.end());
for (int v : path) {
cout << v << " ";
}
cout << "\n";
fflush(stdout);
}
vector<int> get(int u, int v, int l, int r) {
// cout << l << " " << r << endl;
int len = r - l + 1;
if (len <= 4) {
vector<int> path;
for (int i = u; i <= v; i++) {
path.push_back(i);
}
return path;
} else {
int b = sq[len];
u = u - l + 1;
v = v - l + 1;
int ru = (u + b - 1) / b * b;
int rv = (v + b - 1) / b * b;
int lu = ru - b;
int lv = rv - b;
if (ru == rv) {
if (v == rv || u == lu) {
return {u + l - 1, v + l - 1};
} else {
return get(u + l - 1, v + l - 1, lu + l, min(r, ru + l - 2));
}
} else {
return {u + l - 1, ru + l - 1, lv + l - 1, v + l - 1};
}
}
}
bool check_edges(int n) {
set<pair<int, int>> s;
for (int i = 0; i <= n - 1; i++) {
s.insert({i, i + 1});
}
for (auto [a, b, c] : edges) {
if (!s.count({a, b}) || !s.count({b, c}) || s.count({a, c})) {
return false;
}
s.insert({a, c});
}
return true;
}
void stress() {
mt19937 rnd;
while (true) {
edges.clear();
int n = rnd() % 1000;
build(0, n);
if (edges.size() > 6 * n) {
cout << "WA\n";
cout << n << " " << edges.size() << "\n";
break;
} else {
if (!check_edges(n)) {
cout << "WA\n";
cout << n << "\n";
break;
} else {
set<pair<int, int>> s;
for (auto [a, b, c] : edges) {
s.insert({a, c});
}
for (int i = 0; i <= n - 1; i++) {
s.insert({i, i + 1});
}
int q = rnd() % 100 + 1;
while (q--) {
int u = rnd() % (n + 1);
int v = rnd() % (n + 1);
if (u > v) swap(u, v);
auto path = get(u, v, 0, n);
path.erase(unique(path.begin(), path.end()), path.end());
bool ok = path.size() <= 4 && path.front() == u && path.back() == v;
for (int i = 0; i + 1 < path.size(); i++) {
ok &= s.count({path[i], path[i + 1]});
}
if (!ok) {
cout << "WA\n";
cout << n << "\n";
cout << u << " " << v << "\n";
for (int i : path) {
cout << i << " ";
}
cout << "\n";
exit(0);
}
}
cout << "OK\t" << n << "\t" << edges.size() << endl;
}
}
}
exit(0);
}
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(nullptr);
//#ifdef LOCAL
// freopen("input.txt", "r", stdin);
//#endif
precalc();
// stress();
int n;
cin >> n;
build(0, n);
cout << edges.size() << "\n";
for (auto [a, b, c] : edges) {
cout << a << " " << b << " " << c << "\n";
}
fflush(stdout);
int q;
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
print_path(get(u, v, 0, n));
}
#ifdef LOCAL
cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4044kb
input:
9 45 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 4 3 5 3 6 3 7 3 8 3 9 4 5 4 6 4 7 4 8 4 9 5 6 5 7 5 8 5 9 6 7 6 8 6 9 7 8 7 9 8 9
output:
8 2 3 4 0 1 2 2 4 5 5 6 7 3 4 5 5 7 8 6 7 8 2 5 8 0 1 0 2 0 2 3 0 2 4 0 2 5 0 2 5 6 0 2 5 7 0 2 5 8 0 2 8 9 1 2 1 2 3 1 2 4 1 2 5 1 2 5 6 1 2 5 7 1 2 5 8 1 2 8 9 2 3 2 4 2 5 2 5 6 2 5 7 2 5 8 2 8 9 3 4 3 5 3 5 6 3 5 7 3 5 8 3 5 8 9 4 5 4 5 6 4 5 7 4 5 8 4 5 8 9 5 6...
result:
ok edges: 8
Test #2:
score: 0
Accepted
time: 3ms
memory: 4056kb
input:
30 465 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 2 3 2 4 2 5 2 6...
output:
48 4 5 6 4 6 7 4 7 8 2 3 4 1 2 4 0 1 4 4 8 9 9 10 11 9 11 12 9 12 13 7 8 9 6 7 9 5 6 9 9 13 14 14 15 16 14 16 17 14 17 18 12 13 14 11 12 14 10 11 14 14 18 19 19 20 21 19 21 22 19 22 23 17 18 19 16 17 19 15 16 19 19 23 24 24 25 26 24 26 27 24 27 28 22 23 24 21 22 24 20 21 24 24 28 29 27 28 29 26 27 2...
result:
ok edges: 48
Test #3:
score: 0
Accepted
time: 532ms
memory: 3840kb
input:
736 200000 170 268 126 166 565 723 664 735 61 524 226 234 146 314 217 272 294 713 115 381 563 706 74 567 552 614 120 211 472 620 213 432 488 623 447 564 96 129 331 354 79 677 50 547 174 568 56 129 189 227 55 701 244 253 264 715 154 220 380 657 46 390 53 161 325 537 666 696 64 465 391 659 284 448 207...
output:
2688 26 27 28 26 28 29 26 29 30 26 30 31 26 31 32 26 32 33 26 33 34 26 34 35 26 35 36 26 36 37 26 37 38 26 38 39 26 39 40 26 40 41 26 41 42 26 42 43 26 43 44 26 44 45 26 45 46 26 46 47 26 47 48 26 48 49 26 49 50 26 50 51 26 51 52 24 25 26 23 24 26 22 23 26 21 22 26 20 21 26 19 20 26 18 19 26 17 18 2...
result:
ok edges: 2688
Test #4:
score: 0
Accepted
time: 749ms
memory: 10608kb
input:
65536 200000 51949 58727 7943 43298 6290 7369 41493 53070 24229 36675 28087 49947 11703 48217 19923 24739 2144 59777 53830 56793 13509 37211 2300 38595 27415 42879 24616 48531 58341 63327 20628 38407 48616 60290 7450 61685 37010 47595 22164 42732 19181 29850 35383 43587 39257 44397 19340 45183 34523...
output:
368002 255 256 257 255 257 258 255 258 259 255 259 260 255 260 261 255 261 262 255 262 263 255 263 264 255 264 265 255 265 266 255 266 267 255 267 268 255 268 269 255 269 270 255 270 271 255 271 272 255 272 273 255 273 274 255 274 275 255 275 276 255 276 277 255 277 278 255 278 279 255 279 280 255 2...
result:
ok edges: 368002
Test #5:
score: 0
Accepted
time: 1ms
memory: 4084kb
input:
0 0
output:
0
result:
ok edges: 0
Test #6:
score: 0
Accepted
time: 0ms
memory: 3876kb
input:
1 1 0 1
output:
0 0 1
result:
ok edges: 0
Test #7:
score: 0
Accepted
time: 1ms
memory: 3880kb
input:
2 3 0 1 0 2 1 2
output:
0 0 1 0 1 2 1 2
result:
ok edges: 0
Test #8:
score: 0
Accepted
time: 1ms
memory: 4108kb
input:
3 6 0 1 0 2 0 3 1 2 1 3 2 3
output:
0 0 1 0 1 2 0 1 2 3 1 2 1 2 3 2 3
result:
ok edges: 0
Test #9:
score: 0
Accepted
time: 689ms
memory: 10440kb
input:
65535 200000 35006 46944 17075 57351 24605 50445 5938 60705 15221 40233 28599 38915 1132 35574 8555 31494 13644 35806 44940 55401 9503 59206 21011 26540 41156 62487 57510 64305 9254 25610 17301 47249 34083 49167 48018 64394 38855 62175 15464 22525 23728 60275 54028 63810 22711 53902 5984 48625 5838 ...
output:
368002 255 256 257 255 257 258 255 258 259 255 259 260 255 260 261 255 261 262 255 262 263 255 263 264 255 264 265 255 265 266 255 266 267 255 267 268 255 268 269 255 269 270 255 270 271 255 271 272 255 272 273 255 273 274 255 274 275 255 275 276 255 276 277 255 277 278 255 278 279 255 279 280 255 2...
result:
ok edges: 368002
Test #10:
score: 0
Accepted
time: 773ms
memory: 10052kb
input:
64800 200000 55124 62263 24992 39760 32262 37059 25987 42889 10413 64701 7223 43221 45810 63205 11437 29357 10814 52096 1154 36319 10730 54157 18473 26729 9152 23374 5426 12744 3502 37577 5559 37160 30503 62433 12426 47332 14933 62086 8781 21527 27180 53773 29658 46742 20592 61553 8337 27197 8024 38...
output:
357591 253 254 255 253 255 256 253 256 257 253 257 258 253 258 259 253 259 260 253 260 261 253 261 262 253 262 263 253 263 264 253 264 265 253 265 266 253 266 267 253 267 268 253 268 269 253 269 270 253 270 271 253 271 272 253 272 273 253 273 274 253 274 275 253 275 276 253 276 277 253 277 278 253 2...
result:
ok edges: 357591
Extra Test:
score: 0
Extra Test Passed