Submission #428248

#TimeUsernameProblemLanguageResultExecution timeMemory
428248Blistering_BarnaclesArranging Shoes (IOI19_shoes)C++14
100 / 100
957 ms786724 KiB
#include "shoes.h"
//apig's property
//Happiness can be found, even in the darkest of times, if one only remembers to turn on the light
//El Pueblo Unido Jamas Sera Vencido
//The saddest thing about betrayal? is that it never comes from your enemies
//Do or do not... there is no try
//Billions of bilious blue blistering barnacles in a thundering typhoon!
#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
#define F first
#define S second
#define pb push_back
#define vll vector< ll >
#define vi vector< int >
#define pll pair< ll , ll >
#define pi pair< int , int >
#define all(s) s.begin() , s.end()
#define sz(s) s.size()
#define str string
#define md ((s + e) / 2)
#define mid ((l + r) / 2)
#define msdp(dp) memset(dp , -1 , sizeof dp)
#define mscl(dp) memset(dp , 0 , sizeof dp)
#define C continue
#define R return
#define B break
#define lx node * 2
#define rx node * 2 + 1
#define br(o) o ; break
#define co(o) o ; continue
using namespace std;
typedef long long ll;
ll q, dp[105][100005], a[555555] , b[555555], k, l, m, n, o, p;
map < ll , ll > mp;
vll adj[555555];
const ll mod = 1e9+7;
str s;
ll tree[555555] , lazy[555555] ;
void lz(ll s , ll e , ll node){
    if(lazy[node]){
        tree[node] += lazy[node] ;
        if(s != e){
            lazy[lx] += lazy[node] ;
            lazy[rx] += lazy[node] ;
        }
        lazy[node] = 0 ;
    }
}
void update(ll s , ll e , ll node , ll l , ll r , ll val){
    lz(s , e , node) ;
    if(s > r || e < l || l > r)R ;
    if(l <= s && e <= r){
        lazy[node] += val ;
        lz(s , e , node) ;
        R ;
    }
    update(s , md , lx , l , r , val) ;
    update(md + 1 , e , rx , l , r , val) ;
    tree[node] = tree[lx] + tree[rx] ;
}
ll query(ll s , ll e , ll node , ll id){
    lz(s , e , node) ;
    if(s > id || e < id)R 0 ;
    if(s == id && id == e)R tree[node] ;
    R query(s , md , lx , id) + query(md + 1 , e , rx , id) ;
}
queue < ll > low[555555] , high[555555] ;
long long count_swaps(std::vector<int> s) {
	n = sz(s) ;
	for(ll i = 1 ; i <= n ; i++){
        a[i] = s[i - 1] ;
        if(a[i] < 0)low[abs(a[i])].push(i) ;
        else high[a[i]].push(i) ;
	}
    for(ll i = 1  ; i <= n ; i++){
        if(mp[i] || low[abs(a[i])].empty() || high[abs(a[i])].empty())C ;
        o = low[abs(a[i])].front() , p = high[abs(a[i])].front() ;
        low[abs(a[i])].pop() , high[abs(a[i])].pop() ;
        if(o > p)swap(o , p) ;
        k += query(1 , n , 1 , p) + p - query(1 , n , 1 , o) - o - 1 + (a[i] > 0) ;
        update(1 , n , 1 , o + 1 , p - 1 , 1) ;
        mp[o]++ , mp[p]++ ;
    }
    R k ;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...