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 <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 4;
const int INF = 1e9;
const int block = 100;
const int N = 1e6 + 4;
struct seg_tree {
vector<int> t;
seg_tree() {
t.resize(4 * maxn);
}
void update(int pos, int v = 1, int l = 1, int r = maxn - 1) {
if (l == r) {
t[v]++;
return;
}
int m = (l + r) / 2;
if (pos <= m) update(pos, 2 * v, l, m);
else update(pos, 2 * v + 1, m + 1, r);
t[v] = t[2 * v] + t[2 * v + 1];
}
int get(int l, int r, int v = 1, int tl = 1, int tr = maxn - 1) {
if (tl > r || tr < l) return 0;
if (tl >= l && tr <= r) return t[v];
int m = (tl + tr) / 2;
int L = get(l, r, 2 * v, tl, m);
int R = get(l, r, 2 * v + 1, m + 1, tr);
return L + R;
}
} IT;
int n, k, q, a[maxn], p[maxn], sum[maxn], delta[N];
vector<int> pos[maxn];
vector<pair<int,int>> adj[maxn];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> k >> q;
int res = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
res += IT.get(a[i] + 1, n);
pos[a[i]].pb(i);
IT.update(a[i]);
}
for (int i = 1; i <= n; i++)
p[i] = i;
for (int i = 1; i <= q; i++) {
int j; cin >> j;
int x = p[j], y = p[j + 1];
if (pos[x].size() <= block && pos[y].size() <= block) {
int l = 0, inv = 0;
for (int r = 0; r < (pos[x].size()); r++) {
while (l + 1 < (pos[y].size()) && pos[y][l + 1] < pos[x][r])
l++;
if (l < (pos[y].size()) && pos[y][l] < pos[x][r])
inv += l + 1;
}
delta[i] = -2 * inv + pos[x].size() * pos[y].size();
}
else {
if (pos[x].size() > block) adj[x].pb({y, i});
else adj[y].pb({x, -i});
}
swap(p[j], p[j + 1]);
}
for (int x = 1; x <= n; x++) {
if (pos[x].size() <= block) continue;
memset(sum, 0, sizeof sum);
for (auto i : pos[x])
sum[i] = 1;
for (int i = 1; i <= n; i++)
sum[i] += sum[i - 1];
sort(adj[x].begin(), adj[x].end());
int i = 0;
while (i < (adj[x].size())) {
int y = adj[x][i].fi, inv = pos[x].size() * pos[y].size();
for (int j : pos[y])
inv -= sum[j];
inv = -2 * inv + pos[x].size() * pos[y].size();
while (adj[x][i].fi == y && i < (adj[x].size())) {
if (adj[x][i].se > 0) delta[adj[x][i].se] = inv;
else delta[-adj[x][i].se] = -inv;
i++;
}
}
}
for (int i = 1; i <= q; i++)
res += delta[i], cout << res << '\n';
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:67:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int r = 0; r < (pos[x].size()); r++) {
| ~~^~~~~~~~~~~~~~~~~
Main.cpp:68:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | while (l + 1 < (pos[y].size()) && pos[y][l + 1] < pos[x][r])
| ~~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:70:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | if (l < (pos[y].size()) && pos[y][l] < pos[x][r])
| ~~^~~~~~~~~~~~~~~~~
Main.cpp:90:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | while (i < (adj[x].size())) {
| ~~^~~~~~~~~~~~~~~~~
Main.cpp:95:43: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | while (adj[x][i].fi == y && i < (adj[x].size())) {
| ~~^~~~~~~~~~~~~~~~~
# | 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... |