生活随笔
收集整理的這篇文章主要介紹了
POJ 2483 Cows(树状数组)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接
題意
給出N個區間,求大于改區間的數目
AC
- 按照右區間降序排列,這樣可以保證下一個區間的右端點小,這樣樹狀數組就可以重復利用。樹狀數組存放的是以當前左區間到右區間一共有幾個比自己大的區間,當計算一個區間之后,要對樹狀數組更新一次,就是將當前區間加上。
using namespace std;
int c[N], ans[N];
struct cow{
int id, l, r;
}a[N];
bool cmp(
const cow &x,
const cow &y) {
if (x.r != y.r)
return x.r > y.r;
elsereturn x.l < y.l;
}
int get_sum(
int x) {
int res =
0;
while (x >
0) {res += c[x];x -= lowbit(x);}
return res;
}
void update(
int x,
int y,
int val) {
while (x <= y) {c[x] += val;x += lowbit(x);}
}
int main() {
#ifndef ONLINE_JUDGEfreopen(
"in.txt",
"r", stdin);
#endifios::sync_with_stdio(
false);
int n;
while (
cin >> n, n) {mem(ans,
0);mem(c,
0);
for (
int i =
0; i < n; ++i) {
cin >> a[i].l >> a[i].r;a[i].id = i;}sort(a, a + n, cmp);update(a[
0].l +
1, a[
0].r +
1,
1);
for (
int i =
1; i < n; ++i) {
if (a[i].l == a[i -
1].l && a[i].r == a[i -
1].r)ans[a[i].id] = ans[a[i -
1].id];
else {ans[a[i].id] = get_sum(a[i].l +
1);}update(a[i].l +
1, a[i].r +
1,
1);}
cout << ans[
0];
for (
int i =
1; i < n; ++i) {
cout <<
" " << ans[i];}
cout << endl;}
return 0;
}
總結
以上是生活随笔為你收集整理的POJ 2483 Cows(树状数组)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。