题意:给你一个数组,有两种操作,一种区间xor一个值,一个是查询区间xor的结果的种类数

做法一:
对于一个给定的区间,我们可以通过求解线性基的方式求出结果的种类数,而现在只不过将其放在线树上维护区间线性基。

用线段树去维护区间合并

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int maxn = 1e5;
  5. struct node {
  6. int bas[],lazy,st;
  7. void init() {
  8. memset(bas,,sizeof(bas));
  9. lazy = ;st = ;
  10. }
  11. int Count() {
  12. int ans = ;
  13. for(int i = ;i<=;i++) {
  14. if(bas[i]) ans++;
  15. }
  16. return ans;
  17. }
  18. void add(int x) {
  19. for(int i = ;i>=;i--) {
  20. if((x>>i)&){
  21. if(bas[i])x^=bas[i];
  22. else {
  23. bas[i] = x;
  24. break;
  25. }
  26. }
  27. }
  28. }
  29. node operator + (const node &a) const{
  30. node ans; ans.init();
  31. for(int i = ;i<=;i++) {
  32. ans.add(bas[i]);
  33. ans.add(a.bas[i]);
  34. }
  35. ans.add(st^a.st);
  36. ans.st = a.st;
  37. return ans;
  38. }
  39. }tr[*maxn],ans;
  40. int n,q;
  41. void pushdown(int st) {
  42. if(tr[st].lazy) {
  43. tr[st<<].lazy ^= tr[st].lazy;
  44. tr[st<<].st ^= tr[st].lazy;
  45. tr[st<<|].lazy ^= tr[st].lazy;
  46. tr[st<<|].st ^= tr[st].lazy;
  47. tr[st].lazy = ;
  48. }
  49. }
  50. void build(int l,int r,int st) {
  51. tr[st].init();
  52. if(l == r) {
  53. scanf("%d",&tr[st].st);
  54. return ;
  55. }
  56. int mid = (l+r)>>;
  57. build(l,mid,st<<);
  58. build(mid+,r,st<<|);
  59. tr[st] = tr[st<<] + tr[st<<|];
  60. }
  61. void update(int l,int r,int st,int L,int R,int d) {
  62. if(l>=L && r<=R) {
  63. tr[st].lazy ^= d;
  64. tr[st].st ^= d;
  65. return ;
  66. }
  67. pushdown(st);
  68. int mid = (l+r) >>;
  69. if(L<=mid) update(l,mid,st<<,L,R,d);
  70. if(R> mid) update(mid+,r,st<<|,L,R,d);
  71. tr[st] = tr[st<<] + tr[st<<|];
  72. }
  73. void Query(int l,int r,int st,int L,int R) {
  74. if(l >= L && r <= R) {
  75. ans = ans + tr[st];
  76. return ;
  77. }
  78. pushdown(st);
  79. int mid = (l+r) >>;
  80. if(L<=mid) Query(l,mid,st<<,L,R);
  81. if(R>mid) Query(mid+,r,st<<|,L,R);
  82. tr[st] = tr[st<<] + tr[st<<|];
  83. }
  84. int main() {
  85. int op,l,r,d;
  86. scanf("%d %d",&n,&q);
  87. build(,n,);
  88. while(q--) {
  89. scanf("%d %d %d",&op,&l,&r);
  90. if(op == ) {
  91. scanf("%d",&d);
  92. update(,n,,l,r,d);
  93. }
  94. else {
  95. ans.init();
  96. Query(,n,,l,r);
  97. printf("%d\n",<<ans.Count());
  98. }
  99. }
  100. return ;
  101. }

做法二:

对原序列a进行差分,使得b[i] = a[i] ^ a[i+1],那么al,al+1,al+2,ar可以构成的一个异或和,在al,bl,bl+1,bl+2,br-1中肯定也可以被构造出来,因此二者是等价的。

对于a序列的区间[l,r]的更新实际上b序列上只有bl-1和br被影响,中间的数由于是两两所以抵消掉了。

