#include <stdio.h>
#include <stdbool.h>
#include <algorithm>
#include <vector>
using namespace std;
class Action {
public:
Action(int nCol,bool bOn,int n)
{
this->bOn = bOn;
this->nCol = nCol;
this->n = n;
}
bool bOn;
int nCol;
int n;
};
bool actionComp(Action a, Action b) {
if (a.n==b.n)
{
if (a.bOn == false && b.bOn == true) return true;
if (b.bOn == false && a.bOn == true) return false;
}
return a.n < b.n;
}
int main()
{
int nCans,n;
scanf("%d %d\n", &nCans,&n);
bool bDebug = !true;
vector<Action> v;
for (int i = 0; i < n; i++)
{
int nCol, nFrom, nTo;
scanf("%d %d %d\n", &nFrom, &nTo, &nCol);
v.push_back(Action(nCol, true , nFrom ));
v.push_back(Action(nCol, false, nTo + 1));
}
std::sort(v.begin(), v.end(), actionComp);
int color[4] = { 0 };
int nOldTime = 0;
int res = 0;
if (bDebug)
for (int i = 0; i < (int)v.size(); i++)
{
printf("%d %s %d\n", v[i].n, v[i].bOn ? "on" : "off", v[i].nCol);
}
int nStart = 0;
bool bOn = false;
for (int i = -1; i + 1 < (int)v.size(); )
{
do
{
i++;
if (v[i].bOn) color[v[i].nCol]++;
else color[v[i].nCol]--;
}
while (i + 1 < (int)v.size() && v[i].n==v[i+1].n);
if (bDebug) printf("time:%d color(%d,%d,%d)\n", v[i].n, color[1], color[2], color[3]);
if (color[1] > 0 && color[2] > 0 && color[3] <= 0)
{
if (bOn)
{
int ss = v[i].n - nOldTime;
if (bDebug) printf("%d\n", ss);
res += ss;
}
bOn = true;
}
else
{
if (bOn)
{
int ss = v[i].n - nOldTime;
if (bDebug) printf("%d\n", ss);
res += ss;
}
bOn = false;
}
if (nOldTime != v[i].n)
{
nOldTime = v[i].n;
}
}
printf("%d\n", res);
if (bDebug) while (true);
return 0;
}
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #include <stdio.h> #include <stdbool.h> #include <algorithm> #include <vector> using namespace std; class Action { public: Action(int nCol,bool bOn,int n) { this->bOn = bOn; this->nCol = nCol; this->n = n; } bool bOn; int nCol; int n; }; bool actionComp(Action a, Action b) { if (a.n==b.n) { if (a.bOn == false && b.bOn == true) return true; if (b.bOn == false && a.bOn == true) return false; } return a.n < b.n; } int main() { int nCans,n; scanf("%d %d\n", &nCans,&n); bool bDebug = !true; vector<Action> v; for (int i = 0; i < n; i++) { int nCol, nFrom, nTo; scanf("%d %d %d\n", &nFrom, &nTo, &nCol); v.push_back(Action(nCol, true , nFrom )); v.push_back(Action(nCol, false, nTo + 1)); } std::sort(v.begin(), v.end(), actionComp); int color[4] = { 0 }; int nOldTime = 0; int res = 0; if (bDebug) for (int i = 0; i < (int)v.size(); i++) { printf("%d %s %d\n", v[i].n, v[i].bOn ? "on" : "off", v[i].nCol); } int nStart = 0; bool bOn = false; for (int i = -1; i + 1 < (int)v.size(); ) { do { i++; if (v[i].bOn) color[v[i].nCol]++; else color[v[i].nCol]--; } while (i + 1 < (int)v.size() && v[i].n==v[i+1].n); if (bDebug) printf("time:%d color(%d,%d,%d)\n", v[i].n, color[1], color[2], color[3]); if (color[1] > 0 && color[2] > 0 && color[3] <= 0) { if (bOn) { int ss = v[i].n - nOldTime; if (bDebug) printf("%d\n", ss); res += ss; } bOn = true; } else { if (bOn) { int ss = v[i].n - nOldTime; if (bDebug) printf("%d\n", ss); res += ss; } bOn = false; } if (nOldTime != v[i].n) { nOldTime = v[i].n; } } printf("%d\n", res); if (bDebug) while (true); return 0; } |
English