#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int n, m; cin>>n>>m; priority_queue<tuple<int, int,int> > que; while(m--){ int p,q,k; cin>>p>>q>>k; que.push(make_tuple(0-p,0,k)); que.push(make_tuple(0-q-1,-1,k)); } int kol[3]; kol[0]=0;kol[1]=0;kol[2]=0; int wyn=0; while(!que.empty()){ tuple<int,int,int> a=que.top(); int i=get<0>(a); int k = get<2>(a); k--; int t=get<1>(a); que.pop(); if(t==0){ kol[k]++; }else{ kol[k]--; } if(kol[0]>0&&kol[1]>0&&kol[2]==0){ wyn+=get<0>(que.top())-i; } } cout<<0-wyn<<'\n'; }
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 | #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int n, m; cin>>n>>m; priority_queue<tuple<int, int,int> > que; while(m--){ int p,q,k; cin>>p>>q>>k; que.push(make_tuple(0-p,0,k)); que.push(make_tuple(0-q-1,-1,k)); } int kol[3]; kol[0]=0;kol[1]=0;kol[2]=0; int wyn=0; while(!que.empty()){ tuple<int,int,int> a=que.top(); int i=get<0>(a); int k = get<2>(a); k--; int t=get<1>(a); que.pop(); if(t==0){ kol[k]++; }else{ kol[k]--; } if(kol[0]>0&&kol[1]>0&&kol[2]==0){ wyn+=get<0>(que.top())-i; } } cout<<0-wyn<<'\n'; } |