Algorithm


Problem link- https://www.spoj.com/problems/RATING/

RATING - Coder Ratings

 

Some of the more elite (and not-so-elite) coders around take part in a certain unnamed programming contest. In said contest, there are multiple types of competitions. Here, we consider the Open and High School competition types. For each type, each competitor receives a rating, an integer between 1 and 100000, inclusive. A coder's rating is based upon his or her level of performance in matches and is calculated using a complicated formula which, thankfully, you will not be asked to implement.

Although the Open and High School ratings for a coder who has participated in both competition types lately are usually close, this is not always the case. In particular, High School matches are more about speed, since many coders are able to solve all the problems, whereas Open matches require more thinking and there is a steeper curve in terms of problem difficulty.

Problem Statement
You are given N coders (1 ≤ N ≤ 300000), conveniently numbered from 1 to N. Each of these coders participates in both High School and Open matches. For each coder, you are also given an Open rating Ai and a High School rating Hi. Coder i is said to be better than coder j if and only if both of coder i's ratings are greater than or equal to coder j's corresponding ratings, with at least one being greater. For each coder i, determine how many coders coder i is better than.

Input Format
On the first line of input is a single integer N, as described above.
N lines then follow. Line i+1 contains two space-separated integers, Ai and Hi.

Output Format
Line i should contain the number of coders that coder i is better than.

Sample Input

8
1798 1832
862 700
1075 1089
1568 1557
2575 1984
1033 950
1656 1649
1014 1473

 

Sample Output

6
0
2
4
7
1
5
1

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 1e5+5;
int BIT[MAXN];
void update(int idx, int val){
	while(idx <= 100000){
		BIT[idx] += val;
		idx += idx&-idx;
	}
}

int query(int idx){
	int sum = 0;
	while(idx > 0){
		sum += BIT[idx];
		idx -= idx&-idx;
	}
	return sum;
}
int answers[300005];
int main(){
    io;
    int n;
    cin >> n;
    vector<pair<pair<int, int>, int> > table;
    for(int i= 0;i < n; i++){
    	int a, b;
    	cin >> a >> b;
    	table.push_back({{a, b}, i});
    }
    sort(table.begin(), table.end());
    for(int i = 0;i < table.size();){
    	int first_val = table[i].first.first;
    	int second_val = table[i].first.second;
    	int init_idx = i;
    	while(i < table.size() && table[i].first.first == first_val && table[i].first.second == second_val){
     		answers[table[i].second] = query(table[i].first.second);
    		i++;
    	}
    	update(table[i-1].first.second, i-init_idx);
    }
    for(int i = 0;i < table.size(); i++){
    	cout << answers[i] << endl;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
8
1798 1832
862 700
1075 1089
1568 1557
2575 1984
1033 950
1656 1649
1014 1473

Output

x
+
cmd
6
0
2
4
7
1
5
1
Advertisements

Demonstration


SPOJ Solution-Coder Ratings-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python