1335: Gaoqiao and Low-Bridge Time limit:1 sec Memory limit:128 MB
Submit:362 Solved:62
[Submit] [Status] [web board] Description
There was a brain emergency like this: There was a very close one high one low two bridges, after the two floods, Takahashi was flooded twice, but the low bridge was only flooded once, why? The answer is: Because the Low bridge is too low, the water level is still above the low bridge after the first flood, so it is not "flooded twice ". Example:
Assume that the height of the bridge is 5 and 2 respectively, and the initial water level is 1.
First Flood: the water level is increased to 6 (both bridges are flooded) and 2 (the bridge is no longer flooded, but the bridge is still flooded)
Second Flood: Raise the water level to 8 (Takahashi was flooded again) and return to 3.
That's right, text games. The key lies in the meaning of "again. If a bridge is still flooded after a flood exits (that is, the water level is not greater than the height of the Bridge), the next flood may not increase the water level.
Enter the height of the N bridges and the I-flood rising water level AI and the back water level Bi to calculate how many bridges are down K times. The initial water level is 1, and the rising water level of each flood must be greater than that of the previous flood.
Input
The input file contains up to 25 groups of test data. The first behavior of each group of data is three integers: n, m, K (1 <= n, m, k <= 105 ). The second behavior is an integer of n hi (2 <= Hi <= 108), that is, the height of each bridge. Each row of the following m rows contains two integers: AI and Bi (1 <= Bi <= 108, AI> bi-1 ). The input file cannot exceed 5 MB.
Output
For each group of data, the number of bridges that are flooded at least K times is output.
Sample Input
2 2 22 56 28 35 3 22 3 5 65 34 25 2
Sample output
Case 1: 1 case 2: 3
Hint Because the range of N and M is 10 to the power of 5, there is a total of 3*10 to the power of 5, and the bridge height and the tide height is the maximum of 10 to the power of 8, the array must not be saved. Discretization is used to convert discontinuous numbers into continuous ones, greatly reducing the memory size. Modify the interval to calculate the point (downward modification and upward statistics)
It is useless to deal with the height of the tide, and add a data before the first tide as the initial water level 1.
Note: If the tide rises from 2 to 6, the modified range is 3-6, and the bridge height is 2, which is not flooded.
# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # define maxn 300010 using namespace STD; int n, m, K, num; int C [maxn], F [maxn]; int lowbit (int x) {return X &-X;} void Update (int x, int V) {While (x> 0) {C [x] + = V; X-= lowbit (x) ;}} int getsum (int x) {int ans = 0; while (x <= num) {ans + = C [X]; x + = lowbit (x);} return ans;} struct node {int H, I ;} A [maxn]; bool CMP (node X, node y) {return X. h <Y. h;} int main () {Int CAS = 0; while (~ Scanf ("% d", & N, & M, & K) & (N | M | K) {memset (C, 0, sizeof C); memset (F, 0, sizeof F); num = n + 2 * m; For (INT I = 1; I <= num + 1; I ++) {if (I = n + 1) {A [I]. H = 1; // process a [I]. I = I;} else {scanf ("% d", & A [I]. h); A [I]. I = I ;}} sort (a + 1, A + num + 1, CMP); // start discretization f [A [1]. i] = 1; for (INT I = 2; I <= num; I ++) {// change the relative size if (a [I]. h! = A [I-1]. h) {f [A [I]. i] = I;} else f [A [I]. i] = f [A [I-1]. i];} // end discretization for (INT I = n + 1; I <= num; I + = 2) {Update (F [I],-1 ); update (F [I + 1], 1); // downward update} int ans = 0; For (INT I = 1; I <= N; I ++) {If (getsum (F [I])> = k) ans ++; // up statistics} printf ("case % d: % d \ n", ++ cas, ans);} return 0 ;}