]]>pku 2104 K-th Number__綰挎鏍?/title>http://m.shnenglu.com/Ylemzy/articles/116237.html鐏⒊榛?/dc:creator>鐏⒊榛?/author>Mon, 24 May 2010 09:50:00 GMThttp://m.shnenglu.com/Ylemzy/articles/116237.htmlhttp://m.shnenglu.com/Ylemzy/comments/116237.htmlhttp://m.shnenglu.com/Ylemzy/articles/116237.html#Feedback0http://m.shnenglu.com/Ylemzy/comments/commentRss/116237.htmlhttp://m.shnenglu.com/Ylemzy/services/trackbacks/116237.htmlK-th Number
Description
You are
working for Macrohard company in data structures department. After
failing your previous task about key insertion you were asked to write a
new data structure that would be able to return quickly k-th order
statistics in the array segment.
That is, given an array a[1...n] of different integer numbers, your
program must answer a series of questions Q(i, j, k) in the form: "What
would be the k-th number in a[i...j] segment, if this segment was
sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the
question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort
this segment, we get (2, 3, 5, 6), the third number is 5, and therefore
the answer to the question is 5.
Input
The first line of the input file contains n ---
the size of the array, and m --- the number of questions to answer (1
<= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding
109 by their absolute values --- the array for which the
answers should be given.
The following m lines contain question descriptions, each
description consists of three numbers: i, j, and k (1 <= i <= j
<= n, 1 <= k <= j - i + 1) and represents the question Q(i, j,
k).
Output
For
each question output the answer to it --- the k-th number in sorted
a[i...j] segment.
Sample Input
7 3 1 5 2 6 3 7 4 2 5 3 4 4 1 1 7 3
Sample Output
5 6 3
浠g爜錛?br>
#include<stdio.h> #include<stdlib.h> #define maxn 100010 struct T { int l, r, dep; T * pl, *pr; }fn[maxn *3]; int th =0, a, b, sort[30][maxn], s[maxn], s1, s2; T * build(int l, int r, int dep) { T * p =&fn[th++]; p->l = l, p->r = r, p->dep = dep; if (l == r) { sort[dep][l] = s[l]; } else { int m = (l + r) /2, i, j, k; p->pl = build(l, m, dep +1), p->pr = build(m +1, r, dep +1); for (k = i = l, j = m +1;i <= m && j <= r;) { if (sort[dep +1][i] < sort[dep +1][j]) { sort[dep][k++] = sort[dep +1][i++]; } else { sort[dep][k++] = sort[dep +1][j++]; } } for (; i <= m; i++) { sort[dep][k++] = sort[dep +1][i]; } for (; j <= r; j++) { sort[dep][k++] = sort[dep +1][j]; } } return p; } int query(T * p, int num) { int m, l, r, t; //紜畾鎵璇㈤棶鍖洪棿閲屾帓鍦╪um鍓嶇殑鏁扮殑涓暟 if (a <= p->l && p->r <= b) { l = p->l, r = p->r; if (sort[p->dep][p->r] <= num) { return r - l +1; } while (l < r) { m = (l + r) /2; if (sort[p->dep][m] > num) { r = m; } else { l = m +1; } } return r - p->l; } else { m = (p->l + p->r) /2; t =0; if (a <= m) { t += query(p->pl, num); } if (b > m) { t += query(p->pr, num); } return t; } } int main() { int n, m, i, l, r, mid, pre, c; scanf("%d%d", &n, &m); for (i =1; i <= n; i++) { scanf("%d", &s[i]); } T * tree = build(1, n, 0); while (m--) { scanf("%d%d%d", &a, &b, &c); l =1, r = n; while (l < r)//浜屽垎鏋氫婦鏁版潵紜畾鍏跺悕嬈?/span> { mid = (l + r) /2; pre = query(tree, sort[0][mid]); if (pre >= c) { r = mid; } else { l = mid +1; } } printf("%d\n", sort[0][l]); } system("pause"); return0; } /* 7 100 5 3 2 9 8 1 6
]]>pku 1151 Atlantis鈥斺旂嚎孌墊爲(wèi)http://m.shnenglu.com/Ylemzy/articles/116229.html鐏⒊榛?/dc:creator>鐏⒊榛?/author>Mon, 24 May 2010 07:52:00 GMThttp://m.shnenglu.com/Ylemzy/articles/116229.htmlhttp://m.shnenglu.com/Ylemzy/comments/116229.htmlhttp://m.shnenglu.com/Ylemzy/articles/116229.html#Feedback0http://m.shnenglu.com/Ylemzy/comments/commentRss/116229.htmlhttp://m.shnenglu.com/Ylemzy/services/trackbacks/116229.htmlAtlantis
Description
There are several ancient Greek texts that
contain descriptions of the fabled island Atlantis. Some of these texts
even include maps of parts of the island. But unfortunately, these maps
describe different regions of Atlantis. Your friend Bill has to know the
total area for which maps exist. You (unwisely) volunteered to write a
program that calculates this quantity.
Input
The input consists of several test cases. Each
test case starts with a line containing a single integer n (1 <= n
<= 100) of available maps. The n following lines describe one map
each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1
< x2 <= 100000;0 <= y1 < y2 <= 100000), not necessarily
integers. The values (x1; y1) and (x2;y2) are the coordinates of the
top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don't
process it.
Output
For
each test case, your program should output one section. The first line
of each section must be "Test case #k", where k is the number of the
test case (starting with 1). The second one must be "Total explored
area: a", where a is the total explored area (i.e. the area of the union
of all rectangles in this test case), printed exact to two digits to
the right of the decimal point.
Output a blank line after each test case.
]]>pku 3468 A Simple Problem with Integers鈥斺旂嚎孌墊爲(wèi)http://m.shnenglu.com/Ylemzy/articles/115787.html鐏⒊榛?/dc:creator>鐏⒊榛?/author>Wed, 19 May 2010 02:37:00 GMThttp://m.shnenglu.com/Ylemzy/articles/115787.htmlhttp://m.shnenglu.com/Ylemzy/comments/115787.htmlhttp://m.shnenglu.com/Ylemzy/articles/115787.html#Feedback0http://m.shnenglu.com/Ylemzy/comments/commentRss/115787.htmlhttp://m.shnenglu.com/Ylemzy/services/trackbacks/115787.htmlA Simple Problem with Integers
Description
You have N
integers, A1, A2, ... , AN.
You need to deal with two kinds of operations. One type of operation is
to add some given number to each number in a given interval. The other
is to ask for the sum of numbers in a given interval.
Input
The first line
contains two numbers N and Q. 1 ≤ N,Q ≤
100000. The second line contains N numbers, the initial values
of A1, A2, ... , AN.
-1000000000 ≤ Ai ≤ 1000000000. Each of the next Q
lines represents an operation. "C abc" means
adding c to each of Aa, Aa+1,
... , Ab. -10000 ≤ c ≤ 10000. "Q ab"
means querying the sum of Aa, Aa+1,
... , Ab.
Output
You need to answer all Q commands in
order. One answer in a line.
]]>pku 3321 Apple Tree鈥斺旀爲(wèi)鐘舵暟緇?http://m.shnenglu.com/Ylemzy/articles/115313.html鐏⒊榛?/dc:creator>鐏⒊榛?/author>Thu, 13 May 2010 14:37:00 GMThttp://m.shnenglu.com/Ylemzy/articles/115313.htmlhttp://m.shnenglu.com/Ylemzy/comments/115313.htmlhttp://m.shnenglu.com/Ylemzy/articles/115313.html#Feedback0http://m.shnenglu.com/Ylemzy/comments/commentRss/115313.htmlhttp://m.shnenglu.com/Ylemzy/services/trackbacks/115313.htmlApple Tree
Description
There is an apple tree outside of kaka's
house. Every autumn, a lot of apples will grow in the tree. Kaka likes
apple very much, so he has been carefully nurturing the big apple tree.
The
tree has N forks which are connected by branches. Kaka numbers
the forks by 1 to N and the root is always numbered by 1. Apples
will grow on the forks and two apple won't grow on the same fork. kaka
wants to know how many apples are there in a sub-tree, for his study of
the produce ability of the apple tree.
The trouble is that a new
apple may grow on an empty fork some time and kaka may pick an apple
from the tree for his dessert. Can you help kaka?
Input
The first line
contains an integer N (N 鈮?100,000) , which is the number
of the forks in the tree. The following N - 1 lines each
contain two integers u and v, which means fork u
and fork v are connected by a branch. The next line contains
an integer M (M 鈮?100,000). The following M
lines each contain a message which is either "C x"
which means the existence of the apple on fork x has been
changed. i.e. if there is an apple on the fork, then Kaka pick it;
otherwise a new apple has grown on the empty fork. or "Q x"
which means an inquiry for the number of apples in the sub-tree above
the fork x, including the apple (if exists) on the fork x Note
the tree is full of apples at the beginning
Output
For every inquiry, output the correspond
answer per line.
]]>pku 2352 Stars__鏍?wèi)鐘舵暟缁?/title>http://m.shnenglu.com/Ylemzy/articles/115305.html鐏⒊榛?/dc:creator>鐏⒊榛?/author>Thu, 13 May 2010 12:32:00 GMThttp://m.shnenglu.com/Ylemzy/articles/115305.htmlhttp://m.shnenglu.com/Ylemzy/comments/115305.htmlhttp://m.shnenglu.com/Ylemzy/articles/115305.html#Feedback0http://m.shnenglu.com/Ylemzy/comments/commentRss/115305.htmlhttp://m.shnenglu.com/Ylemzy/services/trackbacks/115305.htmlStars
Description
Astronomers often examine star maps where stars
are represented by points on a plane and each star has Cartesian
coordinates. Let the level of a star be an amount of the stars that are
not higher and not to the right of the given star. Astronomers want to
know the distribution of the levels of the stars.
For example, look at the map shown on the figure above. Level of the
star number 5 is equal to 3 (it's formed by three stars with a numbers
1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At
this map there are only one star of the level 0, two stars of the level
1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars
of each level on a given map.
Input
The first line of the input file contains a
number of stars N (1<=N<=15000). The following N lines describe
coordinates of stars (two integers X and Y per line separated by a
space, 0<=X,Y<=32000). There can be only one star at one point of
the plane. Stars are listed in ascending order of Y coordinate. Stars
with equal Y coordinates are listed in ascending order of X coordinate.
Output
The
output should contain N lines, one number per line. The first line
contains amount of stars of the level 0, the second does amount of stars
of the level 1 and so on, the last line contains amount of stars of the
level N-1.
]]>pku 2182 Lost Cows鈥斺旂嚎孌墊爲(wèi)鍏ラ棬棰?/title>http://m.shnenglu.com/Ylemzy/articles/113320.html鐏⒊榛?/dc:creator>鐏⒊榛?/author>Fri, 23 Apr 2010 04:29:00 GMThttp://m.shnenglu.com/Ylemzy/articles/113320.htmlhttp://m.shnenglu.com/Ylemzy/comments/113320.htmlhttp://m.shnenglu.com/Ylemzy/articles/113320.html#Feedback0http://m.shnenglu.com/Ylemzy/comments/commentRss/113320.htmlhttp://m.shnenglu.com/Ylemzy/services/trackbacks/113320.htmlLost Cows
Description
N (2 <= N <= 8,000) cows have unique
brands in the range 1..N. In a spectacular display of poor judgment,
they visited the neighborhood 'watering hole' and drank a few too many
beers before dinner. When it was time to line up for their evening
meal, they did not line up in the required ascending numerical order of
their brands.
Regrettably, FJ does not have a way to sort them. Furthermore, he's
not very good at observing problems. Instead of writing down each
cow's brand, he determined a rather silly statistic: For each cow in
line, he knows the number of cows that precede that cow in line that do,
in fact, have smaller brands than that cow.
Given this data, tell FJ the exact ordering of the cows.
Input
* Line
1: A single integer, N
* Lines 2..N: These N-1 lines describe the number of cows that
precede a given cow in line and have brands smaller than that cow. Of
course, no cows precede the first cow in line, so she is not listed.
Line 2 of the input describes the number of preceding cows whose brands
are smaller than the cow in slot #2; line 3 describes the number of
preceding cows whose brands are smaller than the cow in slot #3; and so
on.
Output
* Lines
1..N: Each of the N lines of output tells the brand of a cow in line.
Line #1 of the output tells the brand of the first cow in line; line 2
tells the brand of the second cow; and so on.
Sample
Input
5 1 2 1 0
Sample Output
2 4 5
#include<stdio.h> #define maxn 8000 int tree[maxn*3], s[maxn], ans[maxn]; void build(int l, int r, int depth) { tree[depth] = r - l +1; if (l != r) { int mid = (l + r) >>1; build(l, mid, (depth <<1)); build(mid +1, r, (depth <<1) +1); } } void find(int l, int r, int index, int num, int depth) { tree[depth]--; if (l == r) { ans[index] = r; return; } else { int mid = (l + r) >>1, m = depth <<1; if (num <= tree[m]) { find(l, mid, index, num, m); } else { num -= tree[m]; find(mid +1, r, index, num, m +1); } } } int main() { int n, i; while (scanf("%d", &n) != EOF) { build(1, n, 1); for (i =2, s[1] =1; i <= n; i++) { scanf("%d", &s[i]); s[i]++; } for (i = n; i >=1; i--) { find(1, n, i, s[i], 1); } for (i =1; i <= n; i++) { printf("%d\n", ans[i]); } } return0; }