覆盖统计
覆蓋統(tǒng)計(axis.c/cpp/pas)
【題目大意】
數(shù)軸上有一些點,從 1 標記到n,逐漸被線段覆蓋, 求
每次覆蓋后未被覆蓋的點的個數(shù)。
【輸入數(shù)據(jù)】
第一行,點數(shù) n,操作數(shù)k。
接下來k 行,每行是線段的左右端點 l,r
【輸出數(shù)據(jù)】
k行,每次操作后未被覆蓋的點。
【輸入樣例】
9 3
3 3
5 7
1 9
【輸出樣例】
8
5
0
【數(shù)據(jù)范圍】
對于 30%的數(shù)據(jù),1<=n,k<=2000;
對于 100%的數(shù)據(jù),1<=n<=200000,k<=600000
----------------------------------------
樹什么的不熟練啊,就想出了一種神奇的方法,覆蓋的地方false掉,用一個next函數(shù)跳轉(zhuǎn),原來覆蓋過的地方就不掃了,這樣可以省去很多時間。最后偶爾更新一下,使next函數(shù)簡化,數(shù)據(jù)全過啦!!!(結(jié)果后來發(fā)現(xiàn)更新反而更花時間,不更新能夠更快AC。。。。)
varb:array[0..200001]of boolean;back,next:array[0..200001]of longint;i,j,n,k,l,r,ans,last:longint; beginassign(input,'axis.in'); assign(output,'axis.out');reset(input); rewrite(output);read(n,k);fillchar(b,sizeof(b),true);ans:=n;for i:=1 to n do next[i]:=i+1;for j:=1 to k dobeginread(l,r);i:=l;repeatbeginif b[i] thenbegini:=i+1;next[i-1]:=r+1;b[i-1]:=not b[i-1];dec(ans);endelse i:=next[i];end;until (i>r)or(i>n);writeln(ans);if j mod 10000=0 then //更新next函數(shù)beginlast:=n+1;for i:=n downto 1 dobeginif (b[i]=false)and(b[i-1]=true) thennext[i]:=last;if (b[i]=true)and(b[i-1]=false) thenlast:=i;end;end;end;close(input); close(output); end.轉(zhuǎn)載于:https://www.cnblogs.com/zjhl2/p/3855967.html
總結(jié)
- 上一篇: hdu4506小明系列故事——师兄帮帮忙
- 下一篇: 一步步编写操作系统(1)