intcheck(int x) { return (k * 2 + x - 1) * x / 2 - (k * 2 + x + n - 1) * (n - x) / 2; }
signedmain() { cin >> T; while (T--) { cin >> n >> k; int l = 1, r = n; while (l < r) { int mid = l + r >> 1; if (check(mid) < 0) l = mid + 1; else r = mid; } cout << min(abs(check(l)), abs(check(l - 1))) << endl; }
signedmain() { cin >> T; while (T--) { cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> a[i]; pre[i] = pre[i - 1] + a[i]; } while (q--) { int x, y; cin >> x >> y; int id1 = (x + n - 1) / n, id2 = y / n; int res = max((int)0, id2 - id1) * pre[n]; x %= n, y %= n; if (id2 >= id1 && !(id2 == id1 && !y)) { if (!x) x = n; res += pre[n] - pre[min(n, id1 + x - 2)]; res += pre[id1 - 1] - pre[max((int)0, x - n + id1 - 2)]; if (y) { id2++; res += pre[min(n, id2 + y - 1)] - pre[id2 - 1]; res += pre[max((int)0, y - n + id2 - 1)]; } } else { if (!x) x = n; if (!y) y = n; res += pre[min(n, id1 + y - 1)] - pre[min(n, id1 + x - 2)]; res += pre[max((int)0, y - n + id1 - 1)] - pre[max((int)0, x - n + id1 - 2)]; } cout << res << endl; } }
intmain() { cin >> T; while (T--) { cin >> n >> k >> q; for (int i = 1; i <= n; i++) cin >> a[i]; map<int, int> p, st; priority_queue<int, vector<int>, less<int>> heap; for (int i = 1; i <= n; i++) { if (i > k) { st[p[a[i - k] - i + k + 1]]++; heap.push(--p[a[i - k] - i + k + 1]); } if (p[a[i] - i + 1]) st[p[a[i] - i + 1]]++; heap.push(++p[a[i] - i + 1]); while (st[heap.top()]) st[heap.top()]--, heap.pop(); f[i] = k - heap.top(); } while (q--) { int l, r; cin >> l >> r; cout << f[r] << endl; } }