Poj 2352 & HDU 1541 stars (tree array)

Source: Internet
Author: User

At first, I thought it was DP, but I didn't think of anything at last. And a brute force attack.

Then, start writing the line segment tree and the result times out. I feel that there is a problem with writing the line segment tree. Write again in another day. Paste the tree array method first.

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

If the tree array doesn't understand, go to Liu lujia's big white book. The picture is very clear.

The coordinates of the stars are given in the ascending order of Y. The points in the lower left of these points represent the series of the points ~ How many levels of N-1? In fact, Y is useful.

Question link: http://poj.org/problem? Id = 2352

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1541

The idea of the tree array is: Every query is equivalent to a query in 1 ~ Number of points between X (including X. So we can read and update while reading.

# Include <cstdio> # include <cstring> # include <algorithm> # define n 32000 + 10 using 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", & X, & Y); X ++; // 1 ~ The range of X. Ans [sum (x)] ++; Update (x, 1); // update with 1. } For (INT I = 0; I <n; I ++) printf ("% d \ n", ANS [I]);} return 0 ;}






Poj 2352 & HDU 1541 stars (tree 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.