이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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, Q = 1e6;
long long p[Q * 2 + 5];
int n, k, q, a[N + 5], x[N + 5], cnt[N + 5];
vector <pair <int,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;
int idx = 0;
while (q--)
{
++idx;
int pos;
cin >> pos;
qry.push_back(pos);
swap(x[pos], x[pos + 1]);
g[x[pos]].push_back({x[pos + 1], idx * 2 - 1});
g[x[pos + 1]].push_back({x[pos], idx * 2});
}
long long nt = 0;
ft.init(k);
for (int i = 1; i <= n; ++i)
{
++cnt[a[i]];
ft.update(a[i], 1);
nt += i - ft.get(a[i]);
for (pair <int,int> j : g[a[i]])
p[j.se] += cnt[j.fi];
}
idx = 0;
for (int pos : qry)
{
++idx;
nt += p[idx * 2 - 1];
nt -= p[idx * 2];
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.";
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void Solve()':
Main.cpp:63:14: warning: unused variable 'pos' [-Wunused-variable]
63 | for (int pos : qry)
| ^~~
Main.cpp: In function 'int main()':
Main.cpp:79:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen ("file.inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:80:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | 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... |