結果
問題 | No.728 ギブ and テイク |
ユーザー | kyawa |
提出日時 | 2021-12-31 16:22:04 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,497 ms / 3,000 ms |
コード長 | 3,645 bytes |
コンパイル時間 | 3,194 ms |
コンパイル使用メモリ | 227,644 KB |
実行使用メモリ | 91,140 KB |
最終ジャッジ日時 | 2024-10-08 14:49:46 |
合計ジャッジ時間 | 17,423 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 2 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 2 ms
5,248 KB |
testcase_12 | AC | 3 ms
5,248 KB |
testcase_13 | AC | 53 ms
8,064 KB |
testcase_14 | AC | 94 ms
11,520 KB |
testcase_15 | AC | 36 ms
6,784 KB |
testcase_16 | AC | 80 ms
10,624 KB |
testcase_17 | AC | 74 ms
9,984 KB |
testcase_18 | AC | 1,049 ms
65,408 KB |
testcase_19 | AC | 1,116 ms
69,504 KB |
testcase_20 | AC | 1,327 ms
80,640 KB |
testcase_21 | AC | 1,202 ms
72,992 KB |
testcase_22 | AC | 446 ms
30,976 KB |
testcase_23 | AC | 277 ms
21,248 KB |
testcase_24 | AC | 874 ms
55,296 KB |
testcase_25 | AC | 869 ms
54,784 KB |
testcase_26 | AC | 430 ms
30,080 KB |
testcase_27 | AC | 1,497 ms
91,140 KB |
testcase_28 | AC | 907 ms
88,764 KB |
testcase_29 | AC | 2 ms
5,248 KB |
testcase_30 | AC | 2 ms
5,248 KB |
testcase_31 | AC | 2 ms
5,248 KB |
testcase_32 | AC | 2 ms
5,248 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 < 0) return 0; if(upper >= (1L << bitsize)) return r - l; 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, 0, A[i] + 1); } cout << res << '\n'; }