제출 #416669

#제출 시각아이디문제언어결과실행 시간메모리
416669inksamuraiSwap Swap Sort (CCO21_day1problem1)C++17
0 / 25
364 ms24696 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) (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 = 200000;
 
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 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}];
    if(memo.find({y,x})!=memo.end()) return memo[{x,y}];
 
    ll cnw=0;

    for(auto i : inds[x]) {
        auto it=upper_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(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]]++;
    }
 
    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";
    // }
    
    vi ina = a;
    cntinv=cinv;
    rep(_,q) {
        ll j;
        cin>>j;
        j--;
        ll x=inarg[j],y=inarg[j+1],cnt=0;
        swpd=0;
        ll cnw=f(y,x);
        // cout << cnw << " " << swpd << "\n";
        if(swpd) {cntinv+=frq[x]*frq[y]; cntinv -= 2*cnw; }
	    else { cntinv-=frq[x]*frq[y]; cntinv += 2*cnw; }

        // rep(i,n)if(ina[i]==x)cnt++;
 
        // rep(i,n) {
        //     if(ina[i]==y) {
        //         cntinv-=cnt;
        //     }
        //     if(ina[i]==x)cnt--;
        // }
        // cnt=0;
        // rep(i,n) {
        //     if(ina[i]==y){
        //         cntinv+=2*cnt;
        //     }
        //     if(ina[i]==x)cnt++;
        // }
        
        swap(inarg[j],inarg[j+1]);
 
        cout<<cntinv<<"\n";
    }
 
/*
*/
 
    return 0;   
}

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp: In function 'int main()':
Main.cpp:144:36: warning: unused variable 'cnt' [-Wunused-variable]
  144 |         ll x=inarg[j],y=inarg[j+1],cnt=0;
      |                                    ^~~
Main.cpp:120:8: warning: unused variable 'ans' [-Wunused-variable]
  120 |     ll ans=cinv;
      |        ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...