题目链接:

C. Foe Pairs

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

 

You are given a permutation p of length n. Also you are given m foe pairs (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi).

Your task is to count the number of different intervals (x, y) (1 ≤ x ≤ y ≤ n) that do not contain any foe pairs. So you shouldn't count intervals (x, y) that contain at least one foe pair in it (the positions and order of the values from the foe pair are not important).

Consider some example: p = [1, 3, 2, 4] and foe pairs are {(3, 2), (4, 2)}. The interval (1, 3) is incorrect because it contains a foe pair(3, 2). The interval (1, 4) is also incorrect because it contains two foe pairs (3, 2) and (4, 2). But the interval (1, 2) is correct because it doesn't contain any foe pair.

Input
 

The first line contains two integers n and m (1 ≤ n, m ≤ 3·10^5) — the length of the permutation p and the number of foe pairs.

The second line contains n distinct integers pi (1 ≤ pi ≤ n) — the elements of the permutation p.

Each of the next m lines contains two integers (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi) — the i-th foe pair. Note a foe pair can appear multiple times in the given list.

Output
 

Print the only integer c — the number of different intervals (x, y) that does not contain any foe pairs.

Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

Examples
 
input
  1. 4 2
    1 3 2 4
    3 2
    2 4
output
  1. 5
input
  1. 9 5
    9 7 2 3 1 4 6 5 8
    1 6
    4 5
    2 7
    7 2
    2 7
output
  1. 20
Note

In the first example the intervals from the answer are (1, 1), (1, 2), (2, 2), (3, 3) and (4, 4).

题意:

给一个数组,问有多少对(i,j)满足[a[i],a[j]]中不完整包含任何一个数对;

思路:

暴力是的复杂度太高,我先把这些数对都处理成原数组的位置,然后把它们搞进线段树里,就是把右端点当成左端点插入线段树时的位置,查询一个区间[i,j]时是否包含一个完整的线段可以看[i,j]中最大的左端点是多大,如果最大的左端点>=i时,我们就知道[i,j]中至少包含一个完整的线段;然后再枚举左端点,尺取法找右端点;然后把长度都加起来就是结果啦;

AC代码:

  1. /*2014300227 652C - 11 GNU C++11 Accepted 311 ms 18796 KB*/
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. const int N=3e5+;
  5. typedef long long ll;
  6. int n,a[N],fa[N],m,l,r;
  7. struct Tree
  8. {
  9. int l,r,ans;
  10. };
  11. Tree tree[*N];
  12. void Pushup(int node)
  13. {
  14. tree[node].ans=max(tree[*node].ans,tree[*node+].ans);
  15. }
  16. void build(int node,int L,int R)
  17. {
  18. tree[node].l=L;
  19. tree[node].r=R;
  20. if(L==R)
  21. {
  22. tree[node].ans=;
  23. return ;
  24. }
  25. int mid=(L+R)>>;
  26. build(*node,L,mid);
  27. build(*node+,mid+,R);
  28. Pushup(node);
  29. }
  30. void update(int node,int num,int pos)
  31. {
  32. if(tree[node].l==tree[node].r&&tree[node].r==pos)
  33. {
  34. tree[node].ans=max(tree[node].ans,num);
  35. return ;
  36. }
  37. int mid=(tree[node].l+tree[node].r)>>;
  38. if(pos<=mid)update(*node,num,pos);
  39. else update(*node+,num,pos);
  40. Pushup(node);
  41. }
  42. int query(int node,int L,int R)
  43. {
  44. if(L<=tree[node].l&&R>=tree[node].r)
  45. {
  46. return tree[node].ans;
  47. }
  48. int mid=(tree[node].l+tree[node].r)>>;
  49. if(R<=mid)return query(*node,L,R);
  50. else if(L>mid)return query(*node+,L,R);
  51. else return max(query(*node,L,mid),query(*node+,mid+,R));
  52. }
  53. struct PO
  54. {
  55. int l,r;
  56. }po[N];
  57. int main()
  58. {
  59. scanf("%d%d",&n,&m);
  60. for(int i=;i<=n;i++)
  61. {
  62. scanf("%d",&a[i]);
  63. fa[a[i]]=i;
  64. }
  65. build(,,n);
  66. for(int i=;i<m;i++)
  67. {
  68. scanf("%d%d",&l,&r);
  69. po[i].l=min(fa[l],fa[r]);
  70. po[i].r=max(fa[l],fa[r]);
  71. update(,po[i].l,po[i].r);//更新;
  72. }
  73. ll ans=;
  74. int l=,r=;
  75. for(int i=;i<=n;i++)
  76. {
  77. l=i;
  78. while(r<=n)
  79. {
  80. int q=query(,i,r);//查询[i,r]中的最大值,即是包含于这个区间的线段最大的左端点;
  81. if(q<i)r++;//r为以l为左端点满足要求的最长的区间的右端点+1;
  82. else break;
  83. }
  84. ans+=(ll)(r-l);
  85. }
  86. cout<<ans<<"\n";
  87. return ;
  88. }

codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)的更多相关文章

  1. 数据结构1 线段树查询一个区间的O(log N) 复杂度的证明

    线段树属于二叉树, 其核心特征就是支持区间加法,这样就可以把任意待查询的区间$[L, R]$分解到线段树的节点上去,再把这些节点的信息合并起来从而得到区间$[L,R]$的信息. 下面证明在线段树上查询 ...

  2. 数据结构1 「在线段树中查询一个区间的复杂度为 $O(\log N)$」的证明

    线段树属于二叉树, 其核心特征就是支持区间加法,这样就可以把任意待查询的区间$[L, R]$分解到线段树的节点上去,再把这些节点的信息合并起来从而得到区间$[L,R]$的信息. 下面证明在线段树上查询 ...

  3. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  4. HDU 1754 I Hate It(线段树单点替换+区间最值)

    I Hate It [题目链接]I Hate It [题目类型]线段树单点替换+区间最值 &题意: 本题目包含多组测试,请处理到文件结束. 在每个测试的第一行,有两个正整数 N 和 M ( 0 ...

  5. HDU 3577Fast Arrangement(线段树模板之区间增减更新 区间求和查询)

    Fast Arrangement Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 140120 ...

  7. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  8. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  9. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

随机推荐

  1. [NSURL URLWithString:] 返回nil

    具体问题原因是url中输入的有中文,那么这个就看作非法的字符无法识别.这种的必须使用post方式来发送消息.具体为: tmp = mainurl;            [parameters app ...

  2. 转: How to Install MongoDB 3.2 on CentOS/RHEL & Fedora (简单易懂)

    from:  http://tecadmin.net/install-mongodb-on-centos-rhel-and-fedora/ MongoDB (named from “huMONGOus ...

  3. treeList获取目录下的所有文件

    /// <summary>/// treeList获取目录下的所有文件/// </summary>public static void InitTreeListGetFiles ...

  4. jquery获取<div></div>之间的内容.text() 和 .html()区别

    jQuery 获取 div 之间的内容,有两种方法,$(selector).text().$(selector).html() . html: <div> <p>test< ...

  5. 怎样高速启动Android模拟器(Android Emulator)

    总所周知,每次我们启动Android Emulator,都须要花费非常长一段时间,几分钟甚至十几分钟.事实上,我们能够使用快照(Snapshot)功能,来高速启动Android模拟器. 首先.须要在A ...

  6. python(23)- 面向对象简单介绍

    面向概述 面向过程:根据业务逻辑从上到下写垒代码 面向过程的设计的核心是过程,过程即解决问题的步骤, 面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西 优点:极大降低了程序的复杂 ...

  7. ios文件系统文件目录操作

    对于一个运行在iPhone得app,它只能访问自己根目录下得一些文件(所谓sandbox). 一个app发布到iPhone上后,目录结构如下: 1.其中获取 app root 可以用 NSHomeDi ...

  8. 整理自Git文件夹下资料及man手册(不包括书籍)

    $ git commit -awhich will automatically notice any modified (but not new) files, add them to the ind ...

  9. Android数据自己主动更新库DataAutoRefresh

    非常多android应用.比方音乐播放器.视频播放器.小说阅读器或者其他须要获取本地磁盘指定数据格式数据列表的应用,在磁盘数据有变化(新增或者删除.比方下载完毕,拔TF卡.换TF卡)时.须要自己主动更 ...

  10. Google Chrome的快捷键

    1.Ctrl + N 打开一个新窗口           &&            Alt + F4    关闭当前窗口 2.Ctrl + T 打开一个新的标签页 && ...