以前一直误解线段树了,看了莫队的线段树发现我以前一直写丑了。。。 这是hnoi03的一道题: program ex4;uses math; var d,next,x,y,z,s,f:array[0..30000] of longint; n,r,i,a,b,c,t,ans:longint; procedure new(l,r,w,o:longint); begin inc(t);next[t]:=d[o];d[o]:=t; x[t]:=l;y[t]:=r;z[t]:=w; end; procedure ins(i,l,r,j:longint);var m,t:longint; begin if (x[j]>l)or(y[j]<r) then begin t:=i*2;m:=(l+r)>>1; if x[j]<=m then ins(t,l,m,j); if y[j]>m then ins(t+1,m+1,r,j); f[i]:=s[i]+max(f[t],f[t+1]); end else begin inc(s[i],z[j]);inc(f[i],z[j]); end; end; begin assign(input,'input.txt');reset(input); assign(output,'output.txt');rewrite(output); readln(n,r); for n:=1 to n do begin readln(a,b,c); new(b,b+r-1,c,a*2); new(b,b+r-1,-c,(a+r-1)*2+1); end; for i:=0 to 10001 do while d[i]<>0 do begin ins(1,0,5011,d[i]); ans:=max(ans,f[1]); d[i]:=next[d[i]]; end; writeln(ans); close(input);close(output); end.