Aizu - 1386 Starting a Scenic Railroad Service Idea + Tree Array

Keywords: less

Topic:

Given n the travel intervals of n passengers, ask them how many seats they need in the worst case and how many seats they need in the best case.

 

Ideas:

The worst case is that everyone chooses seats, no matter how conflicting they are, everyone can do it; here we consider that each person's travel zone will conflict with several other people, and that, with his own number, the largest choice is at least the number of seats needed.

Obviously, n^2 can not be solved, so we sort the left values of intervals from small to large: first traverse from left to right, and the right values of each interval are updated to the tree array. For the current interval, we only need to calculate the number of trees larger than the left values of the current interval; then traverse from right to left, empty the tree array, and store the left values of each interval behind, and then for the current interval. The number of intervals in a tree array that are less than the right value of the current interval, so that the number of intersections between each interval and it can be obtained.

 

For the optimal case, each interval cover time point is updated, see the maximum number of updates at a certain time point is the answer, which is equivalent to the interval updating of line segment tree, but here is a simple way to update the interval is that the left value of the interval updates the positive value, and the right value of the interval updates the negative value, so the prefix and the updates of a point are obtained.

 

 

#include<iostream>
#include <cstdio>
#include<algorithm>
#include <cstring>
#include <set>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
const int maxn = 2e5 + 7;
const int maxd = 1e5 + 7;

int n;
int ans1 = 0, ans2 = 0;

struct node {
  int l, r;
}a[maxn];
bool cmp(node a, node b) {
  if(a.l == b.l) return a.r < b.r;
  return a.l < b.l;
}
int sum[maxd] = {0};
int c[maxd];
int ans[maxn];

int lowbit(int x) {
  return (x&-x);
}
int sum_(int x) {
  int res = 0;
  while(x>0) {
    res += c[x];
    x -= lowbit(x);
  }
  return res;
}
void add(int x, int v) {
  while(x<=(maxd-1)) {
    c[x] += v;
    x += lowbit(x);
  }
}

int main() {
  scanf("%d", &n);
  for(int i = 1; i <= n; ++i) {
    scanf("%d%d", &a[i].l, &a[i].r);
  }
  for(int i = 1; i <= n; ++i) {
    sum[a[i].l]++;
    sum[a[i].r]--;
  }
  int t = 0;
  for(int i = 1; i < maxd; ++i) {
    t += sum[i];
    ans2 = max(ans2, t);
  }

  sort(a+1,a+1+n,cmp);
  memset(c, 0, sizeof c);
  memset(ans, 0, sizeof ans);
  for(int i = 1; i <= n; ++i) {
    ans[i] += (sum_(maxd-1) - sum_(a[i].l));
    add(a[i].r, 1);
  }
  memset(c, 0, sizeof c);
  for(int i = n; i > 0; --i) {
    ans[i] += (sum_(a[i].r-1));
    add(a[i].l, 1);
  }
  for(int i = 1; i <= n; ++i) {
    ans1 = max(ans1, ans[i]);
  }
  printf("%d %d\n", ans1+1, ans2);

  return 0;
}

 

Posted by mukunthan on Mon, 04 Feb 2019 08:27:17 -0800