제출 #283275

#제출 시각아이디문제언어결과실행 시간메모리
283275NaimSSPalembang Bridges (APIO15_bridge)C++14
22 / 100
192 ms25564 KiB
#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];


BIT freq(N+N),sum(N+N);

vi comp;

ll f(int other){
    if(other == 0)other = 1;
    ll cur=0;
    cur += (1ll * freq.query(other) * comp[other-1] - sum.query(other)
          + sum.query(other+1,N+N) - 1ll*comp[other-1]*freq.query(other+1,N+N));

   
    return cur;
}

int32_t main(){
  fastio;
  int k,n;
  cin >> k >> n;
  ll bef = n;
  vi ids;
  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]);
    }
  }

  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;
  }

  ll best = 1e18;
  if(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));
    if(k == 1){cout << cur << endl;
      return 0;
    }else{
      best = cur;
    }
  }
  // till here is OK!!!

  BIT freqx(n + n),sumx(n + n);
  if(sz(comp) == 0)best = bef;
  for(int it=1;it<=sz(comp);it++){
    // X here:
    for(int id : tira[it]){
      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[id]);sum.update(posi[id][1],-T[id]);
        
        // add
        freqx.update(posi[id][0],1);
        freqx.update(posi[id][1],1);
        sumx.update(posi[id][0],S[id]);
        sumx.update(posi[id][1],T[id]);
      }
    }

    ll cur = 1ll * freqx.query(it) * comp[it-1] - sumx.query(it)
           + sumx.query(it+1,n+n) - 1ll * comp[it-1] * freqx.query(it+1,n+n);

    int tot = sz(inside)*2;
    int l = 1, r = tot;
    ll ans= (tot == 0  ? 0 : f(tot/2));
    /*
    while(l<=r){
      int other = (l+r)/2;
      int other2 = (l+r)/2 + 1;
      if(f(other) <= f(other2)){
        ans = min(ans,f(other));
        
        r = other-1;
      }else{
       l = other+1;
       ans = f(other2);
     }
    }
    */
    cur+=ans;
    cur+=bef;
    best = min(best,cur);
  } 

  cout << best << endl;

  // math -> gcd it all
  // Did u check N=1? Did you switch N,M?
}

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

bridge.cpp: In function 'int32_t main()':
bridge.cpp:188:9: warning: unused variable 'l' [-Wunused-variable]
  188 |     int l = 1, r = tot;
      |         ^
bridge.cpp:188:16: warning: unused variable 'r' [-Wunused-variable]
  188 |     int l = 1, r = tot;
      |                ^
#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...