链接:https://ac.nowcoder.com/acm/contest/888/E
来源:牛客网

Gromah and LZR have entered the fifth level. Unlike the first four levels, they should do some moves in this level.
There are nn_{}n​ vertices and mm_{}m​ bidirectional roads in this level, each road is in format (u,v,l,r)(u, v, l, r)_{}(u,v,l,r)​, which means that vertex uu_{}u​ and vv_{}v​ are connected by this road, but the sizes of passers should be in interval [l,r][l, r]_{}[l,r]​. Since passers with small size are likely to be attacked by other animals and passers with large size may be blocked by some narrow roads.
Moreover, vertex 11_{}1​ is the starting point and vertex nn_{}n​ is the destination. Gromah and LZR should go from vertex 11_{}1​ to vertex nn_{}n​ to enter the next level.
At the beginning of their exploration, they may drink a magic potion to set their sizes to a fixed positive integer. They want to know the number of positive integer sizes that make it possible for them to go from 11_{}1​ to nn_{}n​.

Please help them to find the number of valid sizes.

输入描述:

The first line contains two positive integers n,mn,m_{}n,m​, denoting the number of vertices and roads.
Following m lines each contains four positive integers u,v,l,ru, v, l, r_{}u,v,l,r​, denoting a bidirectional road (u,v,l,r)(u, v, l, r)_{}(u,v,l,r)​.
1≤n,m≤105,1≤u<v≤n,1≤l≤r≤1091 \le n,m \le 10^5, 1 \le u < v \le n, 1 \le l \le r \le 10^91≤n,m≤105,1≤u<v≤n,1≤l≤r≤109

输出描述:

Print a non-negative integer in a single line, denoting the number of valid sizes.
示例1

输入

5 5
1 2 1 4
2 3 1 2
3 5 2 4
2 4 1 3
4 5 3 4

输出

2

题意:给定m条边,每条边有一个通过的阈值,问可以从1到n的值有多少个.思路:把这些边放入一个点表示区间的线段树里面.这真是我从未见过的全新套路,在线段树上dfs,相当于枚举权值,由于每一个点代表区间,所以每次就枚举到了一个区间.

枚举之时用并查集判断.

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>#define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define lson l,mid,ls
#define rson mid+1,r,rs
#define ls (rt<<1)
#define rs ((rt<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = 486;
const int maxn = 100086;
const int maxm = 100086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int n,m;
struct edge{int u,v,l,r;
}e[maxn];
int f[maxn],rk[maxn];
int rem[maxn],tot;vector<int>eg[maxn<<2];void update(int l,int r,int rt,int L,int R,int id){if(rt==0){ return;}if(L<=l&&R>=r){eg[rt].push_back(id);return;}int mid = (l+r)>>1;if(L<=mid)update(l,mid,rt*2,L,R,id);if(R>mid)update(mid+1,r,rt*2+1,L,R,id);
}int getf(int x){if(x==f[x]){ return x;}return getf(f[x]);
}int ans = 0;
struct node{int num,type;
};void dfs(int l,int r,int rt){stack<node>tmp;for(auto it:eg[rt]){int t1 = getf(e[it].u);int t2 = getf(e[it].v);if(rk[t1]<rk[t2]){tmp.push(node{f[t1],1});f[t1]=f[t2];}else if(rk[t1]>rk[t2]){tmp.push(node{f[t2],1});f[t2]=f[t1];}else{tmp.push(node{f[t2],2});f[t2]=f[t1];rk[t2]++;}}if(l==r){if(getf(1)==getf(n)&&l!=tot){ans+=rem[r+1]-rem[l];}while (!tmp.empty()){node it = tmp.top();tmp.pop();f[it.num]=it.num;if(it.type==2){rk[it.num]--;}}return;}int mid = (l+r)>>1;dfs(lson);dfs(rson);while (!tmp.empty()){node it = tmp.top();tmp.pop();f[it.num]=it.num;if(it.type==2){rk[it.num]--;}}
}int get_id(int x){return lower_bound(rem+1,rem+1+tot,x)-rem;
}int main() {scanf("%d%d",&n,&m);for(int i=0;i<=n;i++){f[i]=i;rk[i]=1;}for(int i=1;i<=m;i++){scanf("%d%d%d%d",&e[i].u,&e[i].v,&e[i].l,&e[i].r);rem[++tot] = e[i].l;rem[++tot] = e[i].r+1;}sort(rem+1,rem+1+tot);tot = unique(rem+1,rem+1+tot)-rem-1;for(int i=1;i<=m;i++){cout<<get_id(e[i].l)<<" "<<get_id(e[i].r+1)-1<<endl;update(1,tot,1,get_id(e[i].l),get_id(e[i].r+1)-1,i);}dfs(1,tot,1);printf("%d\n",ans);return 0;
}

