結果
問題 | No.728 ギブ and テイク |
ユーザー |
|
提出日時 | 2018-08-24 23:11:22 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,813 ms / 3,000 ms |
コード長 | 1,994 bytes |
コンパイル時間 | 2,526 ms |
コンパイル使用メモリ | 194,328 KB |
実行使用メモリ | 66,540 KB |
最終ジャッジ日時 | 2024-06-23 09:25:28 |
合計ジャッジ時間 | 15,964 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;//1-indexであることに注意template <typename T>struct BIT {int n;vector<T> data;BIT(int n) : n(n), data(n + 1, 0) {}T sum(int i) {T s = 0;while (i > 0) {s += data[i];i -= i & -i;}return s;}void add(int i, T x) {while (i <= n) {data[i] += x;i += i & -i;}}};//引数unzipには圧縮前のvectorを入れるint compress(vector<int>& unzip, map<int, int>& zip) {sort(unzip.begin(), unzip.end());unzip.erase(unique(unzip.begin(), unzip.end()), unzip.end());for (int i = 0; i < unzip.size(); i++) {zip[unzip[i]] = i;}return unzip.size();}int main() {cin.tie(0);ios::sync_with_stdio(false);int n;cin >> n;vector<int> a(n), L(n), R(n);for (int i = 0; i < n; i++) cin >> a[i];for (int i = 0; i < n; i++) {cin >> L[i] >> R[i];}using T = tuple<int, int, int>;vector<T> v;vector<int> unzip;for (int i = 0; i < n; i++) {v.emplace_back(L[i], 0, i);v.emplace_back(R[i], 1, i);unzip.emplace_back(a[i]);unzip.emplace_back(a[i] - L[i]);unzip.emplace_back(a[i] + R[i]);}map<int, int> zip;int sz = compress(unzip, zip);sort(v.begin(), v.end());BIT<int> bt(sz), bt2(sz);for (int i = 0; i < n; i++) {bt.add(zip[a[i]] + 1, 1);bt2.add(zip[a[i]] + 1, 1);}ll ans = 0;for (T& t : v) {int val, idx, flag;tie(val, flag, idx) = t;if (flag) {bt.add(zip[a[idx]] + 1, -1);ans += bt2.sum(zip[a[idx] + R[idx]] + 1) - bt2.sum(zip[a[idx]] + 1);} else {bt2.add(zip[a[idx]] + 1, -1);ans += bt.sum(zip[a[idx]]) - bt.sum(zip[a[idx] - L[idx]]);}}cout << ans << endl;return 0;}