POJ 2352 && HDU 1541 Stars (tree-like array)

Source: Internet
Author: User

At first thought, the total feeling is DP, but finally did not think of anything. And a violent hand.

Then start writing the line segment tree and the result is timed out. Feel a problem with the way your line tree is written. I'll write it sometime. Let's put the tree-like array in the first place.

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

The tree array does not understand to see Rujia's Great White Book, that picture is very clear.

The main idea: the coordinates of the stars are given in the order of Y increments, and the points at the lower left of the dots represent the levels of this point, and how many 0~n-1 are asked? In fact, y is practically wooden.

Title Link: http://poj.org/problem?id=2352

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

The idea of a tree array is that every inquiry is the equivalent of asking how many points there are between 1~x (including x). Then we will read it and update it as well.

#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 + +;    The range of 1~x.            ans[Sum (x)]++;            Update (x,1);  Update with A.        }        for (int i=0;i<n;i++)            printf ("%d\n", Ans[i]);    }    return 0;}






POJ 2352 &amp;&amp; HDU 1541 Stars (tree-like array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.