所以现在就变成了单点更新线段树维护区间线性基,然后再用一个树状数组维护l位置被修改的值,这也用到了差分,这里的作用是要求a[l]的值。

  1. #include <bits/stdc++.h>
  2. #define lson l,mid,rt<<1
  3. #define rson mid+1,r,rt<<1|1
  4. using namespace std;
  5. const int mx = 2e5 + ;
  6. int n,m,a[mx],b[mx];
  7. int pre[mx],mv;
  8. struct node{
  9. int gao[];
  10. void add(int x){
  11. for(int i=;i>=;i--){
  12. if(x&(<<i)){
  13. if(!gao[i]){
  14. gao[i] = x;
  15. break;
  16. }else x ^= gao[i];
  17. }
  18. }
  19. }
  20. node operator + (node A)const
  21. {
  22. node ret = A;
  23. for(int i=;i>=;i--)
  24. if(gao[i]) ret.add(gao[i]);
  25. return ret;
  26. }
  27. }s[mx<<];
  28. void add(int x,int v){
  29. while(x<=n){
  30. pre[x] ^= v;
  31. x += x&(-x);
  32. }
  33. }
  34. int getpre(int x){
  35. int ans = ;
  36. while(x){
  37. ans ^= pre[x];
  38. x -= x&(-x);
  39. }
  40. return ans;
  41. }
  42. void build(int l,int r,int rt)
  43. {
  44. if(l==r){
  45. b[l] = a[l]^a[l+];
  46. for(int i=;i>=;i--)
  47. if((<<i)&b[l]){
  48. s[rt].gao[i] = b[l];
  49. break;
  50. }
  51. return ;
  52. }
  53. int mid = (l+r)>>;
  54. build(lson);build(rson);
  55. s[rt] = s[rt<<] + s[rt<<|];
  56. }
  57. void update(int l,int r,int rt,int M)
  58. {
  59. if(l==r){
  60. b[l] ^= mv;
  61. memset(s[rt].gao,,sizeof(s[rt].gao));
  62. for(int i=;i>=;i--)
  63. if((<<i)&b[l]){
  64. s[rt].gao[i] = b[l];
  65. break;
  66. }
  67. return ;
  68. }
  69. int mid = (l+r)>>;
  70. if(M<=mid) update(lson,M);
  71. else update(rson,M);
  72. s[rt] = s[rt<<] + s[rt<<|];
  73. }
  74. node query(int l,int r,int rt,int L,int R)
  75. {
  76. if(L<=l&&r<=R) return s[rt];
  77. int mid = (l+r)>>;
  78. if(L<=mid&&R>mid) return query(lson,L,R) + query(rson,L,R);
  79. if(L<=mid) return query(lson,L,R);
  80. return query(rson,L,R);
  81. }
  82. int main()
  83. {
  84. scanf("%d%d",&n,&m);
  85. for(int i=;i<=n;i++) scanf("%d",a+i);
  86. if(n>) build(,n-,);
  87. int o,l,r;
  88. while(m--){
  89. scanf("%d%d%d",&o,&l,&r);
  90. if(o==){
  91. scanf("%d",&mv);
  92. add(l,mv);add(r+,mv);
  93. if(l>) update(,n-,,l-);
  94. if(r<n) update(,n-,,r);
  95. }else{
  96. int v = a[l]^getpre(l);
  97. if(n==||l==r) printf("%d\n",v?:);
  98. else{
  99. node ans = query(,n-,,l,r-);
  100. ans.add(v);
  101. int ret = ;
  102. for(int i=;i>=;i--)
  103. if(ans.gao[i]) ret++;
  104. printf("%d\n",<<ret);
  105. }
  106. }
  107. }
  108. return ;
  109. }

