CRATE - Coder Rating

Tác giả: hieult

Ngôn ngữ: C++

#include<cstdio>
#include<cmath>
#include<math.h>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<iterator>
#include<iostream>
#include<cctype>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<list>
#define fi first
#define se second
#define PB push_back
#define MP make_pair
#define ep 0.00001
#define maxn 300011
#define oo 1111111111
#define mod 1000000007
#define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++)
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define FOR(i, a, b) for(int i = int(a); i <= int(b); i++)
#define FORD(i, a, b) for(int i = int(a); i >= int(b); i--)
//#include <conio.h>
//#define g 9.81

const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0;int ptr = 0;
char gc() { if (rsz <= 0) { ptr = 0; rsz = fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; }
void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); }
int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; }
template<class T> bool gi(T &v) {
	v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc();
    while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true;
}

double const PI=4*atan(1.0);

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;
typedef vector<II> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;

void OPEN(){
    freopen("A.in","r",stdin);
    freopen("A.out","w",stdout);
}

struct Point{
    int d1, d2, id;
    Point(){};
    Point(int _d1, int _d2, int _id){
        d1 = _d1;
        d2 = _d2;
        id = _id;
    }
    bool operator <(Point P)const{
        if(d1 != P.d1) return d1 < P.d1;
        return d2 < P.d2;
    }
};

Point A[maxn];
int n, x, y, f[100011] = {0}, res[maxn];

void update(int x){
    while(x <= 100001){
        f[x]++;
        x += x & -x;
    }
}

int get(int x){
    int ret = 0;
    while(x > 0){
        ret += f[x];
        x -= x & -x;
    }
    return ret;
}

int main(){
    //OPEN();
    scanf("%d", &n);
    rep(i, n){
        scanf("%d %d", &x, &y);
        x++, y++;
        A[i] = Point(x, y, i);
    }
    sort(A, A + n);
    rep(i, n){
        if(i > 0 && A[i].d1 == A[i - 1].d1 && A[i].d2 == A[i - 1].d2){
            res[A[i].id] = res[A[i - 1].id];
        }
        else res[A[i].id] = get(A[i].d2);
        update(A[i].d2);
    }
    rep(i, n) printf("%d\n",res[i]);
}

Download