POJ 2352 && HDU 1541 Stars (樹狀數組),poj2352

來源:互聯網
上載者:User

POJ 2352 && HDU 1541 Stars (樹狀數組),poj2352

一開始想,總感覺是DP,可是最後什麼都沒想到。還暴力的交了一發。

然後開始寫線段樹,結果逾時。感覺自己線段樹的寫法有問題。改天再寫。先把樹狀數組的寫法貼出來吧。

~~~~~~~~~~~~~~~~~~~~~~~~

樹狀數組不懂的去看劉汝佳的大白書,那個圖畫得很清楚。

題目大意:星星的座標以y遞增的順序給出,這些點的左下方的點數代表這個點的級數,問0~N-1的級數有多少個?其實y根本木有用。

題目連結:http://poj.org/problem?id=2352

http://acm.hdu.edu.cn/showproblem.php?pid=1541

樹狀數組的思路在於:每一次詢問,就相當於詢問在1~x(包括x)間有多少個點。那麼我們邊讀入,邊更新就好。

#include<cstdio>#include<cstring>#include<algorithm>#define N 32000+10using namespace std;int a[N],ans[N>>1];int lowbit(int x){    return x&(-x);}int Sum(int x){    int tot=0;    while(x>0)    {        tot+=a[x];        x-=lowbit(x);    }    return tot;}void update(int x,int v){    while(x<=N)    {        a[x]+=v;        x+=lowbit(x);    }}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        int x,y;        memset(a,0,sizeof(a));        memset(ans,0,sizeof(ans));        for(int i=0;i<n;i++)        {            scanf("%d%d",&x,&y);            x++;    //1~x的區間。            ans[ Sum(x) ]++;            update(x,1);  //用1來更新。        }        for(int i=0;i<n;i++)            printf("%d\n",ans[i]);    }    return 0;}







這是poj2352的代碼,為何是逾時?麻煩解釋一下然後再更改一下

你搞錯思路了
#include <cstdio>
#include <algorithm>
#include <iostream>
#define MAX 33000
using namespace std;
typedef struct node{int x;int y;}node;
node a[MAX];
int c[MAX];
int n;
int lowbit(int i)
{
return(i&(-i));
}
long long getsum(int i)
{
long long sum =0;
while (i>0)
{
sum += c[i];
i -= lowbit(i);
}
return sum;
}
void update(int i,int val)
{
while (i<=MAX)
{
c[i] += val;
i += lowbit(i);
}
}
int main()
{
int i,j,k=0;
int count[MAX]={0};
memset(c,0,sizeof(c));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
for(i=1;i<=n;i++)
{
count[getsum(a[i].x+1)]++;
update(a[i].x+1,1);
}
for(i=0;i<n;i++)
printf("%d\n",count[i]);
system("pause");
return 0;
}
 
POJ上 對於二叉樹遍曆的題目

最小產生樹演算法(prim,kruskal) (poj1789,poj2485,poj1258,poj3026)
trie樹(靜態建樹、動態建樹) (poj2513)
線段樹. (poj2528,poj2828,poj2777,poj2886,poj2750)
靜態二叉檢索樹. (poj2482,poj2352)
樹狀樹組(poj1195,poj3321)
最短路,最小產生樹,二分圖,最大流問題的相關理論(主要是模型建立和求解)
(poj3155, poj2112,poj1966,poj3281,poj1087,poj2289,poj3216,poj2446)
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.