1774G - Segment Covering - CodeForces Solution


brute force combinatorics constructive algorithms data structures dp trees *3200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define N 200005
#define MOD 998244353
int n,m,ds[N],ps[N],fa[N][18];struct Node {int l,r;}a[N];
int qId(int x) {return lower_bound(ds+1,ds+ds[0]+1,x)-ds;}
int qry(int l,int r)
{
	int t=qId(r);
	for(int i=17;i>=0;--i) if(ps[fa[t][i]]>=l) t=fa[t][i];
	if(ps[t]>=l) t=fa[t][0];return ps[t]!=l-2;
}
int main()
{
	scanf("%d %d",&n,&m);ds[++ds[0]]=1e9;
	for(int i=1,l,r;i<=n;++i)
	{
		scanf("%d %d",&l,&r);++l;
		ds[++ds[0]]=r-1;a[i]=(Node) {l,r};
	}sort(ds+1,ds+ds[0]+1);ds[0]=unique(ds+1,ds+ds[0]+1)-ds-1;
	for(int i=1;i<=ds[0];++i) ps[i]=-1e9;
	for(int i=1,t;i<=n;++i) t=qId(a[i].r),ps[t]=max(ps[t],a[i].l-2);
	for(int i=1;i<=ds[0];++i)
	{
		if(i>1) ps[i]=max(ps[i],ps[i-1]);fa[i][0]=qId(ps[i]);
		for(int j=1;j<=17;++j) fa[i][j]=fa[fa[i][j-1]][j-1];
	}
	for(int i=1,l,r,t;i<=m;++i)
	{
		scanf("%d %d",&l,&r);++l;t=qry(l,r)-qry(l,r-1);
		if(t<0) t+=MOD;printf("%d\n",t);
	}return 0;
}


Comments

Submit
0 Comments
More Questions

97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill