zoukankan      html  css  js  c++  java
  • 【CF652C】Foe Pairs(线性扫描)

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

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

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

            不包含=总数-包含即可

     1 var f,c:array[1..310000]of int64;
     2     n,m,x,y,t:int64;
     3     i:longint;
     4     ans:int64;
     5 
     6 function min(x,y:int64):int64;
     7 begin
     8  if x<y then exit(x);
     9  exit(y);
    10 end;
    11 
    12 begin
    13  //assign(input,'cf652C.in'); reset(input);
    14 // assign(output,'cf652C.out'); rewrite(output);
    15  read(n,m);
    16  for i:=1 to n do
    17  begin
    18   read(x);
    19   c[x]:=i; f[i]:=n+1;
    20  end;
    21  ans:=n*(n+1) div 2;
    22  for i:=1 to m do
    23  begin
    24   read(x,y);
    25   if c[x]>c[y] then begin t:=x; x:=y; y:=t; end;
    26   f[c[x]]:=min(f[c[x]],c[y]);
    27  end;
    28  for i:=n-1 downto 1 do f[i]:=min(f[i],f[i+1]);
    29  for i:=1 to n-1 do ans:=ans-(n-f[i]+1);
    30  writeln(ans);
    31 // close(input);
    32 // close(output);
    33 end.
  • 相关阅读:
    05数组 字符串数组 多维数组
    android textView的渐入效果
    android颜色表
    ExpandableListView置顶和子list收放
    Adb not responding
    android 一个TextView设置多种颜色
    android 循环操作
    Android异步更新
    android 水波纹
    Android 推门效果
  • 原文地址:https://www.cnblogs.com/myx12345/p/6033967.html
Copyright © 2011-2022 走看看