text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 250005, B = 500;
int las[N + N], vis[N + N];
int n, a[N], sum[N], cnt[N], ans = 0, rt;
void check(int x) {
vis[n] = 1;
for (int i = 1; i <= n; ++i) {
if (a[i] == x)
sum[i] = sum[i - 1] + 1;
else if (a[i] == rt)
sum[i] = sum[i - 1] - 1;
else
sum[i] = sum[i - 1];
if (vis[sum[i] + n] != 0)
ans = max(ans, i - las[sum[i] + n]);
else
vis[sum[i] + n] = 1, las[sum[i] + n] = i;
}
for (int i = -n; i <= n; ++i) vis[i + n] = las[i + n] = 0;
}
int shit[N];
void ins(int x, int v) {
shit[cnt[x]]--;
cnt[x] += v;
shit[cnt[x]]++;
}
vector<int> vec;
signed main() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i], cnt[a[i]]++;
int c = *max_element(cnt + 1, cnt + n + 1), res = 0;
for (int i = 1; i <= n; ++i)
if (cnt[i] == c) ++res;
if (res > 1) return cout << n, 0;
for (int i = 1; i <= n; ++i)
if (cnt[i] == c) rt = i;
for (int i = 1; i <= n; ++i)
if (i != rt && cnt[i] > B) vec.push_back(i);
for (int v : vec) check(v);
for (int i = 1; i <= B; ++i) {
for (int j = 1; j <= n; ++j) cnt[j] = shit[j] = 0;
int l = 1, r = 0;
while (r < n) {
++r;
ins(a[r], 1);
while (l < r && cnt[a[r]] > i) ins(a[l], -1), ++l;
if (shit[i] >= 2) ans = max(ans, r - l + 1);
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int MAX = 500;
int solve(int N, const deque<int>& A) {
vector<int> hist(N, 0);
for (int i = 0; i < N; ++i) hist[A[i]]++;
vector<int> mean(N, 0);
iota(mean.begin(), mean.end(), 0);
sort(mean.begin(), mean.end(),
[&](int i, int j) { return hist[i] > hist[j]; });
if (hist[mean[0]] == N) return 0;
int res = 2;
for (int id = 1; id < min(MAX, N); ++id) {
int v = mean[id];
int GETA = A.size(), meannum = 0, vnum = 0;
vector<int> vmin(GETA * 2 + 1, GETA), vmax(GETA * 2 + 1, -1);
vmin[GETA] = vmax[GETA] = 0;
for (int i = 0; i < N; ++i) {
if (A[i] == mean[0])
++meannum;
else if (A[i] == v)
++vnum;
chmin(vmin[meannum - vnum + GETA], i + 1);
chmax(vmax[meannum - vnum + GETA], i + 1);
}
for (int i = 0; i < vmin.size(); ++i) chmax(res, vmax[i] - vmin[i]);
}
for (int k = hist[min(MAX, N) - 1]; k >= 1; --k) {
int left = 0, vmax = 0, num = 0, num2 = 0;
hist.assign(N, 0);
auto push = [&](int id) -> void {
hist[A[id]]++;
chmax(vmax, hist[A[id]]);
if (hist[A[id]] == k)
++num;
else if (hist[A[id]] > k)
--num, ++num2;
};
auto pop = [&](int id) -> void {
hist[A[id]]--;
if (hist[A[id]] == k)
++num, --num2;
else if (hist[A[id]] == k - 1)
--num;
if (num2 == 0 && num > 0)
vmax = k;
else if (num2 == 0 && num == 0)
vmax = k - 1;
};
for (int right = 1; right <= N; ++right) {
push(right - 1);
while (left < right && vmax > k) pop(left++);
if (num >= 2) chmax(res, right - left);
}
}
return res;
}
int main() {
int N;
scanf("%d", &N);
deque<int> A(N);
for (int i = 0; i < N; ++i) scanf("%d", &A[i]), --A[i];
printf("%d\n", solve(N, A));
}
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
template <typename T>
T min(T x, T y) {
return x < y ? x : y;
}
template <typename T>
T max(T x, T y) {
return x > y ? x : y;
};
const long long INF = 20000000050000;
const long long mod = 1000000007;
const long long MAXN = 300005;
int N, a[MAXN], D, ANS;
std::vector<int> Q[MAXN];
int pre[MAXN], nxt[MAXN];
std::set<int> S, rS, S2, rS2;
std::queue<int> del;
std::pair<int, int> find(int pos) {
int r = *S2.lower_bound(pos);
int l = *rS2.lower_bound(-pos);
if (r <= N) del.push(r), S2.erase(r);
if (l < 0) del.push(l), rS2.erase(l);
return {-l, r};
}
void reset() {
if (S2.empty()) {
S2 = S, rS2 = rS;
return;
}
while (!del.empty()) {
if (del.front() > 0)
S2.insert(del.front());
else
rS2.insert(del.front());
del.pop();
}
}
int T[MAXN];
void add(int p, int x) {
while (p <= N) T[p] += x, p += p & -p;
}
int query(int p) {
int ret = 0;
while (p) ret += T[p], p -= p & -p;
return ret;
}
void solve(int val) {
reset();
for (int i = Q[val].size() - 1; i >= 0; i--) {
add(Q[val][i], -1);
std::pair<int, int> t = find(Q[val][i]);
if (t.first > 0) Q[val].push_back(t.first);
if (t.second <= N) Q[val].push_back(t.second);
}
Q[val].push_back(0), Q[val].push_back(N + 1);
std::sort(Q[val].begin(), Q[val].end());
std::unordered_map<int, int> MAP;
for (int i = 1; i < Q[val].size() - 1; i++) {
int sum = query(Q[val][i]);
if (MAP[sum]) {
int l = Q[val][MAP[sum] - 1] + 1, r = Q[val][i + 1] - 1;
if (a[Q[val][MAP[sum]]] == D) l = max(l, pre[Q[val][MAP[sum]]] + 1);
if (a[Q[val][i]] == D) r = min(r, nxt[Q[val][i]] - 1);
ANS = max(ANS, r - l + 1);
}
sum = query(Q[val][i] - 1);
if (!MAP[sum]) MAP[sum] = i;
}
for (int i = 0; i < Q[val].size(); i++)
if (a[Q[val][i]] == val) add(Q[val][i], 1);
}
bool init() {
static int tmp[MAXN];
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%d", a + i), tmp[a[i]]++;
for (int i = 1; i <= N; i++)
if (tmp[i] > tmp[D]) D = i;
int t = 0;
for (int i = 1; i <= N; i++)
if (tmp[i] == tmp[D]) t++;
if (t > 1) {
ANS = N;
return false;
}
Q[D].push_back(0);
for (int i = 1; i <= N; i++) {
Q[a[i]].push_back(i);
if (a[i] == D) S.insert(i), rS.insert(-i), add(i, 1);
}
Q[D].push_back(N + 1);
for (int i = 1; i < Q[D].size() - 1; i++)
pre[Q[D][i]] = Q[D][i - 1], nxt[Q[D][i]] = Q[D][i + 1];
S.insert(0);
S.insert(N + 1);
rS.insert(0);
rS.insert(-N - 1);
return true;
}
int main() {
if (init())
for (int i = 1; i <= N; i++)
if (Q[i].size() && i != D) solve(i);
printf("%d\n", ANS);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template <typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const int N = 200200;
int a[N];
int d[N][2];
int n;
int cnt[N];
vector<int> posForX[N];
int ans = 0;
int countLower(int b, int x) {
return lower_bound(posForX[b].begin(), posForX[b].end(), x) -
posForX[b].begin();
}
void solve(int b, int c) {
set<int> intPos;
for (int i = 0; i < cnt[c]; i++) {
int x = posForX[c][i];
int z = countLower(b, x);
int bal = cnt[b] - z + i;
intPos.insert(bal);
intPos.insert(bal + 1);
}
for (int x : intPos) {
d[x][0] = n;
d[x][1] = -1;
}
set<int> setik;
setik = intPos;
vector<int> pp;
pp.push_back(-1);
for (int x : posForX[c]) pp.push_back(x);
pp.push_back(n);
for (int i = 1; i < (int)pp.size(); i++) {
int x = pp[i], y = pp[i - 1];
int maxBal = cnt[b] - countLower(b, y) + i - 1;
int minBal = cnt[b] - countLower(b, x) + i - 1;
while (true) {
auto it = setik.lower_bound(minBal);
if (it == setik.end()) break;
int bal = *it;
if (bal > maxBal) break;
int toW = bal;
setik.erase(it);
bal = cnt[b] - bal + i - 1;
assert(bal >= 0);
if (toW == maxBal) {
d[toW][0] = y + 1;
} else {
assert(bal > 0);
d[toW][0] = posForX[b][bal - 1] + 1;
}
}
}
setik = intPos;
for (int i = (int)pp.size() - 1; i > 0; i--) {
int x = pp[i], y = pp[i - 1];
int maxBal = cnt[b] - countLower(b, y) + i - 1;
int minBal = cnt[b] - countLower(b, x) + i - 1;
while (true) {
auto it = setik.lower_bound(minBal);
if (it == setik.end()) break;
int bal = *it;
if (bal > maxBal) break;
int toW = bal;
setik.erase(it);
bal = cnt[b] - bal + i - 1;
assert(bal >= 0);
if (toW == minBal) {
d[toW][1] = x;
} else {
d[toW][1] = posForX[b][bal];
}
}
}
for (int x : intPos) ans = max(ans, d[x][1] - d[x][0]);
}
int main() {
startTime = clock();
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
posForX[a[i]].push_back(i);
cnt[a[i]]++;
}
int b = 0;
for (int i = 0; i < N; i++) {
if (cnt[i] > cnt[b]) b = i;
}
for (int i = 0; i < N; i++) {
if (posForX[i].empty()) continue;
if (i == b) continue;
solve(b, i);
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int base = 500;
int a[maxn], cnt[maxn], cc[maxn], cur[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), ++cc[a[i]];
int c = max_element(cc + 1, cc + 1 + n) - cc;
int ans = 0;
for (int i = 1; i <= min(cc[c], base); ++i) {
int sc = 0;
memset(cnt, 0, sizeof cnt);
auto change = [&](int x, int val) {
sc -= (cnt[x] == i);
cnt[x] += val;
sc += (cnt[x] == i);
};
int l = 1, r = 1;
while (r <= n) {
change(a[r], 1);
while (cnt[a[r]] > i) change(a[l++], -1);
if (sc > 1) ans = max(ans, r - l + 1);
++r;
}
}
for (int i = 1; i <= n; ++i)
if (i != c && cc[i] > base) {
memset(cur, -1, sizeof cur);
int sum = 0;
cur[0] = 0;
for (int j = 1; j <= n; ++j) {
sum += (a[j] == c) - (a[j] == i);
if (sum >= 0) {
if (cur[sum] == -1)
cur[sum] = j;
else
ans = max(ans, j - cur[sum]);
}
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
const int M = 200001;
using namespace std;
int main() {
long n;
cin >> n;
long a[n + 1], tp[n + 1], num;
pair<long, long> cnt[M];
memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= n; i++) {
cin >> a[i];
cnt[a[i]].first++;
cnt[a[i]].second = a[i];
}
sort(cnt, cnt + M, greater<pair<long long, long long>>());
long mx = cnt[0].second;
if (cnt[0].first == cnt[1].first)
cout << n;
else {
long rez = 0;
for (int i = 1; cnt[i].first >= sqrt(n); i++) {
if (i != mx) {
long P[2 * n + 1], ts = n;
memset(P, -1, sizeof(P));
P[n] = 0;
for (int j = 1; j <= n; j++) {
ts += (a[j] == mx) - (a[j] == cnt[i].second);
if (P[ts] != -1)
rez = max(rez, j - P[ts]);
else
P[ts] = j;
}
}
}
for (int c = 1; c <= sqrt(n); c++) {
memset(tp, 0, sizeof(tp));
num = 0;
for (int l = 1, r = 1; r <= n; r++) {
num -= (tp[a[r]] == c);
tp[a[r]]++;
num += (tp[a[r]] == c);
while (tp[a[r]] > c) {
num -= (tp[a[l]] == c);
tp[a[l]]--;
num += (tp[a[l]] == c);
l++;
}
if (num > 1) rez = max(rez, (long)(r - l + 1));
}
}
cout << rez;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long M = 1e6;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long binpow(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % M;
a = a * a % M;
b >>= 1;
}
return res;
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
{
for (auto& _re : a) cin >> _re;
};
vector<int> pos[200200];
for (int i = 0; i < n; i++) {
pos[a[i]].emplace_back(i);
}
int mx = 0;
for (int i = 0; i <= 200000; i++) {
if (pos[i].size() > pos[mx].size()) mx = i;
}
vector<int> id(n), ps(n);
int cnt = 0;
for (int i = 0; i < n; i++) {
if (a[i] == mx) {
cnt++;
}
id[i] = cnt - 1;
ps[i] = cnt;
}
int ans = 0;
const int sq = 450;
vector<int> q(2 * n + 5);
for (int v = 0; v <= 200000; v++) {
if (pos[v].size() == 0 or v == mx) continue;
if (pos[v].size() == pos[mx].size()) {
ans = n;
break;
}
if (pos[v].size() >= sq) {
vector<int> p(2 * n + 5);
int d = 0;
for (int i = 1; i <= n; i++) {
if (a[i - 1] == mx) d++;
if (a[i - 1] == v) d--;
if (p[d + n] or d == 0) {
ans = max(ans, i - p[d + n]);
} else
p[d + n] = i;
}
continue;
}
int k = pos[v].size();
for (int i : pos[v]) {
int d = 0;
int li = 0;
int lb = max(id[i] - k - 3, 0);
int rb = min((int)pos[mx].size() - 1, id[i] + k + 3);
vector<int> used;
auto relax = [&](int x, int pt, int idx) {
d += x;
while (idx - 1 >= 0 && pos[v][idx - 1] >= pt) idx--;
while (idx < pos[v].size() && pos[v][idx] <= pt) idx++;
int rmost =
min((id[pt] + 1 < pos[mx].size() ? pos[mx][id[pt] + 1] - 1 : n - 1),
(idx < pos[v].size() ? pos[v][idx] - 1 : n - 1));
if (q[d + n] or d == 0) {
ans = max(ans, (rmost + 1) - q[d + n]);
} else {
q[d + n] = pt + 1;
used.emplace_back(d + n);
}
};
if (lb != 0)
while (pos[v][li] < pos[mx][lb]) li++;
q[n] = min(pos[mx][lb], pos[v][li]);
if (id[q[n] - (q[n] == pos[mx][lb])] == -1) {
q[n] = 0;
}
for (int j = lb; j <= rb; j++) {
while (li < pos[v].size() && pos[v][li] < pos[mx][j]) {
relax(-1, pos[v][li], li);
li++;
}
relax(1, pos[mx][j], li);
}
if (rb == pos[mx].size() - 1) {
while (li < pos[v].size()) {
relax(-1, pos[v][li], li);
li++;
}
}
for (int x : used) {
q[x] = 0;
}
}
}
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
const int maxn = 4e5 + 7;
using namespace std;
int n, x, y, ans;
int a[maxn], b[maxn], c[maxn], last[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[a[i]]++;
}
for (int i = 1; i <= n; i++)
if (b[i] > b[x]) x = i;
int lim = trunc(sqrt(n));
for (int i = 1; i <= n; i++) {
if ((b[i] >= lim) && (i != x)) {
y = i;
for (int j = 1; j <= n; j++) {
if (a[j] == x)
c[j] = 1;
else if (a[j] == y)
c[j] = -1;
else
c[j] = 0;
}
for (int j = 0; j <= 2 * n; j++) last[j] = -1;
last[n] = 0;
c[0] = n;
for (int j = 1; j <= n; j++) {
c[j] += c[j - 1];
if (last[c[j]] != -1)
ans = max(ans, j - last[c[j]]);
else
last[c[j]] = j;
}
}
}
for (int i = 1; i < lim; i++) {
for (int j = 1; j <= n; j++) c[j] = 0;
int l = 1, num = 0;
for (int j = 1; j <= n; j++) {
c[a[j]]++;
if (c[a[j]] == i)
num++;
else {
while (c[a[j]] > i) {
if (c[a[l]] == i) num--;
c[a[l]]--;
l++;
}
}
if (num >= 2) ans = max(ans, j - l + 1);
}
}
printf("%d\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const char nl = '\n';
const int MAX_N = 100011;
const long long INF = (1 << 29) + 123;
const long long MOD = 1000000007;
const long double PI = 4 * atan((long double)1);
template <typename T>
bool ckmin(T& a, const T& b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
bool ckmax(T& a, const T& b) {
return b > a ? a = b, 1 : 0;
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << H << " ";
dbg_out(T...);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MX = 1 << 20;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int m = n;
vector<int> LOC[n + 1];
int A[n];
for (int i = 0; i < n; i++) {
cin >> A[i];
LOC[A[i]].push_back(i);
}
int hi = 0, best = -1;
for (int i = 1; i <= m; i++)
if (ckmax(hi, (int)LOC[i].size())) best = i;
bool done = 0;
for (int i = 1; i <= m; i++)
if (i != best && hi == (int)LOC[i].size()) done = 1;
if (done) {
cout << n << nl;
return 0;
}
n += 2;
int a[n];
for (int i = 0; i < n - 2; i++) a[i + 1] = A[i];
a[0] = a[n - 1] = best;
vector<int> loc[n + 1];
for (int i = 0; i < n; i++) loc[a[i]].push_back(i);
hi = (int)loc[best].size();
int idxBest[n];
for (int i = 0; i < n; i++)
if (i == 0)
idxBest[i] = 1;
else
idxBest[i] = idxBest[i - 1] + (a[i] == best);
int ans = 0;
int SQRTN = sqrt(n) - 100;
for (int i = 1; i <= m; i++) {
int SZ = (int)loc[i].size();
if (i == best || !SZ) continue;
if (SZ > SQRTN) {
int j = 0, k = 0;
int sum = 0;
int firstLoc[2 * n + 1];
for (int ii = 0; ii < 2 * n + 1; ii++) firstLoc[ii] = -1;
firstLoc[sum + n] = 0;
while (j != hi - 1 || k != SZ) {
int curPos;
if (k < SZ && loc[best][j] > loc[i][k]) {
curPos = loc[i][k] + 1;
ckmax(ans, curPos - 1 - firstLoc[sum + n]);
sum++;
k++;
} else {
curPos = loc[best][j] + 1;
ckmax(ans, curPos - 1 - firstLoc[sum + n]);
sum--;
j++;
}
if (firstLoc[sum + n] == -1) firstLoc[sum + n] = curPos;
}
ckmax(ans, loc[best][j] - firstLoc[sum + n]);
} else {
int kkk = 0;
for (int kk = 0; kk < SZ; kk++) {
int j = max(0, idxBest[loc[i][kk]] - SZ - 2);
int jEnd = min(hi, idxBest[loc[i][kk]] + SZ + 2);
while (kkk < SZ && loc[best][j] > loc[i][kkk]) kkk++;
int k = kkk;
int PAD = 2 * SZ + 5;
int firstLoc[2 * PAD + 1];
for (int ii = 0; ii < 2 * PAD + 1; ii++) firstLoc[ii] = -1;
int sum = -1;
if (j == 0) firstLoc[0 + PAD] = 0;
firstLoc[sum + PAD] = loc[best][j] + 1;
j++;
while (j < jEnd) {
int curPos;
if (k < SZ && loc[i][k] < loc[best][j]) {
curPos = loc[i][k] + 1;
ckmax(ans, loc[i][k] - firstLoc[sum + PAD]);
sum++;
k++;
} else {
curPos = loc[best][j] + 1;
ckmax(ans, loc[best][j] - firstLoc[sum + PAD]);
sum--;
j++;
}
if (firstLoc[sum + PAD] == -1) firstLoc[sum + PAD] = curPos;
}
if (j != hi) ckmax(ans, loc[best][j] - firstLoc[sum + PAD]);
}
}
}
cout << ans << nl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, i, j, k, a[N], cnt[N], m[N], ans, s[N], x[N], pos[N * 2], mx, u;
vector<int> g[N];
void cal() {
if (u == 0) return;
int k;
x[u + 1] = n + 1;
for (k = 0; k <= u; ++k) {
if (pos[s[k] + N] == -1)
pos[s[k] + N] = x[k];
else
ans = max(ans, x[k + 1] - 1 - pos[s[k] + N]);
}
for (k = 0; k <= u; ++k) pos[s[k] + N] = -1;
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
g[a[i]].push_back(i);
}
for (i = 1; i <= n; ++i) {
++cnt[a[i]];
if (cnt[a[i]] >= cnt[mx]) mx = a[i];
}
memset(pos, -1, sizeof(pos));
for (i = 1; i <= n; ++i)
if (i != mx && g[i].size())
if (g[i].size() > 300 || n <= 5000) {
int l1 = 0, l2 = 0;
u = 0;
while (l1 < g[i].size() && l2 < g[mx].size())
if (g[i][l1] < g[mx][l2]) {
++u;
s[u] = s[u - 1] + 1;
x[u] = g[i][l1++];
} else {
++u;
s[u] = s[u - 1] - 1;
x[u] = g[mx][l2++];
}
while (l1 < g[i].size()) {
++u;
s[u] = s[u - 1] + 1;
x[u] = g[i][l1++];
}
while (l2 < g[mx].size()) {
++u;
s[u] = s[u - 1] - 1;
x[u] = g[mx][l2++];
}
cal();
} else {
int las = -N;
u = 0;
for (k = 0; k < g[i].size(); ++k) {
int l =
lower_bound(g[mx].begin(), g[mx].end(), g[i][k]) - g[mx].begin();
if (l - las > g[i].size()) {
cal();
u = 0;
for (j = max(0, l - (int)g[i].size() - 1); j < l; ++j) {
++u;
s[u] = s[u - 1] - 1;
x[u] = g[mx][j];
}
}
++u;
s[u] = s[u - 1] + 1;
x[u] = g[i][k];
for (j = l; j < g[mx].size() && j <= l + g[i].size() &&
(k + 1 == g[i].size() || g[mx][j] < g[i][k + 1]);
++j) {
++u;
s[u] = s[u - 1] - 1;
x[u] = g[mx][j];
}
las = j - 1;
}
cal();
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = (int)1e9 + 7;
template <class T>
int bins(const vector<T>& vec, T v) {
int low = 0;
int high = vec.size();
while (low != high) {
int mid = (low + high) >> 1;
if (vec[mid] < v)
low = mid + 1;
else
high = mid;
}
return low;
}
int nxt(int x, int dir, const vector<int>& xs) {
int j = bins(xs, x + 1);
if (dir == 1) {
if (j == xs.size())
return xs.size();
else
return j;
} else {
if (j == 0)
return -1;
else
return j - 1;
}
}
vector<int> build(int n, int max_abs, int i, int t, int dir,
const vector<int>& xs, const vector<int>& ys) {
vector<int> res(2 * max_abs + 1, -INF);
res[max_abs] = 0;
int sx = xs[i];
if (dir == 1) {
int x = xs[i];
int j = nxt(x, 1, ys);
int cur = max_abs;
while (0 <= cur && cur <= 2 * max_abs) {
res[cur] = x - sx;
if (i > t && j == ys.size()) {
x = n - 1;
break;
} else {
bool move_x = 0;
if (i <= t && j < ys.size()) {
if (xs[i] < ys[j]) move_x = 1;
} else if (i <= t) {
move_x = 1;
}
if (move_x) {
--cur;
x = xs[i];
++i;
} else {
x = ys[j] - 1;
res[cur] = x - sx;
++cur;
++x;
++j;
}
}
}
if (0 <= cur && cur <= 2 * max_abs) res[cur] = x - sx;
} else {
int x = xs[i];
int j = nxt(x, -1, ys);
int cur = max_abs;
while (0 <= cur && cur <= 2 * max_abs) {
res[cur] = sx - x;
if (i < t && j == -1) {
x = 0;
break;
} else {
bool move_x = 0;
if (i >= t && j >= 0) {
if (xs[i] > ys[j]) move_x = 1;
} else if (i >= t) {
move_x = 1;
}
if (move_x) {
--cur;
x = xs[i];
--i;
} else {
x = ys[j] + 1;
res[cur] = sx - x;
++cur;
--x;
--j;
}
}
}
if (0 <= cur && cur <= 2 * max_abs) res[cur] = sx - x;
}
for (int i = 1; i <= 2 * max_abs; ++i) res[i] = max(res[i - 1], res[i]);
return res;
}
int solve(int n, int ia, int ib, const vector<int>& xs, const vector<int>& ys) {
if (ia > ib) return 0;
int cou = ib - ia + 1;
int mid = (ia + ib) >> 1;
auto le = build(n, cou, mid, ia, -1, xs, ys);
auto ri = build(n, cou, mid, ib, 1, xs, ys);
int res = 0;
for (int j = 0; j <= 2 * cou - 1; ++j) {
res = max(res, le[j] + ri[(2 * cou - 1) - j] + 1);
}
res = max(res, solve(n, ia, mid - 1, xs, ys));
res = max(res, solve(n, mid + 1, ib, xs, ys));
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> vals(n);
for (int& v : vals) cin >> v;
vector<int> comp = vals;
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
int m = comp.size();
vector<vector<int>> xs(m);
for (int i = 0; i < n; ++i) {
vals[i] = bins(comp, vals[i]);
xs[vals[i]].push_back(i);
}
int j = 0;
bool uniq = 1;
for (int i = 1; i < m; ++i) {
if (xs[i].size() > xs[j].size()) {
j = i;
uniq = 1;
} else if (xs[i].size() == xs[j].size()) {
uniq = 0;
}
}
if (!uniq) {
cout << n << '\n';
} else {
vector<int> ys = xs[j];
swap(xs[j], xs[m - 1]);
int res = 0;
for (int i = 0; i < m - 1; ++i)
res = max(res, solve(n, 0, (int)xs[i].size() - 1, xs[i], ys));
cout << res << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int maxn = 200000 + 1;
constexpr int sp = 447;
int a[maxn], c[maxn];
int mp[maxn * 2 + 10];
int cnt[maxn + 1];
int cxt[maxn + 1];
int m;
void insert(int x) {
cxt[cnt[x]] -= 1;
cnt[x] += 1;
cxt[cnt[x]] += 1;
m = max(m, cnt[x]);
}
void remove(int x) {
cxt[cnt[x]] -= 1;
cnt[x] -= 1;
cxt[cnt[x]] += 1;
if (cxt[m] == 0) m -= 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; i += 1) {
cin >> a[i];
c[a[i]] += 1;
}
int f = *max_element(c + 1, c + n + 1);
if (f == n) {
cout << "0";
return 0;
}
if (count(c + 1, c + n + 1, f) > 1) {
cout << n;
return 0;
}
int x = max_element(c + 1, c + n + 1) - c;
int ans = 0;
for (int i = 1; i <= n; i += 1)
if (i != x and c[i] >= 447) {
for (int j = -n; j <= n; j += 1) mp[j + maxn] = -1;
int sum = 0;
mp[sum + maxn] = 0;
for (int j = 1; j <= n; j += 1) {
if (a[j] == x) sum += 1;
if (a[j] == i) sum -= 1;
if (mp[sum + maxn] != -1)
ans = max(j - mp[sum + maxn], ans);
else
mp[sum + maxn] = j;
}
}
for (int i = 1; i <= min(f, 447); i += 1) {
for (int i = 1; i <= n; i += 1) cnt[i] = cxt[i] = 0;
cxt[0] = n;
m = 0;
for (int j = 1, k = 1, xt = 0; j <= n; j += 1) {
while (k <= n and xt + (a[k] == x) <= i) {
insert(a[k]);
xt += a[k] == x;
k += 1;
}
if (cxt[m] >= 2) ans = max(ans, k - j);
remove(a[j]);
xt -= a[j] == x;
}
}
cout << ans;
return 0;
}
|
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class S, class T> inline S min_L(S a,T b){
return a<=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
(*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
(*arr)=(T*)(*mem);
(*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
walloc1d(arr, x2-x1, mem);
(*arr) -= x1;
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
sort(a, a+N);
}
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
int i;
pair<T1, T2>*arr;
walloc1d(&arr, N, &mem);
for(i=(0);i<(N);i++){
arr[i].first = a[i];
arr[i].second = b[i];
}
sort(arr, arr+N);
for(i=(0);i<(N);i++){
a[i] = arr[i].first;
b[i] = arr[i].second;
}
}
inline int my_getchar(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline void rd(long long &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline int rd_int(void){
int x;
rd(x);
return x;
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar(a);
}
inline void wt_L(int x){
int s=0;
int m=0;
char f[10];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar('-');
}
while(s--){
my_putchar(f[s]+'0');
}
}
template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
a[k] = aval;
}
template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
a[k] = aval;
b[k] = bval;
}
template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
for(i=sz-1;i>k;i--){
c[i] = c[i-1];
}
a[k] = aval;
b[k] = bval;
c[k] = cval;
}
template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
for(i=sz-1;i>k;i--){
c[i] = c[i-1];
}
for(i=sz-1;i>k;i--){
d[i] = d[i-1];
}
a[k] = aval;
b[k] = bval;
c[k] = cval;
d[k] = dval;
}
template<class S, class T> inline S chmin(S &a, T b){
if(a>b){
a=b;
}
return a;
}
template<class S, class T> inline S chmax(S &a, T b){
if(a<b){
a=b;
}
return a;
}
int N;
int C;
int Q;
int A[30000];
int X;
long long Y;
long long cnt[5][30000+2];
int sz;
int lis[5];
int ind[5];
int usz[30000+2];
int ulis[30000+2][5];
int dsz[30000+2];
int dlis[30000+2][5];
long long skipL[5][30000+2];
long long skipR[5][30000+2];
long long skipV[5][30000+2];
int skip = 150;
long long skip2L[5][30000+2];
long long skip2R[5][30000+2];
long long skip2V[5][30000+2];
int skip2 = 30;
int main(){
int t_ynMSdg;
wmem = memarr;
int i;
int j;
int k;
int c;
for(i=(0);i<(5);i++){
cnt[i][0] = 1;
}
for(i=(0);i<(5);i++){
for(k=(0);k<(30000);k++){
for(j=(0);j<(min_L(k, i)+1);j++){
cnt[i][k+1] += cnt[i-j][k-j];
if(cnt[i][k+1] > 2000000000000000000LL){
cnt[i][k+1] = 2000000000000000000LL;
}
}
}
}
int KrdatlYV = rd_int();
for(t_ynMSdg=(0);t_ynMSdg<(KrdatlYV);t_ynMSdg++){
int dtiCQK_a;
rd(N);
rd(C);
rd(Q);
{
int a2conNHc;
for(a2conNHc=(0);a2conNHc<(N);a2conNHc++){
rd(A[a2conNHc]);
}
}
for(k=(0);k<(N);k++){
sz = 0;
for(i=(0);i<(C+1);i++){
if(k+i < N){
arrInsert(sz, sz, ind, i, lis, A[k+i]);
}
}
sortA_L(sz, lis, ind);
j = usz[k] = dsz[k] = 0;
for(i=(0);i<(sz);i++){
if(ind[i] == 0){
j++;
continue;
}
if(j==0){
ulis[k][usz[k]++] = ind[i];
}
if(j==1){
dlis[k][dsz[k]++] = ind[i];
}
}
}
for(k=(0);k<(N);k++){
for(c=(0);c<(C+1);c++){
long long sm = 0;
skipL[c][k] = -4611686016279904256LL;
skipR[c][k] = 4611686016279904256LL;
if(k+skip+5 >= N){
swap(skipL[c][k], skipR[c][k]);
continue;
}
for(j=(k);j<(k+skip);j++){
for(i=(0);i<(usz[j]);i++){
if(c >= ulis[j][i]){
sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1];
}
}
if(sm > 4611686016279904256LL){
sm = 2000000000000000000LL;
}
chmax(skipL[c][k], sm);
chmin(skipR[c][k], sm + cnt[c][N-j-1]);
}
skipV[c][k] = sm;
}
}
for(k=(0);k<(N);k++){
for(c=(0);c<(C+1);c++){
long long sm = 0;
skip2L[c][k] = -4611686016279904256LL;
skip2R[c][k] = 4611686016279904256LL;
if(k+skip2+5 >= N){
swap(skip2L[c][k], skip2R[c][k]);
continue;
}
for(j=(k);j<(k+skip2);j++){
for(i=(0);i<(usz[j]);i++){
if(c >= ulis[j][i]){
sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1];
}
}
if(sm > 4611686016279904256LL){
sm = 2000000000000000000LL;
}
chmax(skip2L[c][k], sm);
chmin(skip2R[c][k], sm + cnt[c][N-j-1]);
}
skip2V[c][k] = sm;
}
}
for(dtiCQK_a=(0);dtiCQK_a<(Q);dtiCQK_a++){
rd(X);X += (-1);
rd(Y);Y += (-1);
if(Y >= cnt[C][N]){
wt_L(-1);
wt_L('\n');
continue;
}
c = C;
for(k=(0);k<(N);k++){
if(skipL[c][k] <= Y && Y < skipR[c][k]){
if(X < k + skip){
wt_L(A[X]);
wt_L('\n');
break;
}
Y -= skipV[c][k];
k += skip - 1;
continue;
}
if(skip2L[c][k] <= Y && Y < skip2R[c][k]){
if(X < k + skip2){
wt_L(A[X]);
wt_L('\n');
break;
}
Y -= skip2V[c][k];
k += skip2 - 1;
continue;
}
for(i=(0);i<(usz[k]);i++){
if(c >= ulis[k][i]){
if(Y < cnt[c-ulis[k][i]][N-k-ulis[k][i]-1]){
if(X <= k + ulis[k][i]){
wt_L(A[k+ulis[k][i]-(X-k)]);
wt_L('\n');
goto qE8LMwYZ;
}
c -= ulis[k][i];
k += ulis[k][i];
goto lQU550vz;
}
else{
Y -= cnt[c-ulis[k][i]][N-k-ulis[k][i]-1];
}
}
}
if(Y < cnt[c][N-k-1]){
if(X == k){
wt_L(A[k]);
wt_L('\n');
break;
}
continue;
}
else{
Y -= cnt[c][N-k-1];
}
for(i=(0);i<(dsz[k]);i++){
if(c >= dlis[k][i]){
if(Y < cnt[c-dlis[k][i]][N-k-dlis[k][i]-1]){
if(X <= k + dlis[k][i]){
wt_L(A[k+dlis[k][i]-(X-k)]);
wt_L('\n');
goto qE8LMwYZ;
}
c -= dlis[k][i];
k += dlis[k][i];
goto lQU550vz;
}
else{
Y -= cnt[c-dlis[k][i]][N-k-dlis[k][i]-1];
}
}
}
lQU550vz:;
}
qE8LMwYZ:;
}
}
return 0;
}
// cLay version 20210103-1
// --- original code ---
// //no-unlocked
// int N, C, Q, A[3d4], X; ll Y;
// ll cnt[5][3d4+2];
// int sz, lis[5], ind[5];
// int usz[3d4+2], ulis[3d4+2][5];
// int dsz[3d4+2], dlis[3d4+2][5];
// ll skipL[5][3d4+2], skipR[5][3d4+2], skipV[5][3d4+2]; int skip = 150;
// ll skip2L[5][3d4+2], skip2R[5][3d4+2], skip2V[5][3d4+2]; int skip2 = 30;
// {
// int i, j, k, c;
// rep(i,5) cnt[i][0] = 1;
// rep(i,5) rep(k,3d4) rep(j,min(k,i)+1){
// cnt[i][k+1] += cnt[i-j][k-j];
// if(cnt[i][k+1] > 2d18) cnt[i][k+1] = 2d18;
// }
// REP(rd_int()){
// rd(N,C,Q,A(N));
//
// rep(k,N){
// sz = 0;
// rep(i,C+1) if(k+i < N) arrInsert(sz, sz, ind, i, lis, A[k+i]);
// sortA(sz, lis, ind);
// j = usz[k] = dsz[k] = 0;
// rep(i,sz){
// if(ind[i] == 0) j++, continue;
// if(j==0) ulis[k][usz[k]++] = ind[i];
// if(j==1) dlis[k][dsz[k]++] = ind[i];
// }
// }
//
// rep(k,N) rep(c,C+1){
// ll sm = 0;
// skipL[c][k] = -ll_inf;
// skipR[c][k] = ll_inf;
// if(k+skip+5 >= N) swap(skipL[c][k], skipR[c][k]), continue;
// rep(j,k,k+skip){
// rep(i,usz[j]) if(c >= ulis[j][i]) sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1];
// if(sm > ll_inf) sm = 2d18;
// skipL[c][k] >?= sm;
// skipR[c][k] <?= sm + cnt[c][N-j-1];
// }
// skipV[c][k] = sm;
// }
//
// rep(k,N) rep(c,C+1){
// ll sm = 0;
// skip2L[c][k] = -ll_inf;
// skip2R[c][k] = ll_inf;
// if(k+skip2+5 >= N) swap(skip2L[c][k], skip2R[c][k]), continue;
// rep(j,k,k+skip2){
// rep(i,usz[j]) if(c >= ulis[j][i]) sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1];
// if(sm > ll_inf) sm = 2d18;
// skip2L[c][k] >?= sm;
// skip2R[c][k] <?= sm + cnt[c][N-j-1];
// }
// skip2V[c][k] = sm;
// }
//
// rep(Q){
// rd(X--, Y--);
// if(Y >= cnt[C][N]) wt(-1), continue;
// c = C;
// rep(k,N){
// if(skipL[c][k] <= Y < skipR[c][k]){
// if(X < k + skip) wt(A[X]), break;
// Y -= skipV[c][k];
// k += skip - 1;
// continue;
// }
// if(skip2L[c][k] <= Y < skip2R[c][k]){
// if(X < k + skip2) wt(A[X]), break;
// Y -= skip2V[c][k];
// k += skip2 - 1;
// continue;
// }
// rep(i,usz[k]) if(c >= ulis[k][i]){
// if(Y < cnt[c-ulis[k][i]][N-k-ulis[k][i]-1]){
// if(X <= k + ulis[k][i]) wt(A[k+ulis[k][i]-(X-k)]), break_break;
// c -= ulis[k][i];
// k += ulis[k][i];
// break_continue;
// } else {
// Y -= cnt[c-ulis[k][i]][N-k-ulis[k][i]-1];
// }
// }
// if(Y < cnt[c][N-k-1]){
// if(X == k) wt(A[k]), break;
// continue;
// } else {
// Y -= cnt[c][N-k-1];
// }
// rep(i,dsz[k]) if(c >= dlis[k][i]){
// if(Y < cnt[c-dlis[k][i]][N-k-dlis[k][i]-1]){
// if(X <= k + dlis[k][i]) wt(A[k+dlis[k][i]-(X-k)]), break_break;
// c -= dlis[k][i];
// k += dlis[k][i];
// break_continue;
// } else {
// Y -= cnt[c-dlis[k][i]][N-k-dlis[k][i]-1];
// }
// }
// }
// }
// }
// }
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
int read(){
int a=0,fh=1;char c=getchar();
while(c>'9'||c<'0'){if(c=='-')fh=-1;c=getchar();}
while('0'<=c&&c<='9'){
a=a*10+c-48;
c=getchar();
}
return a*fh;
}
#define MN 30005
#define pii pair<int,int>
#define mp make_pair
#define x first
#define y second
int T,n,m,c,q;
int p[MN];
int f[MN][5],sum[MN][5];//sum:前面维持原状的花费
int g[MN][5],h[MN][5];//g:保持 h:刚好超过
vector<pii >ans,W[MN][5];
void solve(int st,int i,int j,int c){
if(st>i)return;
if(f[n][c]<j)return;
int l=st-1,r=n+1;
while(l+1<r){
int mid=(l+r)>>1;
int now=j-sum[mid-1][c]+sum[st-1][c],use=0,ok=0;//ok:now用得完
if(now<1){r=mid;continue;}
if(now>g[mid][c]&&now<=h[mid][c]){l=mid;continue;}
for(auto x:W[mid][c]){
use=x.y;
if(now<=f[n-(mid+x.y)][c-x.y]){ok=1; break;}
now-=f[n-(mid+x.y)][c-x.y];
}
if(ok&&use){
if(use)ans.push_back(mp(mid,mid+use));
solve(mid+use+1,i,now,c-use);
return;
}
if(!ok)r=mid;
else l=mid;
}
assert(r==n+1);
}
signed main(){
T=read();
for(int i=0;i<=4;++i)f[0][i]=1;
for(int i=1;i<=30000;++i)
for(int j=0;j<=4;++j){
for(int k=0;k<=j&&k<i;++k){
f[i][j]=(f[i][j]+f[i-k-1][j-k]);
}
}
while(T--){
n=read();c=read();q=read();
for(int i=1;i<=n;++i)p[i]=read();
for(int mid=1;mid<=n;++mid){
vector<pii> w;
for(int k=0;k<=c&&mid+k<=n;++k){
w.push_back(mp(p[mid+k],k));
}
sort(w.begin(),w.end());
for(int k=0;k<=c;++k){
W[mid][k].clear();
g[mid][k]=h[mid][k]=0;
int ok=0;
for(auto i:w){
if(i.y<=k){
W[mid][k].push_back(i);
if(i.y==0){
ok=1;
h[mid][k]+=f[n-mid-i.y][k-i.y];
}
if(!ok){
g[mid][k]+=f[n-mid-i.y][k-i.y];
h[mid][k]+=f[n-mid-i.y][k-i.y];
}
}
}
}
}
for(int i=1;i<=n;++i)
for(int j=1;j<=c;++j){
sum[i][j]=sum[i-1][j];
for(int k=1;k<=j&&i+k<=n;++k)
if(p[i]>p[i+k]){
sum[i][j]+=f[n-i-k][j-k];
}
}
while(q--){
int i=read(),j=read();
if(f[n][c]<j){puts("-1");continue;}
ans.clear();
solve(1,i,j,c);
bool ok=0;
for(auto w:ans){
if(w.x<=i&&i<=w.y){
printf("%lld\n",p[w.y-(i-w.x)]);
ok=1;
}
}
if(!ok)printf("%lld\n",p[i]);
}
}
return 0;
} |
#include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef long long ll;
cs int N = 3e4 + 50;
int T, n, C, q, a[N];
int len[N][5][6];
ll dp[N][5], s[N][5][16], w[N][5][6];
int qry(int p, int k, ll x, int y){
assert(x > 0);
for(int i = 15; ~i; i--)
if((p + (1 << i)) <= n + 1) {
bool ok = s[p][k][i] < x;
ll way = dp[n - p - (1 << i) + 1][k];
ok &= way >= x - s[p][k][i];
if(ok) x -= s[p][k][i], p += 1 << i;
}
if(p > y) return a[y];
for(int i = 0; i <= k; i++) {
if(x <= w[p][k][i]) {
// flip [p, p + len[p][k][i] ]
int l = len[p][k][i];
if(y <= p + l) {
static int b[6];
for(int z = 0; z <= l; z++)
b[z] = a[p + z];
reverse(b, b + l + 1);
return b[y - p];
}
return qry(p + l + 1, k - l, x, y);
} x -= w[p][k][i];
}
}
void Main(){
scanf("%d%d%d", &n, &C, &q);
// cout << n << " " << C<<" "<<q<<endl;
for(int i = 0; i <= n; i++)
for(int j = 0; j <= C; j++)
dp[i][j] = 0;
dp[0][0] = 1;
for(int i = 1; i <= n; i++){
for(int j = 0; j <= C; j++)
for(int k = 0; k <= j; k++)
if(i >= k + 1)
dp[i][j] += dp[i - k - 1][j - k];
}
for(int i = 0; i <= n; i++)
for(int j = 1; j <= C; j++)
dp[i][j] += dp[i][j - 1];
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for(int i = 1; i <= n; i++)
for(int k = 0; k <= C; k++){
vector<pi> z;
for(int j = 0; j <= k && i + j <= n; j++)
z.pb(pi(a[i + j], j));
static ll c[6];
sort(z.begin(), z.end());
for(int j = 0; j < z.size(); j++) {
len[i][k][j] = z[j].se;
w[i][k][j] = c[j] =
dp[n - (i + z[j].se)][k - z[j].se];
}
// cout << "Case " << i << " " << k << endl;
// for(int j = 0; j < z.size(); j++)
// cout <<z[j].se << " " << c[j] <<endl ; cout << endl;
for(int j = 0; j < z.size(); j++){
if(j) c[j] += c[j - 1];
if(z[j].se == 0)
s[i][k][0] = j ? c[j - 1] : 0;
}
}
for(int t = 1; t <= 15; t++)
for(int i = 1; i + (1 << t) - 1 <= n; i++)
for(int j = 0; j <= C; j++)
s[i][j][t] = s[i][j][t - 1] + s[i + (1 << (t - 1))][j][t - 1];
while(q--){
ll x, y; scanf("%lld%lld", &y, &x);
if(x > dp[n][C]) puts("-1");
else cout << qry(1, C, x, y) << '\n';
}
}
int main(){
#ifdef FSYo
freopen("1.in", "r", stdin);
#endif
cin >> T;
while(T--) Main();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int N = 3e4;
#define ll __int128
ll dp[N][11][11];
ll dpsum[N][11];
vector <pair <int, int> > vec[N][11];
ll dppref[N][11];
ll dp2[N][11][11];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n, c, q;
cin >> n >> c >> q;
vector <int> p(n);
for(int i = 0; i < n; i++)
{
cin >> p[i];
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= 10; j++)
{
dppref[i][j] = 0;
dpsum[i][j] = 0;
for(int c1 = 0; c1 <= 10; c1++)
{
dp[i][j][c1] = 0;
}
}
}
for(int i = n - 1; i >= 0; i--)
{
for(int j = 0; j <= c && i + j < n; j++)
{
if(i + j == n - 1)
{
dp[i][j][j] = 1;
}
else
{
for(int k = 0; k + j <= c; k++)
{
dp[i][j + k][j] += dpsum[i + j + 1][k];
}
}
}
for(int j = 0; j <= c; j++)
{
for(int k = 0; k <= c; k++)
{
dpsum[i][j] += dp[i][j][k];
}
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= c; j++)
{
for(int k = 0; k <= c; k++)
{
if(k + i >= n || p[k + i] >= p[i])
{
continue;
}
dppref[i][j] += dp[i][j][k];
}
if(j != 0)
{
dppref[i][j] += dppref[i][j - 1];
}
}
}
for(int i = 1; i < n; i++)
{
for(int j = 0; j <= c; j++)
{
if(i != 0)
{
dppref[i][j] += dppref[i - 1][j];
}
}
}
for(int i = 0; i < n; i++)
{
for(int k = 0; k <= c; k++)
{
for(int j = 0; j <= c; j++)
{
dp2[i][j][k] = dp[i][j][k];
if(j != 0)
{
dp2[i][j][k] += dp2[i][j - 1][k];
}
}
}
}
ll dp1[n][11];
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= c; j++)
{
dp1[i][j] = dp[i][j][0];
if(j != 0){
dp1[i][j] += dp[i][j - 1][0];
}
}
}
ll sumall = 0;
for(int j = 0;j <= c; j++)
{
sumall += dpsum[0][j];
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= c; j++)
{
vec[i][j].clear();
if(j != 0)
{
vec[i][j] = vec[i][j - 1];
}
if(j + i < n)
{
vec[i][j].push_back({p[i + j], j});
sort(vec[i][j].begin(), vec[i][j].end());
}
}
}
while(q--)
{
long long cnt;
int ind;
cin >> ind >> cnt;
ind--;
int ans = -1;
if(sumall >= cnt)
{
int st = 0;
int call = 0;
int ost = c;
while(st != n)
{
if(st != 5)
{
// cout << st << " " << ost << " " << cnt << "\n";
}
int l = st - 1, r = n - 1;
while(r - l > 1)
{
int midd = (r + l) / 2;
ll t1 = dppref[midd][ost];
if(st != 0)
{
t1 -= dppref[st - 1][ost];
}
ll t2 = t1 + dp1[midd][ost];
if(st != 5)
{
// cout << st << " " << midd << " " << t1 << " " << dp1[midd][ost] << "\n";
}
if(t1 < cnt && t2 >= cnt)
{
l = midd;
}
else
{
r = midd;
}
}
if(l != st - 1)
{
ll t1 = dppref[l][ost];
if(st != 0)
{
t1 -= dppref[st - 1][ost];
}
cnt -= t1;
}
if(ind >= st && ind < l + 1)
{
ans = p[ind];
}
// cout << st << " " << vec[st][1].size() << "\n";
st = l + 1;
for(int i = 0; i < vec[st][ost].size(); i++)
{
int it = vec[st][ost][i].second;
ll val = dp2[st][ost][it];
if(val >= cnt)
{
if(ind >= st && ind <= st + it)
{
ans = p[st + it - (ind - st)];
}
ost -= it;
st += it + 1;
break;
}
else
{
cnt -= val;
}
}
call++;
// assert(call <= 20);
//cout << "\n";
}
}
cout << ans << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
using namespace std;
int n,c,m;
int p[300009],ps[300009];
struct node
{
ll rk;
int bg,id;
bool operator<(const node &p)const{return rk<p.rk;}
};
struct seg
{
ll rep;
int l,r,c;
};
vector<node> q[5];
vector<pii> out[300009];
int err[300009];
ll cl[300009][5];
ll lf[300009],rf[300009];
ll cb(int a,int b)
{
if(a<0||b>a||b<0)
return 0;
b=min(b,a-b);
ll ans=1;
for(int i=1;i<=b;i++)
ans*=(a-i+1);
for(int i=1;i<=b;i++)
ans/=i;
return ans;
}
ll cal(int n,int c)
{
if(n==0)
return 1;
ll ans=0;
for(int i=0;i<=c;i++)
ans+=cb(n-1,i);
return ans;
}
void init()
{
for(int i=0;i<=300000;i++)
for(int j=0;j<=4;j++)
cl[i][j]=cal(i,j);
}
void solve()
{
deque<seg> dq;//remaining leng,remaining c
for(int i=c;i>=1;i--)
{
dq.clear();
dq.push_back(seg{1,1,0,i});
lf[n+1]=rf[n+1]=0;
for(int j=n;j>=1;j--)
{
vector<pii> tmp;
vector<seg> tft;
lf[j]=lf[j+1];
rf[j]=rf[j+1];
for(int k=j;k<=min(n,j+i);k++)
tmp.push_back(pii(p[k],k));
int fg=0;
sort(tmp.begin(),tmp.end());
for(pii t:tmp)
{
if(t.se==j)
fg=1;
else if(!fg)
{
tft.push_back(seg{cl[n-t.se][i-(t.se-j)],j,t.se,i-(t.se-j)});
lf[j]-=cl[n-t.se][i-(t.se-j)];
}
else
{
dq.push_back(seg{cl[n-t.se][i-(t.se-j)],j,t.se,i-(t.se-j)});
rf[j]+=cl[n-t.se][i-(t.se-j)];
}
}
for(auto it=tft.rbegin();it!=tft.rend();it++)
dq.push_front(*it);
}
// for(seg t:dq)
// printf("rep:%lld l:%d r:%d c:%d\n",t.rep,t.l,t.r,t.c);
for(auto it=q[i].begin();it!=q[i].end();it++)
{
it->rk+=(lf[it->bg]-lf[1]);
it->bg=1;
}
sort(q[i].begin(),q[i].end());
ll sm=0,lsm=0;
int lp=-1;
for(node t:q[i])
{
while(lp+1<dq.size()&&sm<t.rk)
{
lsm=sm,sm+=dq[++lp].rep;
// printf("lp:%d sm:%lld\n",lp,sm);
}
seg s=dq[lp];
out[t.id].push_back(pii(s.l,s.r));
q[s.c].push_back(node{t.rk-lsm,s.r+1,t.id});
}
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&c,&m);
for(int i=0;i<=c;i++)
q[i].clear();
for(int i=1;i<=n;i++)
scanf("%d",p+i);
ll tot=cl[n][c];
// printf("tot:%d\n",tot);
for(int i=1;i<=m;i++)
{
err[i]=0;
out[i].clear();
int a;
ll b;
scanf("%d%lld",&a,&b);
if(b<=tot)
q[c].push_back(node{b,1,i});
else
err[i]=1;
ps[i]=a;
}
solve();
for(int i=1;i<=m;i++)
if(err[i])
printf("-1\n");
else
{
int s=ps[i];
// printf("s:%d\n",s);
for(pii t:out[i])
if(t.fi<=s&&s<=t.se)
s=t.fi+t.se-s;
printf("%d\n",p[s]);
}
}
return 0;
} |
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mid ((l+r)>>1)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define fors(i, a, b) for(int i = (a); i < (b); ++i)
using namespace std;
const int maxn = 3e4 + 5;
ll dp[maxn][5];
int p[maxn];
int n, c, q;
int idx;
int cmp(int x, int y){
return p[idx+x] < p[idx+y];
}
vector<int> s[maxn];
ll L[maxn][5], R[maxn][5];
ll SL[maxn][5];
void init(){
scanf("%d%d%d", &n, &c, &q);
fors(i,1,n+1) scanf("%d", &p[i]);
p[0] = -1;
fors(i,0,c+1) dp[n+1][i] = 1;
for(int i = n; i > 0; --i){
s[i].clear();
for(int j = 0; j <= c; ++j){
if(i+j <= n) s[i].pb(j);
dp[i][j] = 0;
for(int k = 0; k <= j && i+k <= n; ++k) dp[i][j] += dp[i+k+1][j-k];
}
idx = i;
sort(s[i].begin(), s[i].end(),cmp);
}
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= c; ++j){
ll sum = 0;
for(int x: s[i]){
if(x > j) continue;
if(x == 0) break;
sum += dp[i+x+1][j-x];
}
L[i][j] = sum;
R[i][j] = sum + dp[i+1][j];
}
}
fors(i,1,n+1) fors(j,0,c+1) SL[i][j] = SL[i-1][j] + L[i][j];
//cout<<"s:"<<SL[3][1]<<endl;
}
int work(int pos, ll rk){
if(rk > dp[1][c]) return 0;
int cur = c;
for(int i = 1; i <= n; ++i){
if(i > pos) return pos;
//cout<<"i:"<<L[i][cur]<<endl;
if(rk > L[i][cur] && rk <= R[i][cur]){
//rk -= L[i][cur];
int l = i, r = n;
int res = -1;
ll del = SL[i-1][cur];
//cout<<"i:"<<i<<" del:"<<del<<endl;
while(l <= r){
//cout<<"mid:"<<mid<<" cur:"<<cur<<" L:"<<SL[mid][cur]-del<<" R:"<<SL[mid-1][cur]-del+R[mid][cur]<<endl;
if(SL[mid][cur]-del >= rk || SL[mid-1][cur]-del+R[mid][cur] < rk){
r = mid-1;
}else{
res = mid; l = mid+1;
}
}
assert(res != -1);
//cout<<"res:"<<res<<endl;
rk -= SL[res][cur]-del;
i = res;
}else if(rk <= L[i][cur]){
for(int x: s[i]){
if(x > cur) continue;
if(rk > dp[i+x+1][cur-x]) rk -= dp[i+x+1][cur-x];
else{
cur -= x;
if(pos <= i+x && pos >= i){
pos = i+x+i-pos;
}
i += x;
break;
}
}
}else{//rk > R[i][cur]
rk -= R[i][cur];
int f = 0;
for(int x: s[i]){
if(x > cur) continue;
if(x == 0){f = 1; continue;}
if(f == 0) continue;
if(rk > dp[i+x+1][cur-x]) rk -= dp[i+x+1][cur-x];
else{
cur -= x;
if(pos <= i+x && pos >= i) pos = i+x+i-pos;
i += x;
break;
}
}
}
}
return pos;
}
void sol(){
while(q--){
int pos; ll rk;
scanf("%d%lld", &pos, &rk);
int cur = c;
printf("%d\n", p[work(pos, rk)]);
}
}
int main()
{
int T;cin>>T;
while(T--){
init();
sol();
}
return 0;
}
/*
1
6 4 4
6 5 4 3 1 2
1 1
3 14
1 59
2 6
*/
|
#include <bits/stdc++.h>
int main() {
using namespace std;
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int T; cin >> T;
while (T--) {
int N, C, Q; cin >> N >> C >> Q;
vector<int> P(N); for (auto& a : P) cin >> a;
const int64_t INF = 2e18;
vector<vector<array<int, 2>>> cur_flip(C+1);
vector<vector<int64_t>> weight(C+1);
cur_flip[0] = vector<array<int, 2>>(1, {N, N});
weight[0] = vector<int64_t>({0, 1});
vector<vector<array<int, 2>>> bounds(C+1, vector<array<int, 2>>(N+1, {0,1}));
vector<vector<int64_t>> node_weight(C+1, vector<int64_t>(N+1, 1));
for (int c = 1; c <= C; c++) {
cur_flip[c] = vector<array<int, 2>>(2*N*c+1, {N,N});
weight[c] = vector<int64_t>(2*N*c+2, 0);
int lo = N*c;
int hi = N*c+1;
weight[c][lo] = 0;
weight[c][hi] = 1;
bounds[c][N] = {lo, hi};
for (int i = N-1; i >= 0; i--) {
// we could do nothing
vector<pair<int, int>> opts(min(N-i, c+1));
for (int v = 0; v < min(N-i, c+1); v++) {
opts[v] = {P[i+v], v};
}
sort(opts.begin(), opts.end());
int x0 = 0;
while (opts[x0].second != 0) x0++;
for (int z = x0-1; z >= 0; z--) {
int v = opts[z].second;
assert(v != 0);
auto w = node_weight[c-v][i+v+1];
weight[c][lo-1] = weight[c][lo] - w;
cur_flip[c][lo-1] = {i, i+v};
lo--;
}
for (int z = x0+1; z < int(opts.size()); z++) {
int v = opts[z].second;
assert(v != 0);
auto w = node_weight[c-v][i+v+1];
weight[c][hi+1] = weight[c][hi] + w;
cur_flip[c][hi] = {i, i+v};
hi++;
}
node_weight[c][i] = weight[c][hi] - weight[c][lo];
bounds[c][i] = {lo, hi};
}
}
auto query = [&](int idx, int64_t perm) -> int {
int cur_c = C;
int cur_idx = 0;
if (perm > node_weight[cur_c][cur_idx]) return -1;
while (true) {
// look for the current flip
auto [mi, ma] = bounds[cur_c][cur_idx];
while (ma - mi > 1) {
int md = (mi + ma) / 2;
if (perm <= weight[cur_c][md] - weight[cur_c][mi]) {
ma = md;
} else {
perm -= weight[cur_c][md] - weight[cur_c][mi];
mi = md;
}
}
assert(1 <= perm && perm <= weight[cur_c][ma] - weight[cur_c][mi]);
auto [f1, f2] = cur_flip[cur_c][mi];
if (idx < f1) {
return P[idx];
} else if (f1 <= idx && idx <= f2) {
return P[f1 + f2 - idx];
} else {
cur_c -= f2 - f1;
cur_idx = f2+1;
}
}
};
for (int q = 0; q < Q; q++) {
int idx; int64_t perm; cin >> idx >> perm; idx--;
cout << query(idx, perm) << '\n';
}
}
return 0;
}
|
//starusc
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
int x=0,f=1,c=getchar();
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
return f==1?x:-x;
}
const int N=3e4+4;
int n,c,Q,p[N],st[5][N],ed[5][N],C[N][5],a[6];
inline bool comp(int x,int y){
return p[x]<p[y];
}
inline void solve(){
n=read();c=read();Q=read();
for(int i=1;i<=n;i++)p[i]=read();
for(int j=0;j<=c;j++)
for(int i=1;i<=n;i++){
st[j][i]=st[j][i-1];
for(int u=min(n,i+j);u>i;u--)if(p[u]<p[i]){
st[j][i]+=C[n-u][j-u+i];
}
ed[j][i]=st[j][i]+C[n-i][j]-1;
}
for(int x,k,r,nw,v;Q--;){
x=read();k=read();
if(k>C[n][c]){puts("-1");continue;}
r=c;nw=0;
while(1){
v=min(upper_bound(st[r]+1,st[r]+n+1,k+st[r][nw]-1)-st[r],
upper_bound(ed[r]+1,ed[r]+n+1,k+st[r][nw]-1,greater<int>())-ed[r]);
if(v>x){cout<<p[x]<<"\n";break;}
k-=st[r][v-1]-st[r][nw];//
r=min(r,n-v);//
for(int i=0;i<=r;i++)a[i]=v+i;
sort(a,a+r+1,comp);
for(int i=0;i<=r;i++){
if(k<=C[n-a[i]][r-a[i]+v]){
r-=a[i]-v;
nw=a[i];
break;
}
else k-=C[n-a[i]][r-a[i]+v];
}
if(nw>=x){cout<<p[nw-(x-v)]<<"\n";break;}
}
}
}
signed main(){
for(int i=0;i<=4;i++)st[i][0]=C[0][i]=1;
for(int i=1;i<N;i++){
C[i][0]=1;
C[i][1]=C[i][0]+(i-1);
C[i][2]=C[i][1]+(i-1)*(i-2)/2;
C[i][3]=C[i][2]+(i-1)*(i-2)/2*(i-3)/3;
C[i][4]=C[i][3]+(i-1)*(i-2)/2*(i-3)/3*(i-4)/4;
}
for(int T=read();T--;)solve();
return (0-0);
} |
#include <bits/stdc++.h>
using namespace std;
#define mem(a, w) memset(a, w, sizeof(a))
#define all(a) a.begin(), a.end()
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
const int maxc = 4 + 5;
const int maxn = 30000 + 5;
struct data {
int l, r;
ll w;
data() { }
data(const int &_l, const int &_r, const ll &_w) {
l = _l; r = _r; w = _w;
return;
}
};
int N, Cost, Q, A[maxn];
deque<data> Dq[maxc], tl, tr;
vector<pii> Rev;
int Front[maxc][maxn];
ll Sum[maxc][maxn * maxc];
ll C(int n, int m) {
if(n < m) return 0;
ll ret = 1;
for(int i = 0; i < m; ++i)
ret = ret * (n - i);
for(int i = 1; i <= m; ++i)
ret = ret / i;
return ret;
}
ll ways(int len, int c) {
ll ret = 0;
for(int i = 0; i <= c; ++i) {
if(len <= 1) ret += (i == 0);
else ret += C(len - 1, i);
}
return ret;
}
bool cmp(data x, data y) {
return A[x.r] < A[y.r];
}
data F(int p, int c, ll k) {
int res = 0;
int i = Front[c][p];
int l = i + 1, r = Dq[c].size();
while(l <= r) {
int mid = l + r >> 1;
if(Sum[c][mid] - Sum[c][i] >= k) {
res = mid;
r = mid - 1;
}
else
l = mid + 1;
}
return data(Dq[c][res - 1].l, Dq[c][res - 1].r, Sum[c][res - 1] - Sum[c][i]);
}
void solve() {
scanf("%d%d%d", &N, &Cost, &Q);
for(int i = 1; i <= N; ++i)
scanf("%d", &A[i]);
for(int c = 1; c <= Cost; ++c) {
Dq[c].clear();
for(int i = 1; i <= N; ++i)
Front[c][i] = 0;
Dq[c].push_back(data(N, N, 1));
for(int i = N - 1; i >= 1; --i) {
while(!tl.empty()) tl.pop_back();
while(!tr.empty()) tr.pop_back();
for(int j = 1; j <= min(c, N - i); ++j) {
ll w = ways(N - (i + j), c - j);
if(A[i] < A[i + j]) {
tr.push_back( data(i, i + j, w) );
}
else {
tl.push_front( data(i, i + j, w) );
++Front[c][i + 1];
}
}
sort(all(tl), cmp);
sort(all(tr), cmp);
while(!tl.empty()) {
Dq[c].push_front(tl.back());
tl.pop_back();
}
while(!tr.empty()) {
Dq[c].push_back(tr.front());
tr.pop_front();
}
}
// printf("c: %d\n", c);
// for(int i = 0; i < Dq[c].size(); ++i) {
// printf("(%d, %d, %lld)\n", Dq[c][i].l, Dq[c][i].r, Dq[c][i].w);
// }
// puts("");
for(int i = 1; i <= N; ++i)
Front[c][i] += Front[c][i - 1];
for(int i = 1; i <= Dq[c].size(); ++i)
Sum[c][i] = Sum[c][i - 1] + Dq[c][i - 1].w;
}
ll limit = ways(N, Cost);
while(Q--) {
int pos; ll rk;
scanf("%d%lld", &pos, &rk);
if(rk > limit) {
puts("-1");
continue;
}
while(!Rev.empty()) Rev.pop_back();
int c = Cost, p = 1;
while(c && p <= N) {
data tmp = F(p, c, rk);
//assert(rk - tmp.w > 0);
Rev.push_back(make_pair(tmp.l, tmp.r));
rk -= tmp.w;
p = tmp.r + 1;
c -= (tmp.r - tmp.l);
}
bool flag = 0;
for(auto x: Rev) {
// printf("(%d, %d)\n", x.fi, x.se);
if(x.fi <= pos && pos <= x.se) {
flag = 1;
printf("%d\n", A[x.se - (pos - x.fi)]);
break;
}
}
if(!flag) printf("%d\n", A[pos]);
}
return;
}
int main() {
int T; scanf("%d", &T);
while(T--) {
solve();
}
return 0;
}
/*
1
6 4 4
6 5 4 3 1 2
*/ |
/**
* author: tourist
* created: 05.01.2021 18:25:48
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n, c, q;
cin >> n >> c >> q;
vector<int> p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
--p[i];
}
vector<vector<long long>> ways(n + 1, vector<long long>(c + 1));
for (int j = 0; j <= c; j++) {
ways[n][j] = 1;
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= c; j++) {
ways[i][j] = ways[i + 1][j];
for (int k = 1; k <= j; k++) {
if (i + k < n) {
ways[i][j] += ways[i + k + 1][j - k];
}
}
}
}
vector<vector<long long>> sumL(c + 1, vector<long long>(n + 1));
vector<vector<long long>> sumR(c + 1, vector<long long>(n + 1));
for (int rm = 0; rm <= c; rm++) {
for (int i = 0; i < n; i++) {
sumL[rm][i + 1] = sumL[rm][i];
sumR[rm][i + 1] = sumR[rm][i];
for (int k = 1; k <= rm; k++) {
if (i + k < n) {
if (p[i] < p[i + k]) {
sumR[rm][i + 1] += ways[i + k + 1][rm - k];
} else {
sumL[rm][i + 1] += ways[i + k + 1][rm - k];
}
}
}
}
}
vector<int> order;
while (q--) {
int pos;
long long id;
cin >> pos >> id;
--pos; --id;
if (id >= ways[0][c]) {
cout << -1 << '\n';
continue;
}
int ans = -1;
int i = 0;
int rm = c;
while (i < n) {
assert(id < ways[i][rm]);
int low = i, high = n;
while (low < high) {
int mid = (low + high + 1) >> 1;
if (sumL[rm][mid] - sumL[rm][i] > id || sumR[rm][mid] - sumR[rm][i] > ways[i][rm] - 1 - id) {
high = mid - 1;
} else {
low = mid;
}
}
if (pos < low) {
ans = p[pos];
break;
}
id -= sumL[rm][low] - sumL[rm][i];
assert(id >= 0 && id < ways[low][rm]);
i = low;
order.clear();
for (int k = 0; k <= rm; k++) {
if (i + k < n) {
order.push_back(k);
}
}
sort(order.begin(), order.end(), [&](int x, int y) {
return p[i + x] < p[i + y];
});
int shift = -1;
for (int v : order) {
long long cur = ways[i + v + 1][rm - v];
if (id < cur) {
shift = v;
break;
}
id -= cur;
}
assert(shift != -1);
assert(shift != 0);
if (pos <= i + shift) {
ans = p[i + (i + shift) - pos];
break;
}
i += shift + 1;
rm -= shift;
}
assert(ans != -1);
cout << ans + 1 << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int) ((x).size()))
typedef long long ll;
typedef long double ld;
const ll inf = 1e18 + 10;
int t, n, c, q, p[40100], s[40100][5][5], ts[40100][5];
ll dp[40100][5], dpl[40100][5], dpr[40100][5], pdpl[40100][5];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
cin >> t;
while (t--) {
cin >> n >> c >> q;
for (int i = 0; i < n; i++)
cin >> p[i];
for (int j = 0; j <= c; j++)
dp[n][j] = 1, dpl[n][j] = 0, dpr[n][j] = 1, ts[n][j] = 1, s[n][j][0] = 0;
for (int i = n - 1; i >= 0; i--)
for (int j = 0; j <= c; j++) {
ts[i][j] = 0;
dp[i][j] = 0;
for (int k = 0; k <= j && i + k < n; k++)
s[i][j][k] = k, ts[i][j]++;
sort(s[i][j], s[i][j] + ts[i][j], [&](int si, int sj){return p[i + si] < p[i + sj];});
for (int k = 0; k < ts[i][j]; k++) {
if (s[i][j][k] == 0)
dpl[i][j] = dp[i][j];
dp[i][j] = min(dp[i][j] + dp[i + 1 + s[i][j][k]][j - s[i][j][k]], inf);
if (s[i][j][k] == 0)
dpr[i][j] = dp[i][j] - dpl[i][j];
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j <= c; j++)
pdpl[i][j] = dpl[i][j] + (i > 0 ? pdpl[i - 1][j] : 0);
int qi;
ll qj;
for (int qq = 0; qq < q; qq++) {
cin >> qi >> qj, qi--, qj--;
if (qj >= dp[0][c]) {
cout << "-1\n";
continue;
}
int i = 0, j = c;
while (i < n) {
int l = i, r = n - 1;
while (l < r) {
int m = (l + r) / 2;
ll pdplm = pdpl[m][j] - (i > 0 ? pdpl[i - 1][j] : 0);
if (pdplm <= qj && qj < pdplm + dpr[m][j])
l = m + 1;
else
r = m;
}
qj -= (l > 0 ? pdpl[l - 1][j] : 0) - (i > 0 ? pdpl[i - 1][j] : 0);
i = l;
if (i < n) {
int k = 0;
while (qj >= dp[i + 1 + s[i][j][k]][j - s[i][j][k]])
qj -= dp[i + 1 + s[i][j][k]][j - s[i][j][k]], k++;
int sh = s[i][j][k];
int l = i, r = i + sh;
if (l <= qi && qi <= r)
qi = l + r - qi;
i += sh + 1;
j -= sh;
}
}
cout << p[qi] << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = 30100;
const int LOG = 15;
const int C = 5;
const ll MAX = (ll)2e18 ;
vector<int> E[N][C];
ll W[N][C];
int P[N];
ll add(ll a, ll b){
return min(a + b, MAX);
}
ll pf[C][N];
ll cmp[C][N][LOG];
void solve(){
int n, c, q;
cin >> n >> c >> q;
for(int i = 1; i <= n + 1; i ++ ){
for(int j = 0 ; j <= c; j ++ ){
E[i][j].clear();
W[i][j]=0;
pf[j][i] = 0ll;
cmp[j][i][0] = 0ll;
}
}
for(int i = 1; i <= n; i ++ ){
cin >> P[i];
}
for(int i = 0; i <= c; i ++ ){
W[n + 1][i] = 1;
}
for(int i = n; i >= 1; i -- ){
vector<pii> ord;
for(int j = 0 ; j <= c; j ++ ){
if(i + j <= n){
ord.push_back(mp(P[i + j], j));
}
}
sort(ord.begin(), ord.end());
for(auto x : ord){
for(int j = 0 ; j <= c; j ++ ){
if(j + x.se > c) continue;
E[i][j].push_back(x.se);
W[i][j] = add(W[i][j], W[i + x.se + 1][j + x.se]);
}
}
}
ll w;
for(int v = 0; v <= c; v ++ ){
for(int i = 1; i <= n; i ++ ){
pf[v][i] = pf[v][i - 1];
w = 0;
for(auto x : E[i][v]){
if(x == 0) {
w = W[i + 1][v];
break;
}
pf[v][i] = add(pf[v][i], W[i + x + 1][v + x]);
}
cmp[v][i][0] = add(pf[v][i], w);
}
}
for(int lg = 1; lg < LOG; lg ++ ){
for(int v = 0; v <= c; v ++ ){
for(int i = 1; i <= n; i ++ ){
if((i + (1 << lg) - 1) > n) continue;
cmp[v][i][lg] = min(cmp[v][i][lg-1], cmp[v][i+(1<<(lg-1))][lg - 1]);
}
}
}
int pi;
ll qi;
int ci, cj;
int ai, aj;
int sol;
int nx;
int tat;
for(int iq = 1; iq <= q; iq ++ ){
cin >> pi >> qi;
if(W[1][0] < qi) {
cout << "-1\n";
continue;
}
sol = -1;
ci = 1;
cj = 0;
while(ci <= n){
if(pf[cj][ci] - pf[cj][ci - 1] < qi && cmp[cj][ci][0] >= qi + pf[cj][ci-1]){
nx = ci;
for(int j = LOG - 1; j >= 0 ; j -- ){
tat = (nx + (1 << j));
if(tat > n + 1) continue;
if(pf[cj][tat - 1] - pf[cj][ci - 1] < qi && cmp[cj][nx][j] >= qi + pf[cj][ci-1]){
nx = tat;
}
}
qi -= pf[cj][nx-1]-pf[cj][ci-1];
if(ci <= pi && pi < nx){
sol = P[pi];
}
ci = nx;
}
else{
for(auto x : E[ci][cj]){
if(W[ci + x + 1][cj + x] < qi){
qi -= W[ci + x + 1][cj + x];
}
else{
ai = ci + x + 1;
aj = cj + x;
if(ci <= pi && pi < ai){
sol = P[ci + x - (pi - ci)];
}
ci = ai;
cj = aj;
break;
}
}
}
}
cout << sol << "\n";
}
}
int main(){
fastIO;
int tc;
cin >> tc;
for(int it = 1; it <= tc; it ++ )
solve();
return 0;
}
|
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 30005, MX = 5, Q = 3E5 + 5;
int n, c, q, t, a[N], ans[Q], pos[Q];
long long dp[N][MX], lex[Q], off[MX], cnt[MX + 1];
vector<pair<int, int>> cur, eve[N];
vector<int> dl[N];
set<pair<long long, int>> pro[MX];
void init() {
fill(dp[0], dp[0] + MX, 1);
for (int i = 1; i < N; i++) {
for (int j = 0; j < MX; j++) {
for (int k = 0; k <= j && k < i; k++) {
dp[i][j] += dp[i - k - 1][j - k];
}
}
}
}
int lower(long long u) {
for (int i = 1; i <= MX; i++) {
if (cnt[i] > u) {
return i - 1;
}
}
}
void solve() {
for (int i = 0; i < MX; i++) {
pro[i].clear();
}
fill(off, off + MX, 0);
cin >> n >> c >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= q; i++) {
cin >> pos[i] >> lex[i]; lex[i]--;
if (lex[i] >= dp[n][c]) {
ans[i] = -1;
} else {
eve[1].push_back({i, c});
dl[pos[i]].push_back(i);
ans[i] = 0;
}
}
for (int i = 1; i <= n; i++) {
for (auto [ind, cos] : eve[i]) {
pro[cos].insert({lex[ind] + off[cos], ind});
}
eve[i].clear();
cur.clear();
for (int j = 0; j <= c; j++) {
if (i + j <= n) {
cur.push_back({a[i + j], j});
}
sort(cur.begin(), cur.end());
long long l, r;
for (int k = 0; k < cur.size(); k++) {
auto [_, cos] = cur[k];
cnt[k + 1] = cnt[k] + dp[n - (i + cos + 1) + 1][j - cos];
if (cos == 0) {
l = cnt[k]; r = cnt[k + 1];
}
}
while (!pro[j].empty() && pro[j].begin()->fi - off[j] < l) {
auto [cl, ind] = *pro[j].begin(); pro[j].erase(pro[j].begin());
if (ans[ind] != 0) {
continue;
}
int p = lower(cl - off[j]);
auto [_, cos] = cur[p];
if (i + cos >= pos[ind]) {
ans[ind] = a[2 * i + cos - pos[ind]];
} else {
lex[ind] = cl - off[j] - cnt[p];
eve[i + cos + 1].push_back({ind, j - cos});
}
}
while (!pro[j].empty() && prev(pro[j].end())->fi - off[j] >= r) {
auto [cl, ind] = *prev(pro[j].end()); pro[j].erase(prev(pro[j].end()));
if (ans[ind] != 0) {
continue;
}
int p = lower(cl - off[j]);
auto [_, cos] = cur[p];
if (i + cos >= pos[ind]) {
ans[ind] = a[2 * i + cos - pos[ind]];
} else {
lex[ind] = cl - off[j] - cnt[p];
eve[i + cos + 1].push_back({ind, j - cos});
}
}
off[j] += l;
}
for (int ind : dl[i]) {
if (ans[ind] == 0) {
ans[ind] = a[i];
}
}
dl[i].clear();
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
init();
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cnt(int len, int coins)
{
ll ans = 0;
if (0 <= coins && 0 <= len) ans += 1;
if (1 <= coins && 1 <= len) ans += len - 1;
if (2 <= coins && 2 <= len) ans += 1ll * (len - 1) * (len - 2) / 2;
if (3 <= coins && 3 <= len) ans += 1ll * (len - 1) * (len - 2) * (len - 3) / 6;
if (4 <= coins && 4 <= len) ans += 1ll * (len - 1) * (len - 2) * (len - 3) * (len - 4) / 24;
return ans;
}
void solve()
{
int n, c, q;
cin >> n >> c >> q;
vector<int> p(n);
for (int i = 0; i < n; i++)
cin >> p[i], p[i]--;
struct Segment{
int rL, rR;
long long len, plen;
};
vector<deque<Segment> > s(c + 1);
for (int coins = 0; coins <= c; coins++)
{
s[coins].push_back({n, n, 1});
for (int i = n - 2; i >= 0; i--)
{
int me = p[i];
vector<pair<int, int> > le, ri;
for (int j = i + 1; j <= i + coins && j < n; j++)
{
if (p[j] < me)
{
le.push_back({p[j], j});
}
else
{
ri.push_back({p[j], j});
}
}
sort(le.begin(), le.end());
reverse(le.begin(), le.end());
sort(ri.begin(), ri.end());
for (int j = 0; j < le.size(); j++)
{
s[coins].push_front({i, le[j].second, cnt(n - 1 - le[j].second, coins - le[j].second + i)});
}
for (int j = 0; j < ri.size(); j++)
{
s[coins].push_back({i, ri[j].second, cnt(n - 1 - ri[j].second, coins - ri[j].second + i)});
}
}
for (int i = 0; i < s[coins].size(); i++)
{
s[coins][i].plen = s[coins][i].len + (i ? s[coins][i - 1].plen : 0);
}
}
const long long INFLL = 1e18 + 2222;
vector<vector<long long> > go(c + 1, vector<long long>(n + 1, INFLL));
for (int coins = 0; coins <= c; coins++)
{
for (int j = (int)s[coins].size() - 1; j >= 0; j--)
{
go[coins][s[coins][j].rL] = min(go[coins][s[coins][j].rL], s[coins][j].plen - s[coins][j].len);
}
}
for (int coins = 0; coins <= c; coins++)
{
for (int j = n - 1; j >= 0; j--)
{
go[coins][j] = min(go[coins][j], go[coins][j + 1]);
}
}
vector<int> answer(q, -2);
struct Ask{
long long j;
int i, id;
};
vector<vector<Ask> > asks(c + 1);
for (int ii = 0; ii < q; ii++)
{
long long j;
int i;
cin >> i >> j;
i--, j--;
if (j >= cnt(n, c))
{
answer[ii] = -1;
continue;
}
asks[c].push_back({j, i, ii});
}
for (int level = c; level >= 0; level--)
{
sort(asks[level].begin(), asks[level].end(), [&](Ask A, Ask B){
return A.j < B.j;
});
int pnt = 0;
for (auto e : asks[level])
{
while (pnt + 1 < s[level].size() && s[level][pnt].plen <= e.j)
pnt++;
int L = s[level][pnt].rL;
int R = s[level][pnt].rR;
if (e.i <= R)
{
if (L <= e.i)
answer[e.id] = p[L + R - e.i] + 1;
else
answer[e.id] = p[e.i] + 1;
}
else
{
int NL = level - (R - L);
asks[NL].push_back({e.j - s[level][pnt].plen + s[level][pnt].len + go[NL][R + 1], e.i, e.id});
}
}
vector<Ask> z;
swap(asks[level], z);
}
for (int i = 0; i < q; i++)
{
cout << answer[i] << "\n";
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
}
|
// problem: CF1470E
#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template<typename T> inline void ckmax(T& x, T y) { x = (y > x ? y : x); }
template<typename T> inline void ckmin(T& x, T y) { x = (y < x ? y : x); }
/* --------------- fast io --------------- */ // begin
namespace Fread {
const int SIZE = 1 << 21;
char buf[SIZE], *S, *T;
inline char getchar() {
if (S == T) {
T = (S = buf) + fread(buf, 1, SIZE, stdin);
if (S == T) return '\n';
}
return *S++;
}
} // namespace Fread
namespace Fwrite {
const int SIZE = 1 << 21;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() {
fwrite(buf, 1, S - buf, stdout);
S = buf;
}
inline void putchar(char c) {
*S++ = c;
if (S == T) flush();
}
struct NTR {
~ NTR() { flush(); }
} ztr;
} // namespace Fwrite
#ifdef ONLINE_JUDGE
#define getchar Fread :: getchar
#define putchar Fwrite :: putchar
#endif
namespace Fastio {
struct Reader {
template<typename T>
Reader& operator >> (T& x) {
char c = getchar();
T f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = getchar();
}
x *= f;
return *this;
}
Reader& operator >> (char& c) {
c = getchar();
while (c == '\n' || c == ' ') c = getchar();
return *this;
}
Reader& operator >> (char* str) {
int len = 0;
char c = getchar();
while (c == '\n' || c == ' ') c = getchar();
while (c != '\n' && c != ' ') {
str[len++] = c;
c = getchar();
}
str[len] = '\0';
return *this;
}
Reader(){}
} cin;
const char endl = '\n';
struct Writer {
template<typename T>
Writer& operator << (T x) {
if (x == 0) { putchar('0'); return *this; }
if (x < 0) { putchar('-'); x = -x; }
static int sta[45];
int top = 0;
while (x) { sta[++top] = x % 10; x /= 10; }
while (top) { putchar(sta[top] + '0'); --top; }
return *this;
}
Writer& operator << (char c) {
putchar(c);
return *this;
}
Writer& operator << (char* str) {
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer& operator << (const char* str) {
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer(){}
} cout;
} // namespace Fastio
#define cin Fastio :: cin
#define cout Fastio :: cout
#define endl Fastio :: endl
/* --------------- fast io --------------- */ // end
const int MAXC = 4;
const int MAXN = 3e4;
int n, c, q, a[MAXN + 5];
ll ways_eq(int len, int c) {
// 长度为 len, 操作代价和恰好为 c
// comb(len - 1, c)
if (len <= 1) {
return c == 0;
}
if (len - 1 < c) {
return 0;
}
ll res = 1;
for (int i = len - 1; i >= len - c; --i) {
res *= i;
}
for (int i = c; i > 1; --i) {
res /= i;
}
return res;
}
ll ways_leq(int len, int c) {
// 长度为 len, 操作代价和小于或等于 c
ll res = 0;
for (int i = 0; i <= c; ++i) {
res += ways_eq(len, i);
}
return res;
}
struct Node {
int l, r;
ll w; // 后面的操作方案数
Node() {}
Node(int _l, int _r, ll _w) {
l = _l;
r = _r;
w = _w;
}
};
Node dq[MAXC + 1][MAXN * MAXC * 2 + 10];
int ql[MAXC + 1], qr[MAXC + 1];
int sum[MAXC + 1][MAXN + 5];
ll sumw[MAXC + 1][MAXN * MAXC + 5];
bool cmp(Node lhs, Node rhs) {
return a[lhs.r] < a[rhs.r];
}
Node F(int st, ll rank, int s) {
int i = sum[s][st]; // 在 st 之前的, 一共有这么多区间
/*
// 暴力查找
ll cur = 0;
for (i = ql[s] + i; i <= qr[s]; ++i) {
cur += dq[s][i].w;
assert(cur == sumw[s][i - ql[s] + 1] - sumw[s][sum[s][st]]);
if (cur >= rank) {
return Node(dq[s][i].l, dq[s][i].r, cur - dq[s][i].w);
}
}
*/
int l = i + 1, r = qr[s] - ql[s] + 2;
while (l < r) {
int mid = (l + r) >> 1;
if (sumw[s][mid] - sumw[s][i] >= rank) {
r = mid;
} else {
l = mid + 1;
}
}
assert(l <= qr[s] - ql[s] + 1);
return Node(dq[s][ql[s] + l - 1].l, dq[s][ql[s] + l - 1].r, sumw[s][l - 1] - sumw[s][i]);
}
void solve_case() {
cin >> n >> c >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int s = 1; s <= c; ++s) { // 总代价小于或等于 s
// cerr << "-------- maxcost " << s << " --------" << endl;
ql[s] = MAXN * MAXC + 5, qr[s] = MAXN * MAXC + 4; // 队列清空
for (int i = 1; i <= n; ++i)
sum[s][i] = 0;
dq[s][++qr[s]] = Node(n, n, 1); // 什么都不翻转
for (int i = n - 1; i >= 1; --i) {
int dl = 0, dr = 0;
for (int j = 1; j <= min(s, n - i); ++j) {
// 翻转区间 [i, i + j]
ll w = ways_leq(n - (i + j), s - j);
if (a[i + j] < a[i]) {
// 翻转后是 a[i + j], 不翻转是 a[i], 两者比一比
// 翻转后更小, push_front
dq[s][ql[s] - (++dl)] = Node(i, i + j, w);
sum[s][i + 1]++;
} else {
dq[s][qr[s] + (++dr)] = Node(i, i + j, w);
}
}
if (dl) {
sort(dq[s] + ql[s] - dl, dq[s] + ql[s], cmp);
ql[s] -= dl;
}
if (dr) {
sort(dq[s] + qr[s] + 1, dq[s] + qr[s] + dr + 1, cmp);
qr[s] += dr;
}
}
// cerr << "print queue: " << endl;
// for (int i = ql[s]; i <= qr[s]; ++i) {
// cerr << dq[s][i].l << " " << dq[s][i].r << " " << dq[s][i].w << endl;
// }
// cerr << "queue end" << endl;
for (int i = 1; i <= n; ++i) {
sum[s][i] += sum[s][i - 1];
}
for (int i = ql[s]; i <= qr[s]; ++i) {
sumw[s][i - ql[s] + 1] = sumw[s][i - ql[s]] + dq[s][i].w;
}
}
ll lim = ways_leq(n, c);
for (int tq = 1; tq <= q; ++tq) {
int pos;
ll rank;
// cerr << "-------- query: " << endl;
cin >> pos >> rank;
if (rank > lim) {
cout << -1 << endl;
continue;
}
vector<pii> revs;
int p = 1;
int s = c;
while (1) {
Node t = F(p, rank, s);
// cerr << "** " << t.l << " " << t.r << " " << t.w << endl;
revs.push_back(make_pair(t.l, t.r));
assert(t.w < rank);
rank -= t.w;
s -= (t.r - t.l);
p = t.r + 1;
if (!s)
break;
if (p > n)
break;
}
/*
// 暴力翻转
static int aa[MAXN + 5];
for (int i = 1; i <= n; ++i) {
aa[i] = a[i];
}
for (int i = 0; i < SZ(revs); ++i) {
reverse(aa + revs[i].fi, aa + revs[i].se + 1);
}
cout << aa[pos] << endl;
*/
bool flag = 0;
for (int i = 0; i < SZ(revs); ++i) {
if (revs[i].fi <= pos && revs[i].se >= pos) {
cout << a[revs[i].se - (pos - revs[i].fi)] << endl;
flag = 1;
break;
}
}
if (!flag) {
cout << a[pos] << endl;
}
}
}
int main() {
int T; cin >> T; while (T--) {
solve_case();
}
return 0;
} |
#pragma GCC optimize("Ofast")
//#pragma GCC target ("sse4")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 10;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
template<typename T>
struct SegT {
private:
int sz; vector<T> node;
T init_c;
function<T(T, T)> f;
public:
SegT(vector<T> v, T _init_c, function<T(T, T)> _f) {
init_c = _init_c; f = _f;
int n = v.size();
sz = 1;
while (sz < n)sz *= 2;
node.resize(2 * sz - 1, init_c);
rep(i, n) {
node[i + sz - 1] = v[i];
}
per(i, sz - 1) {
node[i] = f(node[2 * i + 1], node[2 * i + 2]);
}
}
SegT(int n, T _init_c, function<T(T, T)> _f) {
init_c = _init_c; f = _f;
sz = 1;
while (sz < n)sz *= 2;
node.resize(2 * sz - 1, init_c);
}
void update(int k, T a) {
k += sz - 1;
node[k] = a;
while (k > 0) {
k = (k - 1) / 2;
node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
}
}
T query(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)r = sz;
if (r <= a || b <= l)return init_c;
else if (a <= l && r <= b)return node[k];
else {
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return f(vl, vr);
}
}
};
ll num[30005][5];
void init(int c, int n) {
rep(i, n + 1)rep(j, c + 1)num[i][j] = 0;
rep(j, c + 1)num[0][j] = 1;
rep1(i, n) {
rep(j, 5) {
rep(k, j + 1) {
if (i - (k + 1) >= 0) {
num[i][j] += num[i - (k + 1)][j - k];
}
}
}
}
}
void solve() {
int n, c, q; cin >> n >> c >> q;
init(c, n);
vector<int> p(n);
rep(i, n) {
cin >> p[i]; p[i]--;
}
vector<vector<ll>> ad(c+1, vector<ll>(n));
vector<vector<ll>> rad(c + 1, vector<ll>(n + 1));
rep(i, n) {
rep(k, c + 1) {
vector<LP> vs;
rep(j, k + 1) {
if (i + j < n) {
vs.push_back({ p[i + j],num[n - i - (j + 1)][k - j] });
}
}
sort(all(vs));
rep(j, vs.size()) {
if (vs[j].first == p[i])break;
ad[k][i] += vs[j].second;
ad[k][i] = min(ad[k][i], INF);
}
rad[k][i + 1] = rad[k][i] + ad[k][i];
rad[k][i + 1] = min(rad[k][i + 1], INF);
}
}
//cout << ad[4][0] << "\n";
auto query = [&](ll k, int i) {
if (num[n][c] <= k)return -2;
int loc = 0;
int rc = c;
while (true) {
int l = loc-1;
int r = n;
while (r - l > 1) {
int m = (l + r) / 2;
ll cl = rad[rc][m + 1] - rad[rc][loc];
ll cr = cl + num[n - m - 1][rc];
//cout << k <<" "<<m<< " " << cl << " " << cr << "\n";
if (cl <= k && k < cr) {
l = m;
}
else {
r = m;
}
}
//cout << "? " << loc<<" "<<r << "\n";
k -= rad[rc][r] - rad[rc][loc];
loc = r;
if (i < loc) {
return p[i];
}
vector<pair<int,LP>> vs;
rep(j, rc+1) {
if (loc + j < n) {
vs.push_back({ p[loc + j],{j,num[n - loc - j - 1][rc - j]} });
}
}
//cout << loc << " babde " <<k<<" "<<vs.size()<< "\n";
sort(all(vs));
rep(j, vs.size()) {
//cout << k << " " << vs[j].second.second << "\n";
if (vs[j].second.second <= k) {
k -= vs[j].second.second;
}
else {
int len = vs[j].second.first;
//cout << "??? " << len << "\n";
if (loc + len >= i) {
return p[loc + len - (i - loc)];
}
loc += len + 1;
rc -= vs[j].second.first;
break;
}
}
}
};
rep(aa, q) {
int i; ll j; cin >> i >> j; i--;j--;
int ans = query(j, i);
//cout << "ans is ";
cout << ans+1 << "\n";
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(15);
//init_f();
//init();
//expr();
int t; cin >> t; rep(i, t)
solve();
return 0;
} |
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class S, class T> inline S min_L(S a,T b){
return a<=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
(*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
(*arr)=(T*)(*mem);
(*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
walloc1d(arr, x2-x1, mem);
(*arr) -= x1;
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
sort(a, a+N);
}
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
int i;
pair<T1, T2>*arr;
walloc1d(&arr, N, &mem);
for(i=(0);i<(N);i++){
arr[i].first = a[i];
arr[i].second = b[i];
}
sort(arr, arr+N);
for(i=(0);i<(N);i++){
a[i] = arr[i].first;
b[i] = arr[i].second;
}
}
inline int my_getchar(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline void rd(long long &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline int rd_int(void){
int x;
rd(x);
return x;
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar(a);
}
inline void wt_L(int x){
int s=0;
int m=0;
char f[10];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar('-');
}
while(s--){
my_putchar(f[s]+'0');
}
}
template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
a[k] = aval;
}
template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
a[k] = aval;
b[k] = bval;
}
template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
for(i=sz-1;i>k;i--){
c[i] = c[i-1];
}
a[k] = aval;
b[k] = bval;
c[k] = cval;
}
template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
for(i=sz-1;i>k;i--){
c[i] = c[i-1];
}
for(i=sz-1;i>k;i--){
d[i] = d[i-1];
}
a[k] = aval;
b[k] = bval;
c[k] = cval;
d[k] = dval;
}
template<class T> struct Arr1d{
int n;
int mem;
T*d;
T& operator[](int a){
return d[a];
}
void sort(){
reset();
std::sort(d, d+n);
}
int set_cumulative_sum;
int cumulative_sum_mem;
T*cumulative_sum;
void setSum(void){
int i;
set_cumulative_sum = 1;
if(cumulative_sum_mem < n+1){
delete[] cumulative_sum;
cumulative_sum = new T[n+1];
cumulative_sum_mem = n+1;
}
cumulative_sum[0] = 0;
for(i=(0);i<(n);i++){
cumulative_sum[i+1] = cumulative_sum[i] + d[i];
}
}
T getSum(int i, int j){
if(set_cumulative_sum==0){
setSum();
}
return cumulative_sum[j+1] - cumulative_sum[i];
}
int set_const_len_left;
int const_len_left_mem;
int*const_len_left;
void setConstLenLeft(void){
int i;
set_const_len_left = 1;
if(const_len_left_mem < n){
delete[] const_len_left;
const_len_left = new int[n];
const_len_left_mem = n;
}
for(i=(0);i<(n);i++){
const_len_left[i] = 1;
}
for(i=(1);i<(n);i++){
if(d[i]==d[i-1]){
const_len_left[i] = const_len_left[i-1] + 1;
}
}
}
int ConstLenLeft(int st, T val){
if(!set_const_len_left){
setConstLenLeft();
}
if(val != d[st]){
return 0;
}
return const_len_left[st];
}
int ConstLenLeft(int st){
if(!set_const_len_left){
setConstLenLeft();
}
return const_len_left[st];
}
int ConstLenLeftCyclic(int st, T val){
if(!set_const_len_left){
setConstLenLeft();
}
st %= n;
if(st < 0){
st += n;
}
if(val != d[st]){
return 0;
}
if(const_len_left[st] != st+1 || d[st] != d[n-1]){
return const_len_left[st];
}
if(const_len_left[n-1] == n){
return 1073709056;
}
return const_len_left[st] + const_len_left[n-1];
}
int ConstLenLeftCyclic(int st){
if(!set_const_len_left){
setConstLenLeft();
}
st %= n;
if(st < 0){
st += n;
}
if(const_len_left[st] != st+1 || d[st] != d[n-1]){
return const_len_left[st];
}
if(const_len_left[n-1] == n){
return 1073709056;
}
return const_len_left[st] + const_len_left[n-1];
}
int set_const_len_right;
int const_len_right_mem;
int*const_len_right;
void setConstLenRight(void){
int i;
set_const_len_right = 1;
if(const_len_right_mem < n){
delete[] const_len_right;
const_len_right = new int[n];
const_len_right_mem = n;
}
for(i=(0);i<(n);i++){
const_len_right[i] = 1;
}
for(i=(n-1)-1;i>=(0);i--){
if(d[i]==d[i+1]){
const_len_right[i] = const_len_right[i+1] + 1;
}
}
}
int ConstLenRight(int st, T val){
if(!set_const_len_right){
setConstLenRight();
}
if(val != d[st]){
return 0;
}
return const_len_right[st];
}
int ConstLenRight(int st){
if(!set_const_len_right){
setConstLenRight();
}
return const_len_right[st];
}
int ConstLenRightCyclic(int st, T val){
if(!set_const_len_right){
setConstLenRight();
}
if(val != d[st]){
return 0;
}
st %= n;
if(st < 0){
st += n;
}
if(const_len_right[st] != n-st || d[st] != d[0]){
return const_len_right[st];
}
if(const_len_right[0] == n){
return 1073709056;
}
return const_len_right[st] + const_len_right[0];
}
int ConstLenRightCyclic(int st){
if(!set_const_len_right){
setConstLenRight();
}
st %= n;
if(st < 0){
st += n;
}
if(const_len_right[st] != n-st || d[st] != d[0]){
return const_len_right[st];
}
if(const_len_right[0] == n){
return 1073709056;
}
return const_len_right[st] + const_len_right[0];
}
int set_dhist;
int dhist_mem;
int*dhist;
int*dhists;
T dhist_mn;
T dhist_mx;
void setDHist(void){
int i;
int len;
set_dhist = 1;
if(n==0){
return;
}
dhist_mn = dhist_mx = d[0];
for(i=(1);i<(n);i++){
if(dhist_mn > d[i]){
dhist_mn = d[i];
}
if(dhist_mx < d[i]){
dhist_mx = d[i];
}
}
len = dhist_mx - dhist_mn + 1;
if(dhist_mem < len){
delete[] dhist;
dhist = new int[len];
delete[] dhists;
dhists = new int[len+1];
dhist_mem = len;
}
for(i=(0);i<(len);i++){
dhist[i] = 0;
}
for(i=(0);i<(n);i++){
dhist[d[i] - dhist_mn]++;
}
dhists[0] = 0;
for(i=(0);i<(len);i++){
dhists[i+1] = dhists[i] + dhist[i];
}
}
int dHist(T x){
if(set_dhist==0){
setDHist();
}
if(n == 0 || x < dhist_mn || x > dhist_mx){
return 0;
}
return dhist[x - dhist_mn];
}
int dHist(T x, T y){
if(set_dhist==0){
setDHist();
}
if(x < dhist_mn){
x = dhist_mn;
}
if(y > dhist_mx){
y = dhist_mx;
}
if(n == 0 || x > y){
return 0;
}
return dhists[y-dhist_mn+1] - dhists[x-dhist_mn];
}
int set_shist;
int shist_mem;
T*shist;
void setSHist(void){
int i;
set_shist = 1;
if(shist_mem < n){
delete[] shist;
shist = new T[n];
shist_mem = n;
}
for(i=(0);i<(n);i++){
shist[i] = d[i];
}
std::sort(shist, shist + n);
}
int sHist(T x){
if(set_shist==0){
setSHist();
}
auto r = equal_range(shist, shist+n, x);
return r.second - r.first;
}
int sHist(T x, T y){
if(set_shist==0){
setSHist();
}
return upper_bound(shist, shist+n, y) - lower_bound(shist, shist+n, x);
}
int set_prevLE;
int prevLE_mem;
int*prevLE;
int setPrevLE(void *mem = wmem){
int i;
int s = 0;
int*st;
set_prevLE = 1;
if(prevLE_mem < n){
delete[] prevLE;
prevLE = new int[n];
prevLE_mem = n;
}
walloc1d(&st, n, &mem);
for(i=(0);i<(n);i++){
while(s && d[st[s-1]] > d[i]){
s--;
}
if(s==0){
prevLE[i] = -1;
}
else{
prevLE[i] = st[s-1];
}
st[s++] = i;
}
}
int PrevLE(int i){
if(set_prevLE==0){
setPrevLE();
}
return prevLE[i];
}
int set_prevLT;
int prevLT_mem;
int*prevLT;
int setPrevLT(void *mem = wmem){
int i;
int s = 0;
int*st;
set_prevLT = 1;
if(prevLT_mem < n){
delete[] prevLT;
prevLT = new int[n];
prevLT_mem = n;
}
walloc1d(&st, n, &mem);
for(i=(0);i<(n);i++){
while(s && d[st[s-1]] >= d[i]){
s--;
}
if(s==0){
prevLT[i] = -1;
}
else{
prevLT[i] = st[s-1];
}
st[s++] = i;
}
}
int PrevLT(int i){
if(set_prevLT==0){
setPrevLT();
}
return prevLT[i];
}
int set_prevGE;
int prevGE_mem;
int*prevGE;
int setPrevGE(void *mem = wmem){
int i;
int s = 0;
int*st;
set_prevGE = 1;
if(prevGE_mem < n){
delete[] prevGE;
prevGE = new int[n];
prevGE_mem = n;
}
walloc1d(&st, n, &mem);
for(i=(0);i<(n);i++){
while(s && d[st[s-1]] < d[i]){
s--;
}
if(s==0){
prevGE[i] = -1;
}
else{
prevGE[i] = st[s-1];
}
st[s++] = i;
}
}
int PrevGE(int i){
if(set_prevGE==0){
setPrevGE();
}
return prevGE[i];
}
int set_prevGT;
int prevGT_mem;
int*prevGT;
int setPrevGT(void *mem = wmem){
int i;
int s = 0;
int*st;
set_prevGT = 1;
if(prevGT_mem < n){
delete[] prevGT;
prevGT = new int[n];
prevGT_mem = n;
}
walloc1d(&st, n, &mem);
for(i=(0);i<(n);i++){
while(s && d[st[s-1]] <= d[i]){
s--;
}
if(s==0){
prevGT[i] = -1;
}
else{
prevGT[i] = st[s-1];
}
st[s++] = i;
}
}
int PrevGT(int i){
if(set_prevGT==0){
setPrevGT();
}
return prevGT[i];
}
int set_nextLE;
int nextLE_mem;
int*nextLE;
int setNextLE(void *mem = wmem){
int i;
int s = 0;
int*st;
set_nextLE = 1;
if(nextLE_mem < n){
delete[] nextLE;
nextLE = new int[n];
nextLE_mem = n;
}
walloc1d(&st, n, &mem);
for(i=(n)-1;i>=(0);i--){
while(s && d[st[s-1]] > d[i]){
s--;
}
if(s==0){
nextLE[i] = n;
}
else{
nextLE[i] = st[s-1];
}
st[s++] = i;
}
}
int NextLE(int i){
if(set_nextLE==0){
setNextLE();
}
return nextLE[i];
}
int set_nextLT;
int nextLT_mem;
int*nextLT;
int setNextLT(void *mem = wmem){
int i;
int s = 0;
int*st;
set_nextLT = 1;
if(nextLT_mem < n){
delete[] nextLT;
nextLT = new int[n];
nextLT_mem = n;
}
walloc1d(&st, n, &mem);
for(i=(n)-1;i>=(0);i--){
while(s && d[st[s-1]] >= d[i]){
s--;
}
if(s==0){
nextLT[i] = n;
}
else{
nextLT[i] = st[s-1];
}
st[s++] = i;
}
}
int NextLT(int i){
if(set_nextLT==0){
setNextLT();
}
return nextLT[i];
}
int set_nextGE;
int nextGE_mem;
int*nextGE;
int setNextGE(void *mem = wmem){
int i;
int s = 0;
int*st;
set_nextGE = 1;
if(nextGE_mem < n){
delete[] nextGE;
nextGE = new int[n];
nextGE_mem = n;
}
walloc1d(&st, n, &mem);
for(i=(n)-1;i>=(0);i--){
while(s && d[st[s-1]] < d[i]){
s--;
}
if(s==0){
nextGE[i] = n;
}
else{
nextGE[i] = st[s-1];
}
st[s++] = i;
}
}
int NextGE(int i){
if(set_nextGE==0){
setNextGE();
}
return nextGE[i];
}
int set_nextGT;
int nextGT_mem;
int*nextGT;
int setNextGT(void *mem = wmem){
int i;
int s = 0;
int*st;
set_nextGT = 1;
if(nextGT_mem < n){
delete[] nextGT;
nextGT = new int[n];
nextGT_mem = n;
}
walloc1d(&st, n, &mem);
for(i=(n)-1;i>=(0);i--){
while(s && d[st[s-1]] <= d[i]){
s--;
}
if(s==0){
nextGT[i] = n;
}
else{
nextGT[i] = st[s-1];
}
st[s++] = i;
}
}
int NextGT(int i){
if(set_nextGT==0){
setNextGT();
}
return nextGT[i];
}
void reset(){
set_cumulative_sum = 0;
set_const_len_left = 0;
set_const_len_right = 0;
set_dhist = 0;
set_prevLE = set_prevLT = set_prevGE = set_prevGT = 0;
set_nextLE = set_nextLT = set_nextGE = set_nextGT = 0;
}
void memory_expand(int nn){
if(mem < nn){
delete[] d;
d = new T[nn];
mem = nn;
}
}
void malloc(int nn){
reset();
memory_expand(nn);
n = nn;
}
void setN(int nn){
reset();
memory_expand(nn);
n = nn;
}
void set(vector<T> &a){
int i;
int nn = a.size();
setN(nn);
for(i=(0);i<(nn);i++){
d[i] = a[i];
}
}
void set(int nn, T a[]){
int i;
setN(nn);
for(i=(0);i<(nn);i++){
d[i] = a[i];
}
}
void free(){
destructor();
}
void constructor(){
n = mem = 0;
d = NULL;
set_cumulative_sum = 0;
cumulative_sum_mem = 0;
cumulative_sum = NULL;
set_const_len_left = 0;
const_len_left_mem = 0;
const_len_left = NULL;
set_const_len_right = 0;
const_len_right_mem = 0;
const_len_right = NULL;
set_dhist = 0;
dhist_mem = 0;
dhist = NULL;
dhists = NULL;
set_shist = 0;
shist_mem = 0;
shist = NULL;
set_prevLE = set_prevLT = set_prevGE = set_prevGT = 0;
prevLE_mem = prevLT_mem = prevGE_mem = prevGT_mem = 0;
prevLE = prevLT = prevGE = prevGT = NULL;
set_nextLE = set_nextLT = set_nextGE = set_nextGT = 0;
nextLE_mem = nextLT_mem = nextGE_mem = nextGT_mem = 0;
nextLE = nextLT = nextGE = nextGT = NULL;
}
void constructor(int nn){
constructor();
malloc(nn);
}
void destructor(){
delete[] d;
d = NULL;
mem = n = 0;
set_cumulative_sum = 0;
cumulative_sum_mem = 0;
delete[] cumulative_sum;
cumulative_sum = NULL;
set_const_len_left = 0;
const_len_left_mem = 0;
delete[] const_len_left;
const_len_left = NULL;
set_const_len_right = 0;
const_len_right_mem = 0;
delete[] const_len_right;
const_len_right = NULL;
set_dhist = 0;
dhist_mem = 0;
delete[] dhist;
delete[] dhists;
dhist = NULL;
set_shist = 0;
shist_mem = 0;
delete[] shist;
shist = NULL;
set_prevLE = set_prevLT = set_prevGE = set_prevGT = 0;
prevLE_mem = prevLT_mem = prevGE_mem = prevGT_mem = 0;
delete[] prevLE;
delete[] prevLT;
delete[] prevGE;
delete[] prevGT;
prevLE = prevLT = prevGE = prevGT = NULL;
set_nextLE = set_nextLT = set_nextGE = set_nextGT = 0;
nextLE_mem = nextLT_mem = nextGE_mem = nextGT_mem = 0;
delete[] nextLE;
delete[] nextLT;
delete[] nextGE;
delete[] nextGT;
nextLE = nextLT = nextGE = nextGT = NULL;
}
Arr1d(){
constructor();
}
Arr1d(int nn){
constructor(nn);
}
~Arr1d(){
destructor();
}
}
;
int N;
int C;
int Q;
int A[30000];
int X;
long long Y;
long long cnt[5][30000+2];
int sz;
int lis[5];
int ind[5];
int usz[30000+2];
int ulis[30000+2][5];
int dsz[30000+2];
int dlis[30000+2][5];
Arr1d<__int128_t> skip[5];
int main(){
int t_ynMSdg;
wmem = memarr;
int i;
int j;
int k;
int c;
for(i=(0);i<(5);i++){
cnt[i][0] = 1;
}
for(i=(0);i<(5);i++){
for(k=(0);k<(30000);k++){
for(j=(0);j<(min_L(k, i)+1);j++){
cnt[i][k+1] += cnt[i-j][k-j];
if(cnt[i][k+1] > 2000000000000000000LL){
cnt[i][k+1] = 2000000000000000000LL;
}
}
}
}
int KrdatlYV = rd_int();
for(t_ynMSdg=(0);t_ynMSdg<(KrdatlYV);t_ynMSdg++){
int gEg5UqEA;
rd(N);
rd(C);
rd(Q);
{
int a2conNHc;
for(a2conNHc=(0);a2conNHc<(N);a2conNHc++){
rd(A[a2conNHc]);
}
}
for(k=(0);k<(N);k++){
sz = 0;
for(i=(0);i<(C+1);i++){
if(k+i < N){
arrInsert(sz, sz, ind, i, lis, A[k+i]);
}
}
sortA_L(sz, lis, ind);
j = usz[k] = dsz[k] = 0;
for(i=(0);i<(sz);i++){
if(ind[i] == 0){
j++;
continue;
}
if(j==0){
ulis[k][usz[k]++] = ind[i];
}
if(j==1){
dlis[k][dsz[k]++] = ind[i];
}
}
}
for(c=(0);c<(C+1);c++){
skip[c].setN(N);
for(k=(0);k<(N);k++){
skip[c][k] = 0;
for(i=(0);i<(usz[k]);i++){
if(c >= ulis[k][i]){
skip[c][k] += cnt[c-ulis[k][i]][N-k-ulis[k][i]-1];
}
}
}
}
for(gEg5UqEA=(0);gEg5UqEA<(Q);gEg5UqEA++){
rd(X);X += (-1);
rd(Y);Y += (-1);
if(Y >= cnt[C][N]){
wt_L(-1);
wt_L('\n');
continue;
}
c = C;
for(k=(0);k<(N);k++){
int X9Iss0pP;
int szDqbNYU;
int ytthggxT;
X9Iss0pP = k-1;
szDqbNYU = N-1;
while(X9Iss0pP < szDqbNYU){
if((X9Iss0pP + szDqbNYU)%2==0){
ytthggxT = (X9Iss0pP + szDqbNYU) / 2;
}
else{
ytthggxT = (X9Iss0pP + szDqbNYU + 1) / 2;
}
if(skip[c].getSum(k,ytthggxT) <= Y && Y < skip[c].getSum(k,ytthggxT) + cnt[c][N-ytthggxT-1]){
X9Iss0pP = ytthggxT;
}
else{
szDqbNYU = ytthggxT - 1;
}
}
i =szDqbNYU+ 1;
if(X < i){
wt_L(A[X]);
wt_L('\n');
break;
}
Y -= skip[c].getSum(k, i-1);
k = i;
for(i=(0);i<(usz[k]);i++){
if(c >= ulis[k][i]){
if(Y < cnt[c-ulis[k][i]][N-k-ulis[k][i]-1]){
if(X <= k + ulis[k][i]){
wt_L(A[k+ulis[k][i]-(X-k)]);
wt_L('\n');
goto aTqQ6rt8;
}
c -= ulis[k][i];
k += ulis[k][i];
goto zT28qSmp;
}
else{
Y -= cnt[c-ulis[k][i]][N-k-ulis[k][i]-1];
}
}
}
if(Y < cnt[c][N-k-1]){
if(X == k){
wt_L(A[k]);
wt_L('\n');
break;
}
continue;
}
else{
Y -= cnt[c][N-k-1];
}
for(i=(0);i<(dsz[k]);i++){
if(c >= dlis[k][i]){
if(Y < cnt[c-dlis[k][i]][N-k-dlis[k][i]-1]){
if(X <= k + dlis[k][i]){
wt_L(A[k+dlis[k][i]-(X-k)]);
wt_L('\n');
goto aTqQ6rt8;
}
c -= dlis[k][i];
k += dlis[k][i];
goto zT28qSmp;
}
else{
Y -= cnt[c-dlis[k][i]][N-k-dlis[k][i]-1];
}
}
}
zT28qSmp:;
}
aTqQ6rt8:;
}
}
return 0;
}
// cLay version 20210103-1
// --- original code ---
// //no-unlocked
// int N, C, Q, A[3d4], X; ll Y;
// ll cnt[5][3d4+2];
// int sz, lis[5], ind[5];
// int usz[3d4+2], ulis[3d4+2][5];
// int dsz[3d4+2], dlis[3d4+2][5];
// Arr1d<__int128_t> skip[5];
// {
// int i, j, k, c;
// rep(i,5) cnt[i][0] = 1;
// rep(i,5) rep(k,3d4) rep(j,min(k,i)+1){
// cnt[i][k+1] += cnt[i-j][k-j];
// if(cnt[i][k+1] > 2d18) cnt[i][k+1] = 2d18;
// }
// REP(rd_int()){
// rd(N,C,Q,A(N));
//
// rep(k,N){
// sz = 0;
// rep(i,C+1) if(k+i < N) arrInsert(sz, sz, ind, i, lis, A[k+i]);
// sortA(sz, lis, ind);
// j = usz[k] = dsz[k] = 0;
// rep(i,sz){
// if(ind[i] == 0) j++, continue;
// if(j==0) ulis[k][usz[k]++] = ind[i];
// if(j==1) dlis[k][dsz[k]++] = ind[i];
// }
// }
//
// rep(c,C+1){
// skip[c].setN(N);
// rep(k,N){
// skip[c][k] = 0;
// rep(i,usz[k]) if(c >= ulis[k][i]) skip[c][k] += cnt[c-ulis[k][i]][N-k-ulis[k][i]-1];
// }
// }
//
// rep(Q){
// rd(X--, Y--);
// if(Y >= cnt[C][N]) wt(-1), continue;
// c = C;
// rep(k,N){
// i = bsearch_max[int,i,k-1,N-1](skip[c].getSum(k,i) <= Y < skip[c].getSum(k,i) + cnt[c][N-i-1])+ 1;
// if(X < i) wt(A[X]), break;
// Y -= skip[c].getSum(k, i-1);
// k = i;
//
// rep(i,usz[k]) if(c >= ulis[k][i]){
// if(Y < cnt[c-ulis[k][i]][N-k-ulis[k][i]-1]){
// if(X <= k + ulis[k][i]) wt(A[k+ulis[k][i]-(X-k)]), break_break;
// c -= ulis[k][i];
// k += ulis[k][i];
// break_continue;
// } else {
// Y -= cnt[c-ulis[k][i]][N-k-ulis[k][i]-1];
// }
// }
// if(Y < cnt[c][N-k-1]){
// if(X == k) wt(A[k]), break;
// continue;
// } else {
// Y -= cnt[c][N-k-1];
// }
// rep(i,dsz[k]) if(c >= dlis[k][i]){
// if(Y < cnt[c-dlis[k][i]][N-k-dlis[k][i]-1]){
// if(X <= k + dlis[k][i]) wt(A[k+dlis[k][i]-(X-k)]), break_break;
// c -= dlis[k][i];
// k += dlis[k][i];
// break_continue;
// } else {
// Y -= cnt[c-dlis[k][i]][N-k-dlis[k][i]-1];
// }
// }
// }
// }
// }
// }
|
#include <bits/stdc++.h>
using namespace std;
const int N = 30000 + 1;
const int C = 4 + 1;
long long ways[N][C];
long long before[N][C], after[N][C], y;
int perm[N], n, c, q, x;
vector<int> options[N];
void solve(int i, int j) {
int l = i, r = n;
while (l < r) {
int m = (l + r + 1) / 2;
long long a = after[m][j] - after[i][j];
long long b = before[m][j] - before[i][j];
if (b <= y && y + a < ways[n - i][j]) {
l = m;
} else {
r = m - 1;
}
}
if (l == n) {
cout << perm[x] << "\n";
return;
}
y -= before[l][j] - before[i][j];
for (auto k : options[l]) {
int a = l + k + 1;
if (k <= j) {
if (ways[n - a][j - k] <= y) {
y -= ways[n - a][j - k];
} else {
reverse(perm + l, perm + a);
solve(a, j - k);
reverse(perm + l, perm + a);
return;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < C; ++j) {
if (i == 0) {
ways[i][j] = 1;
} else {
ways[i][j] = (j == 0) ? 0 : ways[i][j - 1];
long long choose = 1;
for (int k = 0; k < j; ++k) {
choose *= i - 1 - k;
choose /= k + 1;
}
ways[i][j] += choose;
}
}
}
int t;
cin >> t;
while (t--) {
cin >> n >> c >> q;
for (int i = 0; i < n; ++i) {
cin >> perm[i];
}
for (int i = 0; i < n; ++i) {
options[i].clear();
for (int j = 0; j <= c && i + j < n; ++j) {
options[i].push_back(j);
}
sort(options[i].begin(), options[i].end(), [&](int a, int b) {
return perm[i + a] < perm[i + b];
});
for (int j = 0; j <= c; ++j) {
before[i + 1][j] = after[i + 1][j] = 0;
for (auto k : options[i]) {
int a = i + k + 1;
if (k <= j) {
if (perm[i + k] < perm[i]) {
before[i + 1][j] += ways[n - a][j - k];
} else if (perm[i + k] > perm[i]) {
after[i + 1][j] += ways[n - a][j - k];
}
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= c; ++j) {
before[i + 1][j] += before[i][j];
after[i + 1][j] += after[i][j];
}
}
while (q--) {
cin >> x >> y;
--x, --y;
if (y >= ways[n][c]) {
cout << -1 << "\n";
} else {
solve(0, c);
}
}
}
}
|
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef NEAL_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
void run_case() {
int N, C, Q;
cin >> N >> C >> Q;
vector<int> P(N);
for (auto &p : P)
cin >> p;
vector<vector<array<int64_t, 2>>> range(C + 1, vector<array<int64_t, 2>>(N + 1, {0, 1}));
vector<vector<int64_t>> dp(C + 1, vector<int64_t>(N + 1, 0));
for (int c = 0; c <= C; c++)
dp[c][N] = 1;
for (int start = N - 1; start >= 0; start--) {
dp[0][start] = 1;
for (int c = 1; c <= C; c++)
for (int take = 1; take <= c + 1 && start + take <= N; take++)
dp[c][start] += dp[c - (take - 1)][start + take];
}
for (int start = N - 1; start >= 0; start--)
for (int c = 1; c <= C; c++) {
int64_t range_start = 0, range_end = 0;
for (int take = 1; take <= c + 1 && start + take <= N; take++) {
int last = P[start + take - 1];
if (last < P[start]) {
int64_t add = dp[c - (take - 1)][start + take];
range_start += add;
range_end += add;
} else if (last == P[start]) {
int64_t add = dp[c - (take - 1)][start + take];
range_end += add;
}
}
range[c][start] = {range_start, range_end};
}
vector<vector<int64_t>> range_start_sum(C + 1, vector<int64_t>(N + 2, 0));
for (int c = 0; c <= C; c++)
for (int i = 0; i <= N; i++)
range_start_sum[c][i + 1] = range_start_sum[c][i] + range[c][i][0];
for (int q = 0; q < Q; q++) {
int64_t idx, j;
cin >> idx >> j;
idx--; j--;
int c = C, answer = -1;
if (j >= dp[C][0]) {
cout << -1 << '\n';
continue;
}
int start = 0;
while (start < N) {
if (range[c][start][0] <= j && j < range[c][start][1]) {
if (idx == start) {
answer = P[idx];
goto finish;
}
int low = start, high = N;
while (low < high) {
int mid = low + (high - low) / 2;
int64_t range_sum = range_start_sum[c][mid] - range_start_sum[c][start];
int64_t range_start = range_sum + range[c][mid][0];
int64_t range_end = range_sum + range[c][mid][1];
if (range_start <= j && j < range_end)
low = mid + 1;
else
high = mid;
}
if (start <= idx && idx < low) {
answer = P[idx];
goto finish;
}
j -= range_start_sum[c][low] - range_start_sum[c][start];
start = low;
} else {
vector<pair<int, int>> choices;
for (int i = start; i <= start + c && i < N; i++)
choices.emplace_back(P[i], i);
sort(choices.begin(), choices.end());
for (auto &pr : choices) {
int i = pr.second;
int take = i - start + 1;
int64_t add = dp[c - (take - 1)][start + take];
if (0 <= j && j < add) {
if (start <= idx && idx < start + take) {
answer = P[start + (start + take - 1 - idx)];
goto finish;
}
c -= take - 1;
start += take;
break;
}
j -= add;
}
}
}
finish:
cout << answer << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
#ifndef NEAL_DEBUG
cin.tie(nullptr);
#endif
int tests;
cin >> tests;
while (tests-- > 0)
run_case();
}
|
//#pragma GCC target("avx,avx2")
#include<bits/stdc++.h>
//#include<immintrin.h>
using namespace std;
template <typename T> void chmin(T&x,const T &y)
{
if(x>y)x=y;
}
template <typename T> void chmax(T &x,const T &y)
{
if(x<y)x=y;
}
typedef int64_t s64;
typedef uint64_t u64;
typedef uint32_t u32;
typedef pair<int,int> pii;
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
#define rep0(i,l,r) for(int i=l;i<r;++i)
#define gc (c=getchar())
char readc()
{
char c;
while(isspace(gc));
return c;
}
int read()
{
char c;
while(gc<'-');
if(c=='-')
{
int x=gc-'0';
while(gc>='0')x=x*10+c-'0';
return -x;
}
int x=c-'0';
while(gc>='0')x=x*10+c-'0';
return x;
}
#undef gc
const int C=5,N=3e4+5;
s64 dp[C][N],sum[C][N],sum1[C][N];
int p[N];
vector<int>lk[N];
int main()
{
#ifdef kcz
freopen("1.in","r",stdin);//freopen("1.out","w",stdout);
#endif
rep(c,0,4)
{
dp[c][0]=1;
rep(n,1,30000)
rep(i,0,min(n-1,c))dp[c][n]+=dp[c-i][n-i-1];
}
int tt=read();
while(tt--)
{
int n=read(),c=read(),q=read();
rep(i,1,n)p[i]=read();
per(i,n,1)
{
lk[i].clear();
rep(j,0,c)
{
if(i+j<=n)lk[i].push_back(j);
sort(lk[i].begin(),lk[i].end(),[&](int x,int y){return p[i+x]<p[i+y];});
}
}
rep(i,1,n)
{
rep(j,0,c)
{
sum[j][i]=sum[j][i-1];
rep(k,0,j)
if(i+k<=n && p[i+k]<p[i])sum[j][i]+=dp[j-k][n-i-k];
sum1[j][i]=sum[j][i]+dp[j][n-i];
}
}
while(q--)
{
int query_i=read();
s64 k;
scanf("%lld",&k);
if(k>dp[c][n])
{
puts("-1");
continue;
}
int i=1,nc=c;
while(i<=n)
{
int j=0;
s64 now;
while(1)
{
while(lk[i][j]>nc)++j;
if(k<=(now=dp[nc-lk[i][j]][n-i-lk[i][j]]))break;
k-=now;
++j;
}
nc-=lk[i][j];
int i1=i+lk[i][j];
if(i1>=query_i)
{
printf("%d\n",p[i+i1-query_i]);
break;
}
i=i1+1;
i1=min(lower_bound(sum[nc]+i,sum[nc]+n,k+sum[nc][i-1])-sum[nc],
lower_bound(sum1[nc]+i,sum1[nc]+n,k+sum[nc][i-1]-1,greater<s64>())-sum1[nc]);
if(i1>query_i)
{
printf("%d\n",p[query_i]);
break;
}
k-=sum[nc][i1-1]-sum[nc][i-1];
i=i1;
}
}
}
}
/* stuff you should look for
* int overflow, array bounds, uppercase/lowercase
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= int(b); i++)
#define per(i, a, b) for (int i = (a); i >= int(b); i--)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, ll> foo;
const int maxn = 3e4, maxm = 4 * maxn;
int T, n, c, q, a[maxn + 5];
ll delta[5][maxn + 5];
bool comp1(const foo &a, const foo &b) {
return a.se < b.se;
}
bool comp2(foo x, foo y) {
return a[x.fi.fi + x.fi.se] < a[y.fi.fi + y.fi.se];
}
template<class type>
struct my_deque {
type q[2 * maxm + 10], *l, *r = l = q + maxm + 5;
void init() {
l = r = q + maxm + 5;
}
void push_back(type x) {
*r++ = x;
}
void push_front(type x) {
*--l = x;
}
};
my_deque<foo> D[5];
ll ways(int n, int c) {
if (n == 0) {
return 1;
}
ll cur = 1, sum = 1;
rep(i, 1, c) {
cur *= (n - i);
cur /= i;
sum += cur;
}
return sum;
}
void prepare(my_deque<foo> &D) {
for (foo *x = D.l + 1; x < D.r; x++) {
x->se += (x - 1)->se;
}
}
foo query(int x, int c, ll &k) {
k += delta[c][x];
foo *p = lower_bound(D[c].l, D[c].r, foo(pii(0, 0), k), comp1);
assert(k <= p->se);
return foo(p->fi, p == D[c].l ? 0ll : (p - 1)->se);
}
int main() {
scanf("%d", &T);
while (T --> 0) {
scanf("%d %d %d", &n, &c, &q);
rep(i, 1, n) {
scanf("%d", &a[i]);
}
rep(i, 0, c) {
D[i].init();
D[i].push_back(foo(pii(0, 0), 1ll));
}
rep(k, 1, c) {
memset(delta[k] + 1, 0, n << 3);
per(i, n - 1, 1) {
int cl = 0, cr = 0;
rep(j, 1, min(n - i, k)) {
foo x = foo(pii(i, j), ways(n - i - j, k - j));
if (a[i + j] > a[i]) {
D[k].push_back(x), cr++;
} else {
D[k].push_front(x), cl++;
delta[k][i + 1] += x.se;
}
}
sort(D[k].l, D[k].l + cl, comp2);
sort(D[k].r - cr, D[k].r, comp2);
}
rep(i, 2, n) {
delta[k][i] += delta[k][i - 1];
}
// puts("=====");
// printf("%d\n", k);
// for (foo *x = D[k].l; x < D[k].r; x++) {
// printf("%d %d %lld\n", x->fi.fi, x->fi.se, x->se);
// }
}
rep(k, 0, c) {
prepare(D[k]);
}
ll lim = ways(n, c);
for (int i; q --> 0; ) {
ll j;
scanf("%d %lld", &i, &j);
if (j > lim) {
puts("-1");
continue;
}
int s = 1, l = c;
while (s <= n) {
foo x = query(s, l, j);
pii p = x.fi;
if (p == pii(0, 0) || i < p.fi) {
printf("%d\n", a[i]);
goto next_query;
}
if (i <= p.fi + p.se) {
printf("%d\n", a[2 * p.fi + p.se - i]);
goto next_query;
}
s = p.fi + p.se + 1;
j -= x.se;
l -= p.se;
}
next_query: void();
}
}
return 0;
} |
// Problem: E. Strange Permutation
// Contest: Codeforces - Codeforces Round #694 (Div. 1)
// URL: https://codeforces.com/problemset/problem/1470/E
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int N = 3e4 + 5;
int n, c, q, p[N];
struct Node {
int w, l, r;
bool operator < (const Node &rhs) const {
w < rhs.w;
}
};
int ql[5], qr[5];
Node dq[5][10 * N];
bool cmp(const Node &a, const Node &b) {
return p[a.r] < p[b.r];
}
int C(int n, int m) {
if (n <= 0) return m == 0;
if (n < m) return 0;
int res = 1;
for (int i = n; i > n - m; --i) res *= i;
for (int i = 1; i <= m; ++i) res /= i;
return res;
}
int ways(int len, int c) {
int res = 0;
for (int i = 0; i <= c; ++i) res += C(len - 1, i);
return res;
}
int sumw[5][5 * N], pre_cnt[5][N];
Node F(int start, int rest, int kth) {
int s = pre_cnt[rest][start], l = s + 1, r = qr[rest] - ql[rest] + 1;
while (l < r) {
int mid = (l + r) >> 1;
if (sumw[rest][mid] - sumw[rest][s] >= kth) r = mid;
else l = mid + 1;
}
return Node{ sumw[rest][l - 1] - sumw[rest][s], dq[rest][ql[rest] + l - 1].l, dq[rest][ql[rest] + l - 1].r };
}
void solve() {
cin >> n >> c >> q;
for (int i = 1; i <= n; ++i) cin >> p[i];
for (int s = 1; s <= c; ++s) {
ql[s] = 4 * N + 1, qr[s] = 4 * N;
dq[s][++qr[s]] = Node{ 1, n, n };
for (int i = 1; i <= n; ++i) pre_cnt[s][i] = 0;
for (int i = n - 1; i; --i) {
int dl = 0, dr = 0;
for (int j = 1; j <= min(s, n - i); ++j) {
int w = ways(n - (i + j), s - j);
if (p[i + j] < p[i]) dq[s][ql[s] - (++dl)] = Node{ w, i, i + j }, ++pre_cnt[s][i + 1];
else dq[s][qr[s] + (++dr)] = Node{ w, i, i + j };
}
if (dl) {
sort(dq[s] + ql[s] - dl, dq[s] + ql[s], cmp);
ql[s] -= dl;
}
if (dr) {
sort(dq[s] + qr[s] + 1, dq[s] + qr[s] + dr + 1, cmp);
qr[s] += dr;
}
}
for (int i = 1; i <= n; ++i) pre_cnt[s][i] += pre_cnt[s][i - 1];
for (int i = ql[s]; i <= qr[s]; ++i) sumw[s][i - ql[s] + 1] = sumw[s][i - ql[s]] + dq[s][i].w;
}
int tot = ways(n, c);
while (q--) {
int pos, rank; cin >> pos >> rank;
if (rank > tot) {
cout << -1 << endl;
continue;
}
vector<pii> rev;
int start = 1, rest = c;
while (true) {
Node u = F(start, rest, rank);
rev.push_back(make_pair(u.l, u.r));
rank -= u.w, rest -= (u.r - u.l), start = u.r + 1;
if (start > n || !rest) break;
}
bool flag = false;
for (int i = 0; i < rev.size(); ++i)
if (rev[i].first <= pos && rev[i].second >= pos) {
cout << p[rev[i].second - (pos - rev[i].first)] << endl;
flag = true;
break;
}
if (!flag) cout << p[pos] << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) solve();
return 0;
} |
#include <bits/stdc++.h>
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
constexpr i64 inf = 1e18;
void solve() {
int n, c, q;
std::cin >> n >> c >> q;
std::vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
}
std::vector<std::vector<i64>> dp(n + 2, std::vector<i64>(c + 1));
std::vector<std::vector<i64>> down(n + 2, std::vector<i64>(c + 1));
std::vector<std::vector<i64>> up(n + 2, std::vector<i64>(c + 1));
for (int i = 0; i <= c; i++) {
dp[n + 1][i] = 1;
}
for (int i = n; i >= 1; --i) {
for (int j = 0; j <= c; j++) {
std::vector<std::pair<int, i64>> tmp;
for (int k = i; k <= i + j && k <= n; k++) {
tmp.emplace_back(a[k], dp[k + 1][j - k + i]);
}
std::sort(tmp.begin(), tmp.end());
for (auto t : tmp) {
if (t.first == a[i]) {
down[i][j] = dp[i][j];
up[i][j] = std::min(dp[i][j] + t.second, inf);
}
dp[i][j] = std::min(dp[i][j] + t.second, inf);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= c; j++) {
up[i][j] -= down[i][j];
down[i][j] = std::min(down[i][j] + down[i - 1][j], inf);
}
}
while (q--) {
int i;
i64 j;
std::cin >> i >> j;
if (j > dp[1][c]) {
std::cout << -1 << "\n";
} else {
int pos = 0;
int lft = c;
int ans = 0;
while (!ans) {
int l = pos;
int r = n;
while (l < r) {
int mid = l + r + 1 >> 1;
i64 t = j - down[mid][lft] + down[pos][lft];
if (t >= 1 && t <= up[mid][lft]) {
l = mid;
} else {
r = mid - 1;
}
}
if (l >= i) {
ans = a[i];
} else {
std::vector<std::pair<int, i64>> tmp;
j -= down[l][lft] - down[pos][lft];
++l;
for (int k = l; k <= l + lft && k <= n; k++) {
tmp.emplace_back(a[k], dp[k + 1][lft - k + l]);
}
std::sort(tmp.begin(), tmp.end());
for (auto t : tmp) {
if (j > t.second) {
j -= t.second;
} else {
int p = l;
while (a[p] != t.first) {
++p;
}
if (i <= p) {
ans = a[l + p - i];
} else {
pos = p;
lft = lft - p + l;
}
break;
}
}
}
}
std::cout << ans << "\n";
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
std::cin >> T;
while (T--) {
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int t;
cin >> t;
while (t--) {
int n, c, q;
scanf("%d %d %d", &n, &c, &q);
vector<int> p(n + 1);
for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
vector<vector<ll>> tot(n + 2, vector<ll>(c + 1, 1));
vector<vector<vector<ll>>> dp(n + 1, vector<vector<ll>>(c + 1, {0, 0}));
for (int i = n; i >= 1; i--) {
for (int _c = 0; _c <= c; _c++) {
tot[i][_c] = tot[i + 1][_c];
for (int len = 1; len <= _c && i + len <= n; len++) {
tot[i][_c] += tot[i + len + 1][_c - len];
}
}
}
for (int i = n - 1; i >= 1; i--) {
for (int _c = 0; _c <= c; _c++) {
dp[i][_c] = dp[i + 1][_c];
for (int len = 1; len <= _c && i + len <= n; len++) {
int d = p[i] < p[i + len];
dp[i][_c][d] += tot[i + len + 1][_c - len];
}
}
}
while (q--) {
int pos;
ll rank;
scanf("%d %lld", &pos, &rank);
if (tot[1][c] < rank) {
puts("-1");
continue;
}
int at = 1;
int _c = c;
while (true) {
assert(at <= pos);
int d = dp[at][_c][0] < rank - 1;
ll diff = abs(dp[at][_c][0] - (rank - 1));
int L = at, R = n + 1;
while (L != R) {
int mid = (L + R + 1) / 2;
if (mid > n || dp[mid][_c][d] >= diff) L = mid;
else R = mid - 1;
}
int to = L;
// debug() << name(at) name(to) name(rank) name(diff) name(tot[at][_c]) name(dp[at][_c][0]);
if (pos < to) {
printf("%d\n", p[pos]);
break;
} else {
assert(to < n);
vector<pair<int, ll>> v{{0, dp[to + 1][_c][d]}};
for (int len = 1; len <= _c && to + len <= n; len++) {
if ((p[to] < p[to + len]) == d) {
v.push_back({len, tot[to + len + 1][_c - len]});
}
}
sort(v.begin(), v.end(), [&p, to, d](const pair<int, ll>& p1, const pair<int, ll>& p2) {
return (p[to + p1.first] < p[to + p2.first]) == d;
});
// debug() << name(v);
int len;
ll poss;
for (int i = 0; ; i++) {
assert(i < v.size());
if (diff <= v[i].second) {
len = v[i].first;
poss = v[i].second;
break;
}
diff -= v[i].second;
}
_c -= len;
if (pos <= to + len) {
int _pos = to + len - (pos - to);
printf("%d\n", p[_pos]);
break;
}
at = to + len + 1;
rank = d == 1 ? diff : poss + 1 - diff;
}
}
}
}
return 0;
} |
/*
Author: QAQAutoMaton
Lang: C++
Code: E.cpp
Mail: lk@qaq-am.com
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define int long long
#define debug(qaq...) fprintf(stderr,qaq)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
#define unq(a) sort(all(a)),a.erase(unique(all(a)),a.end())
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef complex<double> cp;
typedef pair<int,int> pii;
int inf=1000000000000000000ll;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T,class T2>int chkmin(T &a,T2 b){return a>b?a=b,1:0;}
template<class T,class T2>int chkmax(T &a,T2 b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;}
template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
template<class T>bool sort2(T &a,T &b){return a>b?swap(a,b),1:0;}
#define min mmin
#define max mmax
#define abs aabs
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
// getchar
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
// print the remaining part
inline void flush () {
fwrite (obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc (char x) {
*oS ++ = x;
if (oS == oT) flush ();
}
template<typename A>
inline bool read (A &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
return 1;
}
inline bool read (char &x) {
while((x=gc())==' '||x=='\n' || x=='\r');
return x!=EOF;
}
inline bool read(char *x){
while((*x=gc())=='\n' || *x==' '||*x=='\r');
if(*x==EOF)return 0;
while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc();
*x=0;
return 1;
}
template<typename A,typename ...B>
inline bool read(A &x,B &...y){
return read(x)&&read(y...);
}
template<typename A>
inline bool write (A x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
return 0;
}
inline bool write (char x) {
putc(x);
return 0;
}
inline bool write(const char *x){
while(*x){putc(*x);++x;}
return 0;
}
inline bool write(char *x){
while(*x){putc(*x);++x;}
return 0;
}
template<typename A,typename ...B>
inline bool write(A x,B ...y){
return write(x)||write(y...);
}
//no need to call flush at the end manually!
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
namespace run{
int n,c,q;
int a[30005];
int f[150055];
int id[30005][5];
int wx[150055],wy[150055];
int l;
pii w[7];
vector<int> to[150055];
int fa[21][150055],sf[21][150055];
bool main(){
read(n,c,q);
l=0;
for(int i=1;i<=n+1;++i)for(int j=0;j<=c;++j){
id[i][j]=++l;
to[l].clear();
wx[l]=i;wy[l]=j;
}
for(int i=1;i<=n;++i){
read(a[i]);
}
for(int i=0;i<=c;++i)f[id[n+1][i]]=1;
for(int i=n;i;--i){
for(int j=0;j<=c;++j){
int &xf=f[id[i][j]];
xf=0;
int l=min(n-i,j)+1;
for(int k=0;k<=j && i+k<=n;++k){
xf+=f[id[i+k+1][j-k]];
w[k+1]={a[i+k],k};
}
chkmin(xf,inf);
sort(w+1,w+l+1);
for(int k=1;k<=l;++k)to[id[i][j]].emplace_back(id[i+w[k].y+1][j-w[k].y]);
}
}
for(int i=1;i<=l;++i){
int mx=inf;
int s=0;
fa[0][i]=0;sf[0][i]=inf;
for(auto x:to[i]){
if(chkmin(mx,x))fa[0][i]=x,sf[0][i]=s;
chkmin(s+=f[x],inf);
}
}
for(int i=1;i<=14;++i)for(int j=1;j<=l;++j){fa[i][j]=fa[i-1][fa[i-1][j]];sf[i][j]=min(sf[i-1][j]+sf[i-1][fa[i-1][j]],inf);}
for(;q;--q){
int s,y;
int lim;
read(y,s);
lim=y*(c+1);
int x=c+1;
if(s>f[x]){write("-1\n");continue;}
while(1){
int i=0;
while(fa[i][x]<=lim && sf[i][x]<s && s<=sf[i][x]+f[fa[i][x]]){
s-=sf[i][x];
x=fa[i][x];
++i;
}
while(i){
--i;
if(fa[i][x]<=lim && sf[i][x]<s && s<=sf[i][x]+f[fa[i][x]]){
s-=sf[i][x];
x=fa[i][x];
}
}
for(auto i:to[x]){
if(s>f[i]){s-=f[i];}
else{
if(i>lim){
int l=wx[x],r=wx[i]-1;
//write(x,' ',i,' ',l,' ',r,' ',y,'\n');
write(a[l+r-y],'\n');
goto _end;
}
else{x=i;break;}
}
}
}
_end:;
}
return 0;
}
}
signed main(){
#ifdef QAQAutoMaton
freopen("E.in","r",stdin);
freopen("E.out","w",stdout);
#endif
int t;
read(t);
for(;t;--t){
run::main();
}
return 0;
}
|
#include<bits/stdc++.h>
#define ll __int128
#define ld long double
#define ull unsigned ll
#define uint unsigned int
#define db double
#define pint pair<int,int>
#define mk make_pair
#define pb push_back
#define eb emplace_back
#define ins insert
#define fi first
#define se second
#define Rep(x,y,z) for(int x=(y);x<=(z);x++)
#define Red(x,y,z) for(int x=(y);x>=(z);x--)
using namespace std;
const int MAXN=3e4+5;
char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny;
inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;}
//inline char gc(){return getchar();}
inline ll read(){
ll x=0;for(ny=1;nc=gc(),(nc<48||nc>57)&&nc!=EOF;)if(nc==45)ny=-1;if(nc<0)return nc;
for(x=nc-48;nc=gc(),47<nc&&nc<58&&nc!=EOF;x=(x<<3)+(x<<1)+(nc^48));return x*ny;
}
int n,c,q,p[MAXN];
struct Node{int l,r;Node(int l=0,int r=0):l(l),r(r){}};
Node L[5][MAXN*10];ll w[5][MAXN*10];int st[5][MAXN],ed[5][MAXN];
inline ll C(int n,int m){ll ans=1;Rep(i,n-m+1,n)ans*=i;Rep(i,1,m)ans/=i;return ans;}
inline ll calc(int n,int c){if(n==0)return 1;ll ans=0;Rep(i,0,c)ans+=C(n-1,i);return ans;}
inline void Init(){
Rep(i,1,c){
int l=n*5+1,r=n*5+1;L[i][l]=Node(1,1);
Red(j,n,1){
static int s[MAXN];Rep(k,1,min(i,n-j))s[k]=j+k;
sort(s+1,s+min(i,n-j)+1,[](int x,int y){return p[x]<p[y];});
Rep(k,1,min(i,n-j))if(p[s[k]]>p[j])L[i][++r]=Node(j,s[k]);
Red(k,min(i,n-j),1)if(p[s[k]]<p[j])L[i][--l]=Node(j,s[k]);
st[i][j]=l,ed[i][j]=r;
}w[i][l-1]=0;Rep(j,l,r)w[i][j]=w[i][j-1]+(L[i][j].r!=L[i][j].l?calc(n-L[i][j].r,i-(L[i][j].r-L[i][j].l)):1);
// Rep(j,l,r)cout<<L[i][j].l<<" "<<L[i][j].r<<"x\t";puts("");Rep(j,l,r)cout<<w[i][j]<<"\t\t";puts("");
}
}
inline int F(int i,int c,ll k){return lower_bound(w[c]+st[c][i],w[c]+ed[c][i]+1,k+w[c][st[c][i]-1])-w[c];}
inline int Solve(int i,ll j){
if(j>calc(n,c))return -1;
else{
static Node pp[MAXN];int now=1,cnt=0,tt=c;
for(;tt>0&&j>0;){
int t=F(now,tt,j);//cout<<j<<" "<<t<<" "<<L[tt][t].l<<" "<<L[tt][t].r<<"\n";
if(L[tt][t].l==L[tt][t].r)break;pp[++cnt]=L[tt][t];
j-=w[tt][t-1]-w[tt][st[tt][now]-1],now=L[tt][t].r+1,tt-=L[tt][t].r-L[tt][t].l;
}//Rep(k,1,cnt)cout<<pp[k].l<<" "<<pp[k].r<<"\n";
Rep(k,1,cnt)if(pp[k].r>=i&&pp[k].l<=i)return p[pp[k].l+pp[k].r-i];return p[i];
}
}
int main(){
// freopen("std.in","r",stdin);
// freopen("std.out","w",stdout);
for(int t=read();t--;){
n=read(),c=read(),q=read();Rep(i,1,n)p[i]=read();Init();
for(ll i,j;q--;)i=read(),j=read(),cout<<Solve(i,j)<<'\n';
memset(w,0,sizeof(w)),memset(st,0,sizeof(st)),memset(ed,0,sizeof(ed));
}
return 0;
}
|
#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const ll B=1e18+1;
const int C=5;
int n,c,q,arr[MAXN];
vector<PII>adj[MAXN];
ll dp[MAXN][C],par[MAXN][C];
ll dfs(int nd,int rem){
if(rem<0)return 0;
if(nd==n)return 1;
ll &ret=dp[nd][rem];
if(~ret)
return ret;ret=0;
tr(it,adj[nd])
if(it->ss<=rem)
ret=min(B,ret+dfs(nd+1+it->ss,rem-it->ss));
return ret;
}
vector<PII>cur;
int rec(int nd,int rem,ll th,int pos){
if(nd==n){
tr(it,cur)reverse(arr+it->ff,arr+it->ss+1);
int ret=arr[pos];
tr(it,cur)reverse(arr+it->ff,arr+it->ss+1);
cur.clear();
return ret;
}
int st=nd+1,en=n-1;
if(st<=en){
int to=nd;
while(st+1<en){
int i=(st+en)>>1;
if(par[nd][rem]-par[i][rem]<th and th<=par[nd][rem]-par[i][rem]+dfs(i,rem))
st=i;
else
en=i;
}int i=en;
if(par[nd][rem]-par[i][rem]<th and th<=par[nd][rem]-par[i][rem]+dfs(i,rem))nd=i;
else{
i=st;
if(par[nd][rem]-par[i][rem]<th and th<=par[nd][rem]-par[i][rem]+dfs(i,rem))nd=i;
}
th-=par[to][rem]-par[nd][rem];
}
tr(it,adj[nd]){
if(th<=dfs(nd+1+it->ss,rem-it->ss)){
if(it->ss)cur.pb(mp(nd,nd+it->ss));
return rec(nd+1+it->ss,rem-it->ss,th,pos);
}
else th-=dfs(nd+1+it->ss,rem-it->ss);
}return -1;
}
int main(){
// freopen("file.in","r",stdin);
int t;
scanf("%d",&t);
while(t--){
memset(dp,-1,sizeof dp);
scanf("%d%d%d",&n,&c,&q);
for(int i=0;i<n;i++)scanf("%d",arr+i);
for(int i=0;i<n;i++){
adj[i].clear();
for(int j=0;j<=c;j++)
if(i+j<n)
adj[i].pb(mp(arr[i+j],j));
sort(all(adj[i]));
}
for(int j=0;j<=c;j++)
for(int i=n;i>=0;i--){
par[i][j]=par[i+1][j];
tr(it,adj[i]){
if(it->ss==0)break;
par[i][j]=min(B,par[i][j]+dfs(i+it->ss+1,j-it->ss));
}
}
while(q--){
ll k;int a;
scanf("%d%lld",&a,&k);
printf("%d\n",rec(0,c,k,a-1));
}
}
return 0;
} |
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#define N_ 30100
using namespace std;
using pii = pair<int,int>;
int n, K, Q, w[N_], Tw[N_];
long long D[5][N_], P[N_], U[5][N_], S[5][N_];
void Solve(){
int i, j, k;
scanf("%d%d%d",&n,&K,&Q);
for(i=1;i<=n;i++){
scanf("%d",&w[i]);
}
for(i=0;i<=4;i++){
for(j=0;j<=n;j++){
U[i][j]=D[i][j]=S[i][j]=0;
}
}
for(i=0;i<=K;i++)U[i][0]=1;
for(i=1;i<=n;i++){
for(j=0;j<=K;j++)U[j][i]=U[j][i-1];
for(j=1;j<=K && j<i;j++){
for(k=j;k<=K;k++){
U[k][i] += U[k-j][i-j-1];
U[k][i] = U[k][i];
}
}
}
for(i=1;i<=K;i++){
for(j=1;j<=n;j++){
for(k=1;k<=i && j+k<=n;k++){
if(w[j+k] < w[j]){
D[i][j] += U[i-k][n-(j+k)];
D[i][j] = D[i][j];
}
}
}
}
for(i=1;i<=K;i++){
for(j=1;j<=n;j++){
S[i][j]=S[i][j-1]+D[i][j];
//if(j%100==0)printf("%lld\n",U[i][j]);
}
//puts("");
}
while(Q--){
int loc;
long long M;
scanf("%d%lld",&loc,&M);
if(M > U[K][n]){
puts("-1");
continue;
}
int ed = 0, TK = K;
while(1){
int bb = ed+1, ee = n, rr = ed;
while(bb<=ee){
int m = (bb+ee)>>1;
long long sm = S[TK][m] - S[TK][ed];
long long dd = U[TK][n-m];
if(sm < M && sm + dd >= M){
rr = m;
bb = m+1;
}
else{
ee = m-1;
}
}
if(loc <= rr){
printf("%d\n",w[loc]);
break;
}
M -= (S[TK][rr] - S[TK][ed]);
int pv = rr+1;
vector<pii>ZZ;
for(i=0;i<=TK && pv+i <=n;i++){
ZZ.push_back({w[pv+i],i});
}
sort(ZZ.begin(),ZZ.end());
int fin = 0;
for(auto &t : ZZ){
int l = t.second;
if(M <= U[TK-l][n-(pv+l)]){
for(i=pv;i<=pv+l;i++)Tw[i]=w[i];
reverse(Tw+pv,Tw+pv+l+1);
if(loc <= pv+l){
printf("%d\n",Tw[loc]);
fin=1;
break;
}
ed = pv+l;
TK-=l;
break;
}
else M-=U[TK-l][n-(pv+l)];
}
if(fin)break;
}
}
}
int main(){
int TC;
scanf("%d",&TC);
while(TC--){
Solve();
}
} |
#include <iostream>
#include <tuple>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using PP = pair<int, int>;
constexpr int maxn = 3e4 + 10;
struct Node
{
int l, r;
ull w = 0;
Node() {}
Node(int _l, int _r, ull _w) :l(_l), r(_r), w(_w) {}
};
ull Com[maxn][5];
Node fun(int x, ull k, vector<PP>& L, vector<Node>& que)
{
int l, r;
tie(l, r) = L[x];
int lb = l - 1;
int ans = -1;
while (l < r)
{
int m = (l + r) >> 1;
if (que[m].w - que[lb].w >= k)
{
ans = m;
r = m;
}
else
l = m + 1;
}
return { que[ans].l,que[ans].r,que[ans - 1].w - que[lb].w };
}
ull SC(int a, int b)
{
if (a < 0)
return 1;
ull ans = 0;
for (int i = 0; i <= b; i++)
ans += Com[a][i];
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
int T;
cin >> T;
int r[5] = { 1,1,2,6,24 };
for (int i = 0; i <= 3e4; i++)
Com[i][0] = 1;
for (int j = 1; j <= 4; j++)
{
ull sum = 1;
for (int i = 1; i <= 3e4; i++)
{
if (i == 3e4)
i = i;
if (i > j)
sum /= (ull)(i - j);
sum *= i;
if (i >= j)
Com[i][j] = sum / r[j];
}
}
while (T--)
{
int N, C, Q;
cin >> N >> C >> Q;
ll tot = SC(N - 1, C);
vector<int>P(N + 1);
for (int i = 1; i <= N; i++)
cin >> P[i];
const int que_len = 2 * N * C + 10;
vector<vector<Node>>que(5, vector<Node>(que_len));
vector<vector<PP>>L(5, vector<PP>(N + 1));
for (int c = 1; c <= C; c++)
{
int l = N * C + 2, r = N * C + 2;
que[c][r++] = { -1, -1, 1 };
L[c][N] = { l,r };
for (int i = N - 1; i >= 1; i--)
{
vector<int>t_vec;
const int ite_c = min(N - i, c);
for (int j = 1; j <= ite_c; j++)
t_vec.push_back(j);
sort(t_vec.begin(), t_vec.end(), [&](const int& a, const int& b)
{
int fa = P[i + a] - P[i];
int fb = P[i + b] - P[i];
if (fa * fb < 0)
return fa < fb;
else
return 1ll * fa * fa < 1ll * fb * fb;
});
for (auto j : t_vec)
{
Node tmp = { i, i + j, SC(N - (i + j) - 1,c - j) };
if (P[i + j] > P[i])
que[c][r++] = tmp;
else
que[c][--l] = tmp;
}
L[c][i] = { l,r };
}
for (int i = l; i < r; i++)
que[c][i].w += que[c][i - 1].w;
}
while (Q--)
{
ull n, k;
int c = C;
cin >> n >> k;
if (k > tot)
{
cout << -1 << "\n";
continue;
}
int x = 1;
while (true)
{
auto t = fun(x, k, L[c], que[c]);
x = t.r + 1;
k -= t.w;
c -= t.r - t.l;
if (t.l <= n && n <= t.r)
{
cout << P[t.r - n + t.l] << "\n";
break;
}
if (c == 0 || n < t.l || t.l == -1)
{
cout << P[n] << "\n";
break;
}
}
}
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 30010;
int T, n, c, q, p[maxn];
ll ways[5][maxn], L[5][maxn], R[5][maxn];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d %d %d", &n, &c, &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i]);
}
for (int i = 0; i <= c; i++) {
ways[i][n + 1] = 1;
L[i][n + 1] = R[i][n + 1] = 0;
for (int j = n; j; j--) {
ways[i][j] = ways[i][j + 1];
L[i][j] = L[i][j + 1], R[i][j] = R[i][j + 1];
for (int k = 1; k <= i && j + k <= n; k++) {
ways[i][j] += ways[i - k][j + k + 1];
if (p[j] > p[j + k]) L[i][j] += ways[i - k][j + k + 1];
else R[i][j] += ways[i - k][j + k + 1];
}
}
}
while (q--) {
int pos; long long id;
scanf("%d %lld", &pos, &id);
if (id > ways[c][1]) { printf("-1\n"); continue; }
int cur = 1, rem = c, ans;
while (cur <= n) {
int l = cur, r = n, m;
while (l <= r) {
int mid = (l + r) >> 1;
if (L[rem][cur] - L[rem][mid] < id && R[rem][cur] - R[rem][mid]
<= ways[rem][cur] - id) l = (m = mid) + 1;
else r = mid - 1;
}
if (m > pos) { ans = pos; break; }
id -= L[rem][cur] - L[rem][m], cur = m;
vector<int> order;
for (int k = 0; k <= rem && cur + k <= n; k++) {
order.push_back(k);
}
sort(order.begin(), order.end(), [&](int x, int y) {
return p[cur + x] < p[cur + y];
});
int cost;
for (int x : order) {
if (id <= ways[rem - x][cur + x + 1]) { cost = x; break; }
else id -= ways[rem - x][cur + x + 1];
}
if (cur + cost >= pos) { ans = cur * 2 + cost - pos; break; }
rem -= cost, cur += cost + 1;
}
printf("%d\n", p[ans]);
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 30010;
typedef long long LL;
int num[N]; LL f[5][N], pre[5][N];
vector<int> pos[5][N];
void solve() {
int n, c, q; cin >> n >> c >> q;
for (int i = 1; i <= n; i++) cin >> num[i];
for (int i = 0; i <= c; i++) f[i][n + 1] = 1, f[i][n + 2] = 0;
for (int i = n; i >= 1; i--)
for (int j = 0; j <= c; j++) {
f[j][i] = 0;
for (int k = 0; k <= j && i + k <= n; k++)
f[j][i] += f[j - k][i + k + 1];
}
for (int j = 0; j <= c; j++)
for (int i = 1; i <= n; i++) {
pos[j][i].clear();
for (int k = 0; k <= j; k++) if (i + k <= n)
pos[j][i].push_back(k);
sort(pos[j][i].begin(), pos[j][i].end(), [&](int a, int b) { return num[i + a] < num[i + b]; });
pre[j][i] = pre[j][i - 1];
for (auto t : pos[j][i]) {
if (!t) break;
pre[j][i] += f[j - t][i + t + 1];
}
}
while (q--) {
LL id; int cur, p = 1, lst = c; cin >> cur >> id;
LL zzh = id;
if (id > f[c][1]) { cout << -1 << endl; continue; }
while (233) {
int l = p, r = n, mid, eql = p - 1;
while (l <= r) {
mid = (l + r) >> 1;
LL tmp = pre[lst][mid] - pre[lst][p - 1];
if (tmp < id && tmp + f[lst][mid + 1] >= id) eql = mid, l = mid + 1;
else r = mid - 1;
}
id -= pre[lst][eql] - pre[lst][p - 1];
if (p <= cur && cur <= eql) { cout << num[cur] << endl; break; }
p = eql + 1; int len = 0;
for (auto t : pos[lst][p]) {
if (f[lst - t][p + t + 1] < id) id -= f[lst - t][p + t + 1];
else { len = t; break; }
}
assert(len);
if (p <= cur && cur <= p + len) { cout << num[p + len - cur + p] << endl; break; }
p += len + 1, lst -= len;
}
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T; cin >> T;
while (T--) solve();
} |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<deque>
#include<vector>
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
using namespace std;
int n,c,m;
int p[300009],ps[300009];
struct node
{
ll rk;
int bg,id;
bool operator<(const node &p)const{return rk<p.rk;}
};
struct seg
{
ll rep;
int l,r,c;
};
vector<node> q[5];
vector<pii> out[300009];
int err[300009];
ll cl[300009][5];
ll lf[300009],rf[300009];
ll cb(int a,int b)
{
if(a<0||b>a||b<0)
return 0;
b=min(b,a-b);
ll ans=1;
for(int i=1;i<=b;i++)
ans*=(a-i+1);
for(int i=1;i<=b;i++)
ans/=i;
return ans;
}
ll cal(int n,int c)
{
if(n==0)
return 1;
ll ans=0;
for(int i=0;i<=c;i++)
ans+=cb(n-1,i);
return ans;
}
void init()
{
for(int i=0;i<=300000;i++)
for(int j=0;j<=4;j++)
cl[i][j]=cal(i,j);
}
void solve()
{
deque<seg> dq;//remaining leng,remaining c
for(int i=c;i>=1;i--)
{
dq.clear();
dq.push_back(seg{1,1,0,i});
lf[n+1]=rf[n+1]=0;
for(int j=n;j>=1;j--)
{
vector<pii> tmp;
vector<seg> tft;
lf[j]=lf[j+1];
rf[j]=rf[j+1];
for(int k=j;k<=min(n,j+i);k++)
tmp.push_back(pii(p[k],k));
int fg=0;
sort(tmp.begin(),tmp.end());
for(pii t:tmp)
{
if(t.se==j)
fg=1;
else if(!fg)
{
tft.push_back(seg{cl[n-t.se][i-(t.se-j)],j,t.se,i-(t.se-j)});
lf[j]-=cl[n-t.se][i-(t.se-j)];
}
else
{
dq.push_back(seg{cl[n-t.se][i-(t.se-j)],j,t.se,i-(t.se-j)});
rf[j]+=cl[n-t.se][i-(t.se-j)];
}
}
for(auto it=tft.rbegin();it!=tft.rend();it++)
dq.push_front(*it);
}
// for(seg t:dq)
// printf("rep:%lld l:%d r:%d c:%d\n",t.rep,t.l,t.r,t.c);
for(auto it=q[i].begin();it!=q[i].end();it++)
{
it->rk+=(lf[it->bg]-lf[1]);
it->bg=1;
}
sort(q[i].begin(),q[i].end());
ll sm=0,lsm=0;
int lp=-1;
for(node t:q[i])
{
while(lp+1<dq.size()&&sm<t.rk)
{
lsm=sm,sm+=dq[++lp].rep;
// printf("lp:%d sm:%lld\n",lp,sm);
}
seg s=dq[lp];
out[t.id].push_back(pii(s.l,s.r));
q[s.c].push_back(node{t.rk-lsm,s.r+1,t.id});
}
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&c,&m);
for(int i=0;i<=c;i++)
q[i].clear();
for(int i=1;i<=n;i++)
scanf("%d",p+i);
ll tot=cl[n][c];
// printf("tot:%d\n",tot);
for(int i=1;i<=m;i++)
{
err[i]=0;
out[i].clear();
int a;
ll b;
scanf("%d%lld",&a,&b);
if(b<=tot)
q[c].push_back(node{b,1,i});
else
err[i]=1;
ps[i]=a;
}
solve();
for(int i=1;i<=m;i++)
if(err[i])
printf("-1\n");
else
{
int s=ps[i];
// printf("s:%d\n",s);
for(pii t:out[i])
if(t.fi<=s&&s<=t.se)
s=t.fi+t.se-s;
printf("%d\n",p[s]);
}
}
return 0;
} |
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author
*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define mp make_pair
using namespace std;
typedef long long int64;
typedef vector<int> vi;
typedef pair<int, int> ii;
struct Op {
int left;
int right;
int oldValue;
int value;
};
struct Fenwick {
vector<int64> a;
Fenwick(int n) : a(n) {
}
void upd(int at, int64 by) {
while (at < a.size()) {
a[at] += by;
at |= at + 1;
}
}
int64 get(int upto) const {
int64 res = 0;
while (upto >= 0) {
res += a[upto];
upto = (upto & (upto + 1)) - 1;
}
return res;
}
int findLastLess(int64 val) const {
int shift = 1;
int pos = -1;
while (pos + shift * 2 < a.size()) shift *= 2;
while (shift > 0) {
if (pos + shift < a.size() && a[pos + shift] <= val) {
val -= a[pos + shift];
pos += shift;
}
shift /= 2;
}
return pos;
}
};
class EStrannayaPerestanovka {
public:
void solveOne(istream &in, ostream &out) {
int n, c, q;
in >> n >> c >> q;
vector<int> p(n);
for (auto& x : p) {
in >> x;
--x;
}
vector<Op> ops;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n && j <= i + c; ++j) {
Op op;
op.left = i;
op.right = j;
op.oldValue = p[i];
op.value = p[j];
ops.push_back(op);
}
}
Op sentinel;
sentinel.left = n;
sentinel.right = n;
sentinel.oldValue = 1;
sentinel.value = 0;
ops.push_back(sentinel);
sort(all(ops), [](const Op& a, const Op& b) {
int kinda = a.value < a.oldValue ? 0 : 1;
int kindb = b.value < b.oldValue ? 0 : 1;
if (kinda != kindb) return kinda < kindb;
if (kinda == 0) {
if (a.left != b.left) {
return a.left < b.left;
}
return a.value < b.value;
} else {
if (a.left != b.left) {
return a.left > b.left;
}
return a.value < b.value;
}
});
vector<vector<int>> toDelete(n + 2);
for (int i = 0; i < ops.size(); ++i) {
toDelete[ops[i].left].push_back(i);
}
vector<vector<int64>> choices(c + 1, vector<int64>(n + 2));
for (int rem = 0; rem <= c; ++rem) {
choices[rem][n] = choices[rem][n + 1] = 1;
for (int start = n - 1; start >= 0; --start) {
choices[rem][start] = choices[rem][start + 1];
for (int finish = start + 1; finish < n && finish <= start + rem; ++finish) {
choices[rem][start] += choices[rem - (finish - start)][finish + 1];
if (choices[rem][start] >= 4e18) assert(false);
}
}
}
vector<Fenwick> fenwick(c + 1, Fenwick(ops.size()));
for (int i = 0; i < ops.size(); ++i) {
auto op = ops[i];
for (int j = op.right - op.left; j <= c; ++j) {
fenwick[j].upd(i, choices[j - (op.right - op.left)][op.right + 1]);
}
}
vector<int64> need(q);
vector<int> needAt(q);
vector<int> avail(q);
vector<int> answer(q, -1);
for (int i = 0; i < q; ++i) {
in >> needAt[i] >> need[i];
--needAt[i];
--need[i];
answer[i] = p[needAt[i]];
avail[i] = c;
}
vector<vector<int>> queue(n + 2);
for (int i = 0; i < q; ++i) {
queue[0].push_back(i);
}
for (int pos = 0; pos < n; ++pos) {
for (int qi : queue[pos]) {
int at = fenwick[avail[qi]].findLastLess(need[qi]);
need[qi] -= fenwick[avail[qi]].get(at);
assert(need[qi] >= 0);
if (at + 1 >= ops.size()) {
need[qi] = 0;
answer[qi] = -2;
continue;
}
auto op = ops[at + 1];
if (op.left <= needAt[qi] && op.right >= needAt[qi]) {
answer[qi] = p[op.left + op.right - needAt[qi]];
}
avail[qi] -= op.right - op.left;
queue[op.right + 1].push_back(qi);
}
queue[pos].clear();
for (int i : toDelete[pos]) {
auto op = ops[i];
for (int j = op.right - op.left; j <= c; ++j) {
fenwick[j].upd(i, -choices[j - (op.right - op.left)][op.right + 1]);
}
}
}
for (int i = 0; i < q; ++i) {
assert(need[i] == 0);
out << answer[i] + 1 << "\n";
}
}
void solve(std::istream &in, std::ostream &out) {
int nt;
in >> nt;
for (int it = 0; it < nt; ++it) {
solveOne(in, out);
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
EStrannayaPerestanovka solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
|
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define dl double long
using namespace std;
const int N = 3e4 + 7;
const long long mod = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,c,q;
int p[N];
ll d[N][5];
ll L[N][5];
ll R[N][5];
vector < int > v[N][5];
void solve()
{
cin >> n >> c >> q;
for( int i = 1; i <= n; i++ ){
cin >> p[i];
}
for( int i = 0; i <= c; i++ )d[n + 1][i] = 1;
for( int i = 0; i <= n; i++ ){
for( int j = 0; j <= c; j++ ){
L[i][j] = R[i][j] = 0;
v[i][j].clear();
}
}
for( int i = n; i >= 1; i-- ){
for( int j = 0; j <= c; j++ ){
d[i][j] = d[i + 1][j];
vector < pair < int , int > > g;
g.push_back({ p[i] , 0 });
for( int h = 1; h <= j; h++ ){
if( i + h <= n ){
g.push_back({ p[i + h] , h });
d[i][j] += d[i + h + 1][j - h];
}
}
sort( g.begin() , g.end() );
for( auto x : g ){
v[i][j].push_back(x.se);
}
}
}
for( int i = 0; i <= c; i++ ){
for( int j = 1; j <= n; j++ ){
for( auto x : v[j][i] ){
if( x == 0 ){
R[j][i] += d[j + 1][i];
break;
}else{
L[j][i] += d[j + x + 1][i - x];
R[j][i] += d[j + x + 1][i - x];
}
}
}
}
for( int i = 0; i <= c; i++ ){
for( int j = 1; j <= n; j++ ){
L[j][i] += L[j - 1][i];
}
}
while( q-- ){
int i;
ll j;
cin >> i >> j;
if( j > d[1][c] ){
cout << "-1\n";
continue;
}
vector < pair < int , int > > seg;
int st = 1;
int nc = c;
while( st <= n ){
int l = st , r = n + 1;
while( l < r ){
int m = (l + r) / 2;
if( L[m][nc] - L[st - 1][nc] < j && j <= L[m - 1][nc] - L[st - 1][nc] + R[m][nc] ){
l = m + 1;
}else r = m;
}
if( l == n + 1 )break;
j -= L[l - 1][nc] - L[st - 1][nc];
st = l;
for( auto x : v[st][nc] ){
if( j > d[st + x + 1][nc - x] ){
j -= d[st + x + 1][nc - x];
}else{
seg.push_back({ st , st + x });
st += x + 1;
nc -= x;
break;
}
}
}
vector < int > f;
for( auto x : seg ){
if( x.fi <= i && i <= x.se ){
for( int h = x.fi; h <= x.se; h++ ){
f.push_back(p[h]);
}
i -= x.fi;
break;
}
}
if( f.empty() ){
cout << p[i] << "\n";
continue;
}
reverse( f.begin() , f.end() );
cout << f[i] << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(0);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
int T;
cin >> T;
while( T-- ){
solve();
}
}
|
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,true:false;}
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,true:false;}
ll readint(){
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,c,q;
int p[30005];
ll d[30005][5],to[30005][5][20];
vector<pll> v[30005][5];
void work(){
n=readint(); c=readint(); q=readint();
for(int i=1;i<=n;i++) p[i]=readint();
for(int i=0;i<=c;i++) d[n+1][i]=1;
for(int i=n;i>=1;i--){
for(int j=0;j<=c;j++) d[i][j]=d[i+1][j];
if(i<=n-1){
for(int j=1;j<=c;j++) d[i][j]+=d[i+2][j-1];
}
if(i<=n-2){
for(int j=2;j<=c;j++) d[i][j]+=d[i+3][j-2];
}
if(i<=n-3){
for(int j=3;j<=c;j++) d[i][j]+=d[i+4][j-3];
}
if(i<=n-4){
for(int j=4;j<=c;j++) d[i][j]+=d[i+5][j-4];
}
for(int j=1;j<=c;j++) if(d[i][j]>1e18) d[i][j]=1e18+1;
}
for(int i=1;i<=n;i++){
for(int j=0;j<=c;j++){
v[i][j].clear();
v[i][j].pb(mp(p[i],d[i+1][j]));
if(i<=n-1&&j>=1) v[i][j].pb(mp(p[i+1],d[i+2][j-1]));
if(i<=n-2&&j>=2) v[i][j].pb(mp(p[i+2],d[i+3][j-2]));
if(i<=n-3&&j>=3) v[i][j].pb(mp(p[i+3],d[i+4][j-3]));
if(i<=n-4&&j>=4) v[i][j].pb(mp(p[i+4],d[i+5][j-4]));
sort(v[i][j].begin(),v[i][j].end());
ll sum=0;
for(auto r:v[i][j]){
if(r.fi==p[i]) to[i][j][0]=sum;
sum+=r.se;
if(sum>1e18) sum=1e18+1;
}
}
}
for(int k=1;k<=14;k++){
for(int i=1;i<=n-(1<<k)+1;i++){
for(int j=1;j<=c;j++){
to[i][j][k]=to[i][j][k-1]+to[i+(1<<(k-1))][j][k-1];
if(to[i][j][k]>1e18) to[i][j][k]=1e18+1;
}
}
}
ll x,y;
while(q--){
y=readint(); x=readint();
if(d[1][c]<x){
printf("-1\n");
continue;
}
int ni=1,nj=c,fl=0;
while(ni<=n){
assert(x<=d[ni][nj]);
for(int i=14;i>=0;i--)
if(ni+(1<<i)<=n+1&&x>to[ni][nj][i]&&x<=to[ni][nj][i]+d[ni+(1<<i)][nj])
x-=to[ni][nj][i],ni+=1<<i;
if(ni>n) break;
ll sum=0;
for(auto r:v[ni][nj]){
if(x<=sum+r.se){
assert(r.fi!=p[ni]);
if(ni<=n-1&&r.fi==p[ni+1]){
if(y==ni) printf("%d\n",p[ni+1]),fl=1;
if(y==ni+1) printf("%d\n",p[ni]),fl=1;
ni+=2,nj--;
}
else if(ni<=n-2&&r.fi==p[ni+2]){
if(y==ni) printf("%d\n",p[ni+2]),fl=1;
if(y==ni+2) printf("%d\n",p[ni]),fl=1;
ni+=3,nj-=2;
}
else if(ni<=n-3&&r.fi==p[ni+3]){
if(y==ni) printf("%d\n",p[ni+3]),fl=1;
if(y==ni+1) printf("%d\n",p[ni+2]),fl=1;
if(y==ni+2) printf("%d\n",p[ni+1]),fl=1;
if(y==ni+3) printf("%d\n",p[ni]),fl=1;
ni+=4,nj-=3;
}
else if(ni<=n-4&&r.fi==p[ni+4]){
if(y==ni) printf("%d\n",p[ni+4]),fl=1;
if(y==ni+1) printf("%d\n",p[ni+3]),fl=1;
if(y==ni+3) printf("%d\n",p[ni+1]),fl=1;
if(y==ni+4) printf("%d\n",p[ni]),fl=1;
ni+=5,nj-=4;
}
x-=sum;
break;
}
sum+=r.se;
if(fl) break;
}
if(fl) break;
}
if(!fl) printf("%d\n",p[y]);
}
}
int main(){
int T=readint();
while(T--) work();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef double db;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef pair<long long,int> PLI;
typedef pair<long long,long long> PLL;
const ll mod=1000000007;
//mt19937_64 mrand(random_device{}());
//int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
struct Node
{
int l,r;
ll w;
};
Node dq[5][400066];
int n,c,q;
int p[30066];
int sum[5][200066];
ll sumw[5][400066];
int l[5],r[5];
vector<PII>vec;
ll f_eq(int len,int c)
{
if(len<=1)return c==0;
if(len-1<c)return 0;
ll ret=1;
for(int i=len-1;i>=len-c;i--)ret*=i;
for(int i=c;i>1;i--)ret/=i;
return ret;
}
ll f(int len,int c)
{
ll ret=0;
for(int i=0;i<=c;i++)ret+=f_eq(len,i);
return ret;
}
bool cmp(Node&lhs,Node&rhs)
{
return p[lhs.r]<p[rhs.r];
}
Node F(int i,int c,ll rank)
{
//printf("____%d %d %lld\n",i,c,rank);
int L=l[c]+sum[c][i],R=r[c]-1;
while(L<R)
{
int mid=(L+R)>>1;
if(sumw[c][mid]-sumw[c][sum[c][i]+l[c]-1]>=rank)
R=mid;
else L=mid+1;
}
//printf("??? %d %d\n",L,sum[c][i]);
return Node{dq[c][L].l,dq[c][L].r,sumw[c][L-1]-sumw[c][sum[c][i]+l[c]-1]};
}
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
scanf("%d%d%d",&n,&c,&q);
//printf("%lld\n",f(n,c));
for(int i=1;i<=n;i++)scanf("%d",&p[i]);
for(int i=0;i<=c;i++)
for(int j=0;j<=n;j++)sum[i][j]=0;
for(int s=1;s<=c;s++)
{
l[s]=200033,r[s]=200033;
dq[s][r[s]++]=Node{n,n,1};
for(int i=n-1;i>0;i--)
{
int L=0,R=0;
for(int j=i+1;j<=min(n,i+s);j++)
{
if(p[j]<p[i])
{
L++;
dq[s][l[s]-L]=Node{i,j,f(n-j,s-(j-i))};
sum[s][i+1]++;
}
else
{
dq[s][r[s]+R]=Node{i,j,f(n-j,s-(j-i))};
R++;
}
}
l[s]-=L;
sort(dq[s]+l[s],dq[s]+l[s]+L,cmp);
r[s]+=R;
sort(dq[s]+r[s]-R,dq[s]+r[s],cmp);
}
for(int i=1;i<=n;i++)sum[s][i]+=sum[s][i-1];
sumw[s][l[s]-1]=0;
for(int i=l[s];i<r[s];i++)
sumw[s][i]=sumw[s][i-1]+dq[s][i].w;
}
// int tmp=4;
// for(int i=l[tmp];i<r[tmp];i++)
// printf("%d %d %lld____\n",dq[tmp][i].l,dq[tmp][i].r,dq[tmp][i].w);
// puts("_____");
// tmp=3;
// for(int i=l[tmp];i<r[tmp];i++)
// printf("%d %d %lld____\n",dq[tmp][i].l,dq[tmp][i].r,dq[tmp][i].w);
// printf("%lld ((\n",sumw[4][200023]);
ll lim=f(n,c);
while(q--)
{
int x;
ll y;
scanf("%d%lld",&x,&y);
vec.clear();
if(y>lim){puts("-1");continue;}
int pos=1,w=c;
while(pos<n&&w>0)
{
auto node=F(pos,w,y);
//puts("@@@");
vec.pb(mp(node.l,node.r));
pos=node.r+1;
w-=(node.r-node.l);
y-=node.w;
}
bool ok=false;
//for(auto node:vec)printf("%d %d__\n",node.fi,node.se);
for(auto node:vec)
{
if(node.fi<=x&&node.se>=x)
{
ok=true;
int ans=p[node.fi+node.se-x];
printf("%d\n",ans);
break;
}
}
if(!ok)printf("%d\n",p[x]);
}
}
return 0;
}
/*
1
3 1 4
1 2 3
1 1
2 1
3 1
1 2
1
6 4 1
6 5 4 3 1 2
3 14
*/ |
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <unordered_set>
#include <unordered_map>
#include <array>
using namespace std;
#define LL long long
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') {
f = -1;
}
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
inline void write(int x) {
static int sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
}
inline void writeln(int x) {
if (x < 0) {
putchar('-'); x = -x;
}
static int sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
putchar('\n');
}
inline LL readll() {
LL x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') {
f = -1;
}
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
inline void writell(LL x) {
if (x < 0) {
putchar('-'); x = -x;
}
static LL sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
}
inline void writellln(LL x) {
if (x < 0) {
putchar('-'); x = -x;
}
static LL sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
putchar('\n');
}
const int hashMod = 999983;
template<class TYPENAME>
class HashNode {
public:
TYPENAME val;
int key, next;
HashNode(TYPENAME _val, int _key, int _next): val(_val), key(_key), next(_next) {}
bool isNull() {
return key == -1;
}
};
template<class TYPENAME>
class Hash {
public:
vector<HashNode<TYPENAME> > nodes;
vector<int> d;
int nodeN;
Hash() {
nodeN = 0;
d = vector<int>(hashMod, -1);
}
void push(int key, TYPENAME val) {
nodes.push_back(HashNode<TYPENAME>(val, key, d[key]));
d[key] = nodeN++;
}
HashNode<TYPENAME> find(int key, TYPENAME val) {
for (int p = d[key]; p != -1; p = nodes[p].next) {
if (nodes[p].val == val) {
return nodes[p];
}
}
return HashNode<TYPENAME>(val, -1, -1);
}
};
int n, c, q;
vector<int> ii;
vector<LL> jj;
vector<int> p;
vector<vector<LL> > ways, wayC;
LL permContained(int l, int r, int cc) {
if (l == -1) {
return 1; // result for {-1, -1}
}
// otherwise, we can rotate (r, n)
// we have n - r - 1 to rotate, and cc - (r - l) coins
return ways[n - r - 1][cc - (r - l)];
}
// we can discretize all the queries, so we have an index for each query
// for each layer, we first save all the queries into boxes
// and take them out
bool pSmaller(const pair<int, int> &a, const pair<int, int> &b) {
return p[a.second] < p[b.second];
}
bool pGreater(const pair<int, int> &a, const pair<int, int> &b) {
return p[a.second] > p[b.second];
}
class Query {
public:
int ii, idx;
LL jj;
Query(int _idx, int _ii, LL _jj): idx(_idx), ii(_ii), jj(_jj) {}
void print() {
printf("Query(idx = %d, ii = %d, jj = %lld) ", idx, ii, jj);
}
};
bool cmpQuery(const Query &a, const Query &b) {
return a.jj < b.jj;
}
void solve() {
n = read(); c = read(); q = read();
// printf("task: n = %d. c = %d, q = %d\n", n, c, q);
p = vector<int>(n);
for (int i = 0; i < n; ++i) {
p[i] = read();
}
ii = vector<int>(q);
jj = vector<LL>(q);
for (int i = 0; i < q; ++i) {
ii[i] = read() - 1; jj[i] = readll() - 1;
// cout << ii[i] << ' ' << jj[i] << endl;
}
// for (int i = 0; i < q; ++i) {
// // ii[i] = read() - 1; jj[i] = readll() - 1;
// cout << ii[i] << ' ' << jj[i] << endl;
// }
wayC = vector<vector<LL> >(n);
for (int i = 0; i < n; ++i) {
wayC[i] = vector<LL>(c + 1, 0);
wayC[i][0] = 1;
}
// wayC[n][c] = C(n, c)
for (int i = 0; i < n; ++i) {
for (int j = 1; j <= c; ++j) {
// consider C(i, j)
if (i < j) {
wayC[i][j] = 0;
} else {
wayC[i][j] = wayC[i - 1][j] + wayC[i - 1][j - 1];
}
}
}
// cout << "finish wayC" << endl;
// for (int i = 0; i < q; ++i) {
// // ii[i] = read() - 1; jj[i] = readll() - 1;
// cout << ii[i] << ' ' << jj[i] << endl;
// }
ways = vector<vector<LL> >(n + 1);
for (int i = 0; i <= n; ++i) {
ways[i] = vector<LL>(c + 1, 1);
}
// ways[n][c] = C(n - 1, 0) + C(n - 1, 1) + ... + C(n - 1, c)
// ways[n][c] = ways[n][c - 1] + wayC[n - 1][c] (n > 0)
for (int i = 1; i <= n; ++i) {
ways[i][0] = wayC[i - 1][0];
for (int j = 1; j <= c; ++j) {
ways[i][j] = ways[i][j - 1] + wayC[i - 1][j];
// printf("ways(len = %d, c = %d) = %lld\n", i, j, ways[i][j]);
}
}
// cout << "finish ways" << endl;
// for (int i = 0; i < q; ++i) {
// // ii[i] = read() - 1; jj[i] = readll() - 1;
// cout << ii[i] << ' ' << jj[i] << endl;
// }
// now ways[len][c] = ways(n, c)
// we should build a deque for each 0 .. c, contains
// the [l, r] as the leftmost rotation(not over c)
// and sorted by the lex order. Our queries are all on layer c(we can use at most c coins)
// we sort all the jj, and scan the deque, every time we find an [l, r] so that jj is behind this
// we know that [0 .. r] has been decided for this jj query. if ii <= r, we can return the query result
// otherwise we want to decide the remaining part jj - ord([l, r]) = jj', with coins c - r + l,
// and start from r + 1
// for each c, we can decide p[l] "permutations before using [l, r] started at l" for each l in [0, n)
// so the query becomes another query (c - r + l, jj' + p[r + 1], ii) on layer c - r + l
// finally we have all queryies on layer 0, so there is only one state for queries
// queries down to here must asks something hard to decide, and here we must have jj = 1
// then we just return i-th
// first to make deques for each c' = [0 .. c]
vector<deque<pair<int, int> > > dq(c + 1);
for (int cc = 0; cc <= c; ++cc) {
// dq[cc].push_back({-1, -1}); // no change
dq[cc].push_back({n - 1, n - 1});
for (int i = n - 1; i >= 0; --i) {
vector<pair<int, int> > lft, rgt;
for (int j = i + 1; j <= i + cc && j < n; ++j) {
// we have rotations [i, j]
// current deque has all permutations with i-th = p[i]
// so [i, j] is with i-th = p[j]
// we should divide it into two parts: p[j] < p[i] && p[j] > p[i]
// for p[j] < p[i], we insert it at front, from the order p[j] large to small
// for p[j] > p[i], we insert it at back, from small to large
// printf("p[j = %d] = %d, p[i = %d] = %d\n", j, p[j], i, p[i]);
if (p[j] < p[i]) {
lft.push_back({i, j});
} else {
rgt.push_back({i, j});
}
}
sort(lft.begin(), lft.end(), pGreater);
sort(rgt.begin(), rgt.end(), pSmaller);
for (int i = 0; i < lft.size(); ++i) {
dq[cc].push_front(lft[i]);
}
for (int i = 0; i < rgt.size(); ++i) {
dq[cc].push_back(rgt[i]);
}
}
// cout << "c = " << cc << endl;
// for (auto pr: dq[cc]) {
// printf("(%d, %d) ", pr.first, pr.second);
// }
// putchar('\n');
}
// cout << "finish creating deques" << endl;
// int mc = 0;
// vector<int> queryIdx = discretize(jj, mc);
// vector<LL> ts(mc + 1, 0);
// for (int i = 0; i < q; ++i) {
// ts[queryIdx[i]] = jj[i];
// }
// vector<vector<vector<Query> > > ids(c + 1);
// for (int i = 0; i <= c; ++i) {
// ids[i] = vector<vector<Query> >(mc + 1);
// }
// for (int i = 0; i < q; ++i) {
// ids[c][queryIdx[i]].push_back(Query(i, ii[i], queryIdx[i], jj[i]));
// }
vector<int> ans(q, -1);
// for (int i = 0; i < q; ++i) {
// ans[i] = -1;
// }
// for (int i = 0; i < q; ++i) {
// cout << ans[i] << ' ';
// }
// cout << endl;
vector<vector<Query> > que(c + 1);
for (int i = 0; i < q; ++i) {
que[c].push_back(Query(i, ii[i], jj[i]));
// que[c].back().print();
}
vector<vector<LL> > pp(c + 1);
// pp[cc][l]: the permutations before the first [l, r] started at l(l in [0, n - 1))
for (int cc = 0; cc <= c; ++cc) {
pp[cc] = vector<LL>(n, -1);
LL perms = 0;
int ll, rr;
for (pair<int, int> pr: dq[cc]) {
ll = pr.first; rr = pr.second;
if (ll == -1) {
continue;
}
if (pp[cc][ll] == -1) {
pp[cc][ll] = perms;
// printf("pp[c = %d][l = %d] = %lld, from (l = %d, r = %d)\n", cc, ll, pp[cc][ll], ll, rr);
}
perms += permContained(ll, rr, cc);
}
// pp[cc][n - 1] = perms - 1;
for (int i = n - 2; i >= 0; --i) {
pp[cc][i] = min(pp[cc][i], pp[cc][i + 1]);
}
for (int i = 0; i < n; ++i) {
// printf("pp[c = %d][l = %d] = %lld\n", cc, i, pp[cc][i]);
}
// how to deal with -1, -1? it means no rotation at all
// and contains exactly 1 permutation p0 ... p_{n - 1}
}
// cout << "finish calculating pp's" << endl;
for (int cc = c; cc >= 0; --cc) {
// work on layer cc
if (que[cc].size() == 0) {
continue;
}
sort(que[cc].begin(), que[cc].end(), cmpQuery);
// now we have queries saved in que[cc]
// and sorted by order of jj
// for [l, r] on layer cc, the undecided part is (r, n), len = n - r - 1
// so it contains ways[n - r - 1][cc - (r - l)]
// we go from the beginning of dq[cc], and use a pointer in que[cc] for current
// when we want to insert a new query c', jj', ii, we just do que[c'].push_back(Query(idx, ii, jj'))
// TODO: !!!!!
int qcur = 0;
LL currPerm = 0;
// cout << "coin " << cc << ": ";
// for (int i = 0; i < que[cc].size(); ++i) {
// // printf("Query(idx = %d, ii = %d, jj = %lld) ", que[cc][i].idx, que[cc][i].ii, que[cc][i].jj);
// que[cc][i].print();
// }
// cout << endl;
// for (int i = 0; i < q; ++i) {
// cout << ans[i] << ' ';
// }
// cout << endl;
for (pair<int, int> pr: dq[cc]) {
int ll = pr.first, rr = pr.second;
// printf("(%d, %d) ", ll, rr);
// printf("l = %d, r = %d, cc = %d, currPerm = %lld\n", ll, rr, cc, currPerm);
LL newPerm = permContained(ll, rr, cc);
// printf("new perm = %lld\n", newPerm);
while (qcur < que[cc].size() && que[cc][qcur].jj < currPerm + newPerm) {
int cii = que[cc][qcur].ii;
LL cjj = que[cc][qcur].jj;
int cidx = que[cc][qcur].idx;
// printf("qcur = %d: idx = %d, ii = %d, jj = %lld\n", qcur, cidx, cii, cjj);
if (ll == -1 || cii < ll) {
// cout << "quit1" << endl;
ans[cidx] = p[cii];
++qcur; continue;
}
if (cii >= ll && cii <= rr) {
// cout << "quit2" << endl;
ans[cidx] = p[rr - (cii - ll)];
++qcur; continue;
}
// otherwise, it is not determined
// we should insert it into cc - (rr - ll)
int nc = cc - (rr - ll);
// what is the rank of the permutation in nc?
// all [0, r] fixed, so we need to find the (que[cc][qucr].jj - currPerm)-th
// in dq[cc], after pp[cc][r + 1]
if (rr >= n - 1) {
// cout << "quit-1" << endl;
ans[cidx] = -1;
++qcur; continue;
}
// otherwise, we should insert it to layer nc
que[nc].push_back(Query(cidx, cii, pp[nc][rr + 1] + cjj - currPerm));
++qcur;
// cout << "quit0" << endl;
}
currPerm += newPerm;
}
}
for (int i = 0; i < q; ++i) {
writeln(ans[i]);
}
}
int main() {
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
int t = read();
for (int i = 0; i < t; ++i) {
solve();
}
return 0;
} |
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define ll long long
#define lb(x) ((x)&-(x))
#define pii pair<int,int>
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define de(x) cout<<#x<<" x"<<endl
#define LCAFA rep(i,1,20)rep(j,1,n+1)fa[j][i]=fa[fa[j][i-1]][i-1]
#define all(x) x.begin(),x.end()
#define ls(x) x<<1
#define rs(x) x<<1|1
#define pr(x) {for(auto v:x)cout<<v<<' ';cout<<#x<<endl;}
using namespace std;
const int N=3e4+9;
const ll mod=998244353;
const ll Inf=1e18;
/*
inline int add(int a,const int &b){
a+=b;
if(a>=mod)a-=mod;
return a;
}
inline int sub(int a,const int &b){
a-=b;
if(a<0)a+=mod;
return a;
}
inline int mul(const int &a,const int &b){return 1ll*a*b%mod;}
int jie[N],inv[N];
ll quick(ll a,ll b){
ll res=1;
while(b){
if(b&1)res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
inline int C(int m,int n){
if(n>m||m<0||n<0)return 0;
return mul(mul(jie[m],inv[n]),inv[m-n]);
}
void pre(){
inv[0]=inv[1]=1,jie[0]=1;
rep(i,2,N)inv[i]=-1ll*mod/i*inv[mod%i]%mod+mod;
rep(i,1,N)inv[i]=mul(inv[i],inv[i-1]),jie[i]=mul(jie[i-1],i);
}
*/
ll dp[N][5];
int p[N];
vi s[N];
ll L[N][5][16],R[N][5][16];
int main(){
//pre();
int T;
scanf("%d",&T);
while(T--){
int n,c,q;
scanf("%d%d%d",&n,&c,&q);
rep(i,0,n)scanf("%d",&p[i]);
rep(i,0,c+1)dp[n][i]=1;
per(i,0,n){
rep(j,0,c+1){
dp[i][j]=0;
rep(k,0,j+1){
if(i+k<n)dp[i][j]+=dp[i+k+1][j-k];
}
}
}
rep(i,0,n){
s[i].clear();
rep(j,0,c+1){
if(i+j<n)s[i].pb(j);
}
sort(all(s[i]),[&](int a,int b){return p[i+a]<p[i+b];});
}
int lim=0;
while((1<<lim)<n)++lim;
rep(i,0,n)rep(j,0,c+1)rep(k,0,lim+1)L[i][j][k]=R[i][j][k]=0;
per(i,0,n){
rep(j,0,c+1){
if(i+j>=n)break;
ll sum=0;
for(auto v:s[i]){
if(v>j)continue;
if(v==0)break;
sum+=dp[i+v+1][j-v];
}
L[i][j][0]=sum;
R[i][j][0]=sum+dp[i+1][j];
rep(k,1,lim+1){
if(i+(1<<k)<=n){
L[i][j][k]=L[i][j][k-1]+L[i+(1<<k-1)][j][k-1];
R[i][j][k]=L[i][j][k-1]+R[i+(1<<k-1)][j][k-1];
}
}
}
}
while(q--){
int pos;
ll id;
scanf("%d%lld",&pos,&id);
if(id>dp[0][c]){
puts("-1");
continue;
}
--pos;
--id;
int i=0,ans=p[pos],r=c;
// cout<<ans<<"ans\n";
int cnt=0;
while(i<n){
++cnt;
if(cnt>6)return 0;
for(int j=lim;j>=0;--j){
if(i+(1<<j)<=n&&L[i][r][j]<=id&&id<R[i][r][j]){
id-=L[i][r][j];
i+=1<<j;
}
}
if(i==n)break;
// cout<<cnt<<' '<<i<<' '<<r<<' '<<id<<"i\n";
for(auto v:s[i]){
if(v>r)continue;
if(id>=dp[i+v+1][r-v]){
id-=dp[i+v+1][r-v];
}
else{
// cout<<v<<"v\n";
int i1=i+v;
r-=v;
if(i<=pos&&pos<=i1){
ans=p[i+i1-pos];
}
i=i1+1;
break;
}
}
}
printf("%d\n",ans);
}
}
}
/*
1
3 1 1
1 2 3
2 1
1
6 4 1
6 5 4 3 1 2
3 14
1
20 4 1
16 5 15 14 13 3 17 18 2 20 19 6 4 1 12 8 11 10 9 7
8 1100
*/
|
/*
Problem :
Algorithm :
Status :
*/
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#define DEBUG cerr << "Passing Line " << __LINE__<< " in Function [" << __FUNCTION__ << "].\n";
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
template<class T> inline bool checkMax(T &a,const T &b) {return a < b ? a = b,1 : 0;}
template<typename T, typename...Args> inline void checkMax(T &a,const Args...arg) {checkMax(a,max(arg...));}
template<class T> inline bool checkMin(T &a,const T &b) {return a > b ? a = b,1 : 0;}
template<typename T, typename...Args> inline void checkMin(T &a,const Args...arg) {checkMin(a,min(arg...));}
namespace FastIO{
const int SIZE = 1 << 21;
char ch;
char in[SIZE],*inS = in,*inT = in + SIZE;
char out[SIZE],*outS = out,*outT = out + SIZE;
#define flush() fwrite(out,1,outS - out,stdout), outS = out
#define getchar() (inS == inT && (inT = (inS = in) + fread(in,1,SIZE,stdin),inS == inT) ? EOF : *inS++)
void putchar(char c) {*outS++ = c; if(outS == outT) flush();}
struct Flusher {~Flusher() {flush();}} flusher;
template<typename T> inline void read(T &x){
static int f; x = 0; f = 1; ch = getchar();
while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
while(isdigit(ch)) {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} x *= f;
}
inline void read(char* s){
static int len; len = 0; ch = getchar();
while(ch == '\n' || ch == ' ' || ch == '\0') ch = getchar();
while(ch != '\n' && ch != ' ' && ch != EOF) s[len++] = ch, ch = getchar(); s[len] = '\0';
}
template<typename T> inline void write(T x){
if(!x) putchar('0');
if(x < 0) {x = -x; putchar('-');}
static int sta[20]; int tot = 0;
while(x) {sta[tot++] = x % 10; x /= 10;}
while(tot) putchar(sta[--tot] + 48);
}
inline void write(char c) {putchar(c);}
template<typename T> inline void writeln(T c) {write(c); putchar('\n');}
template<typename T,typename... Args> inline void read(T &t, Args &...args) {read(t); read(args...);}
}
using FastIO::read;
using FastIO::write;
using FastIO::writeln;
const int INF = 0x3f3f3f3f;
const ll llINF = 1e18;
const int MAXN = 6e4 + 5;
const int MAXC = 5;
int n,C,Q,T;
int cnt[MAXC][MAXN],p[MAXN];
ll comb[MAXN][MAXC],w[MAXC][MAXN * MAXC];
struct Node{
int l,r; ll w;
Node(int _l = 0,int _r = 0,ll _w = 0) : l(_l), r(_r), w(_w) {}
};
struct Deque{
int head,tail;
Node a[MAXN * MAXC * 2 + 100];
Node& operator [] (const int &i) {return a[head + i - 1];}
void push_front(Node u) {a[--head] = u; }
void push_back(Node u) {a[++tail] = u;}
void clear() {head = MAXN * MAXC + 5, tail = MAXN * MAXC + 4;}
int size() {return tail - head + 1;}
Deque() {clear();}
} q[MAXC];
void Init(int n){
comb[0][0] = 1;
for(int i = 1;i <= n;i++){
comb[i][0] = 1;
for(int j = 1;j <= min(MAXC - 1,i);j++)
comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
}
for(int i = 0;i <= n;i++){
for(int j = 1;j < MAXC;j++) comb[i][j] += comb[i][j - 1];
}
}
ll calc(int n,int c){
if(n <= 0) return 1;
return comb[n - 1][c];
}
bool cmp(const Node &x,const Node &y){
return p[x.r] < p[y.r];
}
Node GetNxt(int c,int pos,ll rk){
int num = cnt[c][pos];
int l = num + 1, r = q[c].size(), res = 0;
while(l <= r){
int mid = (l + r) >> 1;
if(w[c][mid] - w[c][num] >= rk) res = mid, r = mid - 1;
else l = mid + 1;
}
return Node(q[c][res].l,q[c][res].r,w[c][res - 1] - w[c][num]);
}
void Solve(){
read(n,C,Q);
for(int i = 1;i <= n;i++) read(p[i]);
for(int c = 1;c <= C;c++){
q[c].clear(); vector<Node> front,back;
for(int i = 1;i <= n;i++) cnt[c][i] = 0;
q[c].push_back(Node(n,n,1));
for(int i = n - 1;i >= 1;i--){
front.clear(); back.clear();
for(int j = 1;j <= min(c,n - i);j++){
if(p[i + j] < p[i])
front.emplace_back(i,i + j,calc(n - i - j,c - j)), cnt[c][i + 1] += 1;
else back.emplace_back(i,i + j,calc(n - i - j,c - j));
}
sort(front.begin(),front.end(),cmp); reverse(front.begin(),front.end());
sort(back.begin(),back.end(),cmp);
for(auto x : front) q[c].push_front(x);
for(auto x : back) q[c].push_back(x);
}
for(int i = 1;i <= n;i++) cnt[c][i] += cnt[c][i - 1];
for(int i = 1, sz = q[c].size();i <= sz;i++)
w[c][i] = w[c][i - 1] + q[c][i].w;
}
while(Q--){
int pos; ll rk; read(pos,rk);
if(rk > calc(n,C)) {writeln(-1); continue;}
int x = 1, c = C; vector<pii> vec; vec.clear();
while(c && x <= n){
Node res = GetNxt(c,x,rk);
rk -= res.w; c -= (res.r - res.l); x = res.r + 1;
vec.emplace_back(res.l,res.r);
}
int flag = 0;
for(auto pi : vec){
if(pos >= pi.first && pos <= pi.second){
writeln(p[pi.second - pos + pi.first]);
flag = 1; break;
}
}
if(!flag) writeln(p[pos]);
}
}
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
Init(MAXN - 1); read(T); while(T--) Solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N=30050;
int t,n,c,q;
unsigned long long a[N][5],presum[N][5],nxtsum[N][5];
int p[N],ch[N][6];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&c,&q);
for(int i=1;i<=n;i++)
{
scanf("%d",&p[i]);
a[i][0]=1;
ch[i][0]=0;
}
for(int j=0;j<=c;j++) a[n+1][j]=1;
for(int j=1;j<=c;j++)
{
for(int i=n;i>=1;i--)
{
a[i][j]=0;
for(int k=0;k<=j&&i+k<=n;k++)
a[i][j]+=a[i+k+1][j-k];
assert(a[i][j]<=1e18);
}
}
pair<int,int> temp[6];
for(int i=1;i<=n;i++)
{
temp[0].first=0;
for(int j=0;j<=c&&i+j<=n;j++)
temp[++temp[0].first]=make_pair(p[i+j],j);
sort(temp+1,temp+temp[0].first+1);
ch[i][0]=temp[0].first;
memcpy(presum+i,presum+i-1,5*sizeof(unsigned long long));
memcpy(nxtsum+i,nxtsum+i-1,5*sizeof(unsigned long long));
bool flag=false;
for(int j=1;j<=temp[0].first;j++)
{
ch[i][j]=temp[j].second;
if(ch[i][j]==0) {flag=true;continue;}
if(!flag)
{
for(int k=ch[i][j];k<=c;k++)
presum[i][k]+=a[i+ch[i][j]+1][k-ch[i][j]];
}
else
{
for(int k=ch[i][j];k<=c;k++)
nxtsum[i][k]+=a[i+ch[i][j]+1][k-ch[i][j]];
}
}
}
while(q--)
{
int x;
unsigned long long y;
scanf("%d%llu",&x,&y);
vector<pair<int,int> > v;
int pos=1,nowc=c;
if(y>a[1][c]) {puts("-1");continue;}
while(pos<=n)
{
if(nowc==0||presum[n][nowc]-presum[pos-1][nowc]+1==y) break;
else if(presum[n][nowc]-presum[pos-1][nowc]>=y)
{
int l=pos,r=n;
while(l<r)
{
int mid=(l+r)>>1;
if(presum[mid][nowc]-presum[pos-1][nowc]>=y) r=mid;
else l=mid+1;
}
y-=presum[l-1][nowc]-presum[pos-1][nowc];
pos=l;
for(int i=1;i<=ch[pos][0];i++)
if(nowc<ch[pos][i]) continue;
else if(a[pos+ch[pos][i]+1][nowc-ch[pos][i]]<y)
y-=a[pos+ch[pos][i]+1][nowc-ch[pos][i]];
else
{
v.push_back(make_pair(pos,ch[pos][i]));
nowc-=ch[pos][i];
pos+=ch[pos][i]+1;
break;
}
}
else
{
y-=presum[n][nowc]-presum[pos-1][nowc]+1;
int l=pos,r=n+1;
while(l<r)
{
int mid=(l+r)>>1;
if(nxtsum[n][nowc]-nxtsum[mid-1][nowc]<y) r=mid;
else l=mid+1;
}
y-=nxtsum[n][nowc]-nxtsum[l-1][nowc];
pos=l-1;
bool flag=false;
for(int i=1;i<=ch[pos][0];i++)
if(ch[pos][i]==0) flag=true;
else if(flag)
{
if(nowc<ch[pos][i]) continue;
else if(a[pos+ch[pos][i]+1][nowc-ch[pos][i]]<y)
y-=a[pos+ch[pos][i]+1][nowc-ch[pos][i]];
else
{
v.push_back(make_pair(pos,ch[pos][i]));
nowc-=ch[pos][i];
pos+=ch[pos][i]+1;
break;
}
}
}
}
int ans=p[x];
for(auto u:v)
{
if(x>=u.first&&x<=u.first+u.second)
ans=p[u.first+u.first+u.second-x];
}
printf("%d\n",ans);
}
}
return 0;
} |
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <list>
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 30000;
const int MAXCOST = 4;
const int MAXQ = 300000;
int n, mxcost, nq;
int p[MAXN];
int qpos[MAXQ]; ll qperm[MAXQ]; int qans[MAXQ];
vector<int> opt[MAXCOST + 1][MAXN];
ll ways[MAXCOST + 1][MAXN + 1];
ll offset[MAXCOST + 1][MAXN + 1];
map<ll, pair<int, int>> nxt[MAXCOST + 1];
void safeinc(ll& a, ll b) { if (a > LLONG_MAX - b) a = LLONG_MAX; else a += b; }
void solve() {
REPE(c,mxcost) REP(i, n) {
opt[c][i].clear();
for (int j = i; j < n && j <= i + c; ++j) opt[c][i].PB(j);
sort(opt[c][i].begin(), opt[c][i].end(), [&](const int& a, const int& b) { return p[a] < p[b]; });
}
REPE(c, mxcost) {
ways[c][n] = 1;
for (int i = n - 1; i >= 0; --i) {
ways[c][i] = 0;
for (int j : opt[c][i]) safeinc(ways[c][i], ways[c - (j - i)][j + 1]);
}
//printf("ways%d:", c); REP(i, n) printf(" %lld", ways[c][i]); puts("");
}
REPE(c, mxcost) {
nxt[c].clear();
ll at = 0;
REP(i, n) {
offset[c][i] = at;
ll to = -1;
for (int j : opt[c][i]) {
if (j == i) {
to = at;
} else {
nxt[c][at] = MP(i, j);
}
safeinc(at, ways[c - (j - i)][j + 1]);
}
assert(to != -1); at = to;
}
offset[c][n] = at;
nxt[c][at] = MP(n, n);
nxt[c][ways[c][0]] = MP(-1, -1);
//printf("nxt%d:", c); for (auto it = nxt[c].begin(); it != nxt[c].end(); ++it) printf(" %lld:%d/%d", it->first, it->second.first, it->second.second); puts("");
}
REP(i, nq) {
int c = mxcost; ll want = qperm[i]; int pos = qpos[i];
//printf("processing %d: %lld\n", i, want);
if (want >= ways[c][0]) { qans[i] = -1; continue; }
vector<pair<int, int>> revs;
while (true) {
auto it = prev(nxt[c].upper_bound(want));
int at = it->second.first, to = it->second.second;
want -= it->first;
if (at == n && to == n) { assert(want == 0); break; }
//printf("\t(%d,%d)\n", at, to);
revs.PB(MP(at, to));
int nc = c - (to - at);
assert(offset[nc][to + 1] != LLONG_MAX);
c = nc, want += offset[nc][to + 1];
}
if (SZ(revs) == 0) { qans[i] = p[pos]; continue; }
if (pos < revs[0].first) qans[i] = p[pos];
FORSZ(j, 1, revs) if (pos > revs[j - 1].second && pos < revs[j].first) qans[i] = p[pos];
if (pos > revs[SZ(revs) - 1].second) qans[i] = p[pos];
REPSZ(j, revs) { int a = revs[j].first, b = revs[j].second; if (a <= pos && pos <= b) qans[i] = p[a + (b - pos)]; }
}
}
void run() {
scanf("%d%d%d", &n, &mxcost, &nq);
REP(i, n) scanf("%d", &p[i]), --p[i];
REP(i, nq) scanf("%d%lld", &qpos[i], &qperm[i]), --qpos[i], --qperm[i];
solve();
REP(i, nq) printf("%d\n", qans[i] == -1 ? -1 : qans[i] + 1);
}
void stress() {
REP(rep, 10000) {
n = MAXN;
mxcost = MAXCOST; // rnd() % (MAXCOST + 1);
nq = MAXQ;
REP(i, n) p[i] = i;
//REP(i, n) { int idx = rnd() % (i + 1); swap(p[i], p[idx]); }
REP(i, nq) qpos[i] = rnd() % n, qperm[i] = (ll)(rnd() % 1000000000) * 1000000000 + rnd() % 1000000000;
solve();
printf(".");
}
}
int main() {
int ncase; scanf("%d", &ncase); FORE(i, 1, ncase) run();
//stress();
return 0;
}
|
#include<bits/stdc++.h>
typedef long long LL;
#define opt operator
#define pb push_back
#define pii std::pair<LL,LL>
const LL maxn=3e4+9,maxc=5;
LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0' && c<='9'){
x=(x<<3ll)+(x<<1ll)+c-'0'; c=getchar();
}return x*f;
}
LL T,n,C,q;
LL p[maxn],dl[maxn],dr[maxn],sumw[maxc][(maxn*maxc<<1)+10];
pii range[maxc][maxn];
struct node{
LL l,r,w;
bool opt < (const node A)const{
return p[r]<p[A.r];
}
};
node L[maxc][(maxn*maxc<<1)+10];
LL Comb(LL N,LL M){
if(N==M) return 1;
if(N<M) return 0;
LL ret(1);
for(LL i=1;i<=M;++i) ret*=(N-i+1);
for(LL i=1;i<=M;++i) ret/=i;
//printf("%lld,%lld:%lld\n",N,M,ret);
return ret;
}
LL Way(LL len,LL c){
//printf("(%lld,%)")
LL ret(1);
for(LL i=1;i<=c;++i) ret+=Comb(len-1,i);
return ret;
}
node F(LL i,LL c,LL w){
LL l(range[c][i].first),r(range[c][i].second),nw(l-1);
//printf("(%lld,%lld):(%lld,%lld)\n",c,i,l-dl[c]+1,r-dl[c]+1);
LL v(sumw[c][range[c][i].first-1]);
while(l<=r){
LL mid(l+r>>1);
if(sumw[c][mid]-v<w){
nw=mid; l=mid+1;
}else r=mid-1;
}
node tmp(L[c][nw+1]); tmp.w=sumw[c][nw]-v;
return tmp;
}
void Init(){
for(LL c=0;c<=C;++c){
dl[c]=maxn*maxc+5; dr[c]=maxn*maxc+4;
L[c][++dr[c]]=(node){n,n,1};
range[c][n]=pii(dl[c],dr[c]);
for(LL i=n-1;i>=1;--i){
LL _l(0),_r(0);
for(LL len=1;len<=std::min(c,n-i);++len){
LL j(i+len);
if(p[j]<p[i]){
++_l;
L[c][dl[c]-_l]=(node){i,j,Way(n-j,c-len)};
}else{
++_r;
L[c][dr[c]+_r]=(node){i,j,Way(n-j,c-len)};
}
}
if(_l){
std::sort(L[c]+dl[c]-_l,L[c]+dl[c]);
dl[c]-=_l;
}
if(_r){
std::sort(L[c]+dr[c]+1,L[c]+dr[c]+_r+1);
dr[c]+=_r;
}
range[c][i]=pii(dl[c],dr[c]);
}
sumw[c][dl[c]-1]=0;
for(LL i=dl[c];i<=dr[c];++i){
sumw[c][i]=sumw[c][i-1]+L[c][i].w;
}
/*
for(LL i=dl[c];i<=dr[c];++i){
printf("(%lld,%lld,%lld)",L[c][i].l,L[c][i].r,L[c][i].w);
}
puts("");
*/
}
}
int main(){
T=Read();
while(T--){
n=Read(); C=Read(); q=Read();
for(LL i=1;i<=n;++i) p[i]=Read();
Init();
//puts("033");
while(q--){
LL pos1(Read()),pos2(Read());
if(pos2>sumw[C][dr[C]]){
puts("-1"); continue;
}
LL pos(1);
LL c(C);
std::vector<node> suq;
while(true){
node tmp(F(pos,c,pos2));
suq.pb(tmp);
//printf("(%lld,%lld:%lld)\n",tmp.l,tmp.r,tmp.w);
//if(!tmp.l) exit(0);
pos2-=tmp.w;
c-=tmp.r-tmp.l;
pos=tmp.r+1;
if(pos>n || !pos2) break;
}
LL flag(0);
for(LL i=0;i<suq.size();++i){
LL l(suq[i].l),r(suq[i].r);
if(l<=pos1 && pos1<=r){
printf("%lld\n",p[r-(pos1-l)]);
flag=1;
break;
}
}
if(!flag){
printf("%lld\n",p[pos1]);
}
}
}
} |
#include<bits/stdc++.h>
#define mz 1000000007
using namespace std;
pair<long long,long long> pp;
vector <pair<long long,long long> > vb[30005],vs[30005];
long long p[30005];
long long c[30005][10],s[30005][10];
long long bg[30005][10],sm[30005][10],sumb[30005][10],sums[30005][10];
long long y,wtf;
long long n,k,x,z,now;
void go(long long l)
{
if(now==0 || l>x || y==0)
return;
long long lp=l-1,r=n-1,mid;
if(y>sums[n-1][now]-sums[lp][now])
{
y-=(sums[n-1][now]-sums[lp][now]);
y--;
if(y==0)
return ;
while(l!=r)
{
mid=(l+r)/2;
if(sumb[n-1][now]-sumb[mid][now]>=y)
l=mid+1;
else
r=mid;
}
y-=sumb[n-1][now]-sumb[l][now];
for(long long i=0;i<vb[l].size();i++)
{
if(now<vb[l][i].second-l)
continue;
if(y<=s[n-vb[l][i].second][now-(vb[l][i].second-l)])
{
if((l<=x) && (vb[l][i].second>=x))
{
z=p[vb[l][i].second-(x-l)];
}
now-=(vb[l][i].second-l);
go(vb[l][i].second+1);
return ;
}
else
y-=s[n-vb[l][i].second][now-(vb[l][i].second-l)];
}
}
else
{
while(l!=r)
{
mid=(l+r)/2;
if((sums[mid][now]-sums[lp][now])>=y)
r=mid;
else
l=mid+1;
}
y-=sums[l-1][now]-sums[lp][now];
for(long long i=0;i<vs[l].size();i++)
{
if(now<vs[l][i].second-l)
continue;
if(y<=s[n-vs[l][i].second][now-(vs[l][i].second-l)])
{
if((l<=x) && (vs[l][i].second>=x))
{
z=p[vs[l][i].second-(x-l)];
}
now-=(vs[l][i].second-l);
go(vs[l][i].second+1);
return ;
}
else
y-=s[n-vs[l][i].second][now-(vs[l][i].second-l)];
}
}
}
signed main()
{
c[1][0]=c[0][0]=1;
for(long long j=0;j<=4;j++)
s[1][j]=s[0][j]=1;
for(long long i=2;i<=30000;i++)
{
c[i][0]=s[i][0]=1;
for(int j=1;j<=4;j++)
{
c[i][j]=c[i-1][j-1]+c[i-1][j];
s[i][j]=s[i][j-1]+c[i][j];
}
}
long long t,q;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld%lld",&n,&k,&q);
for(long long i=1;i<=n;i++)
scanf("%lld",&p[i]);
for(long long i=1;i<n;i++)
{
vs[i].clear();
vb[i].clear();
for(long long j=1;j<=k;j++)
bg[i][j]=sm[i][j]=0;
for(long long j=i+1;j<=n && j-i<=k;j++)
{
pp.first=p[j];
pp.second=j;
if(p[j]<p[i])
{
vs[i].push_back(pp);
for(int l=j-i;l<=k;l++)
sm[i][l]+=s[n-j][l-(j-i)];
}
else
{
vb[i].push_back(pp);
for(int l=j-i;l<=k;l++)
bg[i][l]+=s[n-j][l-(j-i)];
}
}
sort(vs[i].begin(),vs[i].end());
sort(vb[i].begin(),vb[i].end());
for(int j=1;j<=k;j++)
{
sumb[i][j]=sumb[i-1][j]+bg[i][j];
sums[i][j]=sums[i-1][j]+sm[i][j];
}
}
wtf=sumb[n-1][k]+1+sums[n-1][k];
while(q--)
{
scanf("%lld%lld",&x,&y);
if(y>wtf)
z=-1;
else
{
z=p[x];
now=k;
go(1);
}
printf("%lld\n",z);
}
}
return 0;
}
|
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 30001; //check the limits, dummy
ll ch[MX][5];
void solve() {
int N, C, Q; cin >> N >> C >> Q;
set<pl> cur[C+1];
ll del[C+1]; F0R(i, C+1) del[i] = 0;
int rem[Q]; F0R(i, Q) rem[i] = C;
vector<vi> atPos(N);
vpl toAdd[N][C+1];
int pos[Q];
ll nc[Q];
int ans[Q]; F0R(i, Q) ans[i] = -1;
int P[N]; F0R(i, N) cin >> P[i];
F0R(i, Q) {
int X; ll K; cin >> X >> K;
X--; K--;
cur[C].ins({K, i});
pos[i] = X;
nc[i] = K;
atPos[X].pb(i);
}
F0R(i, N) {
F0R(j, C+1) {
trav(a, toAdd[i][j]) { cur[j].ins({a.f+del[j], a.s});
//cout << i << " " << j << " " << a.f << " " << a.s << " " << del[j] << " " << nc[a.s] << nl;
nc[a.s] += del[j];
}
}
F0R(j, C+1) {
vpi ops;
F0R(k, j+1) {
if (i+k < N) {
ops.pb({P[i+k], k});
}
}
sort(all(ops));
ll curSum = 0;
F0R(x, sz(ops)) {
pi a = ops[x];
if (a.s == 0) {
ll num = 1; if (i < N-1) num = ch[N-i-2][j];
del[j] += curSum;
curSum = num;
} else {
ll numWith = 1;
if (N-i-a.s-1 > 0) numWith = ch[N-i-a.s-2][j-a.s];
/* if (i == 1 && j == 1) {
cout << del[j] << " " << curSum << " " << numWith << nl;
}*/
auto it = cur[j].ub({del[j] + curSum, -1});
while (it != cur[j].end() && it->f < del[j] + curSum + numWith) {
//process this
//cout << "PROC " << i << " " << j << " " << a.s << " " << del[j] << " " << curSum << nl;
rem[it->s] -= a.s;
if (pos[it->s] <= i+a.s) {
if (pos[it->s] >= i) ans[it->s] = P[2*i+a.s-pos[it->s]];
} else {
toAdd[i+a.s+1][rem[it->s]].pb({it->f-curSum-del[j], it->s});
nc[it->s] -= curSum + del[j];
}
cur[j].erase(it);
it = cur[j].ub({del[j] + curSum, -1});
}
curSum += numWith;
}
/* if (i == 2 && j == 3) {
cout << a.s << " " << del[j] << " " << curSum << nl;
}*/
}
trav(a, atPos[i]) {
ll num = 1; if (i < N-1) num = ch[N-2-i][j];
/* if (i == 2 && j == 3 && a == 0) {
cout << nc[a] << " " << cur[j].count({nc[a], a}) << " " << num << " " << del[j] << endl;
}*/
if (cur[j].count({nc[a], a}) && num > nc[a]-del[j]) {
ans[a] = P[i];
}
}
}
//deal w/ things to be removed here [uhh what does this mean lol]
}
F0R(i, Q) {
cout << ans[i] << nl;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
ch[0][0] = 1;
FOR(i, 1, 5) ch[0][i] = 0;
FOR(i, 1, MX) {
ch[i][0] = 1;
FOR(j, 1, 5) {
ch[i][j] = ch[i-1][j-1] + ch[i-1][j];
}
}
F0R(i, MX) {
FOR(j, 1, 5) {
ch[i][j] += ch[i][j-1];
}
}
int T = 1;
cin >> T;
while(T--) {
solve();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for ( int i = (a), _end_ = (b); i <= _end_; ++ i )
#define mem(a) memset ( (a), 0, sizeof ( a ) )
#define str(a) strlen ( a )
#define pii pair<int, int>
#define int long long
typedef long long LL;
const int maxn = 3e5 + 10;
const int INF = 1e18;
int T, n, c, q, a[maxn], f[maxn][5], g[maxn][5][5], h[maxn][5][5], dp[maxn][5], C[maxn][5];
vector<pii> Ans;
inline int Find(int pos, int now, int y)
{
int l = pos, r = n, ans = n + 1;
while ( l <= r )
{
int Mid = l + r >> 1;
if ( y <= dp[Mid][now] - dp[pos - 1][now] || y > dp[Mid][now] - dp[pos - 1][now] + h[Mid][now][0] )
{
ans = Mid; r = Mid - 1;
}
else l = Mid + 1;
}
return ans;
}
signed main()
{
scanf("%lld", &T); n = maxn - 10;
C[0][0] = 1; REP(i, 1, n) { C[i][0] = 1; REP(j, 1, 4) C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; }
REP(i, 1, n) REP(j, 0, 4) REP(k, 0, j) f[i][j] += C[i - 1][k]; REP(i, 0, 4) f[0][i] = 1;
while ( T -- )
{
scanf("%lld%lld%lld", &n, &c, &q);
REP(i, 1, n) scanf("%lld", &a[i]);
REP(i, 1, n) REP(now, 0, c)
{
vector<pii> p; p.clear(); REP(j, i, min(n, i + now)) p.push_back(pii(a[j], j));
sort(p.begin(), p.end()); int sum = 0, k = 0;
for ( auto it : p )
{
int j = it.second; g[i][now][k ++] = j; h[i][now][j - i] = f[n - j][now - j + i];
if ( j > i ) sum += f[n - j][now - j + i];
else dp[i][now] = dp[i - 1][now] + sum;
}
}
while ( q -- )
{
int x, y, now = c; scanf("%lld%lld", &x, &y);
if ( y > f[n][c] ) { puts("-1"); continue ; }
int pos = 1, nxt; Ans.clear();
while ( pos <= n )
{
nxt = Find(pos, now, y); if ( nxt == n + 1 ) break ;
y -= dp[nxt - 1][now] - dp[pos - 1][now];
assert(y>=0);
REP(i, 0, c)
{
int t = g[nxt][now][i];
if ( y <= h[nxt][now][t - nxt] )
{
Ans.push_back(pii(nxt, t));
now -= t - nxt; pos = t + 1; break ;
}
y -= h[nxt][now][t - nxt];
}
}
for ( auto it : Ans ) if ( it.first <= x && x <= it.second )
{
printf("%lld\n", a[it.second - x + it.first]);
goto Finish;
}
printf("%lld\n", a[x]);
Finish : ;
}
}
return 0;
} |
#include <bits/stdc++.h>
using std::sort; using std::vector; using std::min; using std::lower_bound;
typedef long long LL;
const int N = 30005, SZ = N * 5;
int T, n, c, Q, a[N]; LL w[N][5];
LL C(int n, int m) {
LL res = 1;
for (int i = n; i > n - m; i--)
res = res * i / (n - i + 1);
return res;
}
struct Node { int l, r; LL w; };
struct Queue {
Node a[SZ]; int l, r;
void clr() { l = r = 0; }
void B(Node x) { a[r++] = x; r %= SZ; }
void F(Node x) { if (!l) l = SZ; a[--l] = x; }
} q[5];
int ql[N][5], qr[N][5];
vector<LL> val[5];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &n, &c, &Q);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= c; i++)
val[i].clear(), q[i].clr(), q[i].B({0, 0, 1});
for (int i = 0; i <= n; i++) {
w[i][0] = 1;
for (int j = 1; j <= c; j++)
w[i][j] = w[i][j - 1] + (i ? C(i - 1, j) : 0);
}
for (int i = n; i; i--) {
int m = min(c, n - i), b[m + 1];
for (int j = 1; j <= m; j++) b[j] = j;
sort(b + 1, b + m + 1, [&](int x, int y) { return a[i + x] < a[i + y]; });
for (int j = m; j; j--)
if (a[i + b[j]] < a[i])
for (int k = b[j]; k <= c; k++)
q[k].F({i, i + b[j], w[n - (i + b[j])][k - b[j]]});
for (int j = 1; j <= m; j++)
if (a[i + b[j]] > a[i])
for (int k = b[j]; k <= c; k++)
q[k].B({i, i + b[j], w[n - (i + b[j])][k - b[j]]});
for (int j = 1; j <= c; j++)
ql[i][j] = q[j].l, qr[i][j] = q[j].r;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= c; j++)
ql[i][j] = (ql[i][j] - q[j].l + SZ) % SZ, qr[i][j] = (qr[i][j] - q[j].l + SZ) % SZ;
for (int i = 1; i <= c; i++) {
for (int j = q[i].l; j != q[i].r; j = (j + 1) % SZ)
val[i].push_back((val[i].size() ? val[i][val[i].size() - 1] : 0) + q[i].a[j].w);
}
while (Q--) {
int x; LL y; scanf("%d%lld", &x, &y);
if (w[n][c] < y) { puts("-1"); continue; }
int i = c, now = 1;
for (;;) {
if (!i) { printf("%d\n", a[x]); break; }
LL y0 = ql[now][i] ? val[i][ql[now][i] - 1] : 0;
int pos = lower_bound(val[i].begin() + ql[now][i], val[i].begin() + qr[now][i], y + y0) - val[i].begin();
Node tmp = q[i].a[(q[i].l + pos) % SZ];
if (!tmp.l || x <= tmp.r) {
printf("%d\n", !tmp.l || x < tmp.l ? a[x] : a[tmp.r - x + tmp.l]);
break;
}
y -= val[i][pos] - y0 - w[n - tmp.r][i - (tmp.r - tmp.l)], i -= tmp.r - tmp.l, now = tmp.r + 1;
}
}
}
return 0;
} |
/*
Problem :
Algorithm :
Status :
*/
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#define DEBUG cerr << "Passing Line " << __LINE__<< " in Function [" << __FUNCTION__ << "].\n";
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
template<class T> inline bool checkMax(T &a,const T &b) {return a < b ? a = b,1 : 0;}
template<typename T, typename...Args> inline void checkMax(T &a,const Args...arg) {checkMax(a,max(arg...));}
template<class T> inline bool checkMin(T &a,const T &b) {return a > b ? a = b,1 : 0;}
template<typename T, typename...Args> inline void checkMin(T &a,const Args...arg) {checkMin(a,min(arg...));}
namespace FastIO{
const int SIZE = 1 << 21;
char ch;
char in[SIZE],*inS = in,*inT = in + SIZE;
char out[SIZE],*outS = out,*outT = out + SIZE;
#define flush() fwrite(out,1,outS - out,stdout), outS = out
#define getchar() (inS == inT && (inT = (inS = in) + fread(in,1,SIZE,stdin),inS == inT) ? EOF : *inS++)
void putchar(char c) {*outS++ = c; if(outS == outT) flush();}
struct Flusher {~Flusher() {flush();}} flusher;
template<typename T> inline void read(T &x){
static int f; x = 0; f = 1; ch = getchar();
while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
while(isdigit(ch)) {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} x *= f;
}
inline void read(char* s){
static int len; len = 0; ch = getchar();
while(ch == '\n' || ch == ' ' || ch == '\0') ch = getchar();
while(ch != '\n' && ch != ' ' && ch != EOF) s[len++] = ch, ch = getchar(); s[len] = '\0';
}
template<typename T> inline void write(T x){
if(!x) putchar('0');
if(x < 0) {x = -x; putchar('-');}
static int sta[20]; int tot = 0;
while(x) {sta[tot++] = x % 10; x /= 10;}
while(tot) putchar(sta[--tot] + 48);
}
inline void write(char c) {putchar(c);}
template<typename T> inline void writeln(T c) {write(c); putchar('\n');}
template<typename T,typename... Args> inline void read(T &t, Args &...args) {read(t); read(args...);}
}
using FastIO::read;
using FastIO::write;
using FastIO::writeln;
const int INF = 0x3f3f3f3f;
const ll llINF = 1e18;
const int MAXN = 6e4 + 5;
const int MAXC = 5;
int n,C,Q,T;
int cnt[MAXC][MAXN],p[MAXN];
ll comb[MAXN][MAXC],w[MAXC][MAXN * MAXC];
struct Node{
int l,r; ll w;
Node(int _l = 0,int _r = 0,ll _w = 0) : l(_l), r(_r), w(_w) {}
};
struct Deque{
int head,tail;
Node a[MAXN * MAXC * 2 + 100];
Node& operator [] (const int &i) {return a[head + i - 1];}
void push_front(Node u) {a[--head] = u; assert(head >= 1);}
void push_back(Node u) {a[++tail] = u; assert(tail < MAXN * MAXC * 2 + 100);}
void clear() {head = MAXN * MAXC + 5, tail = MAXN * MAXC + 4;}
int size() {return tail - head + 1;}
Deque() {clear();}
} q[MAXC];
void Init(int n){
comb[0][0] = 1;
for(int i = 1;i <= n;i++){
comb[i][0] = 1;
for(int j = 1;j <= min(MAXC - 1,i);j++)
comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
}
for(int i = 0;i <= n;i++){
for(int j = 1;j < MAXC;j++) comb[i][j] += comb[i][j - 1];
}
}
ll calc(int n,int c){
if(n <= 0) return 1;
assert(n < MAXN);
return comb[n - 1][c];
}
bool cmp(const Node &x,const Node &y){
return p[x.r] < p[y.r];
}
Node GetNxt(int c,int pos,ll rk){
int num = cnt[c][pos];
int l = num + 1, r = q[c].size(), res = 0;
while(l <= r){
int mid = (l + r) >> 1;
if(w[c][mid] - w[c][num] >= rk) res = mid, r = mid - 1;
else l = mid + 1;
}
assert(res >= 1 && res <= q[c].size());
return Node(q[c][res].l,q[c][res].r,w[c][res - 1] - w[c][num]);
}
void Solve(){
read(n,C,Q);
for(int i = 1;i <= n;i++) read(p[i]);
for(int c = 1;c <= C;c++){
q[c].clear(); vector<Node> front,back;
for(int i = 1;i <= n;i++) cnt[c][i] = 0;
q[c].push_back(Node(n,n,1));
for(int i = n - 1;i >= 1;i--){
front.clear(); back.clear();
for(int j = 1;j <= min(c,n - i);j++){
if(p[i + j] < p[i])
front.emplace_back(i,i + j,calc(n - i - j,c - j)), cnt[c][i + 1] += 1;
else back.emplace_back(i,i + j,calc(n - i - j,c - j));
}
sort(front.begin(),front.end(),cmp); reverse(front.begin(),front.end());
sort(back.begin(),back.end(),cmp);
for(auto x : front) q[c].push_front(x);
for(auto x : back) q[c].push_back(x);
}
for(int i = 1;i <= n;i++) cnt[c][i] += cnt[c][i - 1];
assert(q[c].size() <= MAXN * MAXC + 5);
for(int i = 1, sz = q[c].size();i <= sz;i++)
w[c][i] = w[c][i - 1] + q[c][i].w;
}
while(Q--){
int pos; ll rk; read(pos,rk);
if(rk > calc(n,C)) {writeln(-1); continue;}
int x = 1, c = C; vector<pii> vec; vec.clear();
while(c && x <= n){
Node res = GetNxt(c,x,rk);
assert(res.w < rk);
rk -= res.w; c -= (res.r - res.l); x = res.r + 1;
vec.emplace_back(res.l,res.r);
}
int flag = 0;
for(auto pi : vec){
if(pos >= pi.first && pos <= pi.second){
writeln(p[pi.second - pos + pi.first]);
flag = 1; break;
}
}
if(!flag) writeln(p[pos]);
}
}
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
Init(MAXN - 1); read(T); while(T--) Solve();
return 0;
}
|
#include<bits/stdc++.h>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
const int maxn=30005;
const int L=100;
ll cnt[maxn][5],l[maxn][5],r[maxn][5],sl[maxn][5],sr[maxn][5];
int _,n,p[maxn],m,q;
void solve(){
scanf("%d%d%d",&n,&m,&q);
for (int i=1;i<=n;i++) scanf("%d",&p[i]);
for (int j=0;j<=m;j++) cnt[n+1][j]=1;
for (int i=n;i;i--){
for (int j=0;j<=m;j++){
cnt[i][j]=cnt[i+1][j];
for (int k=1;k<=j;k++)
if (i+k<=n) cnt[i][j]+=cnt[i+k+1][j-k];
}
}
for (int i=1;i<=n;i++)
for (int j=0;j<=m;j++){
ll x=0,y=0;
for (int w=1;w<=j&&i+w<=n;w++){
if (p[i+w]<p[i]) x+=cnt[i+w+1][j-w];
else y+=cnt[i+w+1][j-w];
}
l[i][j]=x;
r[i][j]=y;
sl[i][j]=sl[i-1][j]+x;
sr[i][j]=sr[i-1][j]+y;
}
while (q--){
ll t; int pos,now,cost;
scanf("%d%lld",&pos,&t);
if (t>cnt[1][m]){puts("-1");continue;}
now=1; cost=m;
while (now<=pos){
if (t>l[now][cost]&&t<=cnt[now][cost]-r[now][cost]){
int L=now,R=n+1;
while (R-L>1){
int mid=(L+R)>>1;
ll u=sl[mid][cost]-sl[now-1][cost];
ll v=sr[mid][cost]-sr[now-1][cost];
if (t>u&&t<=cnt[now][cost]-v) L=mid; else R=mid;
}
t-=sl[L][cost]-sl[now-1][cost];now=R;
if (now>pos) printf("%d\n",p[pos]);
} else {
vector<pi> A; A.clear();
for (int i=0;i<=cost&&i+now<=n;i++)
A.pb((pi){p[i+now],i});
sort(A.begin(),A.end());
for (auto x:A){
ll cc=cnt[x.S+now+1][cost-x.S];
if (t<=cc) {
if (pos>=now&&pos<=now+x.S){
printf("%d\n",p[now+now+x.S-pos]);
}
now+=x.S+1;
cost-=x.S;
break;
}
t-=cc;
}
}
}
}
}
int main(){
scanf("%d",&_);
while (_--) solve();
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mid ((l+r)>>1)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define fors(i, a, b) for(int i = (a); i < (b); ++i)
using namespace std;
const int maxn = 3e4 + 5;
ll dp[maxn][5];
int p[maxn];
int n, c, q,idx;
int cmp(int x, int y){return p[idx+x] < p[idx+y];}
vector<int> s[maxn];
ll L[maxn][5], R[maxn][5];
ll SL[maxn][5];
void init(){
scanf("%d%d%d", &n, &c, &q);
fors(i,1,n+1) scanf("%d", &p[i]);
p[0] = -1;
fors(i,0,c+1) dp[n+1][i] = 1;
for(int i = n; i > 0; --i){
s[i].clear();
for(int j = 0; j <= c; ++j){
if(i+j <= n) s[i].pb(j);
dp[i][j] = 0;
for(int k = 0; k <= j && i+k <= n; ++k) dp[i][j] += dp[i+k+1][j-k];
}idx = i;
sort(s[i].begin(), s[i].end(),cmp);
}
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= c; ++j){
ll sum = 0;
for(int x: s[i]){
if(x > j) continue;
if(x == 0) break;
sum += dp[i+x+1][j-x];
}
L[i][j] = sum;
R[i][j] = sum + dp[i+1][j];
}
}
fors(i,1,n+1) fors(j,0,c+1) SL[i][j] = SL[i-1][j] + L[i][j];
}
int work(int pos, ll rk){
if(rk > dp[1][c]) return 0;
int cur = c;
for(int i = 1; i <= n; ++i){
if(i > pos) return pos;
if(rk > L[i][cur] && rk <= R[i][cur]){
int l = i, r = n;
int res = -1;
ll del = SL[i-1][cur];
while(l <= r){
if(SL[mid][cur]-del >= rk || SL[mid-1][cur]-del+R[mid][cur] < rk){
r = mid-1;
}else{
res = mid; l = mid+1;
}
}
rk -= SL[res][cur]-del;
i = res;
}else{
for(int x:s[i]){
if(x > cur) continue;
if(rk > dp[i+x+1][cur-x]) rk -= dp[i+x+1][cur-x];
else{
cur -= x;
if(pos <= i+x && pos >= i){
pos = i+x+i-pos;
}
i += x;
break;
}
}
}
}
return pos;
}
void sol(){
while(q--){
int pos; ll rk;
scanf("%d%lld", &pos, &rk);
int cur = c;
printf("%d\n", p[work(pos, rk)]);
}
}
int main()
{
int T;cin>>T;
while(T--){
init();
sol();
}
return 0;
}
/*
1
6 4 4
6 5 4 3 1 2
1 1
3 14
1 59
2 6
*/
|
// problem: CF1470E
#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template<typename T> inline void ckmax(T& x, T y) { x = (y > x ? y : x); }
template<typename T> inline void ckmin(T& x, T y) { x = (y < x ? y : x); }
/* --------------- fast io --------------- */ // begin
namespace Fread {
const int SIZE = 1 << 21;
char buf[SIZE], *S, *T;
inline char getchar() {
if (S == T) {
T = (S = buf) + fread(buf, 1, SIZE, stdin);
if (S == T) return '\n';
}
return *S++;
}
} // namespace Fread
namespace Fwrite {
const int SIZE = 1 << 21;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() {
fwrite(buf, 1, S - buf, stdout);
S = buf;
}
inline void putchar(char c) {
*S++ = c;
if (S == T) flush();
}
struct NTR {
~ NTR() { flush(); }
} ztr;
} // namespace Fwrite
#ifdef ONLINE_JUDGE
#define getchar Fread :: getchar
#define putchar Fwrite :: putchar
#endif
namespace Fastio {
struct Reader {
template<typename T>
Reader& operator >> (T& x) {
char c = getchar();
T f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = getchar();
}
x *= f;
return *this;
}
Reader& operator >> (char& c) {
c = getchar();
while (c == '\n' || c == ' ') c = getchar();
return *this;
}
Reader& operator >> (char* str) {
int len = 0;
char c = getchar();
while (c == '\n' || c == ' ') c = getchar();
while (c != '\n' && c != ' ') {
str[len++] = c;
c = getchar();
}
str[len] = '\0';
return *this;
}
Reader(){}
} cin;
const char endl = '\n';
struct Writer {
template<typename T>
Writer& operator << (T x) {
if (x == 0) { putchar('0'); return *this; }
if (x < 0) { putchar('-'); x = -x; }
static int sta[45];
int top = 0;
while (x) { sta[++top] = x % 10; x /= 10; }
while (top) { putchar(sta[top] + '0'); --top; }
return *this;
}
Writer& operator << (char c) {
putchar(c);
return *this;
}
Writer& operator << (char* str) {
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer& operator << (const char* str) {
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer(){}
} cout;
} // namespace Fastio
#define cin Fastio :: cin
#define cout Fastio :: cout
#define endl Fastio :: endl
/* --------------- fast io --------------- */ // end
const int MAXC = 4;
const int MAXN = 3e4;
int n, c, q, a[MAXN + 5];
ll ways_eq(int len, int c) {
// 长度为 len, 操作代价和恰好为 c
// comb(len - 1, c)
if (len <= 1) {
return c == 0;
}
if (len - 1 < c) {
return 0;
}
ll res = 1;
for (int i = len - 1; i >= len - c; --i) {
res *= i;
}
for (int i = c; i > 1; --i) {
res /= i;
}
return res;
}
ll ways_leq(int len, int c) {
// 长度为 len, 操作代价和小于或等于 c
ll res = 0;
for (int i = 0; i <= c; ++i) {
res += ways_eq(len, i);
}
return res;
}
struct Node {
int l, r;
ll w; // 后面的操作方案数
Node() {}
Node(int _l, int _r, ll _w) {
l = _l;
r = _r;
w = _w;
}
};
Node dq[MAXC + 1][MAXN * MAXC * 2 + 10];
int ql[MAXC + 1], qr[MAXC + 1];
int sum[MAXC + 1][MAXN + 5];
ll sumw[MAXC + 1][MAXN * MAXC + 5];
bool cmp(Node lhs, Node rhs) {
return a[lhs.r] < a[rhs.r];
}
Node F(int st, int s, ll rank) {
int i = sum[s][st]; // 在 st 之前的, 一共有这么多区间
/*
// 暴力查找
ll cur = 0;
for (i = ql[s] + i; i <= qr[s]; ++i) {
cur += dq[s][i].w;
assert(cur == sumw[s][i - ql[s] + 1] - sumw[s][sum[s][st]]);
if (cur >= rank) {
return Node(dq[s][i].l, dq[s][i].r, cur - dq[s][i].w);
}
}
*/
int l = i + 1, r = qr[s] - ql[s] + 2;
while (l < r) {
int mid = (l + r) >> 1;
if (sumw[s][mid] - sumw[s][i] >= rank) {
r = mid;
} else {
l = mid + 1;
}
}
assert(l <= qr[s] - ql[s] + 1);
return Node(dq[s][ql[s] + l - 1].l, dq[s][ql[s] + l - 1].r, sumw[s][l - 1] - sumw[s][i]);
}
void solve_case() {
cin >> n >> c >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int s = 1; s <= c; ++s) { // 总代价小于或等于 s
// cerr << "-------- maxcost " << s << " --------" << endl;
ql[s] = MAXN * MAXC + 5, qr[s] = MAXN * MAXC + 4; // 队列清空
for (int i = 1; i <= n; ++i)
sum[s][i] = 0;
dq[s][++qr[s]] = Node(n, n, 1); // 什么都不翻转
for (int i = n - 1; i >= 1; --i) {
int dl = 0, dr = 0;
for (int j = 1; j <= min(s, n - i); ++j) {
// 翻转区间 [i, i + j]
ll w = ways_leq(n - (i + j), s - j);
if (a[i + j] < a[i]) {
// 翻转后是 a[i + j], 不翻转是 a[i], 两者比一比
// 翻转后更小, push_front
dq[s][ql[s] - (++dl)] = Node(i, i + j, w);
sum[s][i + 1]++;
} else {
dq[s][qr[s] + (++dr)] = Node(i, i + j, w);
}
}
if (dl) {
sort(dq[s] + ql[s] - dl, dq[s] + ql[s], cmp);
ql[s] -= dl;
}
if (dr) {
sort(dq[s] + qr[s] + 1, dq[s] + qr[s] + dr + 1, cmp);
qr[s] += dr;
}
}
// cerr << "print queue: " << endl;
// for (int i = ql[s]; i <= qr[s]; ++i) {
// cerr << dq[s][i].l << " " << dq[s][i].r << " " << dq[s][i].w << endl;
// }
// cerr << "queue end" << endl;
for (int i = 1; i <= n; ++i) {
sum[s][i] += sum[s][i - 1];
}
for (int i = ql[s]; i <= qr[s]; ++i) {
sumw[s][i - ql[s] + 1] = sumw[s][i - ql[s]] + dq[s][i].w;
}
}
ll lim = ways_leq(n, c);
for (int tq = 1; tq <= q; ++tq) {
int pos;
ll rank;
// cerr << "-------- query: " << endl;
cin >> pos >> rank;
if (rank > lim) {
cout << -1 << endl;
continue;
}
vector<pii> revs;
int p = 1;
int s = c;
while (1) {
Node t = F(p, s, rank);
// cerr << "** " << t.l << " " << t.r << " " << t.w << endl;
revs.push_back(make_pair(t.l, t.r));
assert(t.w < rank);
rank -= t.w;
s -= (t.r - t.l);
p = t.r + 1;
if (!s)
break;
if (p > n)
break;
}
/*
// 暴力翻转
static int aa[MAXN + 5];
for (int i = 1; i <= n; ++i) {
aa[i] = a[i];
}
for (int i = 0; i < SZ(revs); ++i) {
reverse(aa + revs[i].fi, aa + revs[i].se + 1);
}
cout << aa[pos] << endl;
*/
bool flag = 0;
for (int i = 0; i < SZ(revs); ++i) {
if (revs[i].fi <= pos && revs[i].se >= pos) {
cout << a[revs[i].se - (pos - revs[i].fi)] << endl;
flag = 1;
break;
}
}
if (!flag) {
cout << a[pos] << endl;
}
}
}
int main() {
int T; cin >> T; while (T--) {
solve_case();
}
return 0;
} |
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD 1000000007//998244353//1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef ld ValType;
template<typename T> void maxtt(T& t1, T t2) {
t1=max(t1,t2);
}
template<typename T> void mintt(T& t1, T t2) {
t1=min(t1,t2);
}
#define MAX (30135)
bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
const ll MOD2 = (ll)MOD * (ll)MOD;
ll ln, lk, lm;
void etp(bool f = 0) {
puts(f ?"YES" : "NO");
exit(0);
}
void addmod(int &x, int y, int mod = MOD){
x+=y; if (x>=mod) x-=mod;
if(x<0) x+=mod;
assert(x>=0 && x<mod);
}
void et(int x=-1) {
printf("%d\n", x); exit(0);
}
ll fastPow(ll x, ll y, int mod=MOD) {
ll ans = 1;
while(y>0) {
if(y&1) ans = (x * ans)%mod;
x = x*x%mod;
y>>=1;
}
return ans;
}
ll gcd1(ll x, ll y) {
return y?gcd1(y,x%y):x;
}
struct mb {
int sz=0;
pii A[5];
}G[5][MAX];
ll fa[5][MAX];
ll I = 1e18+5;
tuple<int,int,ll> lg[5][15][MAX];
void fmain(int tid) {
int c,q;
scanf("%d%d%d", &n, &c, &q);
vector<int> P(n);
REP(i,n) {
scanf("%d", &P[i]); P[i]--;
}
REP(i,c+1) REP(j,n) G[i][j].sz=0;
REP(i,c+1) REP(j,n) {
for(int k=0;k<=i;k++) {
if(j+k+1>n) break;
G[i][j].A[G[i][j].sz++]={i-k, j+k+1};
}
sort(G[i][j].A, G[i][j].A+G[i][j].sz, [&](pii a, pii b) {
return P[a.second-1] < P[b.second-1];
});
}
REP(i,c+1) fa[i][n]=1;
REP(i,c+1) for(int j=n-1;j>=0;j--) {
fa[i][j]=0;
REP(z, G[i][j].sz) {
int a,x; tie(a,x)=G[i][j].A[z];
fa[i][j]+=fa[a][x]; mintt(fa[i][j], I);
}
}
REP(i,c+1) {
REP(k,15) lg[i][k][n]={-1,-1,I};
}
REP(i,c+1) REP(j,n) {
ll cur=0;
REP(z, G[i][j].sz) {
int a,x; tie(a,x)=G[i][j].A[z];
if (a==i) {
lg[i][0][j]={a,x,cur}; break;
}
cur += fa[a][x]; mintt(cur, I);
}
}
REQ(k,14) REP(i,c+1) REP(j,n) if(lg[i][k-1][j] != make_tuple(-1,-1,I)){
int a,x; ll z; tie(a,x,z)=lg[i][k-1][j];
int b,y; ll w; tie(b,y,w)=lg[a][k-1][x];
lg[i][k][j] = {b,y,min(z+w,I)}; assert(a==i);
} else lg[i][k][j] = {-1,-1,I};
REQ(tt,q) {
int i; ll j; scanf("%d%lld", &i,&j);
i--;j--;
int a=c, x=0;
if (j>=fa[a][x]) {
puts("-1"); continue;
}
vector<pii> vp;
while (1) {
for(int k=14;k>=0;k--) {
int b,y; ll w; tie(b,y,w)=lg[a][k][x];
if (w <=j && j-w < fa[b][y]) {
a=b;x=y; j-=w;
}
}
if (x==n) break;
REP(z, G[a][x].sz) {
int b,y; tie(b,y)=G[a][x].A[z];
if (fa[b][y] <= j) {
j-=fa[b][y];
} else {
vp.push_back({x,y});
a=b;x=y;
break;
}
}
}
for(auto &p : vp) reverse(begin(P)+p.first, begin(P)+p.second);
printf("%d\n", P[i]+1);
for(auto &p : vp) reverse(begin(P)+p.first, begin(P)+p.second);
}
}
int main() {
int t=1;
// init();
scanf("%d", &t);
// init();
REQ(i,t) {
fmain(i);
}
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;
const int N = 30007;
const int M = 5;
struct Transition {
int primary;
int to;
int coins;
};
bool cmp(Transition &x, Transition &y) {
return (x.primary < y.primary);
}
vector<Transition> trans[N][M];
ll cnt[N][M];
const int LG = 17;
pair<ll, ll> what_state[N][M][LG];
pair<ll, ll> edges[N][M][LG];
void solve() {
int n, c, q;
cin >> n >> c >> q;
vector<int> v(n);
for (auto &x : v) {
cin >> x;
x--;
}
for (int i = n; i >= 0; --i) {
for (int j = c; j >= 0; --j) {
trans[i][j].clear();
for (int take = 1; take + i <= n && take - 1 <= j; ++take) {
int P = v[i + take - 1];
trans[i][j].pb({P, i + take, j - (take - 1)});
}
sort(all(trans[i][j]), cmp);
if (!trans[i][j].size()) {
cnt[i][j] = 1;
for (int e = 0; e < LG; ++e) {
what_state[i][j][e] = mp(-1, -1);
edges[i][j][e] = mp(-1, -1);
}
}
else {
for (int e = 0; e < LG; ++e) {
what_state[i][j][e] = mp(-1, -1);
edges[i][j][e] = mp(-1, -1);
}
cnt[i][j] = 0;
for (auto p : trans[i][j]) {
cnt[i][j] += cnt[p.to][p.coins];
}
ll mx = -1;
int index = -1;
int u = 0;
for (auto p : trans[i][j]) {
if (cnt[p.to][p.coins] > mx) {
mx = cnt[p.to][p.coins];
index = u;
}
u++;
}
ll lsum = 0, rsum = 0;
for (int e = 0; e < index; ++e) {
int A = trans[i][j][e].to, B = trans[i][j][e].coins;
lsum += cnt[A][B];
}
for (int e = index + 1; e < trans[i][j].size(); ++e) {
int A = trans[i][j][e].to, B = trans[i][j][e].coins;
rsum += cnt[A][B];
}
what_state[i][j][0] = mp(trans[i][j][index].to, trans[i][j][index].coins);
edges[i][j][0] = mp(lsum, rsum);
for (int e = 1; e < LG; ++e) {
int A = what_state[i][j][e-1].x, B = what_state[i][j][e-1].y;
if (what_state[A][B][e-1] == mp((ll) -1, (ll) -1)) break;
what_state[i][j][e] = what_state[A][B][e-1];
edges[i][j][e] = mp(edges[i][j][e-1].x + edges[A][B][e-1].x, edges[i][j][e-1].y + edges[A][B][e-1].y);
}
}
}
}
for (int i = 0; i < q; ++i) {
int number;
ll need;
cin >> number >> need;
need--;
number--;
if (need >= cnt[0][c]) {
cout << "-1\n";
continue;
}
int pos = 0, rem = c;
bool decided = false;
while (!decided) {
//cout << pos << " " << rem << endl;
ll L = need, R = cnt[pos][rem] - need - 1;
ll ltaken = 0, rtaken = 0;
for (int i = LG-1; i >= 0; --i) {
if (what_state[pos][rem][i] == mp((ll) -1, (ll) -1)) continue;
int A = what_state[pos][rem][i].x, B = what_state[pos][rem][i].y;
//cout << i << " " << pos << " " << rem << " " << A << " " << B << endl;
if (A > number) continue;
ll C = edges[pos][rem][i].x, D = edges[pos][rem][i].y;
if (ltaken + C > L || rtaken + D > R) continue;
pos = A, rem = B, ltaken += C, rtaken += D;
}
need -= ltaken;
//cout << pos << " " << rem << " " << need << endl;
for (int u = 0; u < trans[pos][rem].size(); u++) {
int A = trans[pos][rem][u].to, B = trans[pos][rem][u].coins;
//cout << " === " << cnt[A][B] << " " << A << " " << B << " " << number << endl;
if (cnt[A][B] <= need) {
need -= cnt[A][B];
continue;
}
else {
if (A > number) {
vector<int> Q;
for (int f = pos; f < A; ++f) Q.pb(v[f]);
reverse(all(Q));
int steps = number - pos;
cout << Q[steps] + 1 << '\n';
decided = true;
}
else {
pos = A, rem = B;
}
break;
}
}
}
}
}
int main(){
#ifdef LOCAL
freopen("E_input.txt", "r", stdin);
//freopen("E_output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for (int i = 0; i < t; ++i) solve();
}
|
/***************************************************************
File name: E.cpp
Author: ljfcnyali
Create time: 2021年01月06日 星期三 11时34分15秒
***************************************************************/
#include<bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for ( int i = (a), _end_ = (b); i <= _end_; ++ i )
#define mem(a) memset ( (a), 0, sizeof ( a ) )
#define str(a) strlen ( a )
#define pii pair<int, int>
#define int long long
typedef long long LL;
const int maxn = 3e5 + 10;
const int INF = 1e18;
int T, n, c, q, a[maxn], f[maxn][5], g[maxn][5][5], h[maxn][5][5], dp[maxn][5], C[maxn][5];
vector<pii> Ans;
inline int Find(int pos, int now, int y)
{
int l = pos, r = n, ans = n + 1;
while ( l <= r )
{
int Mid = l + r >> 1;
if ( y <= dp[Mid][now] - dp[pos - 1][now] || y > dp[Mid][now] - dp[pos - 1][now] + h[Mid][now][0] )
{
ans = Mid; r = Mid - 1;
}
else l = Mid + 1;
}
return ans;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
scanf("%lld", &T); n = maxn - 10;
C[0][0] = 1; REP(i, 1, n) { C[i][0] = 1; REP(j, 1, 4) C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; }
REP(i, 1, n) REP(j, 0, 4) REP(k, 0, j) f[i][j] += C[i - 1][k]; REP(i, 0, 4) f[0][i] = 1;
while ( T -- )
{
scanf("%lld%lld%lld", &n, &c, &q);
REP(i, 1, n) scanf("%lld", &a[i]);
REP(i, 1, n) REP(now, 0, c)
{
vector<pii> p; p.clear(); REP(j, i, min(n, i + now)) p.push_back(pii(a[j], j));
sort(p.begin(), p.end()); int sum = 0, k = 0;
for ( auto it : p )
{
int j = it.second; g[i][now][k ++] = j; h[i][now][j - i] = f[n - j][now - j + i];
if ( j > i ) sum += f[n - j][now - j + i];
else dp[i][now] = dp[i - 1][now] + sum;
}
}
while ( q -- )
{
int x, y, now = c; scanf("%lld%lld", &x, &y);
if ( y > f[n][c] ) { puts("-1"); continue ; }
int pos = 1, nxt; Ans.clear();
while ( pos <= n )
{
nxt = Find(pos, now, y); if ( nxt == n + 1 ) break ;
y -= dp[nxt - 1][now] - dp[pos - 1][now];
assert(y>=0);
REP(i, 0, c)
{
int t = g[nxt][now][i];
if ( y <= h[nxt][now][t - nxt] )
{
Ans.push_back(pii(nxt, t));
now -= t - nxt; pos = t + 1; break ;
}
y -= h[nxt][now][t - nxt];
}
}
for ( auto it : Ans ) if ( it.first <= x && x <= it.second )
{
printf("%lld\n", a[it.second - x + it.first]);
goto Finish;
}
printf("%lld\n", a[x]);
Finish : ;
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 300005;
int n, c, q;
int p[N];
int x[N];
vector<pair<int, int> > ans[N];
vector<pair<ll, int> > v[5];
vector<int> vv[5];
ll ways(int n, int c)
{
if (n == 0)
return 1;
ll ans = 0;
ll yans = 1;
ans += yans;
for (int i = n - 1; i >= n - c; --i)
{
yans *= i;
yans /= ((n - 1) - i + 1);
ans += yans;
}
return ans;
}
ll pp[N];
void solv()
{
cin >> n >> c >> q;
for (int i = 1; i <= n; ++i)
ans[i].clear();
for (int i = 0; i <= c; ++i)
{
v[i].clear();
vv[i].clear();
}
for (int i = 1; i <= n; ++i)
cin >> p[i];
for (int i = 1; i <= q; ++i)
{
ll y;
cin >> x[i] >> y;
if (y > ways(n, c))
ans[i].push_back(m_p(-1, -1));
else
v[c].push_back(m_p(y, i));
}
for (int k = c; k >= 0; --k)
{
if (k != c)
{
for (int i = 1; i <= n; ++i)
{
pp[i] = pp[i - 1];
for (int j = i + 1; j <= n && j - i <= k; ++j)
{
if (p[j] < p[i])
pp[i] += ways(n - j, k - (j - i));
}
}
for (int i = 0; i < v[k].size(); ++i)
v[k][i].fi += pp[vv[k][i]];
}
deque<pair<int, int> > q;
q.push_back(m_p(n, n));
for (int i = n; i >= 1; --i)
{
vector<pair<int, int> > v;
for (int j = i + 1; j <= n && j - i <= k; ++j)
{
v.push_back(m_p(p[j], j));
}
sort(all(v));
for (int j = 0; j < sz(v); ++j)
{
if (v[j].fi > p[i])
q.push_back(m_p(i, v[j].se));
}
for (int j = sz(v) - 1; j >= 0; --j)
{
if (v[j].fi < p[i])
q.push_front(m_p(i, v[j].se));
}
}
sort(all(v[k]));
ll u = 0;
int j = 0;
for (int i = 0; i < v[k].size(); ++i)
{
while (v[k][i].fi > u + ways(n - q[j].se, k - (q[j].se - q[j].fi)))
{
u += ways(n - q[j].se, k - (q[j].se - q[j].fi));
++j;
}
ans[v[k][i].se].push_back(q[j]);
if (q[j] != m_p(n, n))
{
v[k - (q[j].se - q[j].fi)].push_back(m_p(v[k][i].fi - u, v[k][i].se));
vv[k - (q[j].se - q[j].fi)].push_back(q[j].se);
}
}
}
for (int i = 1; i <= q; ++i)
{
if (!ans[i].empty() && ans[i][0].fi == -1)
{
cout << "-1\n";
continue;
}
bool z = false;
for (int j = 0; j < ans[i].size(); ++j)
{
if (ans[i][j].fi <= x[i] && x[i] <= ans[i][j].se)
{
cout << p[ans[i][j].se - (x[i] - ans[i][j].fi)] << "\n";
z = true;
break;
}
}
if (!z)
cout << p[x[i]] << "\n";
}
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
|
#include <bits/stdc++.h>
using ll = long long;
const int N = 300010;
const int M = 5;
ll dp[N][M];
int p[N];
ll low[N][M], high[N][M];
ll pre_low[N][M], pre_high[N][M];
void solve(){
int n, c, q;
scanf("%d%d%d", &n, &c, &q);
for (int i = 0; i <= n; ++ i){
memset(dp[i], 0, sizeof(dp[i]));
memset(low[i], 0, sizeof(low[i]));
memset(high[i], 0, sizeof(high[i]));
}
for (int i = 1; i <= n; ++ i){
scanf("%d", &p[i]);
}
for (int i = 0; i <= c; ++ i){
dp[0][i] = 1;
}
for (int i = 1; i <= n; ++ i){
for (int j = 0; j <= c; ++ j){
for (int k = 0; k <= j && k + 1 <= i; ++ k){
ll value = dp[i - k - 1][j - k];
dp[i][j] += value;
if (p[n + 1 - (i - k)] < p[n + 1 - i]){
low[i][j] += value;
}
else if (p[n + 1 - (i - k)] > p[n + 1 - i]){
high[i][j] += value;
}
}
}
}
for (int i = 0; i <= c; ++ i){
for (int j = 1; j <= n; ++ j){
pre_low[j][i] = pre_low[j - 1][i] + low[j][i];
pre_high[j][i] = pre_high[j - 1][i] + high[j][i];
}
}
while (q --){
int pos;
ll num;
scanf("%d%lld", &pos, &num);
if (num > dp[n][c]){
puts("-1");
continue;
}
int len = n, cur = c;
while (len >= n + 1 - pos){
int l = 0, r = len;
while (l < r){
int mid = (l + r + 1) / 2;
ll low_num = pre_low[len][cur] - pre_low[len - mid][cur];
ll high_num = pre_high[len][cur] - pre_high[len - mid][cur];
if (num <= low_num || num >= dp[len][cur] - high_num + 1){
r = mid - 1;
}
else{
l = mid;
}
}
int left = n + 1 - len, right = left + l - 1;
if (left <= pos && pos <= right){
printf("%d\n", p[pos]);
break;
}
num -= pre_low[len][cur] - pre_low[len - l][cur];
len -= l;
if (len == 0){
break;
}
using piii = std::tuple <ll, ll, ll>;
piii pp[M] = {};
int cnt = 0;
for (int i = 0; i <= cur && i + 1 <= len; ++ i){
ll value = dp[len - i - 1][cur - i];
pp[cnt ++] = {p[n + 1 - (len - i)], i, value};
}
std::sort(pp, pp + cnt);
for (int i = 0; i < cnt; ++ i){
auto [u, v, w] = pp[i];
if (num <= w){
left = n + 1 - len, right = left + v;
if (left <= pos && pos <= right){
printf("%d\n", p[right - (pos - left)]);
}
len -= v + 1;
cur -= v;
break;
}
else{
num -= w;
}
}
}
}
}
int main(){
int test;
scanf("%d", &test);
while (test --){
solve();
}
return 0;
} |
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <chrono>
#include <random>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define tisic 47
#define soclose 1e-8
#define patkan 9
#define ff first
#define ss second
using uint = unsigned int;
using cat = long long;
using dbl = long double;
constexpr dbl pi = 3.14159265358979323846;
using namespace std;
#ifdef DONLINE_JUDGE
// palindromic tree is better than splay tree!
#define lld I64d
#endif
template <typename T>
T abs(T x) { return (x < 0) ? (-x) : x; }
cat pw(cat a, cat e, cat mod) {
if(e <= 0) return 1;
cat x = pw(a, e/2, mod);
x = x * x % mod;
return (e&1) ? x * a % mod : x;
}
template <typename T>
class fin {
vector<T> node_val;
int lastone(int x) { return x & (x ^ (x-1)); }
public:
fin(int N, T init_val) {
node_val.resize(N+10, init_val);
}
void put(int pos, T val) {
for(int i = pos+1; i < (int)node_val.size(); i += lastone(i))
node_val[i] += val;
}
T get(int pos) {
T ret = 0;
for(int i = pos+1; i > 0; i -= lastone(i))
ret += node_val[i];
return ret;
}
};
cat gcd(cat a, cat b) {
if(a > b) swap(a, b);
while(a) {
cat c = b%a;
b = a;
a = c;
}
return b;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(12);
int T;
cin >> T;
while(T--) {
int N, C, Q;
cin >> N >> C >> Q;
vector<cat> cnt[5];
cnt[0].resize(N+1, 1);
cat MX = 1000000000000000010LL;
for(int k = 1; k < 5; k++) {
cnt[k].resize(N+1, 0);
for(int i = N-1; i >= 0; i--)
for(int j = i; j <= min(i+k, N-1); j++)
cnt[k][i] = min(MX, cnt[k][i] + cnt[k-(j-i)][j+1]);
}
for(int k = 0; k < 4; k++)
for(int i = 0; i <= N; i++)
cnt[k+1][i] = min(MX, cnt[k+1][i] + cnt[k][i]);
vector<int> P(N);
for(int i = 0; i < N; i++) cin >> P[i];
vector<cat> sum_lt[5];
for(int k = 0; k < 5; k++) {
sum_lt[k].resize(N+1, 0);
for(int i = 0; i < N; i++)
for(int j = i; j <= min(i+k, N-1); j++) if(P[j] < P[i])
sum_lt[k][i] = min(MX, sum_lt[k][i] + cnt[k-(j-i)][j+1]);
for(int i = N-1; i >= 0; i--) sum_lt[k][i] = min(MX, sum_lt[k][i] + sum_lt[k][i+1]);
}
vector<cat> mi[5][17];
for(int k = 0; k < 5; k++) for(int j = 0; j < 17; j++) {
mi[k][j].resize(N+1, MX);
if(j == 0)
for(int i = 0; i <= N; i++) mi[k][j][i] = cnt[k][i]-sum_lt[k][i];
else
for(int i = 0; i <= N; i++) mi[k][j][i] = min(mi[k][j-1][i], mi[k][j-1][min(N, i+(1<<(j-1)))]);
}
for(int q = 0; q < Q; q++) {
int id;
cat p_id;
cin >> id >> p_id;
if(p_id > cnt[C][0]) {
cout << "-1\n";
continue;
}
int c = C;
for(int i = 0; i < id; i++) {
int l = i, r = id+1;
cat x = p_id-sum_lt[c][i];
while(r-l > 1) {
int m = (l+r)/2;
if(sum_lt[c][m] > -x) l = m;
else r = m;
}
int m = i;
for(int k = 16; k >= 0; k--) if(m+(1<<k) <= l && mi[c][k][m+1] >= x) m += 1<<k;
if(id-1 < m) {
cout << P[id-1] << "\n";
break;
}
p_id -= sum_lt[c][i]-sum_lt[c][m];
i = m;
vector< pair<int, cat> > v;
for(int j = i; j <= min(i+c, N-1); j++) v.push_back({P[j], cnt[c-(j-i)][j+1]});
sort(begin(v), end(v));
int cur = 0;
while(v[cur].ss < p_id) {
p_id -= v[cur].ss;
cur++;
}
int d = 0;
while(P[i+d] != v[cur].ff) d++;
if(id-1 <= i+d) {
cout << P[i+d-(id-1-i)] << "\n";
break;
}
i += d;
c -= d;
}
}
}
}
// look at my code
// my code is amazing
|
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e4 + 4;
typedef long long ll;
int p[MAXN];
ll ways[10][MAXN], L[10][MAXN], R[10][MAXN];
int T, c, n, q;
int main() {
scanf("%d", &T);
for(int kase = 1; kase <= T; kase++) {
scanf("%d%d%d", &n, &c, &q);
for(int i = 1; i <= n; i++) scanf("%d", &p[i]);
for(int i = 0; i <= c; i++) {
ways[i][n + 1] = 1;
L[i][n + 1] = R[i][n + 1] = 0;
for(int j = n; j; j--) {
ways[i][j] = ways[i][j + 1], L[i][j] = L[i][j + 1], R[i][j] = R[i][j + 1];
for(int k = 1; k <= i && j + k <= n; k++) {
ways[i][j] += ways[i - k] [j + 1 + k];
if(p[j] > p[j + k]) L[i][j] += ways[i - k][j + k + 1];
else R[i][j] += ways[i - k][j + k + 1];
}
}
}
while(q--) {
int pos; ll id;
scanf("%d%lld", &pos, &id);
if(id > ways[c][1]) { printf("-1\n"); continue;}
int cur = 1, rem = c, ans;
while(cur <= n) {
int l = cur, r = n, m;
while(l <= r) {
int mid = (l + r) >> 1;
if(L[rem][cur] - L[rem][mid] < id && R[rem][cur] - R[rem][mid] <= ways[rem][cur] - id) {
l = (m = mid) + 1;
} else {
r = mid - 1;
}
}
if(m > pos) { ans = pos; break;}
id -= L[rem][cur] - L[rem][m]; cur = m;
vector<int> order;
for(int k = 0; k <= rem && cur + k <= n; k++) order.push_back(k);
sort(order.begin(), order.end(), [&](int x, int y) { return p[cur + x] < p[cur + y];});
int cost;
for(int i : order) {
if(id <= ways[rem - i][cur + i + 1]) { cost = i; break;}
else id -= ways[rem - i][cur + i + 1];
}
if(cost + cur >= pos) { ans = cur * 2 - pos + cost; break;}
rem -= cost, cur += cost + 1;
}
printf("%d\n", p[ans]);
}
}
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int mxn = 30005, mxk = 5;
int n, k, q;
int a[mxn], f[mxn][mxk][mxk];
ll dp[mxn][mxk], s[mxn][mxk];
void answer(){
cin >> n >> k >> q;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i <= k; i++) dp[n][i] = 1, a[n + i] = n + i + 1;
for(int i = n - 1; ~i; i--)
for(int j = 0; j <= k; j++){
dp[i][j] = 0;
for(int l = 0; l <= j && i + l < n; l++){
dp[i][j] += dp[i + l + 1][j - l];
}
}
for(int i = 0; i < n; i++)
for(int j = 0; j <= k; j++){
for(int l = 0; l <= j; l++) f[i][j][l] = l;
sort(f[i][j], f[i][j] + j + 1, [&](int x, int y){
return a[i + x] < a[i + y];
});
}
for(int j = 0; j <= k; j++)
for(int i = 1; i <= n; i++){
s[i][j] = s[i - 1][j];
for(int l = 0; l <= j && f[i - 1][j][l]; l++){
s[i][j] += dp[i + f[i - 1][j][l]][j - f[i - 1][j][l]];
}
}
while(q--){
ll w, z;
cin >> w >> z;
w--, z--;
if(z >= dp[0][k]){
cout << -1 << endl;
continue;
}
int x = 0, y = k, ret = 0;
while(!ret){
for(int i = 0; i <= y; i++){
int v = f[x][y][i];
if(z >= dp[x + v + 1][y - v]){
z -= dp[x + v + 1][y - v];
}else{
if(v){
if(x + v < w) x += v + 1, y -= v;
else ret = a[2 * x + v - w];
}else{
int l = ++x, r = w + 2;
while(r - l > 1){
int mid = (l + r) / 2;
if(s[mid][y] - s[x][y] <= z && s[mid][y] - s[x][y] + dp[mid][y] > z) l = mid;
else r = mid;
}
z -= s[l][y] - s[x][y], x = l;
if(x > w) ret = a[w];
}
break;
}
}
}
cout << ret << endl;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++) answer();
return 0;
} |
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<endl; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
struct Sol {
void Test() {
int n, c, q;
cin>>n>>c>>q;
VI p(n + 2);
RE (i, n) {
cin>>p[i];
}
VVI dp(n + 2, VI(c + 2));
vector<VVI> jumps(n + 2, VVI(c + 2, VI(20, 2e18)));
dp[0] = VI(c + 2, 1);
RE (i, n) {
REP (j, c + 1) {
REP (sub_c, j + 1) {
if (i - 1 - sub_c < 0) { continue; }
dp[i][j] += dp[i - 1 - sub_c][j - sub_c];
}
}
}
debug(dp[n]);
RE (i, n) {
FOR (j, 0, c) {
int omit = 0;
RE (steps, j) {
if (i + steps > n) { continue; }
if (p[i + steps] < p[i]) {
omit += dp[n - i - steps][j - steps];
}
}
jumps[i][j][0] = omit;
//debug(i, j, jumps[i][j][0]);
}
}
cerr<<endl;
RE (layer, 20) {
RE (i, n) {
int dist = 1 << (layer - 1);
if (i + dist > n) { continue; }
FOR (j, 0, c) {
jumps[i][j][layer] = jumps[i][j][layer - 1] + jumps[i + dist][j][layer - 1];
if (jumps[i][j][layer] > 1e18) {
jumps[i][j][layer] = 1e18;
}
}
}
}
RE (i, q) {
int pos, to_omit;
cin>>pos>>to_omit;
to_omit--;
if (to_omit >= dp[n][c]) {
cout<<"-1\n";
continue;
}
int cur_i = 1;
int cur_c = c;
int huh = 0;
while (1) {
int layer = 18;
while (layer >= 0) {
int nxt = cur_i + (1 << layer);
if (nxt > n) {
layer--;
continue;
}
//debug(cur_i, cur_c, layer, jumps[cur_i][cur_c][layer]);
if (jumps[cur_i][cur_c][layer] <= to_omit && dp[n + 1 - nxt][cur_c] > to_omit - jumps[cur_i][cur_c][layer]) {
to_omit -= jumps[cur_i][cur_c][layer];
cur_i = nxt;
//debug("go", layer);
}
layer--;
}
if (cur_i > pos) {
cout<<p[pos]<<"\n";
goto End;
}
// aktualnie mam dp[n + 1 - cur_i][cur_c] mozliwosci
VPII branches;
int control = 0;
FOR (steps, 0, cur_c) {
if (cur_i + steps > n) { continue; }
branches.PB({cur_i + steps, dp[n - cur_i - steps][cur_c - steps]});
control += branches.back().nd;
}
//assert(control == dp[n + 1 - cur_i][cur_c]);
sort(ALL(branches), [&](PII A, PII B) { return p[A.st] < p[B.st]; });
//debug(to_omit);
//debug(branches);
huh++;
assert(huh <= 6);
for (auto branch : branches) {
if (branch.nd <= to_omit) {
to_omit -= branch.nd;
continue;
}
if (pos <= branch.st) {
int from_end = pos - cur_i;
debug(n, from_end, branch.st);
cout<<p[branch.st - from_end]<<"\n";
goto End;
}
assert(branch.st != cur_i);
cur_c -= branch.st - cur_i;
cur_i = branch.st + 1;
debug("manual", cur_i, cur_c);
break;
}
}
End: ;
}
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
cin.tie(0);
//double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
int t;
cin>>t;
RE (i, t) {
Sol sol;
sol.Test();
}
return 0;
}
|
#include <bits/stdc++.h>
#define int i64
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
constexpr i64 inf = 1e18;
void solve() {
int n, c, q;
std::cin >> n >> c >> q;
std::vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
}
std::vector<std::vector<i64>> dp(n + 2, std::vector<i64>(c + 1));
std::vector<std::vector<i64>> down(n + 2, std::vector<i64>(c + 1));
std::vector<std::vector<i64>> up(n + 2, std::vector<i64>(c + 1));
for (int i = 0; i <= c; i++) {
dp[n + 1][i] = 1;
}
for (int i = n; i >= 1; --i) {
for (int j = 0; j <= c; j++) {
std::vector<std::pair<int, i64>> tmp;
for (int k = i; k <= i + j && k <= n; k++) {
tmp.emplace_back(a[k], dp[k + 1][j - k + i]);
}
std::sort(tmp.begin(), tmp.end());
for (auto t : tmp) {
if (t.first == a[i]) {
down[i][j] = dp[i][j];
up[i][j] = std::min(dp[i][j] + t.second, inf);
}
dp[i][j] = std::min(dp[i][j] + t.second, inf);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= c; j++) {
up[i][j] -= down[i][j];
down[i][j] = std::min(down[i][j] + down[i - 1][j], inf);
}
}
while (q--) {
int i, j;
std::cin >> i >> j;
if (j > dp[1][c]) {
std::cout << -1 << "\n";
} else {
int pos = 0;
int lft = c;
int ans = 0;
while (!ans) {
int l = pos;
int r = n;
while (l < r) {
int mid = l + r + 1 >> 1;
int t = j - down[mid][lft] + down[pos][lft];
if (t >= 1 && t <= up[mid][lft]) {
l = mid;
} else {
r = mid - 1;
}
}
if (l >= i) {
ans = a[i];
} else {
std::vector<std::pair<int, i64>> tmp;
j -= down[l][lft] - down[pos][lft];
++l;
for (int k = l; k <= l + lft && k <= n; k++) {
tmp.emplace_back(a[k], dp[k + 1][lft - k + l]);
}
std::sort(tmp.begin(), tmp.end());
for (auto t : tmp) {
if (j > t.second) {
j -= t.second;
} else {
int p = l;
while (a[p] != t.first) {
++p;
}
if (i <= p) {
ans = a[l + p - i];
} else {
pos = p;
lft = lft - p + l;
}
break;
}
}
}
}
std::cout << ans << "\n";
}
}
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
std::cin >> T;
while (T--) {
solve();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 30005;
const int maxm = 240005;
int T, n, c, q, a[maxn];
inline ll C(int n, int m) {
if(!m) return 1;
if(n < m) return 0;
ll res = 1;
for(int i = 0; i < m; i++) res *= n - i;
for(int i = 2; i <= m; i++) res /= i;
return res;
}
inline ll calc(int len, int cs) {
ll res = 0;
for(int i = 0; i <= cs; i++) res += C(len - 1, i);
return res;
}
int L[5][maxn], R[5][maxn];
struct data {
int l, r;
ll w;
data(int _l = 0, int _r = 0, ll _w = 0) { l = _l, r = _r, w = _w; }
};
data A[5][maxm];
ll sum[5][maxm];
data solve(int l, int cs, ll k) {
int nL = L[cs][l], nR = R[cs][l];
int pos = lower_bound(sum[cs] + nL, sum[cs] + nR + 1, sum[cs][nL - 1] + k) - sum[cs];
return data(A[cs][pos].l, A[cs][pos].r, sum[cs][pos - 1] - sum[cs][nL - 1]);
}
int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &c, &q);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int j = 1; j <= c; j++) L[j][n] = R[j][n] = n * c, A[j][L[j][n]] = data(n, n, 1);
static int bot[5], tot;
for(int i = n - 1; i >= 1; i--)
for(int j = 1; j <= c; j++) {
tot = 0;
L[j][i] = L[j][i + 1];
R[j][i] = R[j][i + 1];
for(int k = 1; k <= j && i + k <= n; k++) bot[++tot] = i + k;
sort(bot + 1, bot + tot + 1, [=] (int x, int y) -> bool { return a[x] < a[y]; });
for(int k = tot; k >= 1; k--)
if(a[bot[k]] < a[i]) A[j][--L[j][i]] = data(i, bot[k], calc(n - bot[k], j - bot[k] + i));
for(int k = 1; k <= tot; k++)
if(a[bot[k]] > a[i]) A[j][++R[j][i]] = data(i, bot[k], calc(n - bot[k], j - bot[k] + i));
}
for(int j = 1; j <= c; j++) {
sum[j][L[j][1] - 1] = 0;
for(int i = L[j][1]; i <= R[j][1]; i++) sum[j][i] = sum[j][i - 1] + A[j][i].w;
}
ll all = calc(n, c), x = 0;
int y = 0;
while(q--) {
scanf("%d%lld", &y, &x);
if(x > all) {
puts("-1");
continue;
}
int res = a[y], nowp = 1, nowc = c;
while(x && nowc && nowp <= n) {
data now = solve(nowp, nowc, x);
if(now.l <= y && y <= now.r) {
res = a[now.l + now.r - y];
break;
}
x -= now.w;
nowp = now.r + 1;
nowc -= now.r - now.l;
}
printf("%d\n", res);
}
}
return 0;
} |
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
#define G getchar()
ll read()
{
ll x=0; bool flg=false; char ch=G;
for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
return flg?-x:x;
}
#undef G
#define fi first
#define se second
/*
const int mod=;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,const int &y){x=upd(x+y-mod);}
inline void iadd(int &x,const int &y){x=upd(x-y);}
int qpow(int x,int y){
int res=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1) res=1LL*res*x%mod;
return res;
}
*/
/*
typedef pair<int,int> P;
*/
#define rep(i,l,r) for (int i(l);i<=(r);i++)
#define per(i,l,r) for (int i(r);i>=(l);i--)
int n,m,q,p[30010];
ll binom(int n,int m){
ll res=1;
rep(i,0,m-1) res*=n-i;
rep(i,1,m) res/=i;
return res;
}
ll b[5][30010];
int L[5][30010],R[5][30010];
struct C{
int l,r; ll w;
}c[5][300010];
bool operator<(const C &x,const C &y){
return x.w<y.w;
}
bool cmp(const int &x,const int &y){return p[x]<p[y];}
void solve(){ //init
n=read(),m=read(),q=read();
rep(i,1,n) p[i]=read();
rep(i,0,m) c[i][L[i][n+1]=R[i][n+1]=150003]=(C){-1,-1,1};
rep(j,0,m) b[j][0]=1;
rep(i,1,n){
rep(j,0,m) b[j][i]=binom(i-1,j);
rep(j,1,m) b[j][i]+=b[j-1][i];
}
per(i,1,n){
rep(j,0,m){
L[j][i]=L[j][i+1],R[j][i]=R[j][i+1];
static int f[6],ftop,g[6],gtop; ftop=gtop=0;
rep(k,1,min(j,n-i))
if (p[i+k]<p[i]) f[++ftop]=i+k;
else g[++gtop]=i+k;
sort(f+1,f+ftop+1,cmp); sort(g+1,g+gtop+1,cmp);
per(_k,1,ftop){
int k=f[_k]-i;
c[j][--L[j][i]]=(C){i,i+k,b[j-k][n-i-k]};
}
rep(_k,1,gtop){
int k=g[_k]-i;
c[j][++R[j][i]]=(C){i,i+k,b[j-k][n-i-k]};
}
// rep(k,1,min(j,n-i)){
// if (p[i+k]<p[i]) c[j][--L[j][i]]=(C){i,i+k,b[j-k][n-i-k]};
// else c[j][++R[j][i]]=(C){i,i+k,b[j-k][n-i-k]};
// }
}
}
rep(j,0,m){
rep(i,L[j][1],R[j][1]) c[j][i].w+=c[j][i-1].w;
}
// rep(j,0,m){
// rep(i,L[j][1],R[j][1]) printf("c %d %d: %d %d %lld\n",j,i,c[j][i].l,c[j][i].r,c[j][i].w);
// puts("");
// }
while (q--){
int x=read(); ll rk=read();
if (rk>b[m][n]){puts("-1");continue;}
int e=m,p=1,ans=-1;
while (1){
rk+=c[e][L[e][p]-1].w;
int t=lower_bound(c[e]+L[e][p],c[e]+R[e][p]+1,(C){-1,-1,rk})-c[e];
assert(t<=R[e][p]); const C &V=c[e][t];
if (V.l==-1){ans=::p[x];break;}
if (V.l<=x&&x<=V.r){ans=::p[V.l+V.r-x];break;}
rk-=c[e][t-1].w;
e-=V.r-V.l; p=V.r+1;
}
assert(~ans); //assert(rk==1);
printf("%d\n",ans);
// printf("rk %lld\n",rk);
}
}
int main()
{
for (int T=read();T--;) solve();
}
|
/**
* created: 13/01/2021, 20:24:36
**/
#include <bits/stdc++.h>
using namespace std;
const int max_n = 30033;
const int max_c = 5;
const long long inf = 1000111222000111222LL;
int t, n, c, q, a[max_n];
vector<int> order[max_n];
long long dp[max_n][max_c], A[max_c][max_n], B[max_n][max_c];
__int128 sumA[max_c][max_n];
__int128 get_sum(int c, int l, int r) {
if (l > r) {
return 0;
}
if (!l) {
return sumA[c][r];
}
return sumA[c][r] - sumA[c][l - 1];
}
int get_last(int pos, long long num, int c) {
assert(dp[pos][c] >= num);
int l = pos, r = n + 1;
while (r - l > 1) {
int mid = (l + r) / 2;
if (dp[mid][c] + get_sum(c, pos, mid - 1) >= num) {
l = mid;
} else {
r = mid;
}
}
return l;
}
vector<pair<int, int>> solve(long long num, int c) {
vector<pair<int, int>> res;
int pos = 0;
while (pos < n) {
int last = get_last(pos, num, c);
__int128 sum_pref = 0;
if (pos) {
sum_pref += sumA[c][pos - 1];
}
int first = lower_bound(sumA[c], sumA[c] + n, num + sum_pref) - sumA[c];
//cout << "#" << pos << ": " << first << " " << last << ", c = " << c << ", num = " << num << " " << A[c][first] << endl;
first = min(first, last);
if (first == n) {
break;
}
num -= get_sum(c, pos, first - 1);
//cout << "new num = " << num << endl;
int oldc = c;
for (int len : order[first]) {
if (len > c) {
continue;
}
if (num > dp[first + len + 1][c - len]) {
//cout << "dec " << len << " " << dp[first + len + 1][c - len] << endl;
num -= dp[first + len + 1][c - len];
} else {
assert(len);
res.push_back({first, len});
pos = first + len + 1;
c -= len;
break;
}
}
assert(c != oldc);
}
return res;
}
int get_elem(const vector<pair<int, int>> &v, int pos) {
for (auto p : v) {
if (p.first <= pos && pos <= p.first + p.second) {
pos -= p.first;
pos = p.second - pos;
pos += p.first;
return a[pos];
}
}
return a[pos];
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &n, &c, &q);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
fill(dp[n], dp[n] + c + 1, 1);
for (int i = n - 1; i >= 0; --i) {
for (int j = 0; j <= c; ++j) {
dp[i][j] = 0;
for (int len = 0; i + len < n && len <= j; ++len) {
dp[i][j] += dp[i + len + 1][j - len];
dp[i][j] = min(dp[i][j], inf);
}
}
order[i].clear();
for (int len = 0; len <= c && i + len < n; ++len) {
order[i].push_back(len);
}
sort(order[i].begin(), order[i].end(), [i](int x, int y) { return a[i + x] < a[i + y]; });
for (int j = 0; j <= c; ++j) {
B[j][i] = dp[i][j];
A[j][i] = 0;
for (int len : order[i]) {
if (len == 0) {
break;
}
if (len <= j) {
A[j][i] += dp[i + len + 1][j - len];
A[j][i] = min(A[j][i], inf);
}
}
}
}
for (int j = 0; j <= c; ++j) {
sumA[j][0] = A[j][0];
for (int i = 1; i < n; ++i) {
sumA[j][i] = sumA[j][i - 1] + A[j][i];
}
}
while (q--) {
int pos;
long long num;
scanf("%d%lld", &pos, &num);
--pos;
if (dp[0][c] < num) {
puts("-1");
} else {
vector<pair<int, int>> res = solve(num, c);
int ans = get_elem(res, pos);
printf("%d\n", ans);
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int T,n,c,q,p[30010],st[10],t,now;
long long L[10][30010],R[10][30010],f[10][30010];
bool cmp(int x,int y){
return p[now+x]<p[now+y];
}
int qry(int pos,long long id){
if(id>f[c][1]) return -1;
int op=c;now=1;
while(now<=n){
int l=now,r=n,res,mid;
while(l<=r){
mid=(l+r)>>1;
if(L[op][now]-L[op][mid]<id&&R[op][now]-R[op][mid]<=f[op][now]-id) l=mid+1,res=mid;
else r=mid-1;
}
if(res>pos) return p[pos];
id-=(L[op][now]-L[op][res]),now=res,t=0;
for(int i=0;i<=op&&now+i<=n;i++) st[++t]=i;
sort(st+1,st+t+1,cmp);
int pp=0;
for(int i=1;i<=t;i++){
if(id<=f[op-st[i]][now+st[i]+1]){
pp=st[i];
break;
}
else id-=f[op-st[i]][now+st[i]+1];
}
if(pp+now>=pos) return p[2*now-pos+pp];
op-=pp,now+=pp+1;
}
return p[pos];
}
void solve(){
scanf("%d%d%d",&n,&c,&q);
for(int i=1;i<=n;i++) scanf("%d",&p[i]);
for(int i=0;i<=c;i++){
f[i][n+1]=1,L[i][n+1]=R[i][n+1]=0;
for(int j=n;j>=1;j--){
f[i][j]=f[i][j+1],L[i][j]=L[i][j+1],R[i][j]=R[i][j+1];
for(int k=1;k<=i&&j+k<=n;k++){
long long v=f[i-k][j+k+1];
f[i][j]+=v;
assert(f[i][j]<1e18);
if(p[j]>p[j+k]) L[i][j]+=v;
else R[i][j]+=v;
}
}
}
while(q--){
int pos;
long long id;
scanf("%d%lld",&pos,&id);
printf("%d\n",qry(pos,id));
}
}
int main(){
scanf("%d",&T);
while(T--) solve();
} |
#include<iostream>
#include<cstring>
#include<cassert>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<time.h>
#include<bitset>
#include<cstdio>
#include<algorithm>
using namespace std;
#define REP(i,x,y) for(int i=x;i<=y;i++)
#define rep(i,n) REP(i,1,n)
#define rep0(i,n) REP(i,0,n-1)
#define repG(i,x) for(int i=pos[x];~i;i=e[i].next)
#define ll long long
#define db double
const int N=1e5+7;
const int INF=1e9+7;
int T,n,c,q;
int p[N];
ll f[N][5],L[N][5],R[N][5],l[5],r[5];
int id[N][5];
bool cmp(int x,int y){return p[x]<p[y];}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&c,&q);
rep(i,n)scanf("%d",&p[i]);
rep(i,n+10)rep0(j,c+1)f[i][j]=0;
rep0(i,c+1)f[n+1][i]=1;
for(int i=n;i;i--){
rep0(j,c+1){
f[i][j]=f[i+1][j];
rep(k,j){
if(i+k>n)break;
f[i][j]+=f[i+k+1][j-k];
}
}
}
rep(i,n){
rep0(j,c+1)id[i][j]=i+j;
sort(id[i],id[i]+c+1,cmp);
}
rep0(i,c+1){
L[1][i]=1;
R[1][i]=f[1][i];
rep(j,n){
ll k,vl=0,vr=0;
for(k=0;id[j][k]!=j;k++)if(id[j][k]-j<=i)vl+=f[id[j][k]+1][i-id[j][k]+j];
for(k++;k<=c;k++)if(id[j][k]-j<=i)vr+=f[id[j][k]+1][i-id[j][k]+j];
L[j+1][i]=L[j][i]+vl;
R[j+1][i]=R[j][i]-vr;
}
}
while(q--){
int x,cnt=0,w=1,o=c; ll v; scanf("%d%I64d",&x,&v);
if(v>f[1][c]){
puts("-1");
continue;
}
while(1){
if(L[n+1][o]-L[w][o]+1ll<=v&&v<=R[n+1][o]-L[w][o]+1ll)break;
int a=w,b=n,g;
while(a<b-1){
int mid=(a+b)>>1;
if(L[mid+1][o]-L[w][o]+1ll<=v&&v<=R[mid+1][o]-L[w][o]+1ll)a=mid;
else b=mid;
}
if(L[a+1][o]-L[w][o]+1ll>v||v>R[a+1][o]-L[w][o]+1ll)g=a;
else g=b;
if(v<L[g+1][o]-L[w][o]+1){
v-=(L[g][o]-L[w][o]);
for(int k=0;id[g][k]!=g;k++){
if(id[g][k]-g<=o){
if(v>f[id[g][k]+1][o-id[g][k]+g])v-=f[id[g][k]+1][o-id[g][k]+g];
else{
w=id[g][k]+1;
o=o-id[g][k]+g;
l[cnt]=g;
r[cnt++]=id[g][k];
break;
}
}
}
}
else{
v-=(R[g+1][o]-L[w][o]+1);
int k=0;
for(;id[g][k]!=g;k++);
for(k++;k<=c;k++){
if(id[g][k]-g<=o){
if(v>f[id[g][k]+1][o-id[g][k]+g])v-=f[id[g][k]+1][o-id[g][k]+g];
else{
w=id[g][k]+1;
o=o-id[g][k]+g;
l[cnt]=g;
r[cnt++]=id[g][k];
break;
}
}
}
}
}
int Ans=p[x];
rep0(i,cnt)if(l[i]<=x&&r[i]>=x)Ans=p[l[i]+r[i]-x];
printf("%d\n",Ans);
}
}
return 0;
}
|
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define dl double long
using namespace std;
const int N = 3e4 + 7;
const long long mod = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,c,q;
int p[N];
ll d[N][5];
ll L[N][5];
ll R[N][5];
vector < int > v[N][5];
void solve()
{
cin >> n >> c >> q;
for( int i = 1; i <= n; i++ ){
cin >> p[i];
}
for( int i = 0; i <= c; i++ )d[n + 1][i] = 1;
for( int i = 0; i <= n; i++ ){
for( int j = 0; j <= c; j++ ){
L[i][j] = R[i][j] = 0;
v[i][j].clear();
}
}
for( int i = n; i >= 1; i-- ){
for( int j = 0; j <= c; j++ ){
d[i][j] = d[i + 1][j];
vector < pair < int , int > > g;
g.push_back({ p[i] , 0 });
for( int h = 1; h <= j; h++ ){
if( i + h <= n ){
g.push_back({ p[i + h] , h });
d[i][j] += d[i + h + 1][j - h];
}
}
sort( g.begin() , g.end() );
for( auto x : g ){
v[i][j].push_back(x.se);
}
}
}
for( int i = 0; i <= c; i++ ){
for( int j = 1; j <= n; j++ ){
for( auto x : v[j][i] ){
if( x == 0 ){
R[j][i] += d[j + 1][i];
break;
}else{
L[j][i] += d[j + x + 1][i - x];
R[j][i] += d[j + x + 1][i - x];
}
}
}
}
for( int i = 0; i <= c; i++ ){
for( int j = 1; j <= n; j++ ){
L[j][i] += L[j - 1][i];
}
}
while( q-- ){
int i;
ll j;
cin >> i >> j;
if( j > d[1][c] ){
cout << "-1\n";
continue;
}
int st = 1;
int nc = c;
int ans = p[i];
while( st <= n ){
int l = st , r = n + 1;
while( l < r ){
int m = (l + r) / 2;
if( L[m][nc] - L[st - 1][nc] < j && j <= L[m - 1][nc] - L[st - 1][nc] + R[m][nc] ){
l = m + 1;
}else r = m;
}
if( l == n + 1 )break;
j -= L[l - 1][nc] - L[st - 1][nc];
st = l;
for( auto x : v[st][nc] ){
if( j > d[st + x + 1][nc - x] ){
j -= d[st + x + 1][nc - x];
}else{
if( st <= i && i <= st + x ){
ans = p[st + st + x - i];
}
st += x + 1;
nc -= x;
break;
}
}
}
cout << ans << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(0);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
int T;
cin >> T;
while( T-- ){
solve();
}
}
|
#include<bits/stdc++.h>
#define ci const int&
#define TOT(tc,ql,qr) ((qr>=0?s[qr][tc]:0)-(ql>0?s[ql-1][tc]:0))
using namespace std;
struct elem{
int l,r;
long long num;
};
int T,n,C,Q,p[30010],li[30010][5],ri[30010][5],l,r,mid,qi,nc,ni,opl[5],opr[5],sz;
long long qj,s[120010][5];
vector<elem>L[5],tl[30010][5],tr[30010][5];
bool cmp(elem x,elem y){
return p[x.r]<p[y.r];
}
long long NUM(ci x,ci op){
if(op==0)return 1;
if(x<op)return 0;
if(op==1)return x;
if(op==2)return x*(x-1)>>1;
if(op==3)return 1ll*x*(x-1)*(x-2)/6;
return 1ll*x*(x-1)*(x-2)*(x-3)/24;
}
long long Calc(ci x,ci op){
long long ret=0;
for(int i=0;i<=op;++i)ret+=NUM(x-1,i);
return ret;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&C,&Q);
for(int i=1;i<=4;++i)vector<elem>().swap(L[i]),ri[n+1][i]=0;
for(int i=1;i<=n;++i){
scanf("%d",&p[i]);
for(int j=1;j<=C;++j)vector<elem>().swap(tl[i][j]),vector<elem>().swap(tr[i][j]);
}
for(int c=1;c<=C;++c){
tl[n][c].push_back((elem){n+1,n+1,1});
for(int i=n-1;i>=1;--i){
for(int j=i+1;j<=i+c&&j<=n;++j)p[j]<p[i]?tl[i][c].push_back((elem){i,j,Calc(n-j,c-(j-i))}):tr[i][c].push_back((elem){i,j,Calc(n-j,c-(j-i))});
if(tl[i][c].size())sort(tl[i][c].begin(),tl[i][c].end(),cmp);
if(tr[i][c].size())sort(tr[i][c].begin(),tr[i][c].end(),cmp);
li[i+1][c]=tl[i][c].size(),ri[i+1][c]=li[i+1][c]+ri[i+2][c]+tr[i+1][c].size();
}
li[1][c]=0,ri[1][c]=ri[2][c]+tr[1][c].size();
for(int i=2;i<=n;++i)li[i][c]+=li[i-1][c],ri[i][c]+=li[i-1][c];
for(int i=1;i<=n;++i)for(int j=0;j<tl[i][c].size();++j)L[c].push_back(tl[i][c][j]);
for(int i=n;i>=1;--i)for(int j=0;j<tr[i][c].size();++j)L[c].push_back(tr[i][c][j]);
s[0][c]=L[c][0].num;
for(int i=1;i<L[c].size();++i)s[i][c]=s[i-1][c]+L[c][i].num;
}
while(Q--){
scanf("%d%lld",&qi,&qj),nc=C,sz=0,ni=1;
if(qj>Calc(n,C)){
puts("-1");
goto Skip;
}
while(nc&&ni<=n){
l=li[ni][nc],r=ri[ni][nc];
while(l<r)mid=l+r>>1,TOT(nc,li[ni][nc],mid)>=qj?r=mid:l=mid+1;
++sz,qj-=TOT(nc,li[ni][nc],l-1),opl[sz]=L[nc][l].l,opr[sz]=L[nc][l].r,ni=opr[sz]+1,nc-=L[nc][l].r-L[nc][l].l;
}
for(int i=1;i<=sz;++i)if(opl[i]<=qi&&qi<=opr[i]){
printf("%d\n",p[opl[i]+opr[i]-qi]);
goto Skip;
}
printf("%d\n",p[qi]);
Skip:;
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
} const int N = 5e4 + 5, M = 5;
int C (int x, int y) {
if (x < 1) return (y == 0);
if (x < y) return 0; int s = 1;
for (int i = 0; i < y; ++i) s *= (x - i);
for (int i = 2; i <= y; ++i) s /= i;
return s;
}
int ways (int len, int c) {
int s = 0;
for (int i = 0; i <= c; ++i)
s += C (len - 1, i); return s;
}
int n, c, Q, a[N], L[5], R[5], ll[5][N], rr[5][N], s[5][N * M * 2];
struct node {
int l, r, w;
void set (int L, int R, int p) {
l = L, r = R, w = ways (n - r, p - (r - l));
}
} q[5][N * M * 2];
int o[10]; int tp;
bool cmp (int x, int y) { return a[x] < a[y]; }
void prework () {
for (int i = 1; i <= c; ++i)
L[i] = n * c, R[i] = L[i], q[i][R[i]] = {n, n, 1};
L[0] = R[0] = 1, q[0][1] = {n, n, 1};
for (int i = n; i >= 1; --i) {
ll[0][i] = rr[0][i] = 1;
for (int j = 1; j <= c; ++j) {
tp = 0;
for (int k = 1; k <= j && i + k <= n; ++k) o[++tp] = i + k;
sort (o + 1, o + tp + 1, cmp);
for (int k = tp, l = i, r; k >= 1; --k)
if (a[r = o[k]] < a[l]) --L[j], q[j][L[j]].set (l, r, j);
for (int k = 1, l = i, r; k <= tp; ++k)
if (a[r = o[k]] > a[l]) ++R[j], q[j][R[j]].set (l, r, j);
ll[j][i] = L[j], rr[j][i] = R[j];
}
}
for (int i = 1; i <= c; ++i)
for (int j = L[i]; j <= R[i]; ++j)
s[i][j] = s[i][j - 1] + q[i][j].w;
}
int sum (int c, int l, int r) {
return s[c][r] - s[c][l - 1];
}
node get (int x, int c, int k) {
int l = ll[c][x], r = rr[c][x], mid, res;
while (l <= r) {
mid = l + r >> 1;
if (sum (c, ll[c][x], mid) >= k) res = mid, r = mid - 1;
else l = mid + 1;
}
return {q[c][res].l, q[c][res].r, sum (c, ll[c][x], res - 1)};
}
signed main() {
int T; read (T);
while (T--) {
read (n), read (c), read (Q);
for (int i = 1; i <= n; ++i) read (a[i]);
int x = 0, y = 0, now, cnt, res; prework ();
while (Q--) {
read (y), read (x); now = 1, cnt = c, res = a[y];
if (ways (n, c) < x) { puts ("-1"); continue; }
while (x && cnt && now <= n) {
node tmp = get (now, cnt, x);
if (tmp.l <= y && tmp.r >= y) {
res = a[tmp.l + tmp.r - y]; break;
}
now = tmp.r + 1, cnt -= (tmp.r - tmp.l), x -= tmp.w;
}
printf ("%lld\n", res);
}
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef long long ll;
template <typename _T>
inline void read(_T &f) {
f = 0; _T fu = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); }
while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
f *= fu;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
print(x); putchar(t);
}
const int N = 30005;
struct atom {
int a, b;
atom (int k1 = 0, int k2 = 0) : a(k1), b(k2) {}
};
ll f[N][5], sum[N][5][16];
int a[N];
int T, n, c, q;
ll getf(atom x) { return f[x.a][x.b]; }
void addf(atom x, ll y) { f[x.a][x.b] += y; }
int main() {
read(T);
while (T--) {
memset(f, 0, sizeof(f));
memset(sum, 0, sizeof(sum));
read(n); read(c); read(q);
for (int i = 1; i <= n; i++) read(a[i]);
for (int i = 0; i <= 4; i++) f[n + 1][i] = 1;
for (int i = n; i >= 1; i--) {
for (int j = 0; j <= 4; j++) {
for (int k = 0; k <= j && i + k <= n; k++) {
if (a[i + k] < a[i]) {
sum[i][j][0] += getf(atom(i + k + 1, j - k));
}
addf(atom(i, j), getf(atom(i + k + 1, j - k)));
}
for (int k = 1; k <= 15; k++) {
sum[i][j][k] = sum[i][j][k - 1];
if (i + (1 << (k - 1)) <= n) sum[i][j][k] += sum[i + (1 << (k - 1))][j][k - 1];
}
}
}
while (q--) {
int pos; ll rk;
read(pos); read(rk);
if (f[1][c] < rk) {
print(-1, '\n');
continue;
}
atom now = atom(1, c);
while (1) {
ll ss = 0;
for (int i = 15; i >= 0; i--) {
if (now.a + (1 << i) <= n + 1 && ss + sum[now.a][now.b][i] < rk && rk <= ss + sum[now.a][now.b][i] + getf(atom(now.a + (1 << i), now.b))) {
ss += sum[now.a][now.b][i];
now.a += (1 << i);
}
}
if (now.a > pos) {
print(a[pos], '\n');
break;
}
rk -= ss;
vector <pii> seq;
for (int i = 0; i <= now.b && now.a + i <= n; i++) {
seq.push_back(make_pair(a[now.a + i], i));
}
sort(seq.begin(), seq.end());
int tmp = now.a;
for (int i = 0; i < (int)seq.size(); i++) {
if (rk <= getf(atom(now.a + seq[i].second + 1, now.b - seq[i].second))) {
now = atom(now.a + seq[i].second + 1, now.b - seq[i].second);
break;
}
rk -= getf(atom(now.a + seq[i].second + 1, now.b - seq[i].second));
}
if (now.a > pos) {
print(a[now.a - 1 - (pos - tmp)], '\n');
break;
}
}
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n, c, q;
cin >> n >> c >> q;
vector<int> p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
--p[i];
}
vector<vector<long long>> ways(n + 1, vector<long long>(c + 1));
for (int j = 0; j <= c; j++) {
ways[n][j] = 1;
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= c; j++) {
ways[i][j] = ways[i + 1][j];
for (int k = 1; k <= j; k++) {
if (i + k < n) {
ways[i][j] += ways[i + k + 1][j - k];
}
}
}
}
vector<vector<long long>> sumL(c + 1, vector<long long>(n + 1));
vector<vector<long long>> sumR(c + 1, vector<long long>(n + 1));
for (int rm = 0; rm <= c; rm++) {
for (int i = 0; i < n; i++) {
sumL[rm][i + 1] = sumL[rm][i];
sumR[rm][i + 1] = sumR[rm][i];
for (int k = 1; k <= rm; k++) {
if (i + k < n) {
if (p[i] < p[i + k]) {
sumR[rm][i + 1] += ways[i + k + 1][rm - k];
} else {
sumL[rm][i + 1] += ways[i + k + 1][rm - k];
}
}
}
}
}
vector<int> order;
while (q--) {
int pos;
long long id;
cin >> pos >> id;
--pos; --id;
if (id >= ways[0][c]) {
cout << -1 << '\n';
continue;
}
int ans = -1;
int i = 0;
int rm = c;
while (i < n) {
assert(id < ways[i][rm]);
int low = i, high = n;
while (low < high) {
int mid = (low + high + 1) >> 1;
if (sumL[rm][mid] - sumL[rm][i] > id || sumR[rm][mid] - sumR[rm][i] > ways[i][rm] - 1 - id) {
high = mid - 1;
} else {
low = mid;
}
}
if (pos < low) {
ans = p[pos];
break;
}
id -= sumL[rm][low] - sumL[rm][i];
assert(id >= 0 && id < ways[low][rm]);
i = low;
order.clear();
for (int k = 0; k <= rm; k++) {
if (i + k < n) {
order.push_back(k);
}
}
sort(order.begin(), order.end(), [&](int x, int y) {
return p[i + x] < p[i + y];
});
int shift = -1;
for (int v : order) {
long long cur = ways[i + v + 1][rm - v];
if (id < cur) {
shift = v;
break;
}
id -= cur;
}
assert(shift != -1);
assert(shift != 0);
if (pos <= i + shift) {
ans = p[i + (i + shift) - pos];
break;
}
i += shift + 1;
rm -= shift;
}
assert(ans != -1);
cout << ans + 1 << '\n';
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
template<class t>
void print(t x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t,class u>
void print(const pair<t,u>&p,int suc=1){
print(p.a,2);
print(p.b,suc);
}
template<class T>
void print(const vector<T>&v,int suc=1){
rep(i,v.size())
print(v[i],i==int(v.size())-1?suc:2);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
//#define CAPITAL
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
#ifdef LOCAL
static mt19937_64 gen;
#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class t>
int lwb(const vc<t>&v,const t&a){
return lower_bound(all(v),a)-v.bg;
}
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> readTree(int n){
return readGraph(n,n-1);
}
const int nmax=30010;
const int cmax=5;
int dp[cmax][nmax],sum[cmax][nmax];
vi to[nmax];
void slv(){
int n,c,q;cin>>n>>c>>q;
c++;
vi p=readvi(n);
rep(i,n){
to[i].resize(min(c,n-i));
iota(all(to[i]),0);
sort(all(to[i]),[&](int a,int b){
return p[i+a]<p[i+b];
});
}
per(k,c){
dp[k][n]=1;
per(i,n){
for(auto j:to[i])if(k+j<c){
if(j==0)sum[k][i+1]=dp[k][i];
dp[k][i]+=dp[k+j][i+j+1];
}
}
rep(i,n)sum[k][i+1]+=sum[k][i];
}
rep(_,q){
int pos;cin>>pos;pos--;
int idx;cin>>idx;idx--;
if(idx>=dp[0][0]){
print(-1);
continue;
}
int cur=0,used=0;
while(1){
int lw=cur,up=n+1;
auto check=[&](int i){
int val=sum[used][i]-sum[used][cur];
return val<=idx&&idx<val+dp[used][i];
};
assert(check(lw));
while(up-lw>1){
const int mid=(lw+up)/2;
if(check(mid))lw=mid;
else up=mid;
}
idx-=sum[used][lw]-sum[used][cur];
cur=lw;
if(pos<cur){
print(p[pos]);
break;
}
int done=0;
for(auto j:to[cur])if(used+j<c){
if(idx<dp[used+j][cur+j+1]){
if(pos<=cur+j){
print(p[cur+(cur+j-pos)]);
done=1;
break;
}else{
cur+=j+1;
used+=j;
done=2;
break;
}
}else{
idx-=dp[used+j][cur+j+1];
}
}
assert(done);
if(done==1)break;
}
}
zero(dp);
zero(sum);
rep(i,n)to[i].clear();
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
int t;cin>>t;rep(_,t)
slv();
}
|
// Main correct solution + fast input (IgorI)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T = int> inline T readInt();
template <class T = int64_t> inline T readLong();
struct Segment{
int rL, rR;
long long len, plen;
};
struct Ask{
long long j;
int i, id;
};
const long long INFLL = 1e18 + 2222;
const int Q = 300000 + 1;
const int N = 30000 + 1;
const int C = 5;
ll ways[N][C];
ll cnt(int len, int coins)
{
ll ans = 0;
if (0 <= coins && 0 <= len) ans += 1;
if (1 <= coins && 1 <= len) ans += len - 1;
if (2 <= coins && 2 <= len) ans += 1ll * (len - 1) * (len - 2) / 2;
if (3 <= coins && 3 <= len) ans += 1ll * (len - 1) * (len - 2) * (len - 3) / 6;
if (4 <= coins && 4 <= len) ans += 1ll * (len - 1) * (len - 2) * (len - 3) * (len - 4) / 24;
return ans;
}
int answer[Q];
vector<Ask> asks[C];
pair<int, int> le[C];
int lesz;
pair<int, int> ri[C];
int risz;
void solve()
{
int n = readInt(), c = readInt(), q = readInt();
vector<int> p(n);
for (int i = 0; i < n; i++)
p[i] = readInt() - 1;
vector<deque<Segment> > s(c + 1);
for (int coins = 0; coins <= c; coins++)
{
s[coins].push_back({n, n, 1});
for (int i = n - 2; i >= 0; i--)
{
int me = p[i];
lesz = 0, risz = 0;
for (int j = i + 1; j <= i + coins && j < n; j++)
{
if (p[j] < me)
{
le[lesz++] = {p[j], j};
}
else
{
ri[risz++] = {p[j], j};
}
}
sort(le, le + lesz);
reverse(le, le + lesz);
sort(ri, ri + risz);
for (int j = 0; j < lesz; j++)
{
s[coins].push_front({i, le[j].second, ways[n - 1 - le[j].second][coins - le[j].second + i]});
}
for (int j = 0; j < risz; j++)
{
s[coins].push_back({i, ri[j].second, ways[n - 1 - ri[j].second][coins - ri[j].second + i]});
}
}
for (int i = 0; i < s[coins].size(); i++)
{
s[coins][i].plen = s[coins][i].len + (i ? s[coins][i - 1].plen : 0);
}
}
vector<vector<long long> > go(c + 1, vector<long long>(n + 1, INFLL));
for (int coins = 0; coins <= c; coins++)
{
for (int j = (int)s[coins].size() - 1; j >= 0; j--)
{
go[coins][s[coins][j].rL] = min(go[coins][s[coins][j].rL], s[coins][j].plen - s[coins][j].len);
}
}
for (int coins = 0; coins <= c; coins++)
{
for (int j = n - 1; j >= 0; j--)
{
go[coins][j] = min(go[coins][j], go[coins][j + 1]);
}
}
for (int ii = 0; ii < q; ii++)
{
answer[ii] = -2;
int i = readInt() - 1;
long long j = readLong() - 1;
if (j >= cnt(n, c))
{
answer[ii] = -1;
continue;
}
asks[c].push_back({j, i, ii});
}
for (int level = c; level >= 0; level--)
{
sort(asks[level].begin(), asks[level].end(), [&](Ask A, Ask B){
return A.j < B.j;
});
int pnt = 0;
for (auto e : asks[level])
{
while (pnt + 1 < s[level].size() && s[level][pnt].plen <= e.j)
pnt++;
int L = s[level][pnt].rL;
int R = s[level][pnt].rR;
if (e.i <= R)
{
if (L <= e.i)
answer[e.id] = p[L + R - e.i] + 1;
else
answer[e.id] = p[e.i] + 1;
}
else
{
int NL = level - (R - L);
asks[NL].push_back({e.j - s[level][pnt].plen + s[level][pnt].len + go[NL][R + 1], e.i, e.id});
}
}
vector<Ask> z;
swap(asks[level], z);
}
for (int i = 0; i < q; i++)
{
printf("%d\n", answer[i]);
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < C; j++)
{
ways[i][j] = cnt(i, j);
}
}
int t = readInt();
while (t--)
{
solve();
}
}
static const int buf_size = 4096;
static char buf[buf_size];
static int len = 0, pos = 0;
inline bool isEof() {
if (pos == len) {
pos = 0, len = fread(buf, 1, buf_size, stdin);
if (pos == len)
return 1;
}
return 0;
}
inline int getChar() {
return isEof() ? -1 : buf[pos++];
}
inline int peekChar() {
return isEof() ? -1 : buf[pos];
}
inline int readChar() {
int c = getChar();
while (c != -1 && c <= 32)
c = getChar();
return c;
}
template <class T>
inline T readInt() {
int c = readChar();
T x = 0;
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = getChar();
return x;
}
template <class T>
inline T readLong() {
int c = readChar();
T x = 0;
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = getChar();
return x;
} |
#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template<typename T> inline void ckmax(T& x, T y) { x = (y > x ? y : x); }
template<typename T> inline void ckmin(T& x, T y) { x = (y < x ? y : x); }
const int MAXC = 4;
const int MAXN = 3e4;
int n, c, q, a[MAXN + 5];
ll ways_eq(int len, int c) {
// 长度为 len, 操作代价和恰好为 c
// comb(len - 1, c)
if (len <= 1) {
return c == 0;
}
if (len - 1 < c) {
return 0;
}
ll res = 1;
for (int i = len - 1; i >= len - c; --i) {
res *= i;
}
for (int i = c; i > 1; --i) {
res /= i;
}
return res;
}
ll ways_leq(int len, int c) {
// 长度为 len, 操作代价和小于或等于 c
ll res = 0;
for (int i = 0; i <= c; ++i) {
res += ways_eq(len, i);
}
return res;
}
struct Node {
int l, r;
ll w; // 后面的操作方案数
Node() {}
Node(int _l, int _r, ll _w) {
l = _l;
r = _r;
w = _w;
}
};
Node dq[MAXC + 1][MAXN * MAXC * 2 + 10];
int ql[MAXC + 1], qr[MAXC + 1];
int sum[MAXC + 1][MAXN + 5];
ll sumw[MAXC + 1][MAXN * MAXC + 5];
bool cmp(Node lhs, Node rhs) {
return a[lhs.r] < a[rhs.r];
}
Node F(int st, int s, ll rank) {
int i = sum[s][st]; // 在 st 之前的, 一共有这么多区间
/*
// 暴力查找
ll cur = 0;
for (i = ql[s] + i; i <= qr[s]; ++i) {
cur += dq[s][i].w;
assert(cur == sumw[s][i - ql[s] + 1] - sumw[s][sum[s][st]]);
if (cur >= rank) {
return Node(dq[s][i].l, dq[s][i].r, cur - dq[s][i].w);
}
}
*/
int l = i + 1, r = qr[s] - ql[s] + 2;
while (l < r) {
int mid = (l + r) >> 1;
if (sumw[s][mid] - sumw[s][i] >= rank) {
r = mid;
} else {
l = mid + 1;
}
}
assert(l <= qr[s] - ql[s] + 1);
return Node(dq[s][ql[s] + l - 1].l, dq[s][ql[s] + l - 1].r, sumw[s][l - 1] - sumw[s][i]);
}
void solve_case() {
cin >> n >> c >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int s = 1; s <= c; ++s) { // 总代价小于或等于 s
// cerr << "-------- maxcost " << s << " --------" << endl;
ql[s] = MAXN * MAXC + 5, qr[s] = MAXN * MAXC + 4; // 队列清空
for (int i = 1; i <= n; ++i)
sum[s][i] = 0;
dq[s][++qr[s]] = Node(n, n, 1); // 什么都不翻转
for (int i = n - 1; i >= 1; --i) {
int dl = 0, dr = 0;
for (int j = 1; j <= min(s, n - i); ++j) {
// 翻转区间 [i, i + j]
ll w = ways_leq(n - (i + j), s - j);
if (a[i + j] < a[i]) {
// 翻转后是 a[i + j], 不翻转是 a[i], 两者比一比
// 翻转后更小, push_front
dq[s][ql[s] - (++dl)] = Node(i, i + j, w);
sum[s][i + 1]++;
} else {
dq[s][qr[s] + (++dr)] = Node(i, i + j, w);
}
}
if (dl) {
sort(dq[s] + ql[s] - dl, dq[s] + ql[s], cmp);
ql[s] -= dl;
}
if (dr) {
sort(dq[s] + qr[s] + 1, dq[s] + qr[s] + dr + 1, cmp);
qr[s] += dr;
}
}
// cerr << "print queue: " << endl;
// for (int i = ql[s]; i <= qr[s]; ++i) {
// cerr << dq[s][i].l << " " << dq[s][i].r << " " << dq[s][i].w << endl;
// }
// cerr << "queue end" << endl;
for (int i = 1; i <= n; ++i) {
sum[s][i] += sum[s][i - 1];
}
for (int i = ql[s]; i <= qr[s]; ++i) {
sumw[s][i - ql[s] + 1] = sumw[s][i - ql[s]] + dq[s][i].w;
}
}
ll lim = ways_leq(n, c);
for (int tq = 1; tq <= q; ++tq) {
int pos;
ll rank;
// cerr << "-------- query: " << endl;
cin >> pos >> rank;
if (rank > lim) {
cout << -1 << endl;
continue;
}
vector<pii> revs;
int p = 1;
int s = c;
while (1) {
Node t = F(p, s, rank);
// cerr << "** " << t.l << " " << t.r << " " << t.w << endl;
revs.push_back(make_pair(t.l, t.r));
assert(t.w < rank);
rank -= t.w;
s -= (t.r - t.l);
p = t.r + 1;
if (!s)
break;
if (p > n)
break;
}
/*
// 暴力翻转
static int aa[MAXN + 5];
for (int i = 1; i <= n; ++i) {
aa[i] = a[i];
}
for (int i = 0; i < SZ(revs); ++i) {
reverse(aa + revs[i].fi, aa + revs[i].se + 1);
}
cout << aa[pos] << endl;
*/
bool flag = 0;
for (int i = 0; i < SZ(revs); ++i) {
if (revs[i].fi <= pos && revs[i].se >= pos) {
cout << a[revs[i].se - (pos - revs[i].fi)] << endl;
flag = 1;
break;
}
}
if (!flag) {
cout << a[pos] << endl;
}
}
}
int main() {
int T; cin >> T; while (T--) {
solve_case();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
int read(){
int a=0,fh=1;char c=getchar();
while(c>'9'||c<'0'){if(c=='-')fh=-1;c=getchar();}
while('0'<=c&&c<='9'){
a=a*10+c-48;
c=getchar();
}
return a*fh;
}
#define MN 30005
#define pii pair<int,int>
#define mp make_pair
#define x first
#define y second
int T,n,m,c,q;
int p[MN];
int f[MN][5],sum[MN][5];//sum:前面维持原状的花费
vector<pii >ans,W[MN][5];
void solve(int st,int i,int j,int c){
if(st>i)return;
if(f[n][c]<j)return;
int l=st-1,r=n+1;
while(l+1<r){
int mid=(l+r)>>1;
int now=j-sum[mid-1][c]+sum[st-1][c],use=0,ok=0;//ok:now用得完
if(now<1){r=mid;continue;}
// vector<pii >w=W[mid][c];
for(auto x:W[mid][c]){
use=x.y;
if(now<=f[n-(mid+x.y)][c-x.y]){ok=1; break;}
now-=f[n-(mid+x.y)][c-x.y];
}
if(ok&&use){
if(use)ans.push_back(mp(mid,mid+use));
solve(mid+use+1,i,now,c-use);
return;
}
if(!ok)r=mid;
else l=mid;
}
assert(r==n+1);
}
signed main(){
T=read();
for(int i=0;i<=4;++i)f[0][i]=1;
for(int i=1;i<=30000;++i)
for(int j=0;j<=4;++j){
for(int k=0;k<=j&&k<i;++k){
f[i][j]=(f[i][j]+f[i-k-1][j-k]);
}
}
while(T--){
n=read();c=read();q=read();
for(int i=1;i<=n;++i)p[i]=read();
for(int mid=1;mid<=n;++mid){
vector<pii> w;
for(int k=0;k<=c&&mid+k<=n;++k){
w.push_back(mp(p[mid+k],k));
}
sort(w.begin(),w.end());
for(int k=0;k<=c;++k){
W[mid][k].clear();
for(auto i:w)
if(i.y<=k) W[mid][k].push_back(i);
}
}
for(int i=1;i<=n;++i)
for(int j=1;j<=c;++j){
sum[i][j]=sum[i-1][j];
for(int k=1;k<=j&&i+k<=n;++k)
if(p[i]>p[i+k]){
sum[i][j]+=f[n-i-k][j-k];
}
}
while(q--){
int i=read(),j=read();
if(f[n][c]<j){puts("-1");continue;}
ans.clear();
solve(1,i,j,c);
bool ok=0;
for(auto w:ans){
if(w.x<=i&&i<=w.y){
printf("%lld\n",p[w.y-(i-w.x)]);
ok=1;
}
}
if(!ok)printf("%lld\n",p[i]);
}
}
return 0;
} |
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#pragma GCC optimize("Ofast")
int p[30000];
LLI dp[30001][5];
LLI sub[30001][5][15];
vi order[30001][5];
int main() {
int i,j,k;
int t,n,c,q;
scanf("%d",&t);
while (t--) {
scanf("%d %d %d",&n,&c,&q);
for (i = 0; i < n; i++) scanf("%d",&p[i]),p[i]--;
memset(dp,0,sizeof(dp));
for (i = 0; i <= n; i++) {
for (j = 0; j <= c; j++) order[i][j].clear();
}
for (i = 0; i <= c; i++) dp[n][i] = 1;
for (i = n-1; i >= 0; i--) {
for (j = 0; j <= c; j++) {
vpii vv;
for (k = 0; k <= j; k++) {
if (i+k >= n) break;
dp[i][j] += dp[i+k+1][j-k];
vv.pb(mp(p[i+k],k));
}
sort(vv.begin(),vv.end());
order[i][j].resize(vv.size());
for (k = 0; k < vv.size(); k++) order[i][j][k] = vv[k].second;
sub[i][j][0] = 0;
for (k = 0; k < vv.size(); k++) {
if (vv[k].second == 0) break;
else sub[i][j][0] += dp[i+order[i][j][k]+1][j-order[i][j][k]];
}
}
}
for (i = 1; i < 15; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k <= c; k++) {
if (j+(1 << i) < n) sub[j][k][i] = sub[j][k][i-1]+sub[j+(1 << (i-1))][k][i-1];
}
}
}
int a;
LLI b;
for (i = 0; i < q; i++) {
scanf("%d %I64d",&a,&b),a--;
if (b > dp[0][c]) {
printf("-1\n");
continue;
}
int u = 0,uc = c;
vpii changed;
while (u <= a) {
for (j = 14; j >= 0; j--) {
if (u+(1 << j) >= n) continue;
if ((b > sub[u][uc][j]) && ((dp[u+(1 << j)][uc]+sub[u][uc][j]) >= b))
b -= sub[u][uc][j],u += (1 << j);
}
for (j = 0; j < order[u][uc].size(); j++) {
int v = order[u][uc][j];
if (b > dp[u+v+1][uc-v]) b -= dp[u+v+1][uc-v];
else {
if (v > 0) {
for (k = 0; k <= v; k++) changed.pb(mp(u+k,p[u+v-k]));
}
u += v+1,uc -= v;
break;
}
}
}
for (j = 0; j < changed.size(); j++) {
if (changed[j].first == a) {
printf("%d\n",changed[j].second+1);
break;
}
}
if (j == changed.size()) printf("%d\n",p[a]+1);
}
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std ;
#define MAXN 30007
int n , mx_cost , q ;
int a[ MAXN ] ;
long long cnt[ MAXN ][ 5 ] ;
vector < pair < int , long long > > v[ MAXN ][ 5 ] ;
long long up[ MAXN ][ 5 ] ;
long long down[ MAXN ][ 5 ] ;
void init ( ) {
for ( int i = 0 ; i < 5 ; ++ i ) { cnt[ 0 ][ i ] = 1 ; }
for ( int i = 1 ; i < MAXN ; ++ i ) {
for ( int j = 0 ; j < 5 ; ++ j ) {
for ( int t = 0 ; t <= j ; ++ t ) {
if ( i - ( t + 1 ) >= 0 ) {
cnt[ i ][ j ] += cnt[ i - ( t + 1 ) ][ j - t ] ;
}
}
}
}
}
void input ( ) {
scanf ( "%d%d%d" , &n , &mx_cost , &q ) ;
for ( int i = 1 ; i <= n ; ++ i ) {
scanf ( "%d" , &a[ i ] ) ;
}
}
int get ( int pos , int lft , long long where , long long total ) {
int l , r , mid ;
l = pos ; r = n + 1 ;
while ( r - l > 3 ) {
mid = ( l + r ) / 2 ;
long long sm = down[ mid ][ lft ] - down[ pos - 1 ][ lft ] ;
long long lg = up[ mid ][ lft ] - up[ pos - 1 ][ lft ] ;
if ( sm >= where || lg >= ( total - where + 1 ) ) { r = mid ; }
else { l = mid ; }
}
while ( l <= n ) {
long long sm = down[ l ][ lft ] - down[ pos - 1 ][ lft ] ;
long long lg = up[ l ][ lft ] - up[ pos - 1 ][ lft ] ;
if ( sm < where && lg < ( total - where + 1 ) ) { ++ l ; }
else { break ; }
}
return l ;
}
void solve ( ) {
for ( int i = 1 ; i <= n + 1 ; ++ i ) {
for ( int j = 0 ; j < 5 ; ++ j ) {
up[ i ][ j ] = down[ i ][ j ] = 0 ;
v[ i ][ j ].clear ( ) ;
}
}
for ( int i = 1 ; i <= n ; ++ i ) {
for ( int j = 0 ; j < 5 ; ++ j ) {
for ( int t = 0 ; t <= j ; ++ t ) {
if ( i + t <= n ) {
v[ i ][ j ].push_back ( { t , cnt[ n - ( i + t ) ][ j - t ] } ) ;
}
}
sort ( v[ i ][ j ].begin ( ) , v[ i ][ j ].end ( ) ,
[ & ] ( pair < int , long long > x , pair < int , long long > y ) {
return a[ i + x.first ] < a[ i + y.first ] ;
}
) ;
int sz = v[ i ][ j ].size ( ) ;
bool fl = false ;
for ( int t = 0 ; t < sz ; ++ t ) {
if ( v[ i ][ j ][ t ].first == 0 ) { fl = true ; continue ; }
if ( fl == false ) { down[ i ][ j ] += v[ i ][ j ][ t ].second ; }
else { up[ i ][ j ] += v[ i ][ j ][ t ].second ; }
}
}
}
for ( int i = 1 ; i <= n + 1 ; ++ i ) {
for ( int j = 0 ; j < 5 ; ++ j ) {
up[ i ][ j ] += up[ i - 1 ][ j ] ;
down[ i ][ j ] += down[ i - 1 ][ j ] ;
}
}
while ( q -- ) {
long long where ;
int id ;
scanf ( "%d%lld" , &id , &where ) ;
if ( where > cnt[ n ][ mx_cost ] ) {
printf ( "-1\n" ) ;
continue ;
}
long long total = cnt[ n ][ mx_cost ] ;
int pos = 1 ;
int lft = mx_cost ;
while ( 1 ) {
int nxt_skip = get ( pos , lft , where , total ) ;
if ( nxt_skip > id ) {
printf ( "%d\n" , a[ id ] ) ;
break ;
}
total = cnt[ ( n - nxt_skip + 1 ) ][ lft ] ;
where -= down[ nxt_skip - 1 ][ lft ] - down[ pos - 1 ][ lft ] ;
pos = nxt_skip ;
int sz = v[ pos ][ lft ].size ( ) ;
bool fl = false ;
for ( int i = 0 ; i < sz ; ++ i ) {
if ( where > v[ pos ][ lft ][ i ].second ) {
where -= v[ pos ][ lft ][ i ].second ;
continue ;
}
total = v[ pos ][ lft ][ i ].second ;
int len = v[ pos ][ lft ][ i ].first ;
if ( pos + len >= id ) {
fl = true ;
printf ( "%d\n" , a[ pos + len - ( id - pos ) ] ) ;
}
else { pos += len + 1 ; lft -= len ; }
break ;
}
if ( fl == true ) { break ; }
}
}
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t ;
// cin >> t ;
// t = 1 ;
init ( ) ;
scanf ( "%d" , &t ) ;
while ( t -- ) {
input ( ) ;
solve ( ) ;
}
return 0 ;
}
|
#include <stdio.h>
#include <map>
#include <vector>
int absi(int i) {
if (i > 0) return i;
else return -i;
}
int main()
{
int T;
scanf("%d", &T);
while (T--) {
int n;
long long ans = 0;
scanf("%d", &n);
std::vector<int> tag(n + 1);
std::vector<int> s(n + 1);
std::vector<long long> d(n + 1, 0);
for (int i = 1; i <= n; i++) scanf("%d", &tag[i]);
for (int i = 1; i <= n; i++) scanf("%d", &s[i]);
for (int t = 2; t <= n; t++) {
for (int i = t - 1; i >= 1; i--) {
if (tag[t] != tag[i]) {
long long di = d[i];
long long dt = d[t];
long long ss = absi(s[i] - s[t]);
d[t] = std::max(di + ss, dt);
d[i] = std::max(dt + ss, di);
ans = std::max(ans, d[i]);
ans = std::max(ans, d[t]);
}
}
}
printf("%lld\n", ans);
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
long long dp[5005];
int tag[5005],s[5005];
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&tag[i]);
for (int i=1;i<=n;i++)
scanf("%d",&s[i]);
for (int i=1;i<=n;i++)
{
dp[i]=0;
for (int j=i-1;j>0;j--)
{
if (tag[i]==tag[j])
continue;
long long tmp=dp[i]+abs(s[i]-s[j]);
dp[i]=max(dp[i],dp[j]+abs(s[i]-s[j]));
dp[j]=max(dp[j],tmp);
}
}
printf("%I64d\n",*max_element(dp+1,dp+n+1));
}
}
|
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 5e3 + 10;
const LL inf = 1e15;
int n;
LL f[N], a[N], ne[N];
int tag[N];
int main()
{
int __;
cin >> __;
while (__ --)
{
cin >> n;
for (int i = 1; i <= n; i ++) cin >> tag[i];
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++) f[i] = 0;
for (int i = 2; i <= n; i ++)
{
for (int j = 1; j <= n; j ++) ne[j] = 0;
LL maxn = 0;
for (int j = 1; j < i; j ++)
{
LL g = 0;
if (tag[j] == tag[i])
g = -inf;
else
g = f[j] + abs(a[j] - a[i]);
maxn = max(maxn, g);
ne[j - 1] = g;
}
LL p = -inf;
for (int j = i - 1; j >= 1; j --)
{
p = max(p, ne[j]);
if (tag[i] == tag[j]) continue;
f[j] = max(f[j], p + abs(a[j] - a[i]));
}
f[i] = max(f[i], maxn);
}
LL res = 0;
for (int i = 1; i <= n; i ++)
res = max(res, f[i]);
cout << res << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
#define mkp make_pair
#define pb push_back
#define fi first
#define se second
#define lb(x) ((x)&(-(x)))
#define SZ(x) ((int)((x).size()))
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN=5e3+10;
int n;
int tag[MAXN],s[MAXN];
ll dp[MAXN];
void solve()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&tag[i]);
for(int i=1;i<=n;i++) scanf("%d",&s[i]);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
for(int j=i-1;j>=1;j--)
{
if(tag[i]==tag[j]) continue;
int x=abs(s[i]-s[j]);
ll v1=dp[i],v2=dp[j];
dp[i]=max(dp[i],v2+x);
dp[j]=max(dp[j],v1+x);
}
ll ans=0;
for(int i=1;i<=n;i++)
ans=max(ans,dp[i]);
printf("%lld\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--) solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
#define pb push_back
#define ppp pop_back
#define pii pair<int,int>
#define fi first
#define se second
#define stie std::tie
#define vec vector
#define forn(i, l, r) for (int i=l; i<=r; i++)
#define emp empty
#define beg begin
#define ins insert
#define cle clear
#define era erase
#define que queue
#define pque priority_queue
#define mset multiset
#define deq deque
#define sta stack
#define con const
#define rsz resize
#define ass assign
#define lowb lower_bound
#define uppb upper_bound
template<class T> void mini(T& a, T b) { a = min(a, b); }
template<class T> void maxi(T& a, T b) { a = max(a, b); }
template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> a) { return out << a.first << " " << a.second; }
template<class T> ostream& operator<<(ostream& out, vector<T> a) { for (auto& i : a) out << i << " "; return out; }
template<class T> ostream& operator<<(ostream& out, set<T> a) { for (auto& i : a) out << i << " "; return out; }
template<class T> ostream& operator<<(ostream& out, multiset<T> a) { for (auto& i : a) out << i << " "; return out; }
template<class T1, class T2> ostream& operator<<(ostream& out, map<T1, T2> a) { for (auto& i : a) out << i << "\n"; return out; }
template<class T1, class T2> ostream& operator<<(ostream& out, unordered_map<T1, T2> a) { for (auto& i : a) out << i << "\n"; return out; }
template<class T> ostream& operator<<(ostream& out, queue<T> a) { while (!a.empty()) { out << a.front() << " "; a.pop(); } return out; }
template<class T> ostream& operator<<(ostream& out, deque<T> a) { while (!a.empty()) { out << a.front() << " "; a.pop_front(); } return out; }
template<class T> ostream& operator<<(ostream& out, priority_queue<T> a) { while (!a.empty()) { out << a.top() << "\n"; a.pop(); } return out; }
template<class T> void out(T a) { cout << a << endl; }
template<class T1, class T2> void out(T1 a, T2 b) { cout << a << " " << b << endl; }
template<class T1, class T2, class T3> void out(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << endl; }
template<class T1, class T2, class T3, class T4> void out(T1 a, T2 b, T3 c, T4 d) { cout << a << " " << b << " " << c << " " << d << endl; }
template<class T> void out(vector<vector<T>> a) { for (vector<T> i : a) out(i); }
void out() { cout << "OK" << endl; }
template<class T> void sort(vector<T>& a) { sort(a.begin(), a.end()); }
template<class T> void uniq(vector<T>& a) { sort(a); a.erase(unique(a.begin(), a.end()), a.end()); }
template<class T> vector<T> set_int(vector<T> a, vector<T> b) { sort(a); sort(b); vector<T> res; set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res)); return res; }
clock_t start_time; void start_timer() { start_time = clock(); }
double get_time() { return (double)(clock() - start_time) / CLOCKS_PER_SEC; }
#if __SIZEOF_INT128__ >= 16
typedef __int128 LL;
istream& operator>>(istream& in, __int128& a) { int64_t b; cin >> b; a = b; return in; }
ostream& operator<<(ostream& out, const __int128 a) {
unsigned __int128 b = a < 0 ? -a : a; char buf[128]; char* c = end(buf); do { --c; *c = "0123456789"[b % 10]; b /= 10; } while (b);
if (a < 0) { --c; *c = '-'; } int len = end(buf) - c; out.rdbuf()->sputn(c, len); return out;
}
#endif
con int N = 5005;
int t[N];
ll a[N];
ll dp[N];
void Solve() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
forn(i, 1, n) cin >> t[i];
forn(i, 1, n) cin >> a[i];
forn(i, 1, n) dp[i] = 0;
forn(i, 1, n) {
for (int j=i-1; j>=1; j--) {
if (t[i] == t[j]) continue;
ll x = dp[i];
ll y = dp[j];
ll z = abs(a[i] - a[j]);
maxi(dp[i], y + z);
maxi(dp[j], x + z);
}
}
ll ans = 0;
forn(i, 1, n) {
maxi(ans, dp[i]);
}
out(ans);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
start_timer();
Solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<long long> s(n), tag(n), dp(n, 0);
for (int i = 0; i < n; ++i)
cin >> tag[i];
for (int i = 0; i < n; ++i)
cin >> s[i];
for (int j = 1; j < n; ++j) {
for (int i = j - 1; i >= 0; --i) {
if (tag[i] == tag[j]) continue;
long long dpi = dp[i], dpj = dp[j], p = abs(s[i] - s[j]);
dp[i] = max(dp[i], dpj + p);
dp[j] = max(dp[j], dpi + p);
}
}
cout << *max_element(dp.begin(), dp.end()) << endl;
}
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline ll MIN(ll x,ll y){
return x<y?x:y;
}
inline ll MAX(ll x,ll y){
return x<y?y:x;
}
inline ll ABS(ll x){
return x>=0?x:-x;
}
const ll mxn=5010;
ll I,i,j,k;
ll x,y,z,xx,xy,xz,yx,yy,yz;
bool blx,bly,blz;
ll T,m,n,p;
ll t[mxn],s[mxn];
ll ans;
ll f[mxn];
int main(){
scanf("%lld",&T);
for(I=1;I<=T;I++){
scanf("%lld",&n);
for(i=1;i<=n;i++){
scanf("%lld",&t[i]);
}
for(i=1;i<=n;i++){
scanf("%lld",&s[i]);
}
for(i=1;i<=n;i++){
f[i]=0;
}
for(i=2;i<=n;i++){
for(j=1;j<i;j++){
if(t[j]!=t[i]){
f[i]=MAX(f[i],f[j]+ABS(s[i]-s[j]));
}
}
x=0;
for(j=i-1;j;j--){
if(t[j]!=t[i]){
y=f[j];
f[j]=MAX(f[j],x+ABS(s[i]-s[j]));
x=MAX(x,y+ABS(s[i]-s[j]));
}
}
}
ans=0;
for(i=1;i<=n;i++){
ans=MAX(ans,f[i]);
}
printf("%lld\n",ans);
}
return 0;
} |
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 5010
using namespace std;
int T, n, t[N];
typedef long long ll;
ll f[N], ans, s[N];
int main(){
// freopen("funny.in", "r", stdin);
scanf("%d", &T);
while (T--){
scanf("%d", &n);
for (int i=1; i<=n; i++) scanf("%d", &t[i]);
for (int i=1; i<=n; i++) scanf("%lld", &s[i]);
ans=0;
for (int i=1; i<=n; i++){
f[i]=0;
for (int j=i-1; j; j--){
if (t[i]!=t[j]){
ll ls=f[j]+abs(s[i]-s[j]);
f[j]=max(f[j], f[i]+abs(s[i]-s[j]));
ans=max(ans, f[j]);
f[i]=max(f[i], ls);
}
}
ans=max(ans, f[i]);
}
printf("%lld\n", ans);
}
return 0;
}
|
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define endl '\n'
using namespace std;
const int N=5010;
int tag[N];
ll s[N];
ll dp[2][N][2];
ll dp1[2][N][2];
ll mx[N];
ll mx1[N];
void solve(){
int n;cin>>n;
for (int i=1;i<=n;i++) cin>>tag[i];
for (int i=1;i<=n;i++) cin>>s[i];
for (int i=0;i<2;i++){
for (int j=0;j<=n;j++){
dp[i][j][0]=dp[i][j][1]=-1000000000000000001ll;
dp[i][j][0]=dp[i][j][1]=0;
}
}
for (int i=1;i<=n;i++){
mx[i]=-1000000000000000001ll;
mx1[i]=mx[i];
}
ll res=0;
for (int i=1;i<=n;i++){
for (int j=0;j<=n;j++){
for (int t=0;t<2;t++){
dp[i%2][j][t]=-1000000000000000001ll;
dp1[i%2][j][t]=0;
}
}
for (int j=i-1;j>=1;j--){
dp1[i%2][j][0]=max({dp1[(i-1)%2][j][0],dp[(i-1)%2][j][1],mx[j],0ll});
dp1[i%2][j][1]=max({dp1[i%2][j+1][1],dp[i%2][j+1][0],0ll});
if (tag[i]==tag[j]) continue;
///calc dp[i][j]
dp[i%2][j][0]=max({dp1[(i-1)%2][j][0],dp[(i-1)%2][j][1],mx[j],0ll})+abs(s[i]-s[j]);
///calc dp[j][i]
dp[i%2][j][1]=max({dp1[i%2][j+1][1],dp[i%2][j+1][0],0ll})+abs(s[i]-s[j]);
mx[i]=max(mx[i],dp[i%2][j][0]);
mx1[j]=max(mx1[j],dp[i%2][j][1]);
res=max(res,dp[i%2][j][0]);
res=max(res,dp[i%2][j][1]);
}
}
// for (int i=1;i<=n;i++){
// for (int j=1;j<=n;j++){
// if (tag[i]!=tag[j]) res=max(res,dp[i][j]);
// }
// }
cout<<res<<endl;
}
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
int tt;cin>>tt;
while (tt--){
solve();
}
return 0;
}
|
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 5005;
int n, tag[MAXN], s[MAXN];
ll dp[MAXN];
void solve(){
scanf("%d",&n);
for(int i = 1 ; i <= n ; ++i) scanf("%d",&tag[i]);
for(int i = 1 ; i <= n ; ++i) scanf("%d",&s[i]);
for(int i = 1 ; i <= n ; ++i) dp[i] = 0;
for(int i = 2 ; i <= n ; ++i){
for(int j = i - 1 ; j ; --j){
if(tag[i]==tag[j]) continue;
ll dpi = dp[i], dpj = dp[j], p = abs(s[i]-s[j]);
dp[i] = max(dp[i],dpj+p);
dp[j] = max(dp[j],dpi+p);
}
}
printf("%lld\n",*max_element(dp+1,dp+1+n));
}
int main(){
int t;
scanf("%d",&t);
while(t--) solve();
} |
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
template<typename T> using V = vector<T>;
template<typename T> using VV = vector<vector<T>>;
template<typename T> using PQ = priority_queue<T>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define all(v) (v).begin(),(v).end()
#define siz(v) (ll)(v).size()
#define rep(i,a,n) for(ll i=a;i<(ll)(n);++i)
#define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i)
#define ENDL '\n'
typedef pair<int,int> Pi;
typedef pair<ll,ll> PL;
constexpr ll mod = 1000000007; // 998244353;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 +99);
const ld PI = acos((ld)-1);
constexpr ll dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;}
template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;}
template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;}
inline void Yes() { cout << "Yes" << ENDL; }
inline void No() { cout << "No" << ENDL; }
inline void YES() { cout << "YES" << ENDL; }
inline void NO() { cout << "NO" << ENDL; }
template<typename T,typename Y> inline T mpow(T a, Y n) {
T res = 1;
for(;n;n>>=1) {
if (n & 1) res = res * a;
a = a * a;
}
return res;
}
template <typename T>
vector<T> finddivisor(T x) { //整数xの約数(xを含む)
vector<T> divisor;
for(T i = 1; (i * i) <= x; i++) {
if(x % i == 0) {
divisor.push_back(i);
if(i * i != x) { divisor.push_back(x / i);}
}
}
sort(divisor.begin(), divisor.end());
return divisor;
}
template <typename T> V<T> prefix_sum(const V<T>& v) {
int n = v.size();
V<T> ret(n + 1);
rep(i, 0, n) ret[i + 1] = ret[i] + v[i];
return ret;
}
template<typename T>
T rand(T l,T r){
static random_device rd;
static mt19937 g(rd());
return uniform_int_distribution<T>(l,r)(g);
}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
for(auto&& x: vec) is >> x;
return is;
}
template<typename T,typename Y>
ostream& operator<<(ostream& os,const pair<T,Y>& p){
return os<<"{"<<p.fs<<","<<p.sc<<"}";
}
template<typename T> ostream& operator<<(ostream& os,const V<T>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
template<typename ...Args>
void debug(Args&... args){
for(auto const& x:{args...}){
cerr<<x<<' ';
}
cerr<<ENDL;
}
ll dp[5010]={};
signed main(){
cin.tie(0);cerr.tie(0);ios::sync_with_stdio(false);
cout<<fixed<<setprecision(20);
int qq;cin>>qq;
while(qq--){
ll n;cin>>n;
V<ll> t(n),s(n);cin>>t>>s;
rep(i,0,n)dp[i]=0;
rep(i,0,n){
repr(j,0,i)if(t[i]!=t[j]){
ll dpi=dp[i],dpj=dp[j];
chmax(dp[j],dpi+abs(s[i]-s[j]));
chmax(dp[i],dpj+abs(s[i]-s[j]));
}
}
ll ans=0;
rep(i,0,n)chmax(ans,dp[i]);
cout<<ans<<ENDL;
}
}
//(・_・)(・_・)(・_・)(・_・)
//CHECK overflow,what to output?
//any other simpler approach? |
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
void solve() {
int n; cin >> n;
vector<int> tag(n), s(n);
for (int &x : tag) cin >> x;
for (int &x : s) cin >> x;
vector<ll> dp(n);
// for (int i = 0; i < n; i++) dp[i] = s[i];
for (int i = 1; i < n; i++) {
for (int j = i - 1; j >= 0; j--) {
if (tag[i] == tag[j]) continue;
ll ndpi = dp[j] + abs(s[i] - s[j]);
ll ndpj = dp[i] + abs(s[i] - s[j]);
dp[i] = max(dp[i], ndpi);
dp[j] = max(dp[j], ndpj);
dbg(i, j, dp);
}
}
cout << *max_element(dp.begin(), dp.end()) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.