HOJ1867 经理的烦恼~~~

题目大意

Jerry是一家公司销售部门的经理。这家公司有很多连锁店,编号为1,2,3,... Jerry每天必须关注每家连锁店的商品数量及其变化,一项很乏味的工作。在连锁店比较少的时候,Jerry喜欢计算编号在[i,j]区间内的连锁店中商品数量为素数的有多少家,但是现在连锁店的数量急剧增长,计算量很大,Jerry很难得出结果。 

输入格式

题目有多组输入。每组输入第一行有三个整数:C 连锁店的数量 N 指令的条数 M 每家连锁店初始的商品数量

接下来有N行,每行有一条指令。指令的格式为:0 x y 连锁店x的商品数量变化值为y,y > 0商品数量增加, y < 0减少1 i j 输出编号在[i,j]区间内的连锁店中商品数量为素数的有多少家1 <= i, x, j < 1000000 连锁店中的商品数量a满足 0 <= a < 10000000,C = N = M = 0标志输入结束

输出格式

对于每组输入,输出它的序号。对于一组输入中的1指令输出要求的整数。每组输出后打印一行空行。

样例输入

100000 4 4
0 1 1
1 4 10
0 11 3
1 1 11

20 3 0
1 1 20
0 3 3
1 1 20

0 0 0

样例输出

CASE #1:
0
2

CASE #2:
0
1

思路

简单的树状数组的应用
var
  a:array[1..1000005] of longint;
  c:array[1..1000005] of longint;
  v:array[1..1000005] of boolean;
  n,m,k,tot:longint;
function sum(i:longint):longint;
begin
  sum:=0;
  while i>0 do
    begin
      inc(sum,c[i]);
      dec(i,i and(-i));
    end;
end;
function pd(x:longint):boolean;
var
  i:longint;
begin
  pd:=true;
  if x=1 then exit(false);
  for i:=2 to trunc(sqrt(x)) do
    if x mod i=0 then exit(false);
end;
var
  i,x,y,z,j:longint;
  flag:boolean;
begin
  tot:=0;
  while not eoln do
    begin
      inc(tot);
      readln(n,k,m);
      if m=k then
        writeln('CASE #',tot,':');
      for i:=1 to n do
        begin
          a[i]:=m;
          v[i]:=false;
        end;
      fillchar(c,sizeof(c),0);
      if pd(m) then
        for i:=1 to n do
          begin
            j:=i;
            while j<=1000000 do
              begin
                inc(c[j]);
                inc(j,j and(-j));
              end;
            v[i]:=true;
          end;
      for i:=1 to k do
        begin
          readln(x,y,z);
          if x=0 then
            begin
              inc(a[y],z);
              if pd(a[y])=not v[y] then
                if pd(a[y]) then
                  begin
                    j:=y;
                    while j<=1000000 do
                      begin
                        inc(c[j]);
                        inc(j,j and(-j));
                      end;
                  end
                else
                  begin
                    j:=y;
                    while j<=1000000 do
                      begin
                        dec(c[j]);
                        inc(j,j and(-j));
                      end;
                  end;
              v[y]:=flag;
            end
          else
            begin
              if z>y then
                writeln(sum(z)-sum(y-1))
              else
                writeln(sum(y)-sum(z-1))
            end;
        end;
      writeln;
    end;
end.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值