This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using vi = vector <int>;
// using ii = pair <ll, ll>;
// using vii = vector <ii>;
const ll MAXN = 1E6+16, INF = ll(1E18)+16;
priority_queue <ll> pq[MAXN];
struct Fenwick {
vll tree;
ll n;
Fenwick (ll n): tree(n, 0), n(n) {}
void update (ll id, ll val) {
for (; id < n; id |= id+1) tree[id] += val;
}
ll query (ll id) {
ll ans = 0;
for (; id >= 0; id &= id+1, id--) ans += tree[id];
return ans;
}
};
#define mid ((l+r)>>1)
#define off (2*(mid-l+1))
struct SegTree {
struct Node {
ll val;
bool hasL;
ll lazy;
bool isIdem;
Node (): isIdem(true) {}
Node (ll val): val(val), hasL(false), lazy(0), isIdem(false) {}
Node operator+ (const Node &o) const {
if (isIdem) return o;
if (o.isIdem) return *this;
return Node(max(val, o.val));
}
};
vector <Node> tree;
ll n;
SegTree (ll n): tree(2*n, Node(0)), n(n) {}
void push (ll l, ll r, ll id) {
if (!tree[id].hasL) return;
tree[id].val += tree[id].lazy;
if (l < r) {
tree[id+1].hasL = true;
tree[id+1].lazy += tree[id].lazy;
tree[id+off].hasL = true;
tree[id+off].lazy += tree[id].lazy;
}
tree[id].hasL = false;
tree[id].lazy = 0;
}
void set (ll at, ll val) { set(at, val, 0, n-1, 0); }
void set (ll at, ll val, ll l, ll r, ll id) {
push(l, r, id);
if (at < l || r < at) return;
if (at <= l && r <= at) {
tree[id].val = val;
push(l, r, id);
return;
}
set(at, val, l, mid, id+1);
set(at, val, mid+1, r, id+off);
tree[id] = tree[id+1] + tree[id+off];
}
void update (ll ql, ll qr, ll val) { update(ql, qr, val, 0, n-1, 0); }
void update (ll ql, ll qr, ll val, ll l, ll r, ll id) {
push(l, r, id);
if (qr < l || r < ql) return;
if (ql <= l && r <= qr) {
tree[id].hasL = true;
tree[id].lazy = val;
push(l, r, id);
return;
}
update(ql, qr, val, l, mid, id+1);
update(ql, qr, val, mid+1, r, id+off);
tree[id] = tree[id+1] + tree[id+off];
}
ll query (ll ql, ll qr) { return query(ql, qr, 0, n-1, 0).val; }
Node query (ll ql, ll qr, ll l, ll r, ll id) {
push(l, r, id);
if (qr < l || r < ql) return Node();
if (ql <= l && r <= qr) return tree[id];
return query(ql, qr, l, mid, id+1) + query(ql, qr, mid+1, r, id+off);
}
};
vi countScans (vi a, vi wh, vi val) {
vll ve(a.begin(), a.end());
ll n = a.size();
ll Q = wh.size();
{vll th = ve;
for (int i : val) th.push_back(i);
sort(th.begin(), th.end());
th.resize(unique(th.begin(), th.end()) - th.begin());
for (ll &i : ve) i = lower_bound(th.begin(), th.end(), i) - th.begin();
for (int &i : val) i = lower_bound(th.begin(), th.end(), i) - th.begin();}
Fenwick ftFreq(MAXN);
SegTree stAns(MAXN);
vi ans;
for (ll i = 0; i < n; i++) {
pq[ve[i]].push(i);
ftFreq.update(ve[i], +1);
}
for (ll i = 0; i < MAXN; i++) {
stAns.set(i, -INF);
}
auto redo = [&](ll i) {
while (pq[i].size() && ve[pq[i].top()] != i) pq[i].pop();
stAns.set(i, -INF);
if (!pq[i].size()) return;
stAns.set(i, pq[i].top()+1 - ftFreq.query(i));
};
for (ll i = 0; i < n+wh.size(); i++) redo(i);
for (ll q = 0; q < Q; q++) {
ll val1 = ve[wh[q]], val2 = val[q];
ftFreq.update(ve[wh[q]], -1);
ve[wh[q]] = val[q];
ftFreq.update(ve[wh[q]], +1);
pq[val[q]].push(wh[q]);
stAns.update(val1, MAXN-4, +1);
stAns.update(val2, MAXN-4, -1);
redo(val1);
redo(val2);
ans.push_back(stAns.query(0, MAXN-8));
}
return ans;
}
Compilation message (stderr)
bubblesort2.cpp: In function 'vi countScans(vi, vi, vi)':
bubblesort2.cpp:130:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'long long unsigned int' [-Wsign-compare]
130 | for (ll i = 0; i < n+wh.size(); i++) redo(i);
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |