답안 #1025269

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1025269 2024-07-16T18:53:16 Z NintsiChkhaidze Road Construction (JOI21_road_construction) C++17
65 / 100
10000 ms 145612 KB
#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
#define left (h<<1),l,((l + r) >> 1)
#define right ((h<<1)|1),((l + r)>>1) + 1,r
#pragma GCC optimize("Ofast,unroll-loops")
// #define int ll
#define pli pair <ll,int>
#define pll pair <ll,ll>
using namespace std;
 
const int N = 3e5 + 5;
const ll inf = 1e18;
pli t[2][3*N];
int x[N],y[N],yy[N],n,k,L,R;
multiset <ll> st[2][3*N];
pii arr[N];
vector <ll> answ;
 
void upd(bool i,int h,int l,int r,int idx,ll oldval,ll newval,bool z){
  if (l == r){
    if (!z) st[i][l].erase(st[i][l].find(oldval));
    st[i][l].insert(newval);
    t[i][h] = {*--st[i][l].end(),l};
    return;
  }
 
  if (idx > (l + r)/2) upd(i,right,idx, oldval, newval,z);
  else upd(i,left,idx, oldval, newval,z);
  t[i][h] = max(t[i][h*2],t[i][h*2 + 1]);
}
 
void build(int h,int l,int r){
  t[1][h] = t[0][h] = {-inf,0};
  if (l == r) {
    st[1][l].clear();
    st[0][l].clear();
    return;
  }
  build(left);
  build(right);
}
 
pli get(bool i,int h,int l,int r){
  if (r < L || R < l) return {-inf,0};
  if (L <= l && r <= R) return t[i][h];
  return max(get(i,left),get(i,right));
}
 
int count(ll w,bool z){
 
  build(1,1,n);
  int tot = 0;
  for (int i = 1; i <= n; i++){
    if (tot > k) return tot;
    vector <pair <pii,pll> > roll;
 
    int id = arr[i].s;
    int v1 = x[id] + y[id];
    while (tot <= k){
      L = 1; R = yy[id];
      pli res = get(0,1,1,n);
      ll w1 = v1 - res.f;
      if (w1 > w) break;
      if (z) answ.pb(w1);
 
      tot += 1;
      roll.pb({{0,res.s},{res.f,-inf}});
      upd(0,1,1,n,res.s,res.f, -inf,0);
    }
 
    int v2 = x[id] - y[id];
    while (tot <= k){
      L = yy[id] + 1; R = n;
      pli res = get(1,1,1,n);
      ll w1 = v2 - res.f;
      if (w1 > w) break;
      if (z) answ.pb(w1);
 
      tot += 1;
      roll.pb({{1,res.s},{res.f,-inf}});
      upd(1,1,1,n,res.s,res.f, -inf,0);
    }
 
    for (int j = roll.size() - 1; j >= 0; j--){
      upd(roll[j].f.f,1,1,n,roll[j].f.s,roll[j].s.s,roll[j].s.f,0);
    }
 
    upd(0,1,1,n,yy[id],-inf,v1,1);
    upd(1,1,1,n,yy[id],-inf,v2,1);
  }
 
  return tot;
}
 
