結果
問題 | No.728 ギブ and テイク |
ユーザー |
|
提出日時 | 2019-05-16 19:50:22 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 263 ms / 3,000 ms |
コード長 | 2,261 bytes |
コンパイル時間 | 2,140 ms |
コンパイル使用メモリ | 182,136 KB |
実行使用メモリ | 24,432 KB |
最終ジャッジ日時 | 2024-09-17 05:39:31 |
合計ジャッジ時間 | 7,895 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include<bits/stdc++.h>using namespace std;struct query{int x;int type; // 0: insert, 1: countint y;int idx;query(int _x, int _type, int _y, int _idx): x(_x), type(_type), y(_y), idx(_idx){}bool operator<(const query & rhs){if (x < rhs.x){return true;}else if (x > rhs.x){return false;}else if (type < rhs.type){return true;}else if (type > rhs.type){return false;}else{return y < rhs.y;}}};int compress_y(vector<query> & que){vector<pair<int, int>> ys(que.size());for (int i = 0; i < que.size(); ++i) {ys[i].first = que[i].y;ys[i].second = i;}sort(ys.begin(), ys.end());int pos = 0;que[ys[0].second].y = pos;for (int i = 1; i < que.size(); ++i){if (ys[i].first > ys[i - 1].first) ++pos;que[ys[i].second].y = pos;}return pos;}struct BIT{vector<int> bit;BIT(int n) : bit(n, 0){}int sum(int j){int s = 0;for (; j >= 0; j = (j & (j + 1)) - 1) s += bit[j];return s;}void add(int k, int a) {for (; k < bit.size(); k |= k + 1) bit[k] += a;}};int main(){cin.tie(0);ios::sync_with_stdio(false);int N;cin >> N;vector<int> As(N, 0);for (auto & a : As) cin >> a;vector<int> Ls(N, 0);vector<int> Rs(N, 0);for (int i = 0; i < N; ++i){cin >> Ls[i] >> Rs[i];}vector<query> que;for (int i = 0; i < N; ++i){que.emplace_back(As[i], 0, As[i] - Ls[i], i);que.emplace_back(As[i] + Rs[i], 1, As[i], i);}sort(que.begin(), que.end());int max_y = compress_y(que);BIT bit(max_y + 1);vector<pair<int, int>> aidx(N);for (int i = 0; i < N; ++i){aidx[i].first = As[i];aidx[i].second = i;}sort(aidx.begin(), aidx.end());vector<int> cum(N, 0);for (int i = 0; i < N; ++i){cum[aidx[i].second] = i + 1;}long long ans = 0;for (auto & q : que){if (q.type == 0){bit.add(q.y, 1);}else{ans += bit.sum(q.y) - cum[q.idx];}}cout << ans << endl;return 0;}