#include <bits/stdc++.h>
using namespace std;
/*
John Watson
Mua Code nhu mua Florentino !!
*/
#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
const ll oo = 1e18;
const int maxn = 1e5 + 9;
int n, m;
int arr[maxn];
signed main()
{
if (fopen("test.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
faster();
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> arr[i];
vector<ll> res(m + 5, 0);
for (int i = 1; i < n; i++)
{
int fi = arr[i], se = arr[i + 1];
int app = (fi < se ? se - fi : m - fi + se);
for (int j = 1; j <= m; j++)
{
int new_app = (j < se ? se - j : m - j + se);
res[j] += min(app, new_app);
}
}
ll ans = oo;
for (int i = 1; i <= m; i++)
ans = min(ans, res[i]), cout << res[i] << ' '; cout << '\n';
cout << ans << '\n';
return 0;
}
#include <bits/stdc++.h>
using namespace std;
/*
John Watson
Mua Code nhu mua Florentino !!
*/
#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
const ll oo = 1e18;
const int maxn = 1e5 + 9;
int n, m;
int arr[maxn];
ll segment(int st, int sz) { return 1ll * st * sz - (1ll * (sz - 1) * sz) / 2; }
struct SegmentTree
{
struct Node
{
ll sum;
int a, b;
Node(ll sum = 0ll, int a = -1, int b = -1) : sum(sum), a(a), b(b) {}
Node operator + (const Node& other)
{
Node res;
res.sum = sum + other.sum;
return res;
}
};
vector<Node> st;
int n;
SegmentTree(int n) : n(n) { st.resize(4 * n + 10); }
void pass(int head, int l, int r)
{
if (st[head].a == -1) return;
int mid = l + r >> 1;
int sz = r - l + 1;
int &a = st[head].a, &b = st[head].b;
st[head].sum += min(1ll * sz * b, segment(a, sz));
// cout << l << ' ' << r << ' ' << a << ' ' << sz << ' ' << segment(a, sz) << '\n';
// cout << a << ' ' << a - (mid - l + 1) << ' ' << l << ' ' << r << '\n';
if (l != r)
{
// if (st[2 * head].a != -1)
// {
// pass(2 * head, l, mid);
st[2 * head].a = a;
st[2 * head].b = b;
// }
// if (st[2 * head + 1].a != -1)
// {
// pass(2 * head + 1, mid + 1, r);
st[2 * head + 1].a = a - (mid - l + 1);
st[2 * head + 1].b = b;
// }
}
a = -1;
b = -1;
}
void update(int head, int l, int r, int u, int v, int a, int b)
{
int mid = l + r >> 1;
if (l != r)
{
pass(2 * head, l, mid);
pass(2 * head + 1, mid + 1, r);
}
pass(head, l, r);
if (l > v || r < u) return;
if (u <= l && r <= v)
{
int num = l - u;
st[head].a = a - num;
st[head].b = b;
pass(head, l, r);
return;
}
update(2 * head, l, mid, u, v, a, b);
update(2 * head + 1, mid + 1, r, u, v, a, b);
st[head] = st[2 * head] + st[2 * head + 1];
}
void update(int u, int v, int a, int b) { update(1, 1, n, u, v, a, b); }
ll get(int head, int l, int r, int pos)
{
pass(head, l, r);
if (l == r)
return st[head].sum;
int mid = l + r >> 1;
if (pos <= mid)
return get(2 * head, l, mid, pos);
return get(2 * head + 1, mid + 1, r, pos);
}
ll get(int pos) { return get(1, 1, n, pos); }
};
signed main()
{
if (fopen("test.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
faster();
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> arr[i];
SegmentTree seg(m);
for (int i = 1; i < n; i++)
{
int fi = arr[i];
int se = arr[i + 1];
int tmp = (fi < se ? se - fi : m - fi + se);
seg.update(1, se, se, tmp);
if (se + 1 <= m) seg.update(se + 1, m, m, tmp);
}
ll ans = oo;
for (int i = 1; i <= m; i++)
ans = min(ans, seg.get(i)), cout << seg.get(i) << ' '; cout << '\n';
cout << ans << '\n';
return 0;
}
Compilation message
stove.cpp: In function 'int main()':
stove.cpp:44:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
44 | for (int i = 1; i <= m; i++)
| ^~~
stove.cpp:45:50: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
45 | ans = min(ans, res[i]), cout << res[i] << ' '; cout << '\n';
| ^~~~
stove.cpp: At global scope:
stove.cpp:68:10: error: redefinition of 'const long long int oo'
68 | const ll oo = 1e18;
| ^~
stove.cpp:13:10: note: 'const long long int oo' previously defined here
13 | const ll oo = 1e18;
| ^~
stove.cpp:69:11: error: redefinition of 'const int maxn'
69 | const int maxn = 1e5 + 9;
| ^~~~
stove.cpp:14:11: note: 'const int maxn' previously defined here
14 | const int maxn = 1e5 + 9;
| ^~~~
stove.cpp:71:5: error: redefinition of 'int n'
71 | int n, m;
| ^
stove.cpp:16:5: note: 'int n' previously declared here
16 | int n, m;
| ^
stove.cpp:71:8: error: redefinition of 'int m'
71 | int n, m;
| ^
stove.cpp:16:8: note: 'int m' previously declared here
16 | int n, m;
| ^
stove.cpp:72:5: error: redefinition of 'int arr [100009]'
72 | int arr[maxn];
| ^~~
stove.cpp:17:5: note: 'int arr [100009]' previously declared here
17 | int arr[maxn];
| ^~~
stove.cpp: In member function 'void SegmentTree::pass(int, int, int)':
stove.cpp:100:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
100 | int mid = l + r >> 1;
| ~~^~~
stove.cpp: In member function 'void SegmentTree::update(int, int, int, int, int, int, int)':
stove.cpp:128:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
128 | int mid = l + r >> 1;
| ~~^~~
stove.cpp: In member function 'long long int SegmentTree::get(int, int, int, int)':
stove.cpp:158:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
158 | int mid = l + r >> 1;
| ~~^~~
stove.cpp: At global scope:
stove.cpp:166:8: error: redefinition of 'int main()'
166 | signed main()
| ^~~~
stove.cpp:19:8: note: 'int main()' previously defined here
19 | signed main()
| ^~~~
stove.cpp: In function 'int main()':
stove.cpp:190:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
190 | for (int i = 1; i <= m; i++)
| ^~~
stove.cpp:191:58: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
191 | ans = min(ans, seg.get(i)), cout << seg.get(i) << ' '; cout << '\n';
| ^~~~
stove.cpp: In function 'int main()':
stove.cpp:23:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
stove.cpp:24:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
stove.cpp: In function 'int main()':
stove.cpp:170:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
170 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
stove.cpp:171:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
171 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~