POJ2352 Stars

试了一把树状数组,代码果然很简单。

#include <iostream>

#include <string>

#include <vector>

#include <stack>

#include <sstream>

#include <utility>

using namespace std;



vector<int> cnt;

vector<int> arr; // effective index: 1..N



int lowb(int t) {

  return t & (-t);

}



void add(int i, int x) {

  int size = arr.size();

  for (; i < size; i += lowb(i)) {

    arr[i] += x;

  }

}



int sum(int i) {

  int s = 0;

  for (; i > 0; i -= lowb(i)) {

    s += arr[i];

  }

  return s;

}



int main() {

  int n = 0;

  while (scanf("%d",&n) != EOF) {

    cnt.assign(15010, 0);

    arr.assign(32010, 0);

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

      int x, y;

      scanf("%d%d",&x, &y);

      int tmp = sum(x + 1);

      cnt[tmp]++;

      add(x + 1, 1);

    }

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

      printf("%d\n",cnt[i]);

    }

  } 

  return 0;

}

  

你可能感兴趣的:(tar)