zoukankan      html  css  js  c++  java
  • 【bzoj4137】[FJOI2015]火星商店问题

    *题目描述:
    火星上的一条商业街里按照商店的编号1,2 ,…,n ,依次排列着n个商店。商店里出售的琳琅满目的商品中,每种商品都用一个非负整数val来标价。每个商店每天都有可能进一些新商品,其标价可能与已有商品相同。
    火星人在这条商业街购物时,通常会逛这条商业街某一段路上的所有商店,譬如说商店编号在区间[L,R]中的商店,从中挑选1件自己最喜欢的商品。每个火星人对商品的喜好标准各不相同。通常每个火星人都有一个自己的喜好密码x。对每种标价为val的商品,喜好密码为x的火星人对这种商品的喜好程度与val异或x的值成正比。也就是说,val xor x的值越大,他就越喜欢该商品。每个火星人的购物卡在所有商店中只能购买最近d天内(含当天)进货的商品。另外,每个商店都有一种特殊商品不受进货日期限制,每位火星人在任何时刻都可以选择该特殊商品。每个商店中每种商品都能保证供应,不存在商品缺货的问题。
    对于给定的按时间顺序排列的事件,计算每个购物的火星人的在本次购物活动中最喜欢的商品,即输出val xor x的最大值。这里所说的按时间顺序排列的事件是指以下2种事件:
    事件0,用三个整数0,s,v,表示编号为s的商店在当日新进一种标价为v 的商品。
    事件1,用5个整数1,L,R,x,d,表示一位火星人当日在编号为L到R的商店购买d天内的商品,该火星人的喜好密码为x。
    *输入:
    第1行中给出2个正整数n,m,分别表示商店总数和事件总数。
    第2行中有n个整数,第i个整数表示商店i的特殊商品标价。
    接下来的m行,每行表示1个事件。每天的事件按照先事件0,后事件1的顺序排列。
    *输出:
    将计算出的每个事件1的val xor x的最大值依次输出。
    *样例输入:
    4 6
    1 2 3 4
    1 1 4 1 0
    0 1 4
    0 1 3
    1 1 1 1 0
    1 1 1 1 1
    1 1 2 1 2
    *样例输出:
    5
    0
    2
    5
    *提示:
    n, m <= 100000
    数据中,价格不大于100000
    *题解:
    线段树套Trie树。永久的商品用可持久化Trie来查,有时间的就用树套树来查。(我在bzoj上二分内存树套树终于卡过啦!~)我这里用了一种方法,就是每次查询把线段树的所有区间全部取出来,然后在这些区间上一起二分。
    *代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    
    #ifdef WIN32
        #define LL "%I64d"
    #else
        #define LL "%lld"
    #endif
    
    #ifdef CT
        #define debug(...) printf(__VA_ARGS__)
        #define setfile() 
    #else
        #define debug(...)
        #define filename ""
        #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    #endif
    
    #define R register
    #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1 << 15], *S = B, *T = B;
    inline int FastIn()
    {
        R char ch; R int cnt = 0; R bool minus = 0;
        while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
        ch == '-' ? minus = 1 : cnt = ch - '0';
        while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
        return minus ? -cnt : cnt;
    }
    #define maxn 100010
    #define maxtot 10000010
    namespace Functional_Trie
    {
        int root[maxn], ch[maxtot][2], tot, size[maxtot];
        inline void Insert(R int last, R int val)
        {
            for (R int i = 17; i >= 0; --i)
            {
                R int t = 1 << i;
                size[++tot] = size[last] + 1;
                if (val & t)
                {
                    ch[tot][0] = ch[last][0];
                    ch[tot][1] = tot + 1;
                    last = ch[last][1];
                }
                else
                {
                    ch[tot][0] = tot + 1;
                    ch[tot][1] = ch[last][1];
                    last = ch[last][0];
                }
            }
            size[++tot] = size[last] + 1;
        }
        inline int Query(R int l, R int r, R int val)
        {
            l = root[l - 1], r = root[r];
            R int ans = 0;
            for (R int i = 17; i >= 0; --i)
            {
                R int t = 1 << i, d = (t & val) >> i;
                if (ch[r][!d] - ch[l][!d] > 0)
                {
                    ans |= t;
                    l = ch[l][!d];
                    r = ch[r][!d];
                }
                else
                {
                    l = ch[l][d];
                    r = ch[r][d];
                }
            }
            return ans;
        }
    }
    int M, day, qcnt, q[maxn], tr[1 << 22], ch[maxtot][2], ncnt, tim[maxtot];
    inline void Insert(R int now, R int val)
    {
        tim[now] = day;
        for (R int i = 17; i >= 0; --i)
        {
            R int t = 1 << i, d = (val & t) >> i;
            if (!ch[now][d])
                ch[now][d] = ++ncnt;
            now = ch[now][d];
            tim[now] = day;
        }
    }
    inline void Change(R int x, R int val)
    {
        R int i = x + M; Insert(tr[i], val); i >>= 1;
        for (; i; i >>= 1) Insert(tr[i], val);
    }
    inline void Query(R int s, R int t)
    {
        qcnt = 0;
        for (s = s + M - 1, t = t + M + 1; s ^ t ^ 1; s >>= 1, t >>= 1)
        {
            if (~s & 1) q[++qcnt] = tr[s ^ 1];
            if (t & 1) q[++qcnt] = tr[t ^ 1];
        }
    }
    int main()
    {
        R int n = FastIn(), m = FastIn();
        for (M = 1; M < n; M <<= 1);
        for (R int i = M << 1; i; --i) tr[i] = ++ncnt;
        for (R int i = 1; i <= n; ++i)
        {
            R int v = FastIn();
            Functional_Trie::root[i] = Functional_Trie::tot + 1;
            Functional_Trie::Insert(Functional_Trie::root[i - 1], v);
        }
        for (R int _ = 1; _ <= m; ++_)
        {
            R int opt = FastIn();
            if (opt == 0)
            {
                R int s = FastIn(), v = FastIn();
                Change(s - 1, v);
                ++day;
            }
            else
            {
                R int l = FastIn(), r = FastIn(), x = FastIn(), dd = FastIn();
                R int ans = Functional_Trie::Query(l, r, x);
                Query(l - 1, r - 1);
                R int tmp = 0;
                for (R int i = 17; i >= 0; --i)
                {
                    R bool flag = 0;
                    R int t = 1 << i, d = (x & t) >> i;
                    for (R int j = 1; j <= qcnt && !flag; ++j)
                        if (ch[q[j]][!d] && tim[ch[q[j]][!d]] >= day - dd)
                            flag = 1;
                    if (flag)
                    {
                        tmp |= t;
                        for (R int j = 1; j <= qcnt; ++j)
                            q[j] = ch[q[j]][!d];
                    }
                    else for (R int j = 1; j <= qcnt; ++j) q[j] = ch[q[j]][d];
                }
                printf("%d
    ", dmax(tmp, ans) );
            }
        }
        return 0;
    }
  • 相关阅读:
    golang json字符串合并操作
    goland 无法跳转 struct等
    golang 中mgo update报错: The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage.
    解决windows下使用vscode没有函数提示的问题
    【转载,实测好用】gitlab结合sourcetree使用
    C++单继承、多继承情况下的虚函数表分析
    Linux 日志文件管理——限制大小
    C++ RCSP智能指针简单实现与应用
    Makefile模板(C++)
    Git关于pull,commit,push的总结
  • 原文地址:https://www.cnblogs.com/cocottt/p/5550956.html
Copyright © 2011-2022 走看看