JZOJ 7.11B组第三题 进化序列

来源:互联网 发布:2017手机淘宝店铺装修 编辑:程序博客网 时间:2024/06/10 05:55

题目:

Abathur采集了一系列Primal Zerg 的基因样本,这些基因构成了一个完整的进化链。为了方便,我们用A0,A1...An-1 这n 个正整数描述它们。一个基因Ax 可以进化为序列中在它之后的基因Ay。这个进化的复杂度,等于Ax | Ax+1...| Ay的值,其中| 是二进制或运算。Abathur 认为复杂度小于M 的进化的被认为是温和的。它希望计算出温和的进化的对数。

Input:

4 61 3 5 1
output:

2

分析:

此题可以暴力水解。用线段树来维护区间中a[x]xora[x+1]……a[x+100]的值,每隔一百取一波,用来缩短时间。然后如果下一次再多一百便s[y]>m,那么便开始for找到临界点。最后不断inc一下ans即可。

代码:

const
  maxn=100000;


var
  a,sum:array [0..maxn] of longint;
  n,m:longint;
  ans:int64;


procedure init;
var
  i,j:longint;
begin
  readln(n,m);
  for i:=1 to n do
    read(a[i]);
  for i:=1 to n do
    begin
      sum[i]:=a[i];
      for j:=i+1 to i+100 do
        sum[i]:=sum[i] or a[j];
    end;
end;


procedure main;
var
  i,j,section,back:longint;
  now:int64;
begin
  for i:=1 to n do
    begin
      now:=0;
      section:=i;
      if i+100<=n then
        while true do
          begin
            if now or sum[section]>m then
              break;
            now:=now or sum[section];
            inc(section,101);
            if section+100>n then
              break;
          end;
      if section>i then
        inc(ans,section-i-1);
      back:=0;
      for j:=section to n do
        begin
          if now or a[j]>m then
            break;
          inc(back);
          now:=now or a[j];
        end;
     if back>0 then
       if section=i then
         inc(ans,back-1)
       else
         inc(ans,back);
    end;
  write(ans);
end;


begin
  assign(input,'evolve.in');reset(input);
  assign(output,'evolve.out');rewrite(output);
  init;
  main;
  close(input);close(output);
end.