Codeforces 924D Contact ATC (看題解)
阿新 • • 發佈:2019-03-07
gin 交集 rhs turn vector ble ini lld can
Contact ATC
我跑去列方程, 然後就gg了。。。
我們計每個飛機最早到達時間為L[ i ], 最晚到達時間為R[ i ],
對於面對面飛行的一對飛機, 只要他們的時間有交集則必定滿足條件。
對於相同方向飛行的飛機, 只有其中一個的時間包含另一個的時間才滿足條件。
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #definePII pair<int, int> #define SZ(x) ((int)x.size()) #define ull unsigned long long using namespace std; const int N = 2e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); struct Bit {int a[N]; void init() { memset(a, 0, sizeof(a)); } void modify(int x, int v) { for(int i = x; i < N; i += i & -i) a[i] += v; } int sum(int x) { int ans = 0; for(int i = x; i; i -= i & -i) ans += a[i];return ans; } int query(int L, int R) { if(L > R) return 0; return sum(R) - sum(L - 1); } }; struct Node { Node(LL a, LL b) : a(a), b(b) {} bool operator < (const Node& rhs) const { return a * rhs.b < rhs.a * b; } bool operator == (const Node& rhs) const { return a * rhs.b == rhs.a * b; } void print() { printf("%.5f ", 1.0 * a / b); } LL a, b; }; int n, w, x[N], v[N]; LL ans = 0; vector<PII> vc[2]; vector<Node> hs; Bit bit; bool cmp(PII& a, PII& b) { if(a.fi == b.fi) return a.se < b.se; return a.fi > b.fi; } LL solve(vector<PII>& vc) { bit.init(); LL ans = 0; sort(vc.begin(), vc.end(), cmp); for(int i = 0; i < SZ(vc); i++) { ans += bit.sum(vc[i].se); bit.modify(vc[i].se, 1); } return ans; } int main() { scanf("%d%d", &n, &w); for(int i = 1; i <= n; i++) { scanf("%d%d", &x[i], &v[i]); if(x[i] < 0) { hs.push_back(Node(-x[i], v[i] + w)); hs.push_back(Node(-x[i], v[i] - w)); } else { hs.push_back(Node(x[i], w - v[i])); hs.push_back(Node(x[i], -w - v[i])); } } sort(hs.begin(), hs.end()); hs.erase(unique(hs.begin(), hs.end()), hs.end()); for(int i = 1; i <= n; i++) { if(x[i] < 0) { int L = lower_bound(hs.begin(), hs.end(), Node(-x[i], v[i] + w)) - hs.begin() + 1; int R = lower_bound(hs.begin(), hs.end(), Node(-x[i], v[i] - w)) - hs.begin() + 1; vc[0].push_back(mk(L, R)); } else { int L = lower_bound(hs.begin(), hs.end(), Node(x[i], w - v[i])) - hs.begin() + 1; int R = lower_bound(hs.begin(), hs.end(), Node(x[i], -w - v[i])) - hs.begin() + 1; vc[1].push_back(mk(L, R)); } } ans += solve(vc[0]); ans += solve(vc[1]); ans += 1ll * SZ(vc[0]) * SZ(vc[1]); bit.init(); for(auto& t : vc[1]) bit.modify(t.se, 1); for(auto& t : vc[0]) ans -= bit.sum(t.fi - 1); bit.init(); for(auto& t : vc[1]) bit.modify(t.fi, 1); for(auto& t : vc[0]) ans -= bit.query(t.se + 1, N - 1); printf("%lld\n", ans); return 0; } /* */
Codeforces 924D Contact ATC (看題解)