-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path117_A.cpp
More file actions
48 lines (44 loc) · 771 Bytes
/
117_A.cpp
File metadata and controls
48 lines (44 loc) · 771 Bytes
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
#include<vector>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
vector<int>ans;
for(int i=0;i<n;i++)
{
int si,fi,ti;
cin>>si>>fi>>ti;
if(si==fi)
{
ans.push_back(ti);
continue;
}
if(si<fi)
{
int k = (ti-si+1)/(2*m-2);
if(((ti-si+1)%(2*m-2)) != 0)
k++;
if((si-1) >= ti)
k = 0;
//cout<<"Dwdwwd "<<(ti-i+1)%(2*m-2)<<endl;
//cout<<"dd "<<k<<" "<<(i-1) + k*(2*m-2)<<" "<<(2*m-2)<<endl;
ans.push_back((si-1) + k*(2*m-2) + fi - si);
}
else
{
int k = (ti+si-1)/(2*m-2);
if(((ti+si-1)%(2*m-2))!=0)
k++;
//cout<<"sd "<<k<<endl;
ans.push_back((m-si) + (2*k-1)*(m-1) + abs(fi-si));
}
}
for(int i=0;i<(int)ans.size();i++)
{
cout<<ans[i]<<endl;
}
return 0;
}