#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int> > op[3];
int main()
{
int n,m,a,b,c,i1=0,i2=0,i3=0,w=0;
cin>>n>>m;
for(int i=0; i<m; i++){
cin>>a>>b>>c;
op[c-1].push_back({a,b});
}
sort(op[0].begin(), op[0].end());
sort(op[1].begin(), op[1].end());
sort(op[2].begin(), op[2].end());
op[0].push_back({0,0});
op[1].push_back({0,0});
op[2].push_back({0,0});
for(int i=1; i<=n; i++){
while(i>op[0][i1].second&&i1<op[0].size()-1){
i1++;
}
while(i>op[1][i2].second&&i2<op[1].size()-1){
i2++;
}
while(i>op[2][i1].second&&i3<op[2].size()-1){
i3++;
}
if(i1==op[0].size()-1||i2==op[1].size()-1){
break;
}
if(i>=op[0][i1].first&&i>=op[1][i2].first&&i3==op[2].size()-1){
w++;
}else if(i>=op[0][i1].first&&i>=op[1][i2].first&&i<op[2][i3].first){
w++;
}
}
cout<<w;
}
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 41 | #include <bits/stdc++.h> using namespace std; vector<pair<int,int> > op[3]; int main() { int n,m,a,b,c,i1=0,i2=0,i3=0,w=0; cin>>n>>m; for(int i=0; i<m; i++){ cin>>a>>b>>c; op[c-1].push_back({a,b}); } sort(op[0].begin(), op[0].end()); sort(op[1].begin(), op[1].end()); sort(op[2].begin(), op[2].end()); op[0].push_back({0,0}); op[1].push_back({0,0}); op[2].push_back({0,0}); for(int i=1; i<=n; i++){ while(i>op[0][i1].second&&i1<op[0].size()-1){ i1++; } while(i>op[1][i2].second&&i2<op[1].size()-1){ i2++; } while(i>op[2][i1].second&&i3<op[2].size()-1){ i3++; } if(i1==op[0].size()-1||i2==op[1].size()-1){ break; } if(i>=op[0][i1].first&&i>=op[1][i2].first&&i3==op[2].size()-1){ w++; }else if(i>=op[0][i1].first&&i>=op[1][i2].first&&i<op[2][i3].first){ w++; } } cout<<w; } |
English