TOJ 4602 高桥和低桥 -- 线段树 + 离散化

题目链接:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4602

题目大意:给出n座桥的高度与m次洪水水位(一次洪水有涨水水位和退水水位两个数据),问被淹次数不少于给定值k的桥的数量。(在某一次涨水前水位底于桥高,涨水后水位不底于桥高,则计该桥被淹一次)

分析:把桥高离散化后映射到1..kk(注意一个桥高可能对应多座桥,需记录这个数据以便计算最终答案),每一次涨水时计算被淹的桥的高度区间(映射后的值),区间左端点为涨水前水位,右端点为涨水后水位,更新相应区间就好了。

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
         #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #include 
                  
                    #define mp make_pair #define X first #define Y second #define LL(x) ((x) << 1) #define RR(x) ((x) << 1 | 1) #define MEMSET(a, b) memset(a, b, sizeof(a)) using namespace std; typedef unsigned int ui; typedef long long ll; typedef unsigned long long ull; typedef pair 
                   
                     pii; typedef vector 
                    
                      vi; typedef vi::iterator vi_it; typedef map 
                     
                       mii; typedef priority_queue 
                      
                        pqi; typedef priority_queue 
                       
                         , greater 
                        
                          > rpqi; typedef priority_queue 
                         
                           pqp; typedef priority_queue 
                          
                            , greater 
                           
                             > rpqp; const int MAX_N = 100000 + 2; int cnt[MAX_N]; int h[MAX_N]; struct { int left; int right; int cnt; inline int mid() { return (left + right) >> 1; } } st[MAX_N * 3]; void build(int l, int r, int idx) { st[idx].left = l; st[idx].right = r; st[idx].cnt = 0; if (l != r) { int mid = st[idx].mid(); build(l, mid, LL(idx)); build(mid + 1, r, RR(idx)); } } void update(int l, int r, int v, int idx) { if (l <= st[idx].left && r >= st[idx].right) { st[idx].cnt += v; return; } int mid = st[idx].mid(); if (l <= mid) { update(l, r, v, LL(idx)); } if (r > mid) { update(l, r, v, RR(idx)); } } int query(int pos, int idx) { if (st[idx].left == st[idx].right) { return st[idx].cnt; } if (st[idx].cnt) { st[LL(idx)].cnt += st[idx].cnt; st[RR(idx)].cnt += st[idx].cnt; st[idx].cnt = 0; } int mid = st[idx].mid(); if (pos <= mid) { return query(pos, LL(idx)); } else { return query(pos, RR(idx)); } } int main(int argc, char *argv[]) { // freopen("D:\\in.txt", "r", stdin); int n, m, k, a, b, i, cas = 1; while (cin >> n >> m >> k) { MEMSET(cnt, 0); for (i = 0; i < n; ++i) { scanf("%d", h + i); } sort(h, h + n); int kk = 1; cnt[0] = 1; for (i = 1; i < n; ++i) { if (h[i] == h[i - 1]) { ++cnt[kk - 1]; } else { cnt[kk] = 1; h[kk++] = h[i]; } } build(0, kk - 1, 1); b = 1; while (m--) { scanf("%d", &a); int p1 = upper_bound(h, h + kk, b) - h; int p2 = upper_bound(h, h + kk, a) - h - 1; scanf("%d", &b); if (p2 >= p1) { update(p1, p2, 1, 1); } } int ans = 0; for (i = 0; i < kk; ++i) { if (query(i, 1) >= k) { ans += cnt[i]; } } printf("Case %d: %d\n", cas++, ans); } return 0; } 
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
       
      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值