1144 Number of stars
The problem is solved
Time limit: 564MS memory limit: 65536K
Number of submissions: 193 Hits: 43
Question types: programming language: g++; Gcc
Description
Astronomers like to watch the stars. They treat each star as a point, and the number of stars at the bottom left of each star (that is, the horizontal and vertical scales are not larger) as its rank value. The coordinates of All Stars (N of Stars) are given to calculate and output the rank of the specified number of stars. Note: There are no stars with the same coordinates
Input format
The first behavior n the coordinates of the star with the n behavior number 1 to n (coordinate with an integer) after which the M is followed by a number of M stars n<=100000m<=1000 coordinate range 0<=x,y<=1000000
Output format
It is required to output the rank of this m star sequentially, one line at a
Input sample
50 02 03 01 12 224 5
Output sample
13
Author
Admin
The number of stars on the lower left of a star is the number of other stars in the entire set of data that are equal to the x and Y coordinates of the star's coordinates. You might think of a two-dimensional, tree-like array, but X, Y is most
1000000, so large memory is not open. So put this problem "dimensionality" first: All the stars first Y coordinate from small to large order, if y the same as the x-coordinate of the small row in front; after the sequence of the array, for the first star, sweep it in front of the
I-1 a star, where the x-coordinate is less than or equal to its number, the number of stars at the bottom left of it. here we ask for the i-1 of the preceding elements and can be used in a tree-like array. Use a loop to update from 1~n;
An array of size max_x (the largest x-coordinate in the data) bit[] is initialized to 0. Because the array has been ordered in the way described above, so the 1th updated element Star[1] must not have other stars in its lower left corner (its y and x coordinates are the smallest),
Then update it Updata (star[1].x), that is, bit[star[1].x] plus 1 after the update. And so on, when the subsequent elements are updated, their y-coordinate must be larger than the previous element, so as long as the i-1 of all the elements in the bit[] is calculated.
In addition, the topic is to query the original array of numbers, but the program needs to sort them. So you can use the structure to store the information of the star, plus a variable pos to mark what the star's original subscript is. (PS: To ac this problem, because the data compares the water, its
Real can be in the statistical front I-1 elements of x-coordinate than its small number of this step can be violently swept over ... Complexity O (MN) ...
)
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <cstdlib>7#include <cctype>8#include <queue>9#include <stack>Ten#include <map> One#include <vector> A#include <Set> -#include <utility> - #definell Long Long the #defineINF 0x3f3f3f3f - #defineMax_n 100000 - #defineMax_x 1000000 - using namespacestd; + -typedefstructnode + { A intx, y; at intP//because you want to sort the array and ask for the answer to the subscript of the original arrays, we use a variable p to mark the original subscript of the element. - } node; -Node star[max_n+5]; - BOOLCMP (node A,node b)//Sort by the Y-size ascending order, Y-phase the X-smaller row in front - { - if(a.y!=b.y) in returna.y<b.y; - Else to returna.x<b.x; + } - intN,M,MAXN; the intans[max_n+5];//ans[] Array stores the rank of each star * intbit[max_x+5];//an array of statistics and arrays in a tree-like array $ intSumintPOS)Panax Notoginseng { - intres=0; the while(POS) + { Ares+=Bit[pos]; thepos-= (pos&-POS); + } - returnRes; $ } $ voidUpdata (intPosintvalue) - { - while(pos<=MAXN) the { -bit[pos]+=value;Wuyipos+= (pos&-POS); the } - } Wu intMain () - { About //freopen ("Input.txt", "R", stdin); $memset (bit,0,sizeof(bit)); -scanf"%d",&n); -maxn=-1; - for(intI=1; i<=n; i++) A { +scanf"%d%d",&star[i].x,&star[i].y); thestar[i].x++;//the tree-like subscript cannot have 0, otherwise it will die loop! -star[i].y++;//Why the horizontal ordinate all +1 $star[i].p=i; the if(star[i].x>MAXN) themaxn=star[i].x;//Update maximum x-coordinate the } theSort (star+1, star+n+1, CMP);//sort in ascending y-coordinate - // in for(intI=1; i<=n; i++) the { theAns[star[i].p]=sum (star[i].x);//calculates the number of stars at the bottom left AboutUpdata (star[i].x,1);//update bit[] array the } the // thescanf"%d",&m); + while(m--) - { the inttemp;Bayiscanf"%d",&temp); theprintf"%d\n", Ans[temp]); the } - return 0; -}
1144 Number of stars (tree-like array)