题目描述:
题解:
Trie树。从左向右扫一遍,然后从右向左扫一遍。
代码:
#include#include #include using namespace std;#define N 400050#define M 31*Ninline int rd(){ int f=1,c=0;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){c=10*c+ch-'0';ch=getchar();} return f*c;}int n,p[N];int l[N],r[N];struct Trie{ int ch[M][2],siz[M],tot; void insert(int x) { int u=0; for(int i=29;i>=0;i--) { int k = (x>>i)&1; if(!ch[u][k])ch[u][k]=++tot; u=ch[u][k];siz[u]++; } } int query(int x) { int u = 0,ret = 0; for(int i=29;i>=0;i--) { int k = (x>>i)&1; if(siz[ch[u][!k]]) { ret|=(1< =1;i--) { r[i]=max(r[i+1],tr.query(p[i])); tr.insert(p[i]); } int ans = 0; for(int i=1;i