首页 > 其他分享 >Luogu P2082 区间覆盖(加强版)

Luogu P2082 区间覆盖(加强版)

时间:2023-01-07 05:55:05浏览次数:58  
标签:加强版 .. int Luogu tot mp 区间 P2082

链接
难度: 普及/提高-


有 \(n\) 个区间 \([s_i,t_i]\),求被这 \(n\) 个区间覆盖的长度。

数据范围:\(n\le 10^5, 1\le s_i<t_i\le10^17\)。


把每个区间都换成一组匹配的括号,考虑以下序列(不同的括号代表的是不同的区间):

..[..(].{.)..}..

发现这个部分被覆盖的距离实际就是 [} 的距离。

所以每次当遇到一个括号序列的开头时,记录 \(st=x\),每当一个括号序列结束,记录 \(ed=y\),这一部分对答案的贡献便是 \(ed-st+1\)。

由于 \(s_i,t_i\) 过大所以需要离散化。


// Problem: P2082 区间覆盖(加强版)
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2082
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

# include <bits/stdc++.h>
using namespace std;
map <long long, int> mp;
int main () {
	ios :: sync_with_stdio (false);
	cin .tie (0);
	cout .tie (0);
	int n;
	cin >> n;
	for (int i = 1, a, b; i <= n; ++ i) {
		cin >> a >> b;
		++ mp [a], -- mp [b];
	}
	long long ans = 0, last;
	int tot = 0;
	for (pair <int, int> it : mp) {
		if (! tot) {
			last = it .first;
		}// 开头
		tot += it .second;
		if (! tot) {
			ans += it .first - last + 1;
		}// 结尾
	}
	cout << ans;
	return 0;
}

标签:加强版,..,int,Luogu,tot,mp,区间,P2082
From: https://www.cnblogs.com/lhzQAQ/p/17032063.html

相关文章

  • luogu P2757 [国家集训队]等差子序列
    Link题解降智了。。。首先我们不需要关心\(Len\)是多少,只需要找到长度为\(3\)的等差子序列就行了。然后就枚举中点\(mid\),看看存不存在\(l<mid<r\)使得\(a_{mi......
  • 题解 : Luogu P2197 【模板】nim 游戏
    题目链接:link结论如果$a_1\oplusa_2\oplus...\oplusa_n\not=0$,则先手必胜证明操作到最后时,每堆石子数都是\(0\),\(0\oplus0\oplus...\oplus0=0......
  • Luogu P4178 Tree
    LuoguP4178Tree难度:省选/NOI-标签:点分治\(\mathtt{blog}\)\(n\)个点的树,边\((u_i,v_i)\)有边权\(w_i\),询问距离\(\lek\)的点对数量。数据范围:\(n\le4\times......
  • POJ 2114 Boatherds / Luogu P3806 【模板】点分治1
    POJ2114Boatherds/LuoguP3806【模板】点分治1难度:\(\mathtt{?}\)/省选/NOI-标签:点分治\(\mathtt{blog}\)\(n\)个点的树,边\((u_i,v_i)\)有边权\(w_i\),\(m\)......
  • Luogu P5676 [GZOI2017] 小z玩游戏
    P5676[GZOI2017]小z玩游戏难度:提高+/省选-标签:Tarjan建图\(\mathtt{blog}\)有\(n\)组数\((w_i,e_i)\),如果当前数值为\(w_i\)即可改变为\(e_i\),如果当前数值......
  • Luogu6620 组合数问题 - 第二类斯特林数 -
    题目链接:https://www.luogu.com.cn/problem/P6620题解:其实就一个式子证明可以利用这个式子找一下规律$$k\binom{n}{k}=n\binom{n-1}{k-1}$$回到原题,把多项式拆开之......
  • Luogu4043 支线剧情 - 费用流 -
    题目链接:https://www.luogu.com.cn/problem/P4043题意:求图一个的路径并,使得所有边都包含且所有路径的权值之和最小,而且路径都是从1开始的题解:每条边都必须经过,容量设一......
  • luogu P4002 [清华集训2017]生成树计数
    题面传送门容易想到放到prufer序列上,变成下面的形式。\(\sum\limits_{\sumc_i=n-2}{\frac{(n-2)!}{\prod\limits_{i=1}^{n}{c_i!}}\prod\limits_{i=1}^{n}{a_i^{c_i+1}(......
  • JS防抖函数加强版
    JS防抖函数加强版debounce(fn,wait=500,immediate=false){lettimer=nulllettimer2=nulllettimes=0returnfunction(...args){......
  • luogu P4565 [CTSC2018]暴力写挂
    题面传送门神tm部分分可过。首先这个式子先两倍变成\(d_x+d_y+dist(x,y)-2d'_{LCA(x,y)}\)如果我们按照情报中心那题的方法,枚举\(LCA(x,y)\),将\(d_x\)看成\(x\)的点权,\(......