Cod sursa(job #201400)

Utilizator cypherMircea Grecu cypher Data 31 iulie 2008 12:30:40
Problema Datorii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.49 kb
program datorii_007;
type tree=record
                st,en,l,r,s:longint;
          end;
var t:array[1..50000] of tree;
	a:array[1..20000] of word;
	n,m,p:longint;
	
	procedure maketree(x,y:word);
	var mid,c:word;
	begin
		c:=p;
		t[c].st:=x; t[c].en:=y;
		t[c].s:=0; t[c].l:=0; t[c].r:=0;
		if y-x>0 then begin
			mid:=(x+y) shr 1;
			inc(p); t[c].l:=p; maketree(x,mid);
			inc(p); t[c].r:=p; maketree(mid+1,y);
			t[c].s:=t[t[c].l].s+t[t[c].r].s;
		end
		else t[c].s:=a[x];
	end;
	
	procedure sterge(u,v,k:word);
	var mid:word;
	begin
		dec(t[k].s,v);
			if t[k].en>t[k].st then begin
			mid:=(t[k].st+t[k].en) shr 1;
			if u<=mid then sterge(u,v,t[k].l)
			else sterge(u,v,t[k].r);
		end;
	end;
	
	function sum(u,v,k:word):longint;
	var mid:word;
		s1:longint;
	begin
		with t[k] do begin
			if (st>=u) and (en<=v) then sum:=s
			else begin
				s1:=0;
				mid:=(st+en) shr 1;
				if u<=mid then inc(s1,sum(u,mid,l));
				if v>mid then inc(s1,sum(mid+1,v,r));
				sum:=s1;
			end;
		end;
	end;

	procedure main;
	var fin,fout:text; cod:byte;
		i,ans:longint; u,v:word;
	begin
		p:=1;
		assign(fin,'datorii.in'); reset(fin);
		assign(fout,'datorii.out'); rewrite(fout);
		readln(fin,n,m);
		for i:=1 to n do read(fin,a[i]);
		maketree(1,n);
		for i:=1 to m do begin
			readln(fin,cod,u,v);
			if cod=0 then sterge(u,v,1)
			else begin
				ans:=sum(u,v,1);
				writeln(fout,ans);
			end;
		end;
		close(fin); close(fout);
	end;
	
BEGIN
	main;
END.