signed main() {
  ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
	
  cin>>n>>k;
  
  vector <int> vec;
  for (int i = 1; i <= n; i++){
    cin >> x[i] >> y[i];
    vec.pb(y[i]);
  }
  sort(vec.begin(),vec.end());
  map <int,int> mp;
  int val = 0;
  for (int i=0;i<vec.size();i++){
    if (!i || vec[i] != vec[i - 1]) ++val;
    mp[vec[i]] = val;
  }
 
  for (int i = 1; i<= n; i++){
    yy[i] = mp[y[i]];
    arr[i] = {x[i],i};
  }
  sort(arr+1,arr+n+1);
 
  ll l = 0,r = 4e9,res = -1;
  while (l <= r){
    ll mid = (l + r) >> 1;
    //umciresi w iseti rom w ze naklebi an toli wonis wiboebis raodenoba >= k ze
  
    if (count(mid,0) >= k) {
      res = mid;
      r = mid - 1;
    }else{
      l = mid + 1;
    }
  }
 
  int cnt1 = count(res - 1,1);
  sort(answ.begin(),answ.end());
  for (int i = 1; i <= k - cnt1; i++)
    answ.pb(res);
    
  for (ll x: answ)
    cout<<x<<endl;
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:113:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |   for (int i=0;i<vec.size();i++){
      |                ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2336 ms 95440 KB Output is correct
2 Correct 2331 ms 95348 KB Output is correct
3 Correct 2271 ms 95376 KB Output is correct
4 Correct 2330 ms 95616 KB Output is correct
5 Correct 2446 ms 94400 KB Output is correct
6 Correct 34 ms 90716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7102 ms 137440 KB Output is correct
2 Correct 7053 ms 136860 KB Output is correct
3 Correct 2421 ms 95384 KB Output is correct
4 Correct 7370 ms 136732 KB Output is correct
5 Correct 7272 ms 136908 KB Output is correct
6 Correct 6608 ms 136852 KB Output is correct
7 Correct 6923 ms 136652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5275 ms 145356 KB Output is correct
2 Correct 4962 ms 145612 KB Output is correct
3 Correct 15 ms 90716 KB Output is correct
4 Correct 1766 ms 133840 KB Output is correct
5 Correct 3293 ms 133864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5275 ms 145356 KB Output is correct
2 Correct 4962 ms 145612 KB Output is correct
3 Correct 15 ms 90716 KB Output is correct
4 Correct 1766 ms 133840 KB Output is correct
5 Correct 3293 ms 133864 KB Output is correct
6 Correct 5442 ms 145576 KB Output is correct
7 Correct 5222 ms 145572 KB Output is correct
8 Correct 14 ms 90716 KB Output is correct
9 Correct 13 ms 90656 KB Output is correct
10 Correct 5488 ms 144020 KB Output is correct
11 Correct 1737 ms 133604 KB Output is correct
12 Correct 3520 ms 133860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2336 ms 95440 KB Output is correct
2 Correct 2331 ms 95348 KB Output is correct
3 Correct 2271 ms 95376 KB Output is correct
4 Correct 2330 ms 95616 KB Output is correct
5 Correct 2446 ms 94400 KB Output is correct
6 Correct 34 ms 90716 KB Output is correct
7 Correct 6618 ms 119648 KB Output is correct
8 Correct 6596 ms 119748 KB Output is correct
9 Correct 2367 ms 95572 KB Output is correct
10 Correct 5389 ms 118044 KB Output is correct
11 Correct 4641 ms 117592 KB Output is correct
12 Correct 5174 ms 115028 KB Output is correct
13 Correct 6536 ms 113672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2336 ms 95440 KB Output is correct
2 Correct 2331 ms 95348 KB Output is correct
3 Correct 2271 ms 95376 KB Output is correct
4 Correct 2330 ms 95616 KB Output is correct
5 Correct 2446 ms 94400 KB Output is correct
6 Correct 34 ms 90716 KB Output is correct
7 Correct 7102 ms 137440 KB Output is correct
8 Correct 7053 ms 136860 KB Output is correct
9 Correct 2421 ms 95384 KB Output is correct
10 Correct 7370 ms 136732 KB Output is correct
11 Correct 7272 ms 136908 KB Output is correct
12 Correct 6608 ms 136852 KB Output is correct
13 Correct 6923 ms 136652 KB Output is correct
14 Correct 5275 ms 145356 KB Output is correct
15 Correct 4962 ms 145612 KB Output is correct
16 Correct 15 ms 90716 KB Output is correct
17 Correct 1766 ms 133840 KB Output is correct
18 Correct 3293 ms 133864 KB Output is correct
19 Correct 5442 ms 145576 KB Output is correct
20 Correct 5222 ms 145572 KB Output is correct
21 Correct 14 ms 90716 KB Output is correct
22 Correct 13 ms 90656 KB Output is correct
23 Correct 5488 ms 144020 KB Output is correct
24 Correct 1737 ms 133604 KB Output is correct
25 Correct 3520 ms 133860 KB Output is correct
26 Correct 6618 ms 119648 KB Output is correct
27 Correct 6596 ms 119748 KB Output is correct
28 Correct 2367 ms 95572 KB Output is correct
29 Correct 5389 ms 118044 KB Output is correct
30 Correct 4641 ms 117592 KB Output is correct
31 Correct 5174 ms 115028 KB Output is correct
32 Correct 6536 ms 113672 KB Output is correct
33 Execution timed out 10055 ms 145484 KB Time limit exceeded
34 Halted 0 ms 0 KB -