1771B - Hossam and Friends - CodeForces Solution


binary search constructive algorithms dp two pointers

Please click on ads to support us..

Python Code:

def main():

    t = int(input())
    allans = []
    for _ in range(t):
        n, m = readIntArr()
        nearest_non_friend_on_left = [0] * (n + 1)
        for __ in range(m):
            a, b = readIntArr()
            if a > b:
                a, b = b, a
            nearest_non_friend_on_left[b] = max(nearest_non_friend_on_left[b], a)
        ans = 0
        curr_nearest_left = 0
        for x in range(1, n + 1):
            curr_nearest_left = max(curr_nearest_left, nearest_non_friend_on_left[x])
            ans += x - curr_nearest_left
        allans.append(ans)
    multiLineArrayPrint(allans)
    
    return

import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b):
    print('? {} {}'.format(a, b))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(ans):
    print('! {}'.format(ans))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include <bits/stdc++.h>

using namespace std;

void solve()
{
	int n,m;
    cin>>n>>m;
    int near[n+1] = {0};
    for (int i=0;i<m;i++)
    {
        int a, b;
        cin>>a>>b;
        if (a>b)
        {
            if (near[b]>a) near[b]=a;
            else if (near[b]==0) near[b]=a;
        }
        else 
        {
             if (near[a]>b) near[a]=b;
            else if (near[a]==0) near[a]=b;
        }
    }
    near[n]=n+1;
    for (int i = n-1; i >0; i--)
    {
        // cout<<near[i]<<" ";
        if (near[i]==0)
        {
            near[i]=near[i+1];
        }
        else if (near[i]>near[i+1]) near[i]=near[i+1];
    }
    // cout<<endl;
    long long int sum=0;
    for (int i=1; i<n+1;i++)
    {
        sum+=near[i]-i;
    }
    cout<<sum<<endl;
    
}

int main()
{
	//need to calculate the no. of subarrays with a given XOR
	int t;
	cin>>t;
	while (t--) solve();
}


Comments

Submit
0 Comments
More Questions

174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes