Submission #37961704
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> vec;
void f(int L, int R) {
if (L == R) {
vec.push_back(make_pair(L, R));
return;
}
int m = (L + R) / 2;
for (int i = L; i <= m; i++) vec.push_back(make_pair(i, m));
for (int i = m + 1; i <= R; i++) vec.push_back(make_pair(m + 1, i));
f(L, m);
f(m + 1, R);
return;
}
int main() {
// Phase 1
int n;
cin >> n;
f(1, n);
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
int m = vec.size();
cout << m << endl;
for (auto i : vec) {
cout << i.first << " " << i.second << endl;
}
// preprocess
vector<vector<pair<int, int>>> left2right(n + 1), right2left(n + 1);
// left2right[i]: Phase 1で準備した区間のうち、左端の数がiであるようなものの(右端の数, 添字)のペアの配列
// right2left[i]: 右端の数がiであるようなものの(左端の数, 添字)のペアの配列
for (int i = 0; i < m; i++) {
left2right[vec[i].first].push_back(make_pair(vec[i].second, i + 1));
right2left[vec[i].second].push_back(make_pair(vec[i].first, i + 1));
}
for (int i = 1; i <= n; i++) {
sort(left2right[i].begin(), left2right[i].end());
sort(right2left[i].begin(), right2left[i].end());
}
// Phase 2
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
if (l == r) {
cout << left2right[l][0].second << " " << left2right[l][0].second << endl;
continue;
}
int itr = 0;
for (auto i : left2right[l]) {
while (right2left[r][itr].first <= i.first) itr++;
if (i.first + 1 == right2left[r][itr].first) {
cout << i.second << " " << right2left[r][itr].second << endl;
break;
}
}
}
}
Submission Info
| Submission Time | |
|---|---|
| Task | F - Union of Two Sets |
| User | hanyu |
| Language | C++ (GCC 9.2.1) |
| Score | 500 |
| Code Size | 1842 Byte |
| Status | AC |
| Exec Time | 803 ms |
| Memory | 7360 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 500 / 500 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, example0.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 643 ms | 4936 KiB |
| 001.txt | AC | 760 ms | 6980 KiB |
| 002.txt | AC | 708 ms | 6752 KiB |
| 003.txt | AC | 789 ms | 7144 KiB |
| 004.txt | AC | 745 ms | 6944 KiB |
| 005.txt | AC | 803 ms | 7276 KiB |
| 006.txt | AC | 680 ms | 5636 KiB |
| 007.txt | AC | 718 ms | 7016 KiB |
| 008.txt | AC | 770 ms | 7276 KiB |
| 009.txt | AC | 695 ms | 6948 KiB |
| 010.txt | AC | 784 ms | 7360 KiB |
| 011.txt | AC | 348 ms | 5748 KiB |
| 012.txt | AC | 345 ms | 5836 KiB |
| 013.txt | AC | 411 ms | 5984 KiB |
| 014.txt | AC | 232 ms | 4948 KiB |
| 015.txt | AC | 422 ms | 6084 KiB |
| 016.txt | AC | 749 ms | 6980 KiB |
| 017.txt | AC | 780 ms | 7220 KiB |
| 018.txt | AC | 745 ms | 6852 KiB |
| 019.txt | AC | 760 ms | 7244 KiB |
| 020.txt | AC | 673 ms | 5536 KiB |
| 021.txt | AC | 766 ms | 7256 KiB |
| example0.txt | AC | 8 ms | 3936 KiB |