CodeForces - 587E[线段树+线性基+差分] ->(线段树维护区间合并线性基)的更多相关文章

  1. [ZJOI2019]语言——树剖+树上差分+线段树合并

    原题链接戳这儿 SOLUTION 考虑一种非常\(naive\)的统计方法,就是对于每一个点\(u\),我们维护它能到达的点集\(S_u\),最后答案就是\(\frac{\sum\limits_{i= ...

  2. HDU 1166 【线段树 || 树状数组,单点修改 维护区间和】

    题目链接 HDU 1166 大概题意: 第一行一个整数T,表示有T组数据.每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工 ...

  3. 【Luogu3733】[HAOI2017]八纵八横(线性基,线段树分治)

    [Luogu3733][HAOI2017]八纵八横(线性基,线段树分治) 题面 洛谷 题解 看到求异或最大值显然就是线性基了,所以只需要把所有环给找出来丢进线性基里就行了. 然后线性基不资磁撤销?线段 ...

  4. G - Greg and Array CodeForces - 296C 差分+线段树

    题目大意:输入n,m,k.n个数,m个区间更新标记为1~m.n次操作,每次操作有两个数x,y表示执行第x~y个区间更新. 题解:通过差分来表示某个区间更新操作执行的次数.然后用线段树来更新区间. #i ...

  5. CodeForces 1000C Covered Points Count(区间线段覆盖问题,差分)

    https://codeforces.com/problemset/problem/1000/C 题意: 有n个线段,覆盖[li,ri],最后依次输出覆盖层数为1~n的点的个数. 思路: 区间线段覆盖 ...

  6. Codeforces 558E A Simple Task (计数排序&&线段树优化)

    题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...

  7. [Vani有约会]雨天的尾巴(树上差分+线段树合并)

    首先村落里的一共有n座房屋,并形成一个树状结构.然后救济粮分m次发放,每次选择两个房屋(x,y),然后对于x到y的路径上(含x和y)每座房子里发放一袋z类型的救济粮. 然后深绘里想知道,当所有的救济粮 ...

  8. Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划

    A string t is called nice if a string "2017" occurs in t as a subsequence but a string &qu ...

  9. [NOIP2016 DAY1 T2]天天爱跑步-[差分+线段树合并][解题报告]

    [NOIP2016 DAY1 T2]天天爱跑步 题面: B[NOIP2016 DAY1]天天爱跑步 时间限制 : - MS 空间限制 : 565536 KB 评测说明 : 2s Description ...

随机推荐

  1. Dynamic Programming and Policy Evaluation

    Dynamic Programming divides the original problem into subproblems, and then complete the whole task ...

  2. PS把一张白色背景的图片设为透明

    方法一: 1.双击图层缩略图上的小锁图标(注意,这里不要拖动小锁进行删除锁定),弹出“新建图层”,确定 2.右键左侧第四个功能菜单,选择魔棒工具 3.用魔棒工具在白色背景区域点击一下,选中白色区域背景 ...

  3. 前端 CSS的选择器 高级选择器

    高级选择器分为: 后代选择器 儿子选择器 并集选择器 交集选择器 后代选择器 使用空格表示后代选择器.父元素的后代(包括儿子,孙子,重孙子) 后代选择器 在CSS中使用非常频繁 因为HTML元素可以嵌 ...

  4. TimeUnit类 java.util.concurrent.TimeUnit

    TimeUnit是什么? TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段 主要作用 时间颗粒度转换 延时 常用的颗粒度 TimeUnit.DAYS ...

  5. React手稿之 React-Saga

    Redux-Saga redux-saga 是一个用于管理应用程序副作用(例如异步获取数据,访问浏览器缓存等)的javascript库,它的目标是让副作用管理更容易,执行更高效,测试更简单,处理故障更 ...

  6. Python所有转义字符总汇

    转义字符就是让程序按照已经设置好的字符输出,不然程序当成其他的输出了,下面总结所有python转义字符 \\ 反斜杠符号\' 单引号\" 双引号\a 响铃\b 退格(Backspace)\e ...

  7. VirtualStringTree常用类和属性

    重要的类:TBaseVirtualTree = class(TCustomControl)TCustomVirtualStringTree = class(TBaseVirtualTree)TVirt ...

  8. [APIO2019] 奇怪装置

    $solution:$ 问题其实就是求两个式子的循环节. 钦定 $t\mod B=0$且 $(t\neq 0)$,其 $t$ 为循环节. 则将 $1$ 式拆开得 $\frac{t\times (B+1 ...

  9. MVC框架与MTC框架

    3.WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理 ############## ...

  10. vue打开新窗口

    1. <router-link tag="a" target="_blank" :to="{name:'detail',query:{goods ...