이 제출은 이전 버전의 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(ll i = 0 ; i < n ; i++)
#define drep(i,n) for(ll i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(ll 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 ll max_n = 100000;
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; }
}
};
ll n,k,q;
vi a,inarg;
vi inds[max_n];
ll frq[max_n];
map<pii,ll>memo;
ll f(ll x,ll y) {
// if(memo.find({x,y})!=memo.end()) return memo[{x,y}];
// if(memo.find({y,x})!=memo.end()) return memo[{x,y}];
ll cnw=0;
// if(frq[x]<frq[y]) {
for(auto i : inds[x]) {
auto it=lower_bound(all(inds[y]),i);
if(it==inds[y].begin()) break;
it=prev(it);
ll j=it-inds[y].begin();
cnw += (j+1);
}
// }else {
// for(auto i : inds[y]) {
// auto it=lower_bound(all(inds[x]),i);
// if(it==inds[x].end()) break;
// ll j=it-inds[x].begin();
// cnw += frq[x]-j;
// }
// }
// cout << cnw << "\n";
return cnw;
// 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]]=sz(inds[a[i]]);
}
rep(i,k) inarg.pb(i+1);
ll ans=cinv;
rep(i,q) {
ll j;
cin>>j; j--;
ll x=inarg[j],y=inarg[j+1];
ans -= (ll)sz(inds[x])*sz(inds[y]);
ans += 2*f(y,x);
// f(y,x);
cout<<ans<<"\n";
swap(inarg[j],inarg[j+1]);
// for(auto e : inarg) cout << e << " ";
// cout<<"\n";
}
/*
*/
return 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... |