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("O3","unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define all(v) v.begin(),v.end()
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) a * b / gcd(a, b)
mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}, dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
bool is_square(long long n){long long k = sqrt(n);return k * k == n;}
bool is_prime(long long n){if (n <= 3) return n > 1;if (n % 2 == 0 || n % 3 == 0) return false;for (int i = 5; i <= sqrt(n); i += 6)if (n % i == 0 || n % (i + 2) == 0) return false;return true;}
long long bin_pow(long long a, long long b, long long MOD){long long ans = 1;if (a >= MOD) a %= MOD;while (b){if (b & 1) ans = ans * a % MOD;a = a * a % MOD;b /= 2;}return ans;}
long long bin_mul(long long a, long long b, long long MOD){long long ans = 0;if (a >= MOD) a %= MOD;while (b){if (b & 1) ans = (ans + a) % MOD;a = (a + a) % MOD;b /= 2;}return ans;}
// build(n, mod)
struct math{int N, MOD;vector <int> gt, rl;void build(int n, int mod){N = n;MOD = mod;gt.resize(N + 5);rl.resize(N + 5);gt[0] = rl[0] = 1;for (int i = 1; i <= N; ++i){gt[i] = 1LL * gt[i - 1] * i % MOD;rl[i] = bin_pow(gt[i], MOD - 2, MOD);}}int C(int a, int b){return 1LL * gt[b] * rl[a] % MOD * rl[b - a] % MOD;}};
//build(mod)
struct fibonacci{int MOD;map <long long,int> fibo;void build(int mod){MOD = mod;fibo[1] = fibo[2] = 1;}int fb(long long n){if (fibo.count(n)) return fibo[n];if (n & 1) return fibo[n] = (1LL * fb(n / 2) * fb(n / 2) + 1LL * fb(n / 2 + 1) * fb(n / 2 + 1)) % MOD;return fibo[n] = (1LL * 2 * fb(n / 2 - 1) * fb(n / 2) + 1LL * fb(n / 2) * fb(n / 2)) % MOD;}};
// init(n)
struct fenwick_tree{int N;vector <int> FT;void init(int n){N = n;FT.resize(n + 5);}void clear(){for (int i = 1; i <= N; ++i) FT[i] = 0;}void update(int idx, int val){for (; idx <= N; idx += idx & (-idx)) FT[idx] += val;}int get(int idx){int val = 0;for (; idx; idx -= idx & (-idx)) val += FT[idx];return val;}int sum(int l, int r){return get(r) - get(l - 1);}};
const int N = 1e5;
int n, k, q, a[N + 5], x[N + 5];
map <int,int> mp[N + 5];
set <int> g[N + 5];
fenwick_tree ft;
void Solve()
{
cin >> n >> k >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= k; ++i) x[i] = i;
vector <int> qry;
while (q--)
{
int pos;
cin >> pos;
qry.push_back(pos);
swap(x[pos], x[pos + 1]);
g[x[pos]].insert(x[pos + 1]);
g[x[pos + 1]].insert(x[pos]);
}
int nt = 0;
ft.init(n);
for (int i = 1; i <= n; ++i)
{
ft.update(a[i], 1);
nt += i - ft.get(a[i]);
for (int j : g[a[i]])
mp[a[i]][j] += ft.sum(j, j);
}
for (int i = 1; i <= k; ++i) x[i] = i;
for (int pos : qry)
{
nt += mp[x[pos + 1]][x[pos]];
swap(x[pos], x[pos + 1]);
nt -= mp[x[pos + 1]][x[pos]];
cout << nt << "\n";
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if (fopen("file.inp", "r"))
{
freopen ("file.inp", "r", stdin);
freopen ("file.out", "w", stdout);
}
int TEST = 1;
// cin >> TEST;
while (TEST--) Solve();
cerr << "\nTIME: " << 1000 * clock() / CLOCKS_PER_SEC << "ms.";
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:76:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen ("file.inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:77:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen ("file.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... |