bzoj4571 美味

美味

题目背景:

bzoj4571

分析: 首先,明确一个异或的套路,高位贪心,然后我们就可以发现,想要每一次求出最大值,就是针对给出的bi进行高位贪心,现在我们来一步步解决此题,从简化版开始考虑。

1、     有每一道菜的美味值ai,并且没有lr的限制,也没有偏爱值xi

方法:因为需要满足高位贪心,那么,我们只需要将每一个ai计入一个trie树中,然后将每一个bi放在trie上跑,每次判定是否在当前的位置上是否有与之不同的一个(为0,找1,为1,找0),然后走到最后一个即可。

2、     有每一道菜的美味值ai,没有lr的限制,有偏爱值xi

方法:因为这一次有了偏爱值的限制,那么显然的,我们不能直接将值放入trie树,然后我们考虑,我们每一次寻找的都是一段权值的区间,那么我们将这个区间的最小和最大都减去xi,所得到的就是我们想要找的区间,那么如何求的在这样一个值域中是否存在有数呢,那么用一个权值线段树就可以简单的维护啦

3、     现在我们考虑题目的最终版,有lr的限制,有偏爱值xi

方法:有了lr那么每一次我们该如何查找呢,这意味着我们需要在每一位上建立权值线段树,那么结果当然就是主席树了,每一次加入一个树然后对于询问的区间查找在当前的区间中,是否存在当前值域限制中的数即可,最终的复杂度应该是 O((询问个数) * (位数的枚举) * (主席树)) = O(nlog2n)

Source

#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 bool iosig; static char c; 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 + 10; const int MAXM = 100000 + 10; int n, m, b, x, l, r, tot; int root[MAXN], judge[MAXN]; struct node { int left; int right; int sum; } tree[MAXN * 40]; inline void readin() { R(n), R(m); for (int i = 1; i <= n; ++i) R(judge[i]); } inline void build(int x, int &y, int l, int r, int num) { tree[++tot] = tree[x], y = tot; tree[y].sum++; if (l == r) return ; int mid = l + r >> 1; if (num <= mid) build(tree[x].left, tree[y].left, l, mid, num); else build(tree[x].right, tree[y].right, mid + 1, r, num); } inline int query(int L, int R, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return tree[R].sum - tree[L].sum; int mid = l + r >> 1, ret = 0; if (ql <= mid) ret += query(tree[L].left, tree[R].left, l, mid, ql, qr); if (qr > mid) ret += query(tree[L].right, tree[R].right, mid + 1, r, ql, qr); return ret; } inline void work() { root[0] = 0; for (int i = 1; i <= n; ++i) build(root[i - 1], root[i], 0, MAXM, judge[i]); for (int i = 1; i <= m; ++i) { R(b), R(x), R(l), R(r); int target = 0; for (int i = 17; i >= 0; --i) { /*高位贪心,从最高位开始判断是否能取到相反的位置*/ if (b & (1 << i)) { if (target + (1 << i) - 1 - x < 0) target |= (1 << i); else if (!query(root[l - 1], root[r], 0, MAXM, max(target - x, 0), min(target + (1 << i) - 1 - x, MAXM))) target |= (1 << i); } else { target |= (1 << i); if (target + (1 << i) - 1 - x < 0) target -= (1 << i); else if (!query(root[l - 1], root[r], 0, MAXM, max(target - x, 0), min(target + (1 << i) - 1 - x, MAXM))) target -= (1 << i); } } cout << (target ^ b) << '\n'; } } int main() { //freopen("food.in", "r", stdin); //freopen("food.out", "w", stdout); readin(); work(); flush(); return 0; } 
            
           
          
         
       
      
      
     
     
    
    
   
   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值