Codeforces 924D Contact ATC (看题解)

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>
#define PII 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;
}

/*
*/

猜你喜欢

转载自www.cnblogs.com/CJLHY/p/10486254.html