# Gridland Metro

### Problem Statement :

```The city of Gridland is represented as an n x m matrix where the rows are numbered from 1 to n and the columns are numbered from 1 to m.

Gridland has a network of train tracks that always run in straight horizontal lines along a row. In other words, the start and end points of a train track are  and , where  represents the row number,  represents the starting column, and  represents the ending column of the train track.

The mayor of Gridland is surveying the city to determine the number of locations where lampposts can be placed. A lamppost can be placed in any cell that is not occupied by a train track.

Given a map of Gridland and its  train tracks, find and print the number of cells where the mayor can place lampposts.

Note: A train track may overlap other train tracks within the same row.

In this case, there are five open cells (red) where lampposts can be placed.

Function Description

Complete the gridlandMetro function in the editor below.

gridlandMetro has the following parameter(s):

int n:: the number of rows in Gridland
int m:: the number of columns in Gridland
int k:: the number of tracks
track[k][3]: each element contains  integers that represent , all 1-indexed
Returns

int: the number of cells where lampposts can be installed
Input Format

The first line contains three space-separated integers  and , the number of rows, columns and tracks to be mapped.

Each of the next  lines contains three space-separated integers,  and , the row number and the track column start and end.```

### Solution :

```                            ```Solution in C :

In    C++  :

#include <bits/stdc++.h>
using namespace std;

int main() {
long long h, w, K;
cin >> h >> w >> K;

long long ans = h * w;

map<int, vector<pair<int, int>>> mp;

for (int i = 0; i < K; i++) {
int y, l, r;
scanf("%d %d %d", &y, &l, &r);
mp[y].emplace_back(l, r);
}

for (auto &kv : mp) {
vector<pair<int, int>> evs;

for (auto p : kv.second) {
evs.emplace_back(p.first, 0);
evs.emplace_back(p.second, 1);
}

sort(evs.begin(), evs.end());

int cnt = 0;
int l = 0;

for (auto e : evs) {
if (e.second == 0) {
if (cnt == 0) l = e.first;
cnt++;
} else {
cnt--;
if (cnt == 0) ans -= e.first - l + 1;
}
}
}

cout << ans << endl;
}

In   Java :

import java.util.*;
import java.awt.*;

public class Solution {
ArrayList<Point> locs;

public Solution(){
locs = new ArrayList<>();
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long rows = in.nextInt(), cols = in.nextInt();
int tracks = in.nextInt();
ArrayList<Integer> check = new ArrayList<>();
HashMap<Integer, Solution> found = new HashMap<>();

for(int i = 0; i<tracks; i++){
int curRow = in.nextInt(), start = in.nextInt(), end = in.nextInt();
if(!found.containsKey(curRow)){
Solution sol = new Solution();
found.put(curRow,sol);
}else{
Solution sol = found.get(curRow);
}
}

//Computing the total # of tracks - those of train tracks
long total = rows * cols;
for(int r : check){
//  System.out.println(r);
Solution sol = found.get(r);
ArrayList<Point> myPoints = sol.locs;
Collections.sort(myPoints, new myComparator());
Point first = myPoints.get(0);
total -= (first.y - first.x+1);
int lastEnd = first.y+1;
for(int i = 1; i<myPoints.size(); i++){
Point curPoint = myPoints.get(i);
if(curPoint.y< lastEnd) continue;
int begin = Math.max(curPoint.x, lastEnd);
total -=(curPoint.y - begin + 1);
lastEnd = curPoint.y+1;
}
}

System.out.println(total);
}
}
class myComparator implements Comparator<Point>{
public int compare(Point p1, Point p2){
return p1.x - p2.x;
}
}

In   C  :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

struct points
{
long int r;
long int c1;
long int c2;
};

typedef struct points points;

int comp(const void* a,const void* b)
{
points* a1=(points*)a;
points* b1=(points*)b;
if((a1->r)!=(b1->r))
return (a1->r)-(b1->r);
else
{
if((a1->c1)!=(b1->c1))
return (a1->c1)-(b1->c1);
else
return (a1->c2)-(b1->c2);
}
}

long int min(long int a,long int b)
{
if(a<b)
return a;
return b;
}

long int max(long int a,long int b)
{
if(a>b)
return a;
return b;
}

int main() {

long int n,m,r,c1,c2,temp,r_s,r_e;
int k,i,j;
scanf("%ld %ld %d",&n,&m,&k);
points p[k];
for(i=0;i<k;i++)
scanf("%ld %ld %ld",&p[i].r,&p[i].c1,&p[i].c2);
qsort(p,k,sizeof(p[0]),comp);
int visited[k];
memset(visited,0,sizeof(visited));
long long ans=n*m;
for(i=0;i<k;i++)
{
long int sub=0;
if(visited[i]==0){
for(j=i+1;j<k;j++)
{
if(p[i].r==p[j].r)
{
if(p[i].c2<p[j].c1)
{
sub+=p[i].c2-p[i].c1+1;
p[i].c1=p[j].c1;
p[i].c2=p[j].c2;
}
else
{
r_s=min(p[i].c1,p[j].c1);
r_e=max(p[i].c2,p[j].c2);
p[i].c1=r_s;
p[i].c2=r_e;
}
visited[j]=1;
}
else
break;
}
ans-=sub;
ans-=(p[i].c2-p[i].c1+1);
}
}
printf("%lld",ans);
return 0;
}

In   Python3  :

from collections import defaultdict

n, m, k = map(int, input().split())

tracks = defaultdict(list)
for _ in range(k):
row, c1, c2 = map(int, input().split())
tracks[row].append((c1, -1))
tracks[row].append((c2 + 1, 1))

ans = 0
for row in tracks:
tracks[row].sort()

prev = tracks[row][0][0]
depth = 1
for event in tracks[row][1:]:
if depth > 0:
ans += (event[0] - prev)

depth -= event[1]
prev = event[0]

print(n * m - ans)```
```

## Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

## Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

## Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

## Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

## Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

## Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan