NUCLEAR - Hai nhà máy điện nguyên tử

Tác giả: RR

Ngôn ngữ: C++

#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair

#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;

//Buffer reading
int INP,AM,REACHEOF;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
    if(!*inp) { \
        if (REACHEOF) return 0;\
        memset(BUF,0,sizeof BUF);\
        int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
        if (inpzzz != BUFSIZE) REACHEOF = true;\
        inp=BUF; \
    } \
    INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
    AM=0;\
    GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
    if (INP=='-') {AM=1;GETCHAR(INP);} \
    j=INP-'0'; GETCHAR(INP); \
    while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
    if (AM) j=-j;\
}
//End of buffer reading

const long double PI = acos((long double) -1.0);
const int MN = 1000111;

struct Point {
    long long x, y;

    Point(long long x = 0, long long y = 0) : x(x), y(y) {}

    Point operator - (Point a) { return Point(x-a.x, y-a.y); }

    long long sqlen() { return x*x + y*y; }
    long long len() { return (long long) ceil(sqrt(sqlen()) - 1e-6); }
} a[MN], P, Q;

int d1[MN], d2[MN], n, q, res[MN], bit[MN];

#define _(x) ((x) & (-(x)))

void update(int u) {
    ++bit[u];
    u += _(u);
    if (u <= 1000000) update(u);
}

int get(int u) {
    if (u <= 0) return 0;
    return bit[u] + get(u - _(u));
}

struct Query {
    int r1, r2;
    int id;
} queries[MN];

bool operator < (const Query &a, const Query &b) {
    if (a.r1 != b.r1) return a.r1 < b.r1;
    if (a.r2 != b.r2) return a.r2 < b.r2;
    return a.id < b.id;
}

void init() {
    FOR(i,1,n) {
        d1[i] = (P - a[i]).len();
        d2[i] = (Q - a[i]).len();
    }
}

int main() {
    scanf("%d", &n);
    int x, y;
    FOR(i,1,n) {
        scanf("%d%d", &x, &y);
        a[i] = Point(x, y);
    }
    scanf("%d%d", &x, &y); P = Point(x, y);
    scanf("%d%d", &x, &y); Q = Point(x, y);
    init();

    scanf("%d", &q);
    FOR(i,1,q) {
        scanf("%d%d", &queries[i].r1, &queries[i].r2);
        queries[i].id = i;
    }
    int saveq = q;
    FOR(i,1,n) {
        ++q;
        queries[q].r1 = d1[i];
        queries[q].r2 = d2[i];
        queries[q].id = 0;
    }

    sort(d1+1, d1+n+1);
    sort(d2+1, d2+n+1);
    d1[n+1] = d2[n+1] = 1000111000;

    sort(queries + 1, queries + q + 1);

    FOR(i,1,q) {
        if (queries[i].id == 0) {
            update(queries[i].r2);
        }
        else {
            res[queries[i].id] =
                    upper_bound(d1+1, d1+n+2, queries[i].r1) - d1 - 1
                    + upper_bound(d2+1, d2+n+2, queries[i].r2) - d2 - 1
                    - get(queries[i].r2);
        }
    }
    FOR(i,1,saveq) printf("%d\n", res[i]);
    return 0;
}

Download