Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

终于AC了,取二进制中1的个数写错了,以前只统计了第16位……

Posted by frankvista at 2011-01-25 14:39:19 on Problem 2777
const
	MAXTREE = 1 shl 18;
	m1 = %01010101010101010101010101010101;
	m2 = %00110011001100110011001100110011;
	m3 = %00001111000011110000111100001111;
	m4 = %00000000111111110000000011111111;
	m5 = %00000000000000001111111111111111;

var
	flag : array[1..MAXTREE] of byte;
	tree : array[1..MAXTREE] of longint;
	tmp, A, B, C, T, O, i, base, m, L : longint;
	ch : char;

procedure add_new_change(x,c : longint);
begin
	if c > 0 then begin
		tree[x] := 1 shl c;
		flag[x] := c;
	end;
end;

procedure refresh_to_son(x : longint);
begin
	add_new_change(x shl 1, flag[x]);
	add_new_change((x shl 1) or 1, flag[x]);
	flag[x] := 0;
end;

procedure refresh_to_leaf(x : longint);
var i : longint;
begin
	for i := m downto 1 do
		refresh_to_son(x shr i);
end;

function ask(A, B : longint) : longint;
begin
	A := (A - 1) or base; B := (B + 1) or base;
	refresh_to_leaf(A); refresh_to_leaf(B);

	ask := 0;
	while B - A > 1 do begin
		if A and 1 = 0 then ask := ask or tree[A or 1];
		if B and 1 = 1 then ask := ask or tree[B xor 1]; 
		A := A shr 1; B := B shr 1;
	end;

	ask := (ask and m1) + ((ask shr 1) and m1);
	ask := (ask and m2) + ((ask shr 2) and m2);
	ask := (ask and m3) + ((ask shr 4) and m3);
	ask := (ask and m4) + ((ask shr 8) and m4);
	ask := (ask and m5) + ((ask shr 16) and m5);
end;

	
procedure modify(A, B, C : longint);
var i, l, r, x, y : longint;
begin
	A := (A - 1) or base; B := (B + 1) or base;
	refresh_to_leaf(A); refresh_to_leaf(B);

	l := A; r := B;
	while r - l > 1 do begin
		if l and 1 = 0 then add_new_change(l or 1, C);
		if r and 1 = 1 then add_new_change(r xor 1, C);
		l := l shr 1; r := r shr 1;
	end;

	for i := 1 to m do begin
		{Union}
		x := A shr i; y := B shr i;
		tree[x] := tree[x shl 1] or tree[(x shl 1) or 1];
		tree[y] := tree[y shl 1] or tree[(y shl 1) or 1];
	end;
end;

begin
//	assign(input, '2777.in'); reset(input);

	readln(L, T, O);

	{Fix L to 2^m - 2}

	base := 1; m := 0;
	while base < L + 2 do begin
		inc(m); base := base shl 1;
	end;

	filldword(tree, base shl 1, 2); fillchar(flag, base shl 1, 0);

	for i := O downto 1 do begin
		read(ch);
		if ch = 'C' then begin
			readln(A, B, C);
			if A > B then begin
				tmp := A; A := B; B := tmp;
			end;
			modify(A, B, C);
		end
		else begin
			readln(A, B);
			if A > B then begin
				tmp := A; A := B; B := tmp;
			end;
			writeln(ask(A, B));
		end;
	end;
end.



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator