給定兩個 非嚴格遞增 的整數陣列 $L$ 與 $R$,請將它們合併為一個 非嚴格遞增 的新陣列 $\text{arr}$。
你需要實作一個函式 merge
,將合併後的結果放入陣列 $\text{arr}$ 中。
merge
函式應在 線性時間 $O(n)$ 內完成(其中 $n = n_1 + n_2$)。std::sort
等一般排序函式將會發生 TLE
。評測時,你所實作的 merge
函式會被放入以下程式碼中進行測試:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void merge(vector<int> &arr, vector<int> &L, vector<int> &R){
// TODO
}
int main(){
// input
int n1, n2;
int x1, a1, b1, x2, a2, b2;
const int MOD = 1e9;
cin >> n1 >> n2;
cin >> x1 >> a1 >> b1 >> x2 >> a2 >> b2;
vector<int> L(n1), R(n2);
L[0] = x1;
for (int i = 1; i < n1; i++) {
L[i] = (1LL * L[i-1] * a1 + b1) % MOD + 1;
}
sort(L.begin(), L.end());
R[0] = x2;
for (int i = 1; i < n2; i++) {
R[i] = (1LL * R[i-1] * a2 + b2) % MOD + 1;
}
sort(R.begin(), R.end());
// process
vector<int> arr(n1 + n2);
merge(arr, L, R);
// output
int output = 0;
for (int i = 0; i < n1 + n2; i++) {
output ^= (i + arr[i]);
}
cout << output << "\n";
return 0;
}
請上傳你實作的 merge 函式,並包含需要使用的標頭檔與命名空間,例如:
#include <...>
using namespace ...;
void merge(vector<int> &arr, vector<int> &L, vector<int> &R){
// TODO
}
第一行輸入兩個正整數 $n_1, n_2$,分別代表 $L$ 與 $R$ 的長度。
- 第二行輸入六個正整數 $x_1, a_1, b_1, x_2, a_2, b_2$,作為生成陣列 $L$ 與 $R$ 的參數。
Constraints
- $1 \leq n_1, n_2 \leq 5 \times 10 ^ 6$
- $1 \leq x_1, a_1, b_1, x_2, a_2, b_2 \leq 5 \times 10 ^ 6$
- 傳入的 $L, R$ 均為非嚴格遞增的陣列(已排序)。
輸出一個正整數,代表下列運算結果:
$$ \bigoplus_{i=0} ^ {n_1 + n_2 - 1} \left( i + arr[i] \right) $$
其中 $\bigoplus$ 表示 bitwise XOR 運算。
範例測資中,輸入所生成的 $L$ 與 $R$ 分別如下:
1 6 16 36 76
2 11 38 119 362 1091
合併後的 $\text{arr}$ 應為:
1 2 6 11 16 36 38 76 119 362 1091
No. | Testdata Range | Score |
---|---|---|
1 | 0~9 | 100 |