#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair P; typedef tuple T; typedef vector vec; inline bool cheak(ll x, ll y, ll xMax, ll yMax){ return x >= 0 && y >= 0 && xMax > x && yMax > y; } inline ll toInt(string s) { ll v; istringstream sin(s); sin >> v; return v; } template inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template inline T sqr(T x) { return x*x; } #define For(i,a,b) for(ll (i) = (a);i < (b);(i)++) #define rep(i,n) For(i,0,n) #define rFor(i,a,b) for(ll (i) = (a-1);i >= (b);(i)--) #define rrep(i,n) rFor(i,n,0) #define clr(a) memset((a), 0 ,sizeof(a)) #define mclr(a) memset((a), -1 ,sizeof(a)) #define all(a) (a).begin(),(a).end() #define sz(a) (sizeof(a)) const ll dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 }, dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 }; const ll mod = 1e9 + 7; const ll INF = 1e9 + 9; class Signal{ int place, length, interval; bool permit; int count; public : Signal(int place, int length, int interval,bool permit){ this->place = place; this->length = length; this->interval = interval; this->permit = true; this->count = 0; } int getPlace(){ return place; } bool isPermit(){ return permit; } int getLength(){ return length; } int getInterval(){ return interval; } //信号が切り替わるまでの残り時間を返す int restTime(){ return interval - count; } void elapsedTime(int elapseTime){ //切り替えを行う回数 int changeNum = (count + elapseTime) / interval; //切り替えを奇数回行う場合、最終的に信号は変わる bool change = (changeNum % 2) == 1; if (change){ permit = !permit; } count = (count + elapseTime) % interval; } }; class SignalDirector{ vector *signalVec; public : void setData(vector *signalVec){ this->signalVec = signalVec; } void elapsedTime(int elapseTime){ for (int i = 0; i < signalVec->size(); i++){ (*signalVec)[i].elapsedTime(elapseTime); } } }; int main(){ int signalNum, targetPlace; cin >> signalNum >> targetPlace; vector signalVec; for (int i = 0; i < signalNum; i++){ int place, length, interval; cin >> place >> length >> interval; signalVec.push_back(Signal(place, length, interval, true)); } SignalDirector signalDirector; signalDirector.setData(&signalVec); //yuki君の位置 int myPlace = 0; int time = 0; for (int i = 0; i < signalNum; i++){ auto &nextSignal = signalVec[i]; int nextPlace = nextSignal.getPlace(); int elapseTime = nextPlace - myPlace; signalDirector.elapsedTime(elapseTime); time += elapseTime; int waitTime = 0; if (nextSignal.isPermit() == true){ if (nextSignal.restTime() < nextSignal.getLength()){ waitTime = nextSignal.restTime() + nextSignal.getInterval(); } } else{ waitTime = nextSignal.restTime(); } signalDirector.elapsedTime(waitTime); time += waitTime; myPlace = nextPlace; } time += targetPlace - myPlace; cout << time << endl; return 0; }