This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(int i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18;
struct FenwickTree{
int n;
vector<long long> bit;
FenwickTree(){}
FenwickTree(int n): n(n), bit(n+1, 0LL){}
void update(int idx, long long v){
if(idx <= 0) return;
for(;idx<=n;idx+=(idx&-idx)) bit[idx]+=v;
}
long long getSum(int idx){
if(idx <= 0) return 0;
long long res = 0;
for(;idx;idx-=idx&-idx) res += bit[idx];
return res;
}
};
int n, k, q, a[maxn], p[maxn], res[maxn];
vector<int> pos[maxn];
vector<pair<int,int>> qu[maxn];
unordered_map<int, ll> mp[maxn];
FenwickTree bit;
ll ans;
ll count_inv(int x, int y){
if(mp[x].find(y) != mp[x].end()) return mp[x][y];
ll res = 0;
int i = 0;
for(int j=0;j<pos[y].size();j++){
while(i < pos[x].size() && pos[x][i] < pos[y][j]) i++;
res += i;
}
mp[x][y] = res;
return res;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(__file_name ".inp", "r")){
freopen(__file_name ".inp","r",stdin);
freopen(__file_name ".out","w",stdout);
}
// code here
cin >> n >> k >> q;
bit = FenwickTree(k);
f1(i,n) {
cin >> a[i];
ans += bit.getSum(k) - bit.getSum(a[i]);
bit.update(a[i], 1);
pos[a[i]].push_back(i);
p[i] = i;
// pp[i] = i;
}
// cout << ans << endl;
// qu[i] = {j, idx} -> number of elements j that is before i
f1(i,q){
int j; cin >> j;
// count(x, y): number of elements x that is before y
// answer += -pos[p[j]].size()*pos[p[j+1]].size() + 2*count_inv(p[j+1], p[j])
// or answer += pos[p[j]].size()*pos[p[j+1]].size() - 2*count_inv(p[j], p[j+1])
// answer += count_inv(p[j], p[j+1]) - count_inv(p[j+1], p[j])
if(p[j] < p[j+1]){
ans += -1LL*pos[p[j]].size()*pos[p[j+1]].size() + 2*count_inv(p[j], p[j+1]);
}else{
ans += +1LL * pos[p[j]].size()*pos[p[j+1]].size() - 2*count_inv(p[j+1], p[j]);
}
swap(p[j], p[j+1]);
cout << ans;el;
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'long long int count_inv(int, int)':
Main.cpp:40:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int j=0;j<pos[y].size();j++){
| ~^~~~~~~~~~~~~~
Main.cpp:41:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | while(i < pos[x].size() && pos[x][i] < pos[y][j]) i++;
| ~~^~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen(__file_name ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | freopen(__file_name ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |