1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<bits/stdc++.h>
#include<unistd.h>
#include<string>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    multiset<int>V[3];
    int n;
    cin >> n;
    for(int i=0; i<n; i++)
    {
        int x,y,z;
        cin >> x >> y >> z;
        V[x].insert(z-y);
    }
    auto i=V[1].begin(),j=V[2].begin();
    int wynik=0;
    while(i != V[1].end() || j != V[2].end())
    {
        while(i != V[1].end() && ((*i) < (*j) || j == V[2].end()))
            i++;
        while(j != V[2].end() && ((*i) > (*j) || i == V[1].end()))
            j++;
        if(i != V[1].end() && j != V[2].end() && (*i) == (*j))
        {
            wynik+=min(V[1].count(*i), V[2].count(*i));
            int c=(*i);
            while(i != V[1].end() && ((*i) == c || j == V[2].end()))
                i++; 
            while(j != V[2].end() && (c == (*j) ||  i == V[1].end()))
                j++;       
        }
    }
    cout << wynik;
}