【CF652C】Foe Pairs(线性扫描)

时间:2021-12-31 23:37:48

题意:给你1-n的一个排列和m组数对,问有多少区间不包含任意一个数对。 (1 ≤ n, m ≤ 3·105)

思路:数据范围过大,不能用容斥原理

f[i]表示以位置i上的数为左端点,右端点最小到哪里

不包含=总数-包含即可

 var f,c:array[..]of int64;
n,m,x,y,t:int64;
i:longint;
ans:int64; function min(x,y:int64):int64;
begin
if x<y then exit(x);
exit(y);
end; begin
//assign(input,'cf652C.in'); reset(input);
// assign(output,'cf652C.out'); rewrite(output);
read(n,m);
for i:= to n do
begin
read(x);
c[x]:=i; f[i]:=n+;
end;
ans:=n*(n+) div ;
for i:= to m do
begin
read(x,y);
if c[x]>c[y] then begin t:=x; x:=y; y:=t; end;
f[c[x]]:=min(f[c[x]],c[y]);
end;
for i:=n- downto do f[i]:=min(f[i],f[i+]);
for i:= to n- do ans:=ans-(n-f[i]+);
writeln(ans);
// close(input);
// close(output);
end.