codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
2016-04-15 15:25
435 查看
题目链接:
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
output
input
output
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代码:
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
4 2 1 3 2 4 3 2 2 4
output
5
input
9 5 9 7 2 3 1 4 6 5 8 1 6 4 5 2 7 7 2 2 7
output
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代码:
/*2014300227 652C - 11 GNU C++11 Accepted 311 ms 18796 KB*/ #include <bits/stdc++.h> using namespace std; const int N=3e5+4; typedef long long ll; int n,a ,fa ,m,l,r; struct Tree { int l,r,ans; }; Tree tree[4*N]; void Pushup(int node) { tree[node].ans=max(tree[2*node].ans,tree[2*node+1].ans); } void build(int node,int L,int R) { tree[node].l=L; tree[node].r=R; if(L==R) { tree[node].ans=0; return ; } int mid=(L+R)>>1; build(2*node,L,mid); build(2*node+1,mid+1,R); Pushup(node); } void update(int node,int num,int pos) { if(tree[node].l==tree[node].r&&tree[node].r==pos) { tree[node].ans=max(tree[node].ans,num); return ; } int mid=(tree[node].l+tree[node].r)>>1; if(pos<=mid)update(2*node,num,pos); else update(2*node+1,num,pos); Pushup(node); } int query(int node,int L,int R) { if(L<=tree[node].l&&R>=tree[node].r) { return tree[node].ans; } int mid=(tree[node].l+tree[node].r)>>1; if(R<=mid)return query(2*node,L,R); else if(L>mid)return query(2*node+1,L,R); else return max(query(2*node,L,mid),query(2*node+1,mid+1,R)); } struct PO { int l,r; }po ; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); fa[a[i]]=i; } build(1,1,n); for(int i=0;i<m;i++) { scanf("%d%d",&l,&r); po[i].l=min(fa[l],fa[r]); po[i].r=max(fa[l],fa[r]); update(1,po[i].l,po[i].r);//更新; } ll ans=0; int l=1,r=1; for(int i=1;i<=n;i++) { l=i; while(r<=n) { int q=query(1,i,r);//查询[i,r]中的最大值,即是包含于这个区间的线段最大的左端点; if(q<i)r++;//r为以l为左端点满足要求的最长的区间的右端点+1; else break; } ans+=(ll)(r-l); } cout<<ans<<"\n"; return 0; }
相关文章推荐
- 进程控制开发[fork() exec exit _exit wait waitpid 守护进程]
- 219. Contains Duplicate II
- 编译过程中,报failed to resolve:com.andriod.databinding.adapters:1.1
- Lesson 3: More About Jobs and Job Details
- textview 计算宽
- CDISC SDTM CM domain 学习笔记
- http://blog.csdn.net/zhou452840622/article/details/41820301
- URLDecoder: Incomplete trailing escape (%) pattern错误处理
- 查找字符串(containsString和rangeOfString的区别)
- CGRectContainsPoint 用法
- 国外braintree支付手段配置以及使用
- xcode7.3 iTunes Store operation failed问题
- arcgis engine 监听element的添加、更新和删除事件(使用IGraphicsContainerEvents)
- Pku oj 1218 THE DRUNK JAILER(开灯问题)
- “selection does not contain a main type”解决方法
- NLTEST to test the trust relationship between a workstation and domain
- WARNING: The first attempt to start Xvnc failed, possibly because the font
- Pku oj 2027 No Brainer(水题)
- LeetCode笔记:217. Contains Duplicate
- 基于Alphabet剪枝的五子棋AI