CodeForces 665E Beautiful Subarrays

题目:Beautiful Subarrays

链接:Here

题意:给一个数组,给一个 ‘完美区间‘ 的定义:l 到r 区间内的所有数异或和大于等于k,问给定数组有多少完美区间。

思路:

  异或运算可以前缀和处理,用w[i]表示i 前面的数异或和,那么w[5]^w[3]就是4、5两数异或的值。

  现在我们要开始建字典树了(感觉异或和字典树老扯上关系),我们从前往后一个个地把前缀加入字典树(补齐,高位补0),在w[i]加入字典树前,判断w[i]和前面的哪个前缀可以异或并且值大于等于k。这里要用树形DP,根据性质,假设高位异或后,在那一位>k在那一位的值,那就不用往下继续了,因为高位有差别,大小就已经判断出来了,直接加上所有以这个为前缀的w[i]的数量就可以了,细节小心处理,大概就是这样。

AC代码:(没整理,乱七八糟的)

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<math.h>
  5 #include<set>
  6 #include<map>
  7 #include<list>
  8 #include<stack>
  9 #include<queue>
 10 #include<vector>
 11 #include<string>
 12 #include<iostream>
 13 #include<algorithm>
 14 using namespace std;
 15 #define lson rt<<1
 16 #define rson rt<<1|1
 17 #define N 1000010
 18 #define M 100010
 19 #define Mod 1000000007
 20 #define LL long long
 21 #define INF 0x7fffffff
 22 #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;i++)
 23 #define For(i,f_start,f_end) for(int i=f_start;i<f_end;i++)
 24 #define REP(i,f_end,f_start) for(int i=f_end;i>=f_start;i--)
 25 #define Rep(i,f_end,f_start) for(int i=f_end;i>f_start;i--)
 26 #define MT(x,i) memset(x,i,sizeof(x))
 27 #define gcd(x,y) __gcd(x,y)
 28 const double PI = acos(-1);
 29
 30 struct Node
 31 {
 32   int num;
 33   int next[2];
 34 }v[N*33];
 35 int vNum;
 36
 37 int c[33],co;
 38 LL ans;
 39
 40 void add(int x)
 41 {
 42   int bt[33],bo=0;
 43   while(x)
 44   {
 45     bt[bo++] = x&1;
 46     x>>=1;
 47   }
 48   for(int i=bo;i<33;i++)
 49   {
 50     bt[i]=0;
 51   }
 52   int p=0,i;
 53   for(i=32;i>=0;i--)
 54   {
 55     int pos = !bt[i];
 56
 57     if(v[p].next[pos]!=-1)
 58     {
 59       if(c[i]==0) ans+=v[v[p].next[pos]].num;
 60       else
 61       {
 62         p = v[p].next[pos];
 63         if(p==-1) break;
 64         continue;
 65       }
 66     }
 67     else if(v[p].next[!pos]==-1) break;
 68     if(c[i]==1) break;
 69     p = v[p].next[!pos];
 70     if(p==-1) break;
 71   }
 72   if(i==-1) ans+=v[p].num;
 73   p=0;
 74   for(int i=32;i>=0;i--)
 75   {
 76     int pos = bt[i];
 77     if(v[p].next[pos]==-1)
 78     {
 79       v[vNum].next[0]=v[vNum].next[1]=-1;
 80       v[vNum].num=0;
 81       v[p].next[pos]=vNum++;
 82     }
 83     p = v[p].next[pos];
 84     v[p].num++;
 85   }
 86 }
 87
 88 int w[N];
 89 int main()
 90 {
 91   int n,k,x;
 92   while(~scanf("%d%d",&n,&k))
 93   {
 94     int ttt=k;
 95     w[0]=0;
 96     for(int i=1;i<=n;i++)
 97     {
 98       scanf("%d",&x);
 99       w[i]=w[i-1]^x;
100     }
101     co=0;
102     while(k)
103     {
104       c[co++]=k&1;
105       k>>=1;
106     }
107     for(int i=co;i<33;i++) c[i]=0;
108     v[0].next[0]=v[0].next[1]=-1;
109     v[0].num=0;
110     vNum=1;
111     ans=0;
112     k=ttt;
113     for(int i=1;i<=n;i++)
114     {
115       add(w[i]);
116       if(w[i]>=k) ans++;
117     }
118     printf("%I64d\n",ans);
119   }
120   return 0;
121 }
时间: 2024-08-13 15:23:22

CodeForces 665E Beautiful Subarrays的相关文章

CodeForces 665E Beautiful Subarrays 字典树

Beautiful Subarrays 题解: 把数字转化成2进制之后,用字典树去维护. 想到字典树之后就应该是一道很容易写的题目了. 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #define LL long

Educational Codeforces Round 12 E. Beautiful Subarrays trie求两异或值大于等于k对数

E. Beautiful Subarrays One day, ZS the Coder wrote down an array of integers a with elements a1,  a2,  ...,  an. A subarray of the array a is a sequence al,  al  +  1,  ...,  ar for some integers (l,  r) such that 1  ≤  l  ≤  r  ≤  n. ZS the Coder th

[2016-03-08][651][codeforces][B][Beautiful Paintings]

[2016-03-08][651][[codeforces]][B][Beautiful Paintings] 题目编号:CF 651 B 题目大意:画展有n个画,每个画beauty值已知,客人每经过一个比之前一个画更美的,客人会变得beauty,重新安排画的顺序,求客人开心时间的最大值 输入:n 和每个话的beauty 输出:时间 分析: 如果beauty为a的画,数量为 k,那么beaty小于或者大于a的k个,总能安排让客人开心,所以,除了数量k最大的那个beauty之外,每个beauty总

Codeforces 55D Beautiful Number

Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. Input The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two

看个人思路吧,清晰的话就简单 CodeForces 271A - Beautiful Year

It seems like the year of 2013 came only yesterday. Do you know a curious fact? The year of 2013 is the first year after the old 1987 with only distinct digits. Now you are suggested to solve the following problem: given a year number, find the minim

CodeForces 55D Beautiful numbers 数位DP+数学

题意大概是,判断一个正整数区间内有多少个整数能被它自身的每一个非零的数字整除. 因为每一个位置上的整数集s = {0,1,2,3,4,5,6,7,8,9} lcm(s) = 2520 现在有一个整数t是由s中一个或者多个数字构成的,记为abcde,显然t = a*10^4+b*10^3+c*10^2+d*10^1+e 要使得t能被a,b,c,d,e整除,必然有t % lcm(a,b,c,d,e) = 0 因为a,b,c,d,e去重之后一定是s的一个子集,所以lcm(s)一定是lcm(a,b,c,

Codeforces 55D. Beautiful numbers(数位DP,离散化)

Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得只需要记录搜到当前位出现了哪些数字作为状态即可,明显是假算法...感觉这是一道数位DP好题.可以这样思考:一个数要想被其各位数字分别都整除,等价于它被那些数字的LCM整除.因此记录当前位,当前数对(1~9的LCM)取模的结果,当前出现的数字的LCM这三个值作为状态才合理,即dp[pos][sum][

CodeForces - 893B Beautiful Divisors

题目链接 CodeForces - 893B time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output Recently Luba learned about a special kind of numbers that she calls beautiful numbers. The number is called beautifu

CodeForces 55D Beautiful numbers(数位dp&amp;&amp;离散化)

题目链接:[kuangbin带你飞]专题十五 数位DP A - Beautiful numbers 题意 ps:第一道数位dp,题真好,虽然是参考大牛方法悟过才a,但仍收获不少. 求一个区间内的Beautiful numbers有多少个.Beautiful numbers指:一个数能整除所有组成它的非0数字. 例如15可以被1和5整除,所以15是Beautiful numbers. 思路 Beautiful numbers指:一个数能整除所有组成它的非0数字. 等同于 一个数能整除 所有组成它的