Submission #68415
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <cmath> using namespace std; struct Mo { private : int n; vector<pair< int , int > > lr; public : Mo( int n) : n(n) {} /* [l, r) */ void insert( int l, int r){ lr.emplace_back(l, r); } template < typename A, typename E, typename O> void build( const A &add, const E &erase, const O &out){ int q = ( int ) lr.size(); int bs = n / min< int >(n, sqrt (q)); vector< int > ord(q); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&]( int a, int b){ int ablock = lr[a].first / bs, bblock = lr[b].first / bs; if (ablock != bblock) return (ablock < bblock); return (lr[a].second < lr[b].second); }); int l = 0, r = 0; for (auto idx : ord){ while (l > lr[idx].first) add(--l); while (r < lr[idx].second) add(r++); while (l < lr[idx].first) erase(l++); while (r > lr[idx].second) erase(--r); out(idx); } } }; template < typename T > struct BinaryIndexedTree { private : vector< T > data; public : BinaryIndexedTree() = default ; explicit BinaryIndexedTree( size_t sz) : data(sz + 1, 0) {} void add( int k, const T &x) { for (++k; k < ( int ) data.size(); k += k & -k) data[k] += x; } T fold( int r) const { T ret = T(); for (; r > 0; r -= r & -r) ret += data[r]; return ret; } T fold( int l, int r) const { return fold(r) - fold(l); } }; int main() { cin.tie(nullptr); ios_base::sync_with_stdio( false ); int n, m, d; cin >> n >> m >> d; vector< int > c(n), cc(n), cr(n), cl(n), zip; for ( int i = 0; i < n; ++i) { cin >> c[i]; zip.emplace_back(c[i]); zip.emplace_back(c[i] - d); zip.emplace_back(c[i] + d); } sort(zip.begin(), zip.end()); zip.erase(unique(zip.begin(), zip.end()), zip.end()); for ( int i = 0; i < n; ++i) { cc[i] = lower_bound(zip.begin(), zip.end(), c[i]) - zip.begin(); cl[i] = lower_bound(zip.begin(), zip.end(), c[i] - d) - zip.begin(); cr[i] = lower_bound(zip.begin(), zip.end(), c[i] + d) - zip.begin(); } Mo mo(n); for ( int i = 0; i < m; ++i) { int l, r; cin >> l >> r; --l, --r; mo.insert(l, r + 1); } BinaryIndexedTree< int > bit(zip.size()); vector< long long > res(m); long long sum = 0; auto add = [&]( int k) { sum += bit.fold(cl[k], cr[k] + 1); bit.add(cc[k], 1); }; auto erase = [&]( int k) { bit.add(cc[k], -1); sum -= bit.fold(cl[k], cr[k] + 1); }; auto out = [&]( int k) { res[k] = sum; }; mo.build(add, erase, out); for (auto &e : res) { cout << e << '\n' ; } return (0); } |
ステータス
項目 | データ |
---|---|
問題 | 1524 - Artist |
ユーザー名 | ei1903 |
投稿日時 | 2021-09-12 18:09:35 |
言語 | C++17 |
状態 | Accepted |
得点 | 6 |
ソースコード長 | 3034 Byte |
最大実行時間 | 1772 ms |
最大メモリ使用量 | 33628 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 6 / 6 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in01.txt | AC | 27 ms | 604 KB |
1
|
in02.txt | AC | 24 ms | 648 KB |
1
|
in03.txt | AC | 29 ms | 440 KB |
1
|
in04.txt | AC | 340 ms | 3044 KB |
1
|
in05.txt | AC | 185 ms | 4016 KB |
1
|
in06.txt | AC | 658 ms | 4424 KB |
1
|
in07.txt | AC | 307 ms | 3616 KB |
1
|
in08.txt | AC | 1264 ms | 6944 KB |
1
|
in09.txt | AC | 820 ms | 6496 KB |
1
|
in10.txt | AC | 112 ms | 3708 KB |
1
|
in11.txt | AC | 1712 ms | 9684 KB |
1
|
in12.txt | AC | 1686 ms | 10540 KB |
1
|
in13.txt | AC | 1725 ms | 11396 KB |
1
|
in14.txt | AC | 1697 ms | 12252 KB |
1
|
in15.txt | AC | 1701 ms | 13108 KB |
1
|
in16.txt | AC | 1772 ms | 13964 KB |
1
|
in17.txt | AC | 1753 ms | 14948 KB |
1
|
in18.txt | AC | 1683 ms | 15936 KB |
1
|
in19.txt | AC | 1717 ms | 16796 KB |
1
|
in20.txt | AC | 1698 ms | 17780 KB |
1
|
in21.txt | AC | 144 ms | 18640 KB |
1
|
in22.txt | AC | 105 ms | 19624 KB |
1
|
in23.txt | AC | 1231 ms | 18940 KB |
1
|
in24.txt | AC | 1247 ms | 19172 KB |
1
|
in25.txt | AC | 1250 ms | 19528 KB |
1
|
in26.txt | AC | 1746 ms | 21164 KB |
1
|
in27.txt | AC | 1698 ms | 22144 KB |
1
|
in28.txt | AC | 1750 ms | 23004 KB |
1
|
in29.txt | AC | 1707 ms | 23860 KB |
1
|
in30.txt | AC | 1743 ms | 24716 KB |
1
|
in31.txt | AC | 1748 ms | 25568 KB |
1
|
in32.txt | AC | 1717 ms | 26680 KB |
1
|
in33.txt | AC | 1723 ms | 27540 KB |
1
|
in34.txt | AC | 1519 ms | 27500 KB |
1
|
in35.txt | AC | 1498 ms | 28492 KB |
1
|
in36.txt | AC | 1704 ms | 30120 KB |
1
|
in37.txt | AC | 1762 ms | 30976 KB |
1
|
in38.txt | AC | 124 ms | 31060 KB |
1
|
in39.txt | AC | 122 ms | 31408 KB |
1
|
in40.txt | AC | 1236 ms | 30856 KB |
1
|
in41.txt | AC | 1238 ms | 30952 KB |
1
|
in42.txt | AC | 1185 ms | 31440 KB |
1
|
in43.txt | AC | 1184 ms | 31924 KB |
1
|
in44.txt | AC | 44 ms | 28700 KB |
1
|
in45.txt | AC | 45 ms | 28984 KB |
1
|
in46.txt | AC | 30 ms | 28124 KB |
1
|
in47.txt | AC | 30 ms | 28060 KB |
1
|
in48.txt | AC | 18 ms | 27196 KB |
1
|
in49.txt | AC | 27 ms | 27280 KB |
1
|
in50.txt | AC | 28 ms | 28260 KB |
1
|
in51.txt | AC | 60 ms | 29076 KB |
1
|
in52.txt | AC | 22 ms | 27640 KB |
1
|
in53.txt | AC | 63 ms | 30876 KB |
1
|
in54.txt | AC | 28 ms | 28984 KB |
1
|
in55.txt | AC | 281 ms | 33628 KB |
1
|
in56.txt | AC | 100 ms | 31428 KB |
1
|
sample.txt | AC | 18 ms | 29376 KB |
1
|