2019牛客暑期多校训练营(第八场)E.Explorer
链接:https://ac.nowcoder.com/acm/contest/888/E
来源:牛客网
Please help them to find the number of valid sizes.
输入描述:
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.
输入
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 = ;
const int maxn = ;
const int maxm = ;
const int inf = 0x3f3f3f3f;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
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<<]; void update(int l,int r,int rt,int L,int R,int id){
if(rt==){ return;}
if(L<=l&&R>=r){
eg[rt].push_back(id);
return;
}
int mid = (l+r)>>;
if(L<=mid)update(l,mid,rt*,L,R,id);
if(R>mid)update(mid+,r,rt*+,L,R,id);
} int getf(int x){
if(x==f[x]){ return x;}
return getf(f[x]);
} int ans = ;
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],});
f[t1]=f[t2];
}else if(rk[t1]>rk[t2]){
tmp.push(node{f[t2],});
f[t2]=f[t1];
}else{
tmp.push(node{f[t2],});
f[t2]=f[t1];
rk[t2]++;
}
} if(l==r){
if(getf()==getf(n)&&l!=tot){
ans+=rem[r+]-rem[l];
}
while (!tmp.empty()){
node it = tmp.top();
tmp.pop();
f[it.num]=it.num;
if(it.type==){
rk[it.num]--;
}
}
return;
}
int mid = (l+r)>>;
dfs(lson);
dfs(rson);
while (!tmp.empty()){
node it = tmp.top();
tmp.pop();
f[it.num]=it.num;
if(it.type==){
rk[it.num]--;
}
}
} int get_id(int x){
return lower_bound(rem+,rem++tot,x)-rem;
} int main() { scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
f[i]=i;
rk[i]=;
} for(int i=;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+;
}
sort(rem+,rem++tot);
tot = unique(rem+,rem++tot)-rem-; for(int i=;i<=m;i++){
cout<<get_id(e[i].l)<<" "<<get_id(e[i].r+)-<<endl;
update(,tot,,get_id(e[i].l),get_id(e[i].r+)-,i);
}
dfs(,tot,);
printf("%d\n",ans);
return ;
}
2019牛客暑期多校训练营(第八场)E.Explorer的更多相关文章
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...
- [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem
链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 2019牛客暑期多校训练营(第二场)J-Subarray(思维)
>传送门< 前言 这题我前前后后看了三遍,每次都是把网上相关的博客和通过代码认真看了再思考,然并卵,最后终于第三遍也就是现在终于看懂了,其实懂了之后发现其实没有那么难,但是的的确确需要思维 ...
- 2019牛客暑期多校训练营(第一场)-A (单调栈)
题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...
- 2019牛客暑期多校训练营(第一场)A - Equivalent Prefixes(单调栈)
题意 给定两个$n$个元素的数组$a,b$,它们的前$p$个元素构成的数组是"等价"的,求$p$的最大值."等价"的意思是在其任意一个子区间内的最小值相同. $ ...
随机推荐
- C++之ARX,Acstring,ACahr转char
AcDbText* pText = AcDbText::cast(pEnt); AcString sText = DBHelper::AcStringFree(pText->textString ...
- 条件变量用例--解锁与signal的顺序问题
我们知道,当调用signal/broadcast唤醒等待条件变量的其他线程时,既可以在加锁的情况下调用signal/broadcast,也可以在解锁的情况下调用. 那么,到底哪种情况更好呢?man手册 ...
- 【NS2】ns2 otcl与c++关联(转载)
最近几天,对ns2进行研究,ns2为什么要使用两种语言,因为C++执行速度快,因此对于一些不需要经常改变的东西:例如包的发送.而对于需要经常进行修改的就不能够使用C++,而使用OTcl脚本语言.所有O ...
- Java面向对象----方法重载
方法重载(over load):方法名相同,参数列表不同,返回类型无关 package com.tanlei.newer; public class OverLoadDemo { public sta ...
- iOS tableView优化
iOS: Autolayout和UITableViewCell的动态高度 http://www.mgenware.com/blog/?p=507 优化UITableViewCell高度计算的那些事 h ...
- Java练习 SDUT-1230_平方和与立方和
平方和与立方和 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇 ...
- @codeforces - 702F@ T-Shirts
目录 @description@ @solution@ @accepted code@ @details@ @description@ 有 n 件 T-shirt,第 i 件 T-shirt 有一个 ...
- AspNetPager 样式
使用方法: 1.引入样式表. 将 想要使用的样式表加入到本页面<style type="text/css"></style>标记中,或者新建一个css文件如 ...
- 模板—中国剩余定理+拓展GCD
int exgcd(int a,int b,int &x,int &y) { ) { x=,y=; return a; } int gcd=exgcd(b,a%b,x,y); int ...
- vue实现购物车逻辑
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...