QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#465942 | #1877. Matryoshka Dolls | GenshinImpactsFault | TL | 5ms | 26172kb | C++14 | 3.0kb | 2024-07-07 14:00:11 | 2024-07-07 14:00:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 500010, BLO = 3;
int n, m;
int a[N], rev[N];
int pre[N], nxt[N], spre[N], snxt[N];
bool vis[N];
ll ans[N], cur, scur;
vector<pair<pair<int, int>, int> > q[N / BLO + 10];
vector<pair<int, int> > vec[N / BLO + 10];
int t1[N], lt1, t2[N], lt2, t[N], lt;
ll solve(int l, int r) {
int bll = (l - 1) / BLO + 1, blr = (r - 1) / BLO + 1;
ll res = 0;
if(bll == blr) {
lt = 0;
for(auto v : vec[bll]) {
if(v.second >= l && v.second <= r) t[++lt] = v.second;
}
for(int i = 2; i <= lt; i++) res += abs(t[i] - t[i - 1]);
return res;
}
lt1 = lt2 = 0;
for(auto v : vec[bll]) {
if(v.second >= l) t1[++lt1] = v.second;
}
for(auto v : vec[blr]) {
if(v.second <= r) t2[++lt2] = v.second;
}
lt = 0;
for(int i = 1, j = 1; i <= lt1 || j <= lt2;) {
if(j > lt2 || (i <= lt1 && a[t1[i]] < a[t2[j]])) {
t[++lt] = t1[i], ++i;
}
else {
t[++lt] = t2[j], ++j;
}
}
for(int i = 2; i <= lt; i++) res += abs(t[i] - t[i - 1]);
return res;
}
void build() {
cur = 0;
int tmp = 0;
pre[0] = 0, nxt[n + 1] = n + 1;
for(int j = 1; j <= n; j++) {
if(!vis[j]) continue;
if(tmp != 0) {
cur += abs(rev[tmp] - rev[j]);
}
nxt[tmp] = j, pre[j] = tmp;
tmp = j;
}
nxt[tmp] = n + 1, pre[n + 1] = tmp;
for(int j = 0; j <= n + 1; j++) {
spre[j] = pre[j], snxt[j] = nxt[j];
}
lt = 0;
}
void erase(int x, int flag) {
x = a[x];
if(pre[x] != 0) {
cur -= abs(rev[pre[x]] - rev[x]);
}
if(nxt[x] != n + 1) {
cur -= abs(rev[nxt[x]] - rev[x]);
}
if(pre[x] != 0 && nxt[x] != n + 1) {
cur += abs(rev[nxt[x]] - rev[pre[x]]);
}
if(flag) {
t[++lt] = x, t[++lt] = pre[x], t[++lt] = nxt[x];
}
nxt[pre[x]] = nxt[x], pre[nxt[x]] = pre[x];
if(!flag) {
snxt[pre[x]] = nxt[x], spre[nxt[x]] = pre[x];
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(nullptr);
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> a[i], rev[a[i]] = i;
}
for(int i = 1; i <= n; i++) {
vec[(i - 1) / BLO + 1].push_back({a[i], i});
}
for(int i = 1; i <= (n - 1) / BLO + 1; i++) {
sort(vec[i].begin(), vec[i].end());
}
for(int i = 1; i <= m; i++) {
int l, r; cin >> l >> r;
if(r - l + 1 <= BLO) {
ans[i] = solve(l, r);
continue;
}
q[(l - 1) / BLO + 1].push_back({{l, r}, i});
}
for(int i = 1; i <= (n - 1) / BLO + 1; i++) {
sort(q[i].begin(), q[i].end(), [](pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
return x.first.second > y.first.second;
});
for(int j = 1; j <= n; j++) vis[j] = 0;
for(int j = (i - 1) * BLO + 1; j <= n; j++) vis[a[j]] = 1;
build();
int l = (i - 1) * BLO + 1, r = n;
for(auto v : q[i]) {
for(; r > v.first.second; erase(r, 0), --r);
scur = cur;
for(; l < v.first.first; erase(l, 1), ++l);
ans[v.second] = cur;
cur = scur;
l = (i - 1) * BLO + 1;
for(; lt; --lt) {
pre[t[lt]] = spre[t[lt]];
nxt[t[lt]] = snxt[t[lt]];
}
}
}
for(int i = 1; i <= m; i++) cout << ans[i] << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 5ms
memory: 26092kb
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: 26172kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Time Limit Exceeded
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:
4999950000