Submission #510113

#TimeUsernameProblemLanguageResultExecution timeMemory
510113ETKSwap Swap Sort (CCO21_day1problem1)C++14
25 / 25
3249 ms88188 KiB
#include <bits/stdc++.h> #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define per(i,a,b) for(int i=(a);i>=(b);--i) #define pii pair<int,int> #define vi vector<int> #define mp make_pair #define fi first #define se second #define pb push_back #define debug(...) fprintf(stderr, __VA_ARGS__) #define ALL(x) x.begin(),x.end() #define sz(x) int(x.size()) #define ll long long using namespace std; inline ll read(){ ll x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();} while (isdigit(ch)){x=x*10+ch-48;ch=getchar();} return x*f; } const int N=2e5+5; int n,k,q,id[N],a[N]; int t[N]; void add(int x,int v){for(;x <= n;x += x & -x)t[x] += v;} ll Q(int x){ ll res = 0; for(;x;x -= x & -x)res += t[x]; return res; } vector <int> pos[N]; map <pii,ll> S; ll work(int a,int b){ ll tot = (ll)sz(pos[a]) * sz(pos[b]); if(S.count(mp(a,b)))return S[mp(a,b)]; if(S.count(mp(b,a)))return tot - S[mp(b,a)]; ll res = 0; if(sz(pos[a]) < sz(pos[b])){ for(auto x : pos[a]) res += lower_bound(ALL(pos[b]),x) - pos[b].begin(); }else{ for(auto x : pos[b]) res += pos[a].end() - upper_bound(ALL(pos[a]),x); } return S[mp(a,b)] = res; } int main(){ n = read(),k = read(),q = read(); rep(i,1,n){ a[i] = read(); pos[a[i]].pb(i); } rep(i,1,n)id[i] = i; ll ans = 0; rep(i,1,n){ ans += Q(k) - Q(a[i]); add(a[i],1); } while(q--){ int x = read(); ans += (ll)sz(pos[id[x]]) * sz(pos[id[x+1]]) - 2 * work(id[x],id[x+1]); printf("%lld\n",ans); swap(id[x],id[x+1]); } 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...