Cerinta completa

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has favorite shots. Each shot has a particular range.
The range of the shot is from i to i. That means his favorite shot can be anywhere in this range. Each player on the opposite team
can field only in a particular range. Player can field from i to i. You are given the favorite shots of Mr. X and the range of players.

represents the strength of each player i.e. the number of shots player can stop.
Your task is to find:

.

Game Rules: A player can stop the shot if the range overlaps with the player’s fielding range.

For more clarity about overlapping, study the following figure:

Input Format

The first line consists of two space separated integers, and .
Each of the next lines contains two space separated integers. The line contains and .
Each of the next lines contains two integers. The line contains integers and .

Output Format

You need to print the sum of the strengths of all the players: .

Constraints:


Sample Input

4 4                
1 2 
2 3
4 5
6 7
1 5
2 3
4 7
5 7   

Sample Output

9

Explanation

Player 1 can stop the 1st, 2nd and 3rd shot so the strength is .
Player 2 can stop the 1st and 2nd shot so the strength is .
Player 3 can stop the 3rd and 4th shot so the strength is .
Player 4 can stop the 3rd and 4th shot so the strength is .

The sum of the strengths of all the players is .


Limbajul de programare folosit: cpp14

Cod:

#include <bits/stdc++.h>
using namespace std;
#define fo(i,n) for(int i=0;i<(n);i++)
#define MOD 1000000007
#define PB push_back
typedef long long ll;

int N, M;
vector<int> p, m;
ll ans;

int main () {
        scanf("%d %d", &N, &M);
        fo(i, N) {
                int a, b; scanf("%d %d", &a, &b);
                p.PB(a), m.PB(b+1);
        }
        sort(p.begin(), p.end()), sort(m.begin(), m.end());
        fo(i, M) {
                int a, b; scanf("%d %d", &a, &b);
                ans += N
                        - int(upper_bound(m.begin(), m.end(), a) - m.begin())
                        - int(p.end() - upper_bound(p.begin(), p.end(), b));
        }
        printf("%lld\n", ans);
        return 0;
}

Scor obtinut: 1.0

Submission ID: 464653341

Link challenge: https://www.hackerrank.com/challenges/x-and-his-shots/problem

Mr. X and His Shots