Submission #1268268

#TimeUsernameProblemLanguageResultExecution timeMemory
1268268julia_08Advertisement 2 (JOI23_ho_t2)C++20
0 / 100
1 ms328 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e3 + 10;

int x[MAXN], e[MAXN];

int deg[MAXN];

int bit[MAXN];

int n;

void bit_add(int pos, int x){

  for(int i=pos; i<=n; i+=(i&-i)){
    bit[i] += x;
  }

}

int bit_query(int pos){
    
  int sum = 0;

  for(int i=pos; i>0; i-=(i&-i)){
    sum += bit[i];
  }

  return sum;

}

void compress(){

  vector<pair<pair<int, int>, int>> r;

  for(int i=0; i<n; i++) r.push_back({{x[i], e[i]}, i});

  sort(r.begin(), r.end());
    
  vector<int> to_erase;

  int last_x = -1, last_e = -1;

  for(int i=0; i<n; i++){

    if(r[i].first.first == last_x && r[i].first.second == last_e){
      to_erase.push_back(i);
    }

    last_x = r[i].first.first;
    last_e = r[i].first.second;

  }

  while(!to_erase.empty()){
    swap(r[to_erase.back()], r.back());
    r.pop_back();
    to_erase.pop_back();
  }

  n = r.size();

  sort(r.begin(), r.end()); // ordenando por x

  for(int i=0; i<n; i++){
    x[i + 1] = r[i].first.first;
    e[i + 1] = r[i].first.second;
  }

}

int main(){
  cin.tie(0)->sync_with_stdio(0);

  cin >> n;

  for(int i=0; i<n; i++) cin >> x[i] >> e[i];

  compress();

  /*
    
    eh uma dag agora! 
    dai da para fazer bonitinho e deixar todo
    mundo com out <= 2 (usando stack?)
    
  */
  
  stack<int> q;

  for(int i=1; i<=n; i++){

    // primeiro menor à esquerda, porque da certo quando eh maior
    while(!q.empty() && x[q.top()] - e[q.top()] >= x[i] - e[i]) q.pop();

    if(!q.empty()){
      bit_add(q.top() + 1, 1);
      bit_add(i, -1);
    }

    q.push(i);

  }

  while(!q.empty()) q.pop();

  for(int i=n; i>=1; i--){

    // primeiro maior à direita, porque da certo quando eh menor
    while(!q.empty() && x[q.top()] + e[q.top()] <= x[i] + e[i]) q.pop();

    if(!q.empty()){
      bit_add(i + 1, 1);
      bit_add(q.top(), -1);
    }

    q.push(i);

  }
  
  int ans = 0;

  for(int i=1; i<=n; i++) ans += (bit_query(i) == bit_query(i - 1));

  cout << ans << "\n";

  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...