thinking process
use struct to store order info.
struct ty{
int l, r, c;
}a[100010];
compare operator defination:
a.c == b.c ? a.l < b.l : a.c < b.c
the reason why we do this is simple. Given (l,r) and let u solve the individual item value, it's inevitable to be striked that merge intervals and difference. when the code is the same, and the ranges are crossed, we merge, if not we make difference. On the contrary, we directly merge and update the l,r,c.
ok, everything is ok. let's code!
#include<stdio.h>
#include<algorithm>
#include<math.h>
struct ty{
int l, r, c;
}a[100010];
int delta[100010];
int kind[100010];
bool cmp(ty a, ty b){
if(a.c != b.c) return a.c < b.c;
return a.l < b.l;
}
using namespace std;
int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0;i < m;i ++) {
scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].c);
}
sort(a, a + m, cmp);
int l = a[0].l, r = a[0].r, c = a[0].c;
for(int i = 1;i < m;i ++) {
// order with the same code
if(a[i].c == c) {
// merge intervals
if(a[i].l <= r) {
r = max(r, a[i].r);
}
// not
else {
++ delta[l], -- delta[r + 1];
l = a[i].l, r = a[i].r, c = a[i].c;
}
}
// not
else {
++ delta[l], -- delta[r + 1];
l = a[i].l, r = a[i].r, c = a[i].c;
}
}
++ delta[l], -- delta[r + 1];
for(int i = 1; i <= n ;i ++ ) kind[i] = kind[i - 1] + delta[i];
int max = 0, pos = -1;
for(int i = 0;i <= n;i ++ )
if(max < kind[i])
max = kind[i], pos = i;
printf("%d", pos);
}