ZROI#1014

ZROI#1014

ZROI#1014

在某位置插入一个数,查询一段区间内所有数异或一个值之后的最大值,这看起来非常的数据结构,事实上这就是一道数据结构题...

题解是怎样的的呢?

\(std\) 是这种写法 \(:\) 对时间分块后用可持久化 \(Trie\) 树维护.

不会,咋办?莫慌,我们有万能的 \(vector\).

在位置 \(pos\) 插入一个数 \(x\)

v.insert ( v.begin () + pos - 1 , x )

\(-1\) 的原因是 \(vector\) 的下标从 \(0\) 开始.

查询 \([l,r]\) 中所有数异或一个 \(x\) 的最大值就 \(:\)

for (vector < int > :: iterator it = v.begin () + l - 1 ; it != v.begin () + r ; ++ it)
    ans = max ( ans , ( *it ^ x ) ) ;

为什么初始是 \(v.begin () + l - 1\) 而结束就是 \(v.begin () + r\) 呢?
因为左闭右开,其实还是因为下标从 \(0\) 开始,一个道理.

你可能疑惑,这怎么可能过啊,明明就是 \(\Theta(n^2)\).

确实...但 \(vector\) 谜一样速度谁也说不清...之前还有 \(vector\) 爆踩平衡树板子的操作呢.

当然,这样做你要略微卡卡常.

\(Code:\)

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define pii pair < int , int >
#define one first
#define two second
#define rint read<int>
#define LL long long
#define pb push_back
#define db double
#define ull unsigned long long
#define lowbit(x) ( x & ( - x ) )

using std::queue ;
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

struct ios {
    inline char gc(){
        static const int IN_LEN=1<<18|1;
        static char buf[IN_LEN],*s,*t;
        return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++;
    }

    template <typename _Tp> inline ios & operator >> (_Tp&x){
        static char ch,sgn; ch = gc(), sgn = 0;
        for(;!isdigit(ch);ch=gc()){if(ch==-1)return *this;sgn|=ch=='-';}
        for(x=0;isdigit(ch);ch=gc())x=x*10+(ch^'0');
        sgn&&(x=-x); return *this;
    }
} io ;

const int inf = 0x7f7f7f7f ;

vector < int > v ;

int main () {
    int lastans = 0 , ans ;
    int T , x , y , k ;
    bool opt , online ;
    io >> T >> online ;
    while ( T -- ) {
        io >> opt >> x >> y ;
        if ( online ) { x ^= lastans ; y ^= lastans ; }
        if ( ! opt ) v.insert ( v.begin () + x - 1 , y ) ;
        else {
            io >> k ; ans = - inf ; if ( online ) k ^= lastans ;
            for (auto it = v.begin () + x - 1 ; it != v.begin () + y ; ++ it)
                ans = max ( ans , (int)( *it ^ k ) ) ;
            printf ("%d\n" , lastans = ans ) ;
        }
    }
    return 0 ;
}
posted @ 2019-10-22 14:36  Phecda  阅读(95)  评论(0编辑  收藏  举报

Contact with me