이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>
#include <stdio.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (ll)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <ll,ll>
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <ll>
#define vec(s) vector<s>
#define rsz(a,n) a.resize(n)
#define rszv(a,n,v) a.resize(n,v)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine
const int max_n = 103002;
using namespace std;
struct fnw {
private:
ll n;
vector <ll> bit;
public:
fnw(ll m) {
n=m;
bit.resize(n+2,0);
}
ll get(ll i) {
ll s = 0;
// while (i > 0) { s += bit[i]; i -= i & -i; }
while(i <= n) { s += bit[i]; i += i & -i; }
return s;
}
void add(ll i,ll x) {
while(i > 0) { bit[i] += x; i -= i & -i; }
// while (i <= n) { bit[i] += x; i += i & -i; }
}
};
int n,k,q;
vi a,inarg;
vi inds[max_n];
ll frq[max_n];
map <pii,ll> memo;
ll cntinv=0;
bool swpd=0;
ll f(ll x,ll y) {
if(frq[x]>frq[y]) {swap(x,y);swpd=1;}
if(memo.find({x,y})!=memo.end()) return memo[{x,y}];
ll cnw=0;
for(auto i : inds[x]) {
auto it=lower_bound(all(inds[y]),i);
if(it==inds[y].begin()) continue;
it=prev(it);
ll j=it-inds[y].begin();
cnw += (j+1);
}
return memo[{x,y}] = cnw;
}
int main(){
fcin;
cin>>n>>k>>q;
a.resize(n,0);
fnw bit(k);
ll cinv=0;
rep(i,n) {
cin>>a[i];
cinv += bit.get(a[i]);
bit.add(a[i]-1,1);
inds[a[i]].pb(i);
frq[a[i]]++;
}
rep(i,k) inarg.pb(i+1);
cntinv=cinv;
rep(_,q) {
int j;
cin>>j;
j--;
int x=inarg[j],y=inarg[j+1],cnt=0;
swpd=0;
ll cnw=f(y,x);
if(swpd) {cntinv+=frq[x]*frq[y]; cntinv -= 2*cnw; }
else { cntinv-=frq[x]*frq[y]; cntinv += 2*cnw; }
swap(inarg[j],inarg[j+1]);
cout<<cntinv<<"\n";
}
/*
*/
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:126:37: warning: unused variable 'cnt' [-Wunused-variable]
126 | int x=inarg[j],y=inarg[j+1],cnt=0;
| ^~~
# | 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... |