結果
問題 | No.728 ギブ and テイク |
ユーザー | kyawa |
提出日時 | 2021-12-20 20:32:25 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,651 bytes |
コンパイル時間 | 2,907 ms |
コンパイル使用メモリ | 226,200 KB |
実行使用メモリ | 91,136 KB |
最終ジャッジ日時 | 2024-09-15 15:21:10 |
合計ジャッジ時間 | 13,417 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 4 ms
5,376 KB |
testcase_11 | AC | 3 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | AC | 52 ms
8,192 KB |
testcase_14 | AC | 89 ms
11,520 KB |
testcase_15 | AC | 35 ms
6,784 KB |
testcase_16 | AC | 79 ms
10,624 KB |
testcase_17 | AC | 73 ms
10,240 KB |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | AC | 870 ms
88,752 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 2 ms
5,376 KB |
testcase_32 | AC | 2 ms
5,376 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; class WaveletMatrix{ public: WaveletMatrix(vector<long> table) : table_origin(table){ long table_max = *max_element(table.begin(), table.end()); tablesize = table.size(); bitsize = 0; while(table_max){ bitsize++; table_max >>= 1; } assert(bitsize); matrix.resize(bitsize, vector<bool>(tablesize)); zero_count.resize(bitsize, vector<long>(tablesize + 1, 0)); for(long i = 0; i < bitsize; i++){ for(long j = 0; j < tablesize; j++){ matrix[i][j] = (table[j] >> (bitsize - i - 1)) & 1; if(not matrix[i][j]) zero_count[i][j + 1]++; zero_count[i][j + 1] += zero_count[i][j]; } stable_sort(table.begin(), table.end(), [&](auto a, auto b){return ((a >> (bitsize - i - 1)) & 1) < ((b >> (bitsize - i - 1)) & 1);}); } table_sorted = table; } long kth_min(long l, long r, long k/*0-indexed*/){ long L = l; long R = r; for(long level = 0; level < bitsize; level++){ auto [Lcount0, Lcount1] = counts(0, L, level); auto [count0, count1] = counts(L, R, level); if(k < count0){ L = Lcount0; R = L + count0; }else{ L = zero_count[level][tablesize] + Lcount1; R = L + count1; k -= count0; } } return table_sorted[L]; } long range_freq(long l, long r, long lower, long upper){ //count i s.t. l <= i < r and lower <= table[i] < upper return range_freq(l, r, upper) - range_freq(l, r, lower); } private: vector<vector<bool>> matrix; vector<vector<long>> zero_count;//count 0 in matrix[i][0, r) long tablesize; long bitsize; vector<long> table_origin; vector<long> table_sorted; pair<long,long> counts(long l, long r, long level){ //{0の個数, 1の個数} in matrix[level][l, r)を返す return {zero_count[level][r] - zero_count[level][l], (r - zero_count[level][r]) - (l - zero_count[level][l])}; } long range_freq(long l, long r, long upper){ //count i s.t. l <= i < r and table[i] < upper if(upper >= (1 << bitsize)) return r - l; if(upper < 0) return 0; long res = 0; long L = l; long R = r; for(long level = 0; level < bitsize; level++){ auto [Lcount0, Lcount1] = counts(0, L, level); auto [count0, count1] = counts(L, R, level); if(((upper >> (bitsize - level - 1)) & 1) == 0){ //matrix[level][i] = 1 なら 必ず upper < table[i] L = Lcount0; R = L + count0; }else{ //matrix[level][i] = 0 なら 必ず table[i] < upper res += count0; L = zero_count[level][tablesize] + Lcount1; R = L + count1; } } return res; } }; int main(){ long res = 0; long N; cin >> N; vector<long> A(N); for(long i = 0; i < N; i++) cin >> A[i]; for(auto &a : A) a += 1000000000; vector<long> AminusL(N), AplusR(N); for(long i = 0; i < N; i++){ long L, R; cin >> L >> R; AminusL[i] = A[i] - L; AplusR[i] = A[i] + R; } WaveletMatrix wm(AminusL); for(long i = 0; i < N; i++){ auto d = distance(A.begin(), upper_bound(A.begin(), A.end(), AplusR[i])); res += wm.range_freq(i + 1, d, LONG_MIN, A[i] + 1); } cout << res << '\n'; }