Ural 1028. Stars(树状数组)
2016-08-10 20:43
369 查看
题目链接:点击打开链接
思路:
为了满足第一个条件, 我们可以先按照x坐标排序, 然后我们用树状数组来维护y坐标大小关系, 就可以在O(nlogn)的时间内求出答案了。
细节参见代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 15111;
int T,n,m,bit[32111],len,ans[maxn];
struct node {
int x, y;
node(int x=0, int y=0):x(x), y(y) {}
bool operator < (const node& rhs) const {
if(x != rhs.x) return x < rhs.x;
else return y < rhs.y;
}
}a[maxn];
void add(int x, int d) {
while(x <= len) {
bit[x] += d;
x += x & -x;
}
}
int sum(int x) {
int ans = 0;
while(x > 0) {
ans += bit[x];
x -= x & -x;
}
return ans;
}
int main() {
while(~scanf("%d",&n)) {
memset(bit, 0, sizeof(bit));
len = 0;
for(int i = 1; i <= n; i++) {
scanf("%d%d", &a[i].x, &a[i].y);
a[i].x++; a[i].y++;
ans[i-1] = 0;
len = max(len, a[i].y);
}
sort(a+1, a+n+1);
for(int i = 1; i <= n; i++) {
int cur = sum(a[i].y);
ans[cur]++;
add(a[i].y, 1);
}
for(int i = 0; i < n; i++) printf("%d\n", ans[i]);
}
return 0;
}
思路:
为了满足第一个条件, 我们可以先按照x坐标排序, 然后我们用树状数组来维护y坐标大小关系, 就可以在O(nlogn)的时间内求出答案了。
细节参见代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 15111;
int T,n,m,bit[32111],len,ans[maxn];
struct node {
int x, y;
node(int x=0, int y=0):x(x), y(y) {}
bool operator < (const node& rhs) const {
if(x != rhs.x) return x < rhs.x;
else return y < rhs.y;
}
}a[maxn];
void add(int x, int d) {
while(x <= len) {
bit[x] += d;
x += x & -x;
}
}
int sum(int x) {
int ans = 0;
while(x > 0) {
ans += bit[x];
x -= x & -x;
}
return ans;
}
int main() {
while(~scanf("%d",&n)) {
memset(bit, 0, sizeof(bit));
len = 0;
for(int i = 1; i <= n; i++) {
scanf("%d%d", &a[i].x, &a[i].y);
a[i].x++; a[i].y++;
ans[i-1] = 0;
len = max(len, a[i].y);
}
sort(a+1, a+n+1);
for(int i = 1; i <= n; i++) {
int cur = sum(a[i].y);
ans[cur]++;
add(a[i].y, 1);
}
for(int i = 0; i < n; i++) printf("%d\n", ans[i]);
}
return 0;
}
相关文章推荐
- ural1028 Stars (树状数组)
- Ural 1028 Stars(树状数组)
- ural 1028【树状数组】
- Ural_1028. Stars(数状数组)
- HDU1541 Stars (树状数组)
- POJ 2481 Cows && POJ 2352 Stars(树状数组妙用)
- POJ-2352-Stars(树状数组)
- poj 2352 Stars(树状数组)
- poj2352——Stars(树状数组)
- poj2352 - Stars(树状数组)
- POJ2352 Stars 【树状数组】or【线段树】
- POJ-2352 Stars【树状数组】
- URAL 1707. Hypnotoad's Secret(树状数组)
- poj 2352 OR hdu 1541 Stars(数据结构:树状数组)
- poj------2352 Stars(树状数组)
- hdu 1541 Stars (树状数组)
- POJ 2352 Stars (树状数组)
- HDU 1541 Stars(树状数组)
- 【树状数组】POJ 2352 Stars
- POJ 2481 Cows && POJ 2352 Stars(树状数组妙用)