zoukankan      html  css  js  c++  java
  • poj 2352 Stars(线段树)

    题目:http://poj.org/problem?id=2352

    大意:一些星星有自己的优先级,优先级是x坐标和y坐标小于等于该星星坐标的星星个数

    思路:由于这个题的y值是从小到大排列,所以对x建立线段树,找小于等于该星星x坐标的星星个数即可

    代码:

    #include <iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn=32010;
    const int man=15010;
    int res[man];
    int tree[maxn*4];
    int lz[maxn*4];
    int num;
    int find(int l,int r,int w,int L,int R)
    {
        if(L<=l&&R>=r)
        {
            return tree[w];
        }
        int m=(l+r)/2;
        int ans=0;
        if(L>m)
        ans+=find(m+1,r,w*2+1,L,R);
        else if(R<=m)
        {
            ans+=find(l,m,w*2,L,R);
        }
        else
        {
            ans+=find(l,m,w*2,L,m)+find(m+1,r,w*2+1,m+1,R);
        }
        return ans;
    
    }
    void update(int l,int r,int w,int x)
    {
        tree[w]++;
        if(x==l&&x==r)
        {
            return ;
        }
        int m=(l+r)/2;
        if(x<=m)
        update(l,m,w*2,x);
        else
        update(m+1,r,w*2+1,x);
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            int i;
            int x,y;
            memset(tree,0,sizeof(tree));
            memset(lz,0,sizeof(lz));
            memset(res,0,sizeof(res));
            for(i=0;i<n;i++)
            {
                scanf("%d%d",&x,&y);
                x++;
                num=0;
                res[find(1,32005,1,1,x)]++;
                update(1,32005,1,x);
    
            }
            for(i=0;i<n;i++)
            {
                printf("%d
    ",res[i]);
            }
    
        }
        return 0;
    }
    View Code
  • 相关阅读:
    I Hate It
    满减优惠[Offer收割]编程练习赛4
    积水的城市 hiho[Offer收割]编程练习赛4
    Subsequence 尺取法
    526. 优美的排列
    401. 二进制手表
    306. 累加数
    216. 组合总和 III
    131. 分割回文串
    ubuntu deepin-软件 分辨率的问题
  • 原文地址:https://www.cnblogs.com/wanglin2011/p/3175551.html
Copyright © 2011-2022 走看看