Poj3109 tree array + scanning line

Source: Internet
Author: User

Q: On an infinitely large chessboard, the white child surrounded by horizontal and vertical nodes will become sunspots. How many sunspots will the final sunspots be solved?

Analysis:

First, this board is very big, but the number of black spots has been given to 1 E5. We need to discretization. The so-called discretization is the redefinition of the lower mark of the array.

The discretization function is provided here, and the number of arrays after discretization is returned.

 1 int compress(int *p,int N) 2 { 3     vector<int> ps(N); 4     for (int i = 0; i < N; ++i)ps[i] = p[i]; 5     sort(ps.begin(), ps.end()); 6     ps.erase(unique(ps.begin(), ps.end()), ps.end()); 7     for (int i = 0; i < N; ++i) 8     { 9         p[i] = 1 + distance(ps.begin(), lower_bound(ps.begin(), ps.end(), p[i]));10     }11     return ps.size();12 }
View code

Scanning line algorithm:

The scanning line algorithm is actually an algorithm in computer ry, which is referenced here

Basic Ideas

In the scanning line Order, calculate the intersection interval between the scanning line and the polygon, and then use the required color to display the pixels of these intervals to complete the filling.

The scanning line filling process can be divided into four steps:

(1) intersection calculation: calculates the intersection point between the scanning line and each side of the polygon.

(2) Sort all verticesXSort by incremental coordinates

(3) pairing: Determine the intersection interval between the scanning line and the polygon, the first and second, the third and the fourth, and so on. Each intersection represents a intersection interval between the scanning line and the polygon.

(4) Fill: display the pixels of the intersection Interval

How can we use this question?

First, construct the scanning line. Here the Y axis is used. The X axis is actually the same.

For each scan line, the points are sorted by X coordinates, and then n intervals are obtained. We need to calculate how many white points can be changed to black spots in this interval.

The condition is that both the top and bottom are surrounded by black spots (the left and right already match)

How can I determine whether there are black spots in the upper and lower layers? Here, we use a bool array used. used [I] indicates whether the vertical line x = I has been blacklisted.

In the scanning line algorithm, the scanning line is enumerated by Y from small to large, so if there are black spots before, and the current scan point is also black spots, then this interval must be surrounded by black spots.

Now we can solve the interval statistics problem, which is the strength of the tree array. (Range addition and subtraction + interval summation)

In fact, only the addition and subtraction of intervals and single point queries are used here.

The following algorithm is provided:

First, discretization is performed, and then scanning lines are constructed.

Then follow y from small to large enumeration scanning line, for each scanning line points by X from small to large sorting, traverse all the points can get n intervals [A [I-1], A [I]

For the interval [A [I-1], a [I], the current traversal to a [I], if used [X] on the vertical axis X = A [I] = true, You need to add the answer to the query (x, x) of the number of white points on the current vertical axis ), deny setting used [x] = true (because the current traversal is a black dot). After calculating the range, remember to clear the tree array of the range, because of the interval and maintained by the tree array, if it is unclear, it will lead to repeated calculation (points between two scanning lines ),

As for the update is very simple, is to the range (A [I-1], a [I]) plus 1, because the range is to meet the left and right white spots surrounded by black spots. (Do not cross-border)

Last code:

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <set> 5 # include <algorithm> 6 # include <map> 7 # include <queue> 8 # include <cmath> 9 # include <vector> 10 # define maxn 10000011 # define maxm 10001012 # define mod 100000000000000000013 # define INF 0x3f3f3f14 using namespace STD; 15 typedef long ll; 16 inline int lowbit (int x) {17 return X &-X; 18} 19 struct bit {20 int N; 21 LL Bit [maxn + 10], bit2 [maxn + 10]; 22 void Init (int n) {// create bit23 n = N; 24 memset (bit, 0, sizeof (BIT); 25 memset (bit2, 0, sizeof (bit2); 26} 27 void add (LL * t, int X, ll V) {// single point addition and subtraction 28 for (INT I = x; I <= N; I + = lowbit (I) T [I] + = V; 29} 30 LL sum (LL * t, int X) {// [1, x] prefix and 31 LL ans = 0; 32 for (INT I = X; i> 0; I-= lowbit (I) ans + = T [I]; 33 return ans; 34} 35 void Update (INT L, int R, ll K) {// range plus or minus 36 add (bit, l, k); 37 add (bit, R + 1,-k); 38 add (bit2, L, K * L); 39 add (bit2, R + 1,-K * (R + 1); 40} 41 LL getsum (int x) {42 return sum (bit, x) * (x + 1)-sum (bit2, x); 43} 44 ll query (int l, int R) {// sum of 45 return getsum (r) -getsum L-1); 46} 47} t; 48 int X [maxn], Y [maxn]; 49 int compress (int * P, int N) 50 {51 vector <int> PS (n); 52 for (INT I = 0; I <n; ++ I) pS [I] = P [I]; 53 sort (PS. begin (), PS. end (); 54 ps. erase (unique (PS. begin (), PS. end (), PS. end (); 55 for (INT I = 0; I <n; ++ I) 56 {57 P [I] = 1 + distance (PS. begin (), lower_bound (PS. begin (), PS. end (), P [I]); 58} 59 return ps. size (); 60} 61 bool used [maxn]; 62 vector <int> line [maxn]; // Scan Line 63 int main () {64 int N; 65 while (scanf ("% d", & N )! = EOF) {66 T. init (maxn); 67 memset (used, false, sizeof (used); 68 for (INT I = 0; I <n; ++ I) scanf ("% d", & X [I], & Y [I]); 69 int W = compress (x, N ); 70 int H = compress (Y, n); 71 for (INT I = 0; I <n; ++ I) {72 line [Y [I]. push_back (X [I]); 73} 74 ll ans = N; 75 for (INT I = 1; I <= H; ++ I) {76 vector <int> & V = line [I]; 77 sort (v. begin (), V. end (); 78 for (vector <int>: iterator J = v. begin (); J! = V. end (); ++ J) {79 int x = * j; 80 ll s = T. query (x, x); 81 If (used [x]) ans + = s; 82 else used [x] = true; 83 t. update (x, x,-S); 84 If (J! = V. end ()-1) T. update (x + 1, * (J + 1)-1,1); 85} 86} 87 printf ("% LLD \ n", ANS); 88} 89}
View code

 

 

 

 

 

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.