1366B - Shuffle - CodeForces Solution


math two pointers *1300

Please click on ads to support us..

Python Code:

import heapq
from collections import *
import math
def solve():

    n, x, m = map(int, input().split())
    maxi, mini = float("-inf"), float("inf")
    x1, x2 = x, x
    for i in range(m):
        a, b = map(int, input().split())
        f = 0
        if (x1 >= a and x1 <= b) or (x2 >=a and x2<=b):
            f = 1
            maxi = max(maxi, a, b)
            mini = min(mini, a, b)
        if f == 1:
            x1 = mini
            x2 = maxi
    if maxi - mini + 1 == float("-inf"):
        print(1)
        return 
    print(maxi - mini + 1)


t = int(input())
for _ in range(t):
    solve()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mod 1000000007
#define ff first
#define se second
#define vc vector
ll bpow(ll a,ll b)
{
    ll res=1;
    while(b)
    {
        if(b&1)
        {
            res=res*a;
            res%=mod;
        }
        a=a*a;
        a%=mod;
        b=b>>1;
    }
    return res;
}
ll fact(ll p)
{
	ll ans=1;
	for(ll i=1;i<=p;i++)
	{
		ans*=i;
		ans%=mod;
	}
	return ans;
}
string num(ll n)
{
	string res="";
	while(n)
	{
		ll po=n%10;
		n=n/10;
		res+=to_string(po);
	}
	reverse(res.begin(),res.end());
	return res;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		 ll n,i,j,x,m;
	    cin>>n>>x>>m;
	    vector<pair<ll,ll>>v1(m);
	    for(i=0;i<m;i++)
	    {
	        ll l1,r1;
	        cin>>l1>>r1;
	        v1[i]={l1,r1};
	    }
	    ll curl=0,curr=0,op;
	    for(i=0;i<m;i++)
	    {
	        ll l2,r2;
	        l2=v1[i].ff;
	        r2=v1[i].se;
	        if(l2<=x and x<=r2)
	        {
	            curl=l2;
	            curr=r2;
	            op=i;
	            break;
	        }
	    }
	    if(curl!=0 and curr!=0)
	    {
	        for(i=op+1;i<m;i++)
	        {
	            ll l2,r2;
	        l2=v1[i].ff;
	        r2=v1[i].se;
	        if(l2>curr or r2<curl)
	        continue;
	        curl=min(curl,l2);
	        curr=max(curr,r2);
	        }
	    }
	    ll ans;
	    if(curl==0 and curr==0)
	    ans=1;
	    else 
	    ans=curr-curl+1;
	    cout<<ans<<"\n";
	}
}


Comments

Submit
0 Comments
More Questions

2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff