Skip to content

Commit

Permalink
Binary Indexed Trees practice
Browse files Browse the repository at this point in the history
  • Loading branch information
dqiushuang committed Jul 28, 2013
1 parent d27a5b7 commit 6caca55
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
37 changes: 37 additions & 0 deletions BinaryIndexedTrees/P2352-Stars-TLE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// P2325-Stars.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>

#define N 15005
int x[N], y[N];
int res[N];
int n;

int _tmain(int argc, _TCHAR* argv[])
{
int i = 0 ,j = 0, count = 0;
scanf("%d", &n);
for(i = 0; i < n; ++i) {
scanf("%d%d", &x[i], &y[i]);
}

//memset(res, 0, sizeof(res));

res[0] = 1;
for(i = 1; i < n; ++i) {
count = 0;
for(j = 0; j < i; ++j) {
if(x[j] <= x[i])
count++;
}
res[count]++;
}

for(i = 0; i < n; ++i){
printf("%d\n", res[i]);
}
return 0;
}

46 changes: 46 additions & 0 deletions BinaryIndexedTrees/P2352-Stars.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// P2352-Stars.cpp : 定义控制台应用程序的入口点。
//As Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
//So, the stars those contribute to the star(x, y) are scanfed before (x, y). Using x coordinate is OK.
#include "stdafx.h"
#include <stdio.h>

#define N 15005
#define MAXVAL 32005
int tree[MAXVAL];
int res[N];
int n;

void update(int x, int val) {
while(x <= MAXVAL) {
tree[x] += val;
x += (x & -x);
}
}

int read(int x) {
int sum = 0;
while(x > 0) {
sum += tree[x];
x -= (x & -x);
}
return sum;
}

int _tmain(int argc, _TCHAR* argv[])
{
int i = 0 ,j = 0, count = 0;
int x, y;
scanf("%d", &n);
for(i = 0; i < n; ++i) {
scanf("%d%d", &x, &y);
res[read(++x)]++;
update(x, 1);
}

for(i = 0; i < n; ++i){
printf("%d\n", res[i]);
}
getchar();getchar();
return 0;
}

76 changes: 76 additions & 0 deletions BinaryIndexedTrees/P2481-Cows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// P2481-Cows.cpp : 定义控制台应用程序的入口点。
//this problem is similar to p2352. in p2352, the secquence is order by y asc, x asc.
//so, here we order the secquence by e desc, s asc.
//submitted in c++
#include "stdafx.h"
#include <iostream>
#include <algorithm>
using namespace std;

#define N 100005

int n, tree[N], res[N];

struct cow{
int s;
int e;
int idx;
}cows[N];

bool comp(cow a, cow b) {
if(a.e == b.e)
return a.s < b.s;
return a.e > b.e ;
}

int read(int x) {
int sum = 0;
while(x > 0) {
sum += tree[x];
x -= (x & -x);
}
return sum;
}

void update(int x) {
while(x <= n) {
tree[x]++;
x += (x & -x);
}
}

int _tmain(int argc, _TCHAR* argv[])
{
int i, j;
while(scanf("%d", &n), n != 0) {
memset(cows, 0, sizeof(cows));
memset(tree, 0, sizeof(tree));
memset(res, 0, sizeof(res));

for(i = 0; i < n; ++i) {
scanf("%d%d", &cows[i].s, &cows[i].e);
cows[i].s++;
cows[i].idx = i;
}
sort(cows, cows + n, comp); //the idea is the same as P2352
struct cow pre = {-1, -1};
int minus = 0;
for(i = 0; i < n; ++i) {
if(pre.e == cows[i].e && pre.s == cows[i].s)
minus++;
else {
minus = 0;
pre.e = cows[i].e;
pre.s = cows[i].s;
}
res[cows[i].idx] = read(cows[i].s) - minus;
update(cows[i].s);
}
for(i = 0; i < n; i++)
printf("%d ", res[i]);
printf("\n");
}
getchar(); getchar();
return 0;
}

0 comments on commit 6caca55

Please sign in to comment.