Submission #416630

#TimeUsernameProblemLanguageResultExecution timeMemory
416630inksamuraiSwap Swap Sort (CCO21_day1problem1)C++17
0 / 25
5033 ms6948 KiB
#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) (int)(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 <int,int>
#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 <int> 
#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 = 200000;

using namespace std;
struct fnw {
private:
    int n;
    vector <int> bit;

public:
    fnw(int m) {
		n=m;
		bit.resize(n+2,0);
    }
    int get(int i) {
		int s = 0;
		// while (i > 0) { s += bit[i]; i -= i & -i; }
		while(i <= n) { s += bit[i]; i += i & -i;  }
		return s;
	}
	void add(int i,int 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];
int frq[max_n];


map<pii,ll>memo;

ll f(int x,int 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);
			int 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;
	// 		int 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(max_n+10);
	
	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) {
		int j;
		cin>>j; j--;
		int 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...