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
| #include <bits/stdc++.h> #define N 1000010 using namespace std;
typedef long long ll;
struct SegmentTree { #define le(x) (x << 1) #define ri(x) (x << 1 | 1) #define leftSubtree(x) (tree[le(x)]) #define rightSubtree(x) (tree[ri(x)]) int l, r, size; ll sum; } tree[N << 2];
void pushup(int idx) { tree[idx].sum = leftSubtree(idx).sum + rightSubtree(idx).sum; }
void build(int idx, int l, int r) { tree[idx].l = l, tree[idx].r = r; tree[idx].size = r - l + 1; if (l == r) return; int mid = (l + r) >> 1; build(le(idx), l, mid); build(ri(idx), mid + 1, r); pushup(idx); }
void modify(int idx, int uid) { if (tree[idx].size == 1 && tree[idx].l == uid) { tree[idx].sum++; return; } int mid = (tree[idx].l + tree[idx].r) >> 1; if (uid <= mid) modify(le(idx), uid); if (uid > mid) modify(ri(idx), uid); pushup(idx); }
ll query(int idx, int l, int r) { if (l <= tree[idx].l && tree[idx].r <= r) return tree[idx].sum; int mid = (tree[idx].l + tree[idx].r) >> 1; ll ret = 0; if (l <= mid) ret += query(le(idx), l, r); if (r > mid) ret += query(ri(idx), l, r); return ret; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n, k; cin >> n >> k; k = min(k, n - k); build(1, 0, n - 1); int pos = 0; ll section = 1; for (int i = 1; i <= n; i++) { int L = (pos + 1) % n, R = (pos + k - 1 + n) % n; ll sum = R < L ? query(1, 0, R) + query(1, L, n - 1) : query(1, L, R); modify(1, pos); modify(1, (pos + k) % n); section += sum + 1; pos = (pos + k) % n; cout << section << ' '; }
return 0; }
|