QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#455264 | #1877. Matryoshka Dolls | makrav | WA | 297ms | 301832kb | C++20 | 5.1kb | 2024-06-26 04:02:45 | 2024-06-26 04:02:45 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define all(a) (a).begin(), (a).end()
#define sz(a) (int) a.size()
#define pb push_back
#define ff first
#define sc second
#define pii pair<int, int>
#define f(i, l, r) for (int i = (l); i < (r); i++)
#define double ld
const int mod = 998244353;
const int K = 550;
const int N = 100010;
int ans_block[(N + K) / K][K][K];
int SPMIN[(N + K) / K][10][K], SPMAX[(100010 + K) / K][10][K];
int nxt[(N + K) / K][100010];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q; cin >> n >> q;
vector<int> a(n);
vector<int> Log2(n + 1, 0);
for (int i = 2; i <= n; i++) Log2[i] = Log2[i / 2] + 1;
for (int i = 0; i < n; i++) cin >> a[i];
vector<pair<int, int>> b(n);
for (int i = 0; i < n; i++) {
b[i] = {a[i], i};
}
sort(all(b));
vector<int> pos(n + 1);
for (int i = 0; i < n; i++) {
pos[a[i]] = i;
}
vector<int> bl(n, -1);
int ind= -1;
for (int i = 0; i < n; i += K) {
ind++;
vector<int> ppos;
for (int j = i; j < min(n, i + K); j++) {
bl[b[j].sc] = ind;
ppos.pb(b[j].sc);
}
nxt[ind][n] = n + 1;
int INDD = sz(ppos) - 1;
for (int j = n - 1; j >= 0; j--) {
nxt[ind][j] = nxt[ind][j + 1];
if (bl[j] == ind) nxt[ind][j] = INDD--;
}
sort(all(ppos));
vector<vector<int>> bd_right(sz(ppos), vector<int>(sz(ppos))), bd_left(sz(ppos), vector<int>(sz(ppos)));
for (int I = 0; I < sz(ppos) - 1; I++) {
bd_right[I][I] = 1e9;
bd_left[I][I] = 1e9;
for (int j = I + 1; j < sz(ppos); j++) {
bd_right[I][j] = bd_right[I][j - 1];
bd_left[I][j] = bd_left[I][j - 1];
if (a[ppos[j]] > a[ppos[I]] && bd_right[I][j] > a[ppos[j]] - a[ppos[I]]) {
bd_right[I][j] = a[ppos[j]] - a[ppos[I]];
}
if (a[ppos[j]] < a[ppos[I]] && bd_left[I][j] > a[ppos[I]] - a[ppos[j]]) {
bd_left[I][j] = a[ppos[I]] - a[ppos[j]];
}
}
}
for (int I = 0; I < sz(ppos); I++) {
SPMIN[ind][0][I] = a[ppos[I]];
SPMAX[ind][0][I] = a[ppos[I]];
}
for (int j = 1; j < 10; j++) {
for (int I = 0; I + (1 << j) - 1 < sz(ppos); I++) {
SPMIN[ind][j][I] = min(SPMIN[ind][j - 1][I], SPMIN[ind][j - 1][I + (1 << (j - 1))]);
SPMAX[ind][j][I] = max(SPMAX[ind][j - 1][I], SPMAX[ind][j - 1][I + (1 << (j - 1))]);
}
}
vector<vector<int>> ans(sz(ppos), vector<int>(sz(ppos)));
for (int I = sz(ppos) - 1; I >= 0; I--) {
ans[I][I] = 0;
ans_block[ind][I][I] = ans[I][I];
for (int j = I - 1; j >= 0; j--) {
ans[j][I] = ans[j + 1][I];
int rg = (bd_right[j][I] == 1e9 ? -1 : a[ppos[j]] + bd_right[j][I]);
int lf = (bd_left[j][I] == 1e9 ? -1 : a[ppos[j]] - bd_left[j][I]);
if (rg != -1) ans[j][I] += abs(pos[rg] - pos[a[ppos[j]]]);
if (lf != -1) ans[j][I] += abs(pos[lf] - pos[a[ppos[j]]]);
if (lf != -1 && rg != -1) ans[j][I] -= abs(pos[lf] - pos[rg]);
ans_block[ind][j][I] = ans[j][I];
}
}
}
vector<vector<int>> qrs(q);
for (int i = 0; i < q; i++) {
int l, r; cin >> l >> r; l--; r--;
qrs[i] = {l, r, i};
}
sort(all(qrs), [&](vector<int> &x, vector<int> &y) {
return x[1] < y[1];
});
auto getmax = [&](int block, int l, int r) {
int lg = Log2[r - l + 1];
return max(SPMAX[block][lg][l], SPMAX[block][lg][r - (1 << lg) + 1]);
};
auto getmin = [&](int block, int l, int r) {
int lg = Log2[r - l + 1];
return min(SPMIN[block][lg][l], SPMIN[block][lg][r - (1 << lg) + 1]);
};
int IND = 0;
ind++;
vector<int> last(ind, -1);
vector<int> ANSW(q);
vector<int> cnt(ind);
for (int r = 0; r < n; r++) {
last[bl[r]] = cnt[bl[r]]++;
//continue;
while (IND < qrs.size() && qrs[IND][1] == r) {
int L = qrs[IND][0], R = qrs[IND][1];
ll ANS = 0;
for (int j = 0; j < ind; j++) {
if (nxt[j][L] <= last[j] && last[j] != -1) {
ANS += ans_block[j][nxt[j][L]][last[j]];
}
}
vector<int> lol;
int LAST = -1;
for (int j = 0; j < ind; j++) {
if (nxt[j][L] <= last[j]&&last[j] > -1) {
int RG = getmax(j, nxt[j][L], last[j]), LF = getmin(j, nxt[j][L], last[j]);
if (LAST != -1) ANS += abs(pos[LF] - pos[LAST]);
LAST = RG;
}
}
ANSW[qrs[IND][2]] = ANS;
IND++;
}
}
for (auto &u : ANSW) cout << u << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 9632kb
input:
5 5 1 5 2 4 3 1 5 1 4 1 3 1 2 1 1
output:
7 5 3 1 0
result:
ok 5 number(s): "7 5 3 1 0"
Test #2:
score: 0
Accepted
time: 0ms
memory: 9748kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 297ms
memory: 301832kb
input:
100000 1 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 ...
output:
704982704
result:
wrong answer 1st numbers differ - expected: '4999950000', found: '704982704'