CSU 1335: Takahashi and Low bridge (binary lookup, tree-like array)

Source: Internet
Author: User

Description

One of the brain teasers is this: there is a very close to a low two bridge, two floods after the Takahashi was flooded two times, the low bridge was only flooded once, why? The answer is: Because the low bridge is too low, the first flood after the water is still on the low bridge, so does not count as "flooded two times." To illustrate:

It is assumed that the height of Takahashi and Low Bridge is 5 and 2, the initial water level is 1

First flood: water level raised to 6 (two bridges flooded), retreated to 2 (Takahashi no longer flooded, but low bridge is still flooded)

Second flood: The water level was raised to 8 (Takahashi was flooded again) and retreated to 3.

Yes, word games. The key is the meaning of "another". If a bridge is still flooded after a flood has receded (that is, the water level is not less than the height of the bridge), then the next flood will not be "flooded" once again.

Enter the height of the N bridge and the swollen level AI and water level bi for the flood of the first I, and count how many bridges have been flooded at least k times. The initial water level is 1, and the swollen water level of each flood must be greater than the water level of the last flood.

Input

The input file contains up to 25 sets 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 n integers hi (2<=hi<=108), which is the height of each bridge. The following m lines contain two integers of AI and bi (1<=bi<ai<=108, ai>bi-1) per line. The input file does not exceed 5MB.

Output

For each set of data, output the number of bridges that have been flooded at least k times.

Sample Input
2 2 22 56 28 35 3 22 3 4 5 65 34 25 2
Sample Output
Case 1:1case 2:3
One of the key words of the topic: the initial water level of 1, and each flood swollen water level must be greater than the last flood of the water level.
Solution, Segment tree statistics + binary search optimization
AC Code 1:
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5 using namespacestd;6 Const intN = 1e5+Ten;7 intC[n],m,n,k,a[n];8 intX[n],y[n];9 intLowbit (intk)Ten { One     returnk& (-k); A } - voidAddintKintHe//add a value for each item - { the      while(k>0) -     { -c[k]+=he; -k-=Lowbit (k); +     } - } + intSearchintx) A  { at      inttmp=x; -      intL=1; -      intR=N; -      intmid; -       while(l<R) -      { in          //printf ("%d%d\n", l,r); -Mid= (l+r) >>1); to          if(a[mid]>=tmp) r=mid; +          ElseL=mid+1; -      } the      returnR; * } $ intQ (intk)Panax Notoginseng { -     intquery=0; the      while(k<=N) +     { Aquery+=C[k]; thek+=Lowbit (k); +     } -     returnquery; $ } $ intMain () - { -     intT from, to,he,kkk=1; the      while(~SCANF ("%d%d%d",&n,&m,&k)) -     {WuyiMemset (c,0,sizeof(c)); the        for(intI=1; i<=n;i++) -scanf"%d",&a[i]); WuSort (A +1, a+n+1); -         for(intI=1; i<=m;i++) About        { $scanf"%d%d",&x[i],&y[i]); -            if(i = =1)  -            { -                 from=i; Ato =search (x[i]); +            } the           Else -           { $                from= Search (y[i-1]); theto =search (x[i]); the           } theAdd from,-1); theAdd (To,1); -        } in         intAns =0 ; the          for(intI=1; i<=n;i++) the         { About             intKK =Q (i); the             if(kk>=k) theAns = ans+1; the         } +printf"Case %d:%d\n", kkk++, ans); -     } the     return 0;Bayi}

AC Code Two:

1#include <iostream>2#include <algorithm>3#include <cstring>4#include <string>5#include <cmath>6#include <cstdio>7#include <queue>8 #defineMAXN 1000009 using namespacestd;Ten intV[MAXN]; One intF[MAXN]; A intMain () - { -     intn,m,qu,i,j,k,sum,a,b,t,da,o=1; the     intLpintRpint*p; -      while(~SCANF (" %d%d%d",&n,&m,&qu)) -     { -Memset (F,0,sizeof(f)); +a=-1, sum=0, da=0; -          for(i=0; i<n;i++) +scanf"%d",&v[i]); ASort (v,v+n); at          for(i=0; i<m;i++) -         { -scanf"%d%d",&b,&t); -Lp=lower_bound (v,v+n,a+1)-v; -Rp=lower_bound (v,v+n,b+1)-v; -f[lp]+=1; inf[rp]-=1; -A=T; to         } +          for(i=0; i<n;i++) -         { thef[i]+=sum; *sum=F[i]; $             if(f[i]>=qu)Panax Notoginsengda++; -             //cout<<f[i]<< ' \ t '; the         } +printf"Case %d:%d\n", o++, DA); A     } the     return 0; +}

CSU 1335: Takahashi and Low bridge (binary lookup, tree-like 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.