BJ模拟(2) D1T2 摩尔庄园

 摩尔庄园

题目背景:

这个题并没有他的题目那么弱智······

我们先考虑,题目给出的条件造就了一个非常优美的性质,就是最终建出来的是一棵完全二叉树,树高是log的,那么我们完全可以采取暴力爬树高·····首先可以发现,30%的数据是非常简单的直接暴力建边,每一次强行跑一次费用流就可以了,但是这样的复杂度显然是不对的,那么我们考虑怎么做,首先,我们发现当我们确定好一直拉姆的路径后,如果之后的某一只拉姆和这只拉姆的路径有相对的运动,那么其实可以找到更优的方法,比如有一个拉姆从上到下,另一只从下到上且路径有所重合,那么我们完全可以让在上面的直接走上面,下面的直接走下面而使路径不会交叉,从而更优,那么我们就可以采取打标记的方式,标记在当前路径上走过的拉姆的方向,如果发现相逆的方向就直接-2就好,然后暴力更改走过路径的mnid已经mark就可以了(有log树高的保证)

Source

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             using namespace std; inline char read() { static const int IN_LEN = 1024 * 1024; static char buf[IN_LEN], *s, *t; if (s == t) { t = (s = buf) + fread(buf, 1, IN_LEN, stdin); if (s == t) return -1; } return *s++; } template 
            
              inline bool R(T &x) { static char c; static bool iosig; for (c = read(), iosig = false; !isdigit(c); c = read()) { if (c == -1) return false; if (c == '-') iosig = true; } for (x = 0; isdigit(c); c = read()) x = (x << 3) + (x << 1) + (c ^ '0'); if (iosig) x = -x; return true; } const int OUT_LEN = 1024 * 1024; char obuf[OUT_LEN], *oh = obuf; inline void writechar(char c) { if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf; *oh++ = c; } template 
             
               inline void W(T x) { static int buf[30], cnt; if (!x) writechar(48); else { if (x < 0) writechar('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) writechar(buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); } const int MAXN = 200000 + 50; int n, m; int c[MAXN], p[MAXN]; struct data { int mn, id, mark; /*mn : 从当前结点到自己的子树中最近的有食物的地方所需要的代价*/ /*id : 从当前点出发能够到的在自己子树中的代价最小的点的编号*/ /*mark : 标记为向上或向下*/ } node[MAXN]; inline int get_lca(int a, int b) { while (a != b) { if (a < b) swap(a, b); a >>= 1; } return a; } inline void readin() { R(n), R(m); for (int i = 1; i <= n; ++i) R(c[i]); for (int i = 1; i <= m; ++i) R(p[i]); } inline void pre() { for (int i = 1; i < 2 * n + 50; ++i) node[i].mn = ~0u >> 2; /*注意初始化只能初始化mn数组,剩下两个保持为0*/ for (int i = n; i >= 1; --i) { if (c[i]) node[i].mn = 0, node[i].id = i; if (i == 1) break; int fa = (i >> 1); if (node[fa].mn > node[i].mn + 1) node[fa].mn = node[i].mn + 1, node[fa].id = node[i].id; } } inline void work() { long long ans = 0; for (int i = 1; i <= m; ++i) { int u = p[i], pos = -1, dis = ~0u >> 2; for (int j = u, d = 0; j >= 1; j >>= 1) { if (d + node[j].mn < dis) dis = node[j].mn + d, pos = node[j].id; d++, d -= 2 * (node[j].mark < 0); } ans += (long long)dis; int lca = get_lca(u, pos); for (int j = u; j != lca; j >>= 1) node[j].mark++; for (int j = pos; j != lca; j >>= 1) node[j].mark--; c[pos]--; if (!c[pos]) node[pos].mn = ~0u >> 2, node[pos].id = -1; for (int j = u; j; j >>= 1) { if (c[j]) node[j].mn = 0, node[j].id = j; else { node[j].mn = ~0u >> 2, node[j].id = -1; int lc = (j << 1), rc = (j << 1 | 1); if (node[j].mn > node[lc].mn - 2 * (node[lc].mark > 0) + 1) node[j].mn = node[lc].mn - 2 * (node[lc].mark > 0) + 1, node[j].id = node[lc].id; if (node[j].mn > node[rc].mn - 2 * (node[rc].mark > 0) + 1) node[j].mn = node[rc].mn - 2 * (node[rc].mark > 0) + 1, node[j].id = node[rc].id; } } for (int j = pos; j >= 1; j >>= 1) { if (c[j]) node[j].mn = 0, node[j].id = j; else { node[j].mn = ~0u >> 2, node[j].id = -1; int lc = (j << 1), rc = (j << 1 | 1); if (node[j].mn > node[lc].mn - 2 * (node[lc].mark > 0) + 1) node[j].mn = node[lc].mn - 2 * (node[lc].mark > 0) + 1, node[j].id = node[lc].id; if (node[j].mn > node[rc].mn - 2 * (node[rc].mark > 0) + 1) node[j].mn = node[rc].mn - 2 * (node[rc].mark > 0) + 1, node[j].id = node[rc].id; } } W(ans), writechar(' '); } } int main() { // freopen("in.in", "r", stdin); readin(); pre(); work(); flush(); return 0; } 
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值