P1230: [Usaco2008 Nov]lites 开关灯

嗯嗯,这是一道线段树的题,询问区间内亮着的灯的个数,我们可以把区间修改的线段树改一下,原本的求和改成若有奇数次更改则取反(总长度-亮着的灯个数),而判断是否奇数次只要数组加一个delta的值,update的时候delta xor 1 就够了,代码如下。

 1 type
 2   tpoint=record
 3     l,r,sum,delta,mid:longint;
 4   end;
 5 var n,m,s,e,i,j,c,sum:longint;
 6 tree:array[0..300000] of tpoint;
 7 procedure build(x,l,r:longint);
 8 begin
 9   tree[x].l:=l; tree[x].r:=r; tree[x].sum:=0; tree[x].delta:=0; tree[x].mid:=(l+r) shr 1;
10   if l>=r then exit;
11   build(x*2,l,(l+r) shr 1);
12   build(x*2+1,((l+r) shr 1)+1,r);
13 end;
14 procedure maitain(x:longint);
15 begin
16   tree[x].sum:=0;
17   if tree[x].l<tree[x].r then
18     tree[x].sum:=tree[x*2].sum+tree[x*2+1].sum;
19   tree[x].sum:=abs(tree[x].delta*(tree[x].r-tree[x].l+1)-tree[x].sum);
20 end;
21 procedure insert(x:longint);
22 begin
23   if (s<=tree[x].l) and (tree[x].r<=e) then tree[x].delta:=tree[x].delta xor 1
24     else begin
25       if s<=tree[x].mid then insert(x*2);
26       if e>tree[x].mid then insert(x*2+1);
27     end;
28   maitain(x);
29 end;
30 procedure query(x,add:longint);
31 begin
32   if (s<=tree[x].l) and (tree[x].r<=e) then
33     sum:=sum+abs(add*(tree[x].r-tree[x].l+1)-tree[x].sum)
34   else begin
35     if s<=tree[x].mid then query(x*2,add xor tree[x].delta);
36     if e>tree[x].mid then query(x*2+1,add xor tree[x].delta);
37   end;
38 end;
39 begin
40   readln(n,m);
41   build(1,1,n);
42   for i:=1 to m do
43     begin
44       readln(c,s,e);
45       sum:=0;
46       if c=0 then insert(1)
47         else begin
48           query(1,0);
49           writeln(sum);
50         end;
51     end;
52 end.
View Code

(转载请注明出处:http://www.cnblogs.com/Kalenda/)

posted @ 2015-09-20 11:35  LovelyMonster丶  阅读(309)  评论(0编辑  收藏  举报