[关闭]
@ysner 2018-08-03T17:32:57.000000Z 字数 1706 阅读 1806

[HEOI2013]ALO

可持久化Trie Trie


题面

现在你拥有颗宝石,每颗宝石有一个能量密度,记为。现在你可以选取连续的一些宝石(必须多于一个)进行融合,则融合而成的宝石的能量密度为这些宝石中能量密度的次大值与其他任意一颗宝石的能量密度按位异或的值
现在你需要知道你怎么选取需要融合的宝石,才能使生成的宝石能量密度最大。

题面

显然要可持久化树。
唯一有点费脑子的地方是次大值

在一个区间找次大值会把复杂度弄得很鬼。
换个角度想,如果枚举次大值,则有两种情况:

实际上我们可以用链表维护大小关系。
从小到大枚举,用完这个数就把它删掉,这样左右端就是两个比它大的数,可以确定区间左右端点。
于是分情况统计答案即可。
不加rt调1h系列

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<vector>
  8. #define re register
  9. #define il inline
  10. #define ll long long
  11. #define max(a,b) ((a)>(b)?(a):(b))
  12. #define min(a,b) ((a)<(b)?(a):(b))
  13. #define fp(i,a,b) for(re int i=a;i<=b;i++)
  14. #define fq(i,a,b) for(re int i=a;i>=b;i--)
  15. using namespace std;
  16. const int mod=1e9+7,N=1e5+100;
  17. struct dat{int w,id;bool operator < (const dat &o) const {return w<o.w;}}a[N];
  18. int ysn,sum[N*50],t[N*50][2],l[N],r[N],ans,n,rt[N*50];
  19. il ll gi()
  20. {
  21. re ll x=0,t=1;
  22. re char ch=getchar();
  23. while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
  24. if(ch=='-') t=-1,ch=getchar();
  25. while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
  26. return x*t;
  27. }
  28. il void Build(re int x,re int &y,re int w,re int dep)
  29. {
  30. sum[y=++ysn]=sum[x]+1;
  31. if(dep<0) return;
  32. re int p=(w>>dep)&1;
  33. t[y][p^1]=t[x][p^1];
  34. Build(t[x][p],t[y][p],w,dep-1);
  35. }
  36. il int Query(re int x,re int y,re int w,re int dep)
  37. {
  38. if(dep<0) return 0;
  39. re int p=(w>>dep)&1,tmp=sum[t[y][p^1]]-sum[t[x][p^1]];
  40. if(tmp) return (1<<dep)+Query(t[x][p^1],t[y][p^1],w,dep-1);
  41. else return Query(t[x][p],t[y][p],w,dep-1);
  42. }
  43. int main()
  44. {
  45. n=gi();
  46. fp(i,1,n) a[i].w=gi(),a[i].id=i,l[i]=i-1,r[i]=i+1,Build(rt[i-1],rt[i],a[i].w,30);
  47. sort(a+1,a+1+n);
  48. fp(i,1,n)
  49. {
  50. re int x=a[i].id,z=l[x],y=r[x];
  51. r[z]=y;l[y]=z;
  52. if(z) ans=max(ans,Query(rt[l[z]],rt[y-1],a[i].w,30));
  53. if(y<=n) ans=max(ans,Query(rt[z],rt[r[y]-1],a[i].w,30));
  54. }
  55. printf("%d\n",ans);
  56. return 0;
  57. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注