View Code

转载于:https://www.cnblogs.com/ZGQblogs/p/11344044.html

2019牛客暑期多校训练营(第八场)E.Explorer相关推荐

  1. 2019牛客暑期多校训练营(第八场) CDMA

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言1048576K Special Judge, 64bit IO Format: %lld 题目描述   Gro ...

  2. 【2019牛客暑期多校训练营(第二场) - H】Second Large Rectangle(单调栈,全1子矩阵变形)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/H 来源:牛客网 题目描述 Given a N×MN \times MN×M binary matrix. ...

  3. 2019牛客暑期多校训练营(第一场)E-ABBA(dp)

    链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  4. 2019牛客暑期多校训练营(第一场)

    传送门 参考资料: [1]:官方题解(提取码:t050 ) [2]:标程(提取码:rvxr ) [3]:牛客题解汇总 A.Equivalent Prefixes(单调栈) •题意 定义两个数组 u,v ...

  5. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  6. 【2019牛客暑期多校训练营(第二场)- E】MAZE(线段树优化dp,dp转矩阵乘法,线段树维护矩阵乘法)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/E?&headNav=acm 来源:牛客网 Given a maze with N rows an ...

  7. 【2019牛客暑期多校训练营(第二场)- F】Partition problem(dfs,均摊时间优化)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/F 来源:牛客网 Given 2N people, you need to assign each of ...

  8. 【2019牛客暑期多校训练营(第二场) - D】Kth Minimum Clique(bfs,tricks)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/D 来源:牛客网 Given a vertex-weighted graph with N vertice ...

  9. 【2019牛客暑期多校训练营(第一场) - A】Equivalent Prefixes(单调栈,tricks)

    题干: 链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Two arrays u and v each with m distinct elem ...

  10. 【2019牛客暑期多校训练营(第一场) - H】XOR(线性基,期望的线性性)

    题干: 链接:https://ac.nowcoder.com/acm/contest/881/H 来源:牛客网 Bobo has a set A of n integers a1,a2,-,ana1, ...

最新文章

  1. 《C语言及程序设计》实践参考——水仙花数
  2. 单例模式 - 深究剖析
  3. (转载)[MySQL技巧]INSERT INTO… ON DUPLICATE KEY UPDATE
  4. SAP UI5应用里的页面路由处理
  5. 在ie6下实现position-fixed的效果--------续集---对联效果(02)
  6. TS对象中的实例属性和静态属性
  7. APP自动化测试系列之3种元素定位工具
  8. 语言用符号打印出落叶的图案_普通语言学概要(第一章第二节,语言是符号系统)...
  9. 清华大学深圳研究生院自动化系九推,2018/9
  10. asp小偷转html,ASP之XMLHTTP小偷程序的简单代码范例
  11. Panasonic: FP-X0 L30R 使用 FPWIN GR7 通讯及编程
  12. 最详细的Mysql操作手册(一)
  13. 19、STM8单片机RS485串口通讯实验
  14. 【AI创造营】网抑云选手等级鉴定器
  15. 大学计算机学科入门培训,大学计算机基础培训总结
  16. sqlite 简明教程
  17. 批处理备份及删除,forfiles命令详解
  18. HEVC代码学习35:xEncodeCU函数
  19. 从netfilter的NF_IP_PRE_ROUTING抓包 和 用libpcap抓包有什么区别?
  20. 将markdown标记换成html标签,Markdown常用标记

热门文章

  1. syslinux linux 启动盘,syslinux启动盘制作
  2. 成都Uber优步司机奖励政策(2月22日)
  3. oracle 计算时间差 毫秒,Oracle计算时间差为毫秒的实现代码
  4. 国外近年智慧出行项目清单
  5. 作为面试官的一点心得
  6. Polkadot的PLO第一阶段: Equilibrium在DOT上筹集了850万美元
  7. 判断是否是anagram
  8. 滴滴出行DIDI美国IPO上市路演PPT:Roadshow Presentation
  9. 腾讯单点登录系统跨域劫持漏洞
  10. mac QQ 语音或视频时其他声音变小的解决办法