TOJ 1550 Fiber Communications -- 枚举 + 线段树

本文详细介绍了使用线段树优化解决环形问题的算法过程,包括枚举环的缺口和线段树的更新优化,旨在提高解决此类问题的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

分析:枚举环的缺口,其中在每一次枚举中我用线段树做的统计。另一个优化是线段树更新时,如果区间已经被全部覆盖,就不必更新了(不加这个优化会TLE)。

#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 = 1000 + 2; const int MAX_P = 10000 + 2; pii edge[MAX_P]; struct { int left; int right; int cnt; int sum; inline int mid() { return (left + right) >> 1; } } st[MAX_N * 6]; void build(int l, int r, int idx) { st[idx].left = l; st[idx].right = r; st[idx].cnt = st[idx].sum = 0; if (l != r) { int mid = st[idx].mid(); build(l, mid, LL(idx)); build(mid + 1, r, RR(idx)); } } inline void update(int idx) { if (st[idx].cnt) { st[LL(idx)].cnt += st[idx].cnt; st[LL(idx)].sum = st[LL(idx)].right - st[LL(idx)].left + 1; st[RR(idx)].cnt += st[idx].cnt; st[RR(idx)].sum = st[RR(idx)].right - st[RR(idx)].left + 1; st[idx].cnt = 0; } } void update(int l, int r, int v, int idx) { if (st[idx].sum == st[idx].right - st[idx].left + 1) { return; } if (l <= st[idx].left && st[idx].right <= r) { st[idx].cnt += v; st[idx].sum = st[idx].right - st[idx].left + 1; return; } update(idx); int mid = st[idx].mid(); if (l <= mid) update(l, r, v, LL(idx)); if (r > mid) update(l, r, v, RR(idx)); st[idx].sum = st[LL(idx)].sum + st[RR(idx)].sum; } int main(int argc, char *argv[]) { // freopen("D:\\in.txt", "r", stdin); int n, p; cin >> n >> p; for (int i = 0; i < p; ++i) { scanf("%d%d", &edge[i].X, &edge[i].Y); } int ans = n - 1; for (int i = 1; i <= n; ++i) { int s = i, t = s + n - 1; build(s, t - 1, 1); for (int j = 0; j < p; ++j) { int x = edge[j].X, y = edge[j].Y; if (x < s) x += n; if (y < s) y += n; if (x > y) x += y, y = x - y, x -= y; update(x, y - 1, 1, 1); } if (ans > st[1].sum) ans = st[1].sum; } cout << ans << endl; return 0; } 
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
       
      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值