This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define td(v) v.begin(),v.end()
#define sz(v) (int)v.size()
//#define int long long
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
#define left sefude
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
typedef long long ll;
typedef pair<int,int> pii;
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll a,ll b,ll m){
if(b==0LL) return 1LL;
if(b==1LL) return mod(a,m);
ll k = mod(exp(a,b/2,m),m);
if(b&1LL){
return mod(a*mod(k*k,m),m);
}
else return mod(k*k,m);
}
const int N = 100100;
int S[N],T[N];
char P[N],Q[N];
struct BIT{
int n;
int MAX;
vector<ll> bit;
BIT(){}
BIT(int _n){
n = _n;
MAX = n +2;
bit.resize(n+10,0);
}
ll query(int x){
ll r=0;
while(x>0){
r+=bit[x];
x-=(x&-x);
}
return r;
}
ll query(int l,int r){
assert(l<=r);
return query(r) - query(l-1);
}
void update(int x,ll val){
while(x<MAX){
bit[x]+=val;
x+=(x&-x);
}
}
ll find_kth(ll k){
ll sum=0;
ll pos=0;
// 20 == log -> trocar conforme necessario
for(int i=20;i>=0;i--){
if(pos+(1LL<<i)>n)continue;
int atual = bit[pos+(1LL<<i)];
if(sum+atual<k){
sum+=atual;
pos+=(1LL<<i);
}
}
//acha a maior pos com <k;
return pos+1;
}
};
vi tira[N*2];
int posi[N][2];
int32_t main(){
fastio;
int k,n;
cin >> k >> n;
ll bef = n;
vi ids;
vi comp;
set<int> inside;
for(int i=1;i<=n;i++){
cin >> P[i] >> S[i] >> Q[i] >> T[i];
if(P[i] == Q[i]){
bef+= abs(S[i] - T[i]) - 1;
}else{
ids.pb(i);
inside.insert(i);
comp.pb(S[i]);comp.pb(T[i]);
}
}
BIT freq(n + n),sum(n + n);
sort(all(comp));
comp.erase(unique(all(comp)),comp.end());
for(int i : ids){
int pos = lower_bound(all(comp),S[i]) - comp.begin() + 1;
tira[pos].pb(i);
freq.update(pos,1);
sum.update(pos,S[i]);
posi[i][0] = pos;
pos = lower_bound(all(comp),T[i]) - comp.begin() + 1;
tira[pos].pb(i);
freq.update(pos,1);
sum.update(pos,T[i]);
posi[i][1] = pos;
}
if(sz(comp) == 0){
cout << bef<<endl;
return 0;
}
if(k == 1){
int other = freq.find_kth(sz(inside));
ll cur = bef;
cur += (1ll * freq.query(other) * comp[other-1] - sum.query(other)
+ sum.query(other+1,n+n) - comp[other-1]*freq.query(other+1,n+n));
cout << cur << endl;
return 0;
}
ll best = 1e18;
BIT freqx(n + n),sumx(n + n);
if(sz(comp) == 0)best = bef;
for(int i=1;i<=sz(comp);i++){
// X here:
for(int id : tira[i]){
if(inside.find(id)!=inside.end()){
// erase
inside.erase(id);
freq.update(posi[id][0],-1);freq.update(posi[id][1],-1);
sum.update(posi[id][0],-S[i]);sum.update(posi[id][1],-T[i]);
// add
freqx.update(posi[id][0],1);
freqx.update(posi[id][1],1);
sumx.update(posi[id][0],S[i]);
sumx.update(posi[id][1],T[i]);
}
}
ll cur = 1ll * freqx.query(i) * comp[i-1] - sumx.query(i)
+ sumx.query(i+1,n+n) - 1ll * comp[i-1] * freqx.query(i+1,n+n);
int tot = sz(inside)*2;
assert(tot == freq.query(n+n));
int other = freq.find_kth((tot+1)/2);
if(other == 0)other = 1;
cur += (1ll * freq.query(other) * comp[other-1] - sum.query(other)
+ sum.query(other+1,n+n) - comp[other-1]*freq.query(other+1,n+n));
cur+=bef;
best = min(best,cur);
}
cout << best << endl;
// math -> gcd it all
// Did u check N=1? Did you switch N,M?
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |