# Animal Transport

### Problem Statement :

```Capeta is working part-time for an animal shipping company. He needs to pick up animals from various zoos and drop them to other zoos. The company ships four kinds of animals: elephants, dogs, cats, and mice.

There are m zoos, numbered 1 to m. Also, there are n animals. For each animal i, Capeta knows its type ti (E for elephant, D for dog, C for cat and M for mouse), source zoo si where Capeta has to pick it up from, and destination zoo di where Capeta needs to deliver it to.

image

Capeta is given a truck with a huge capacity where n animals can easily fit. He is also given additional instructions:

1.He must visit the zoos in increasing order. He also cannot skip zoos.

2.Dogs are scared of elephants, so he is not allowed to bring them together at the same time.

3.Cats are scared of dogs, so he is not allowed to bring them together at the same time.
4.Mice are scared of cats, so he is not allowed to bring them together at the same time.
5.Elephants are scared of mice, so he is not allowed to bring them together at the same time.

Because of these reasons, Capeta might not be able to transport all animals. He will need to ignore some animals. Which ones? The company decided to leave that decision for Capeta. He is asked to prepare a report and present it at a board meeting of the company.

Capeta needs to report the minimum number of zoos that must be reached so that she is able to transport x animals, for each x from 1 to n.

Complete the function minimumZooNumbers and return an integer array where the xth integer is the minimum number of zoos that Capeta needs to reach so that she is able to transport x animals, or -1 if it is impossible to transport x animals.

He is good at driving, but not so much at planning. Hence, he needs your help.

Input Format

The first line contains a single integer t, the number of test cases.

Each test case consists of four lines. The first line contains two space-separated integers m and n. The second line contains n space-separated characters t1,t2,...,tn. The third line contains n space-separated integers s1,s2,..,sn. The fourth line contains n space-separated integers d1,d2,...,dn.

ti, si and di are the details for the th animal, as described in the problem statement.

Constraints
1 <= t <= 10
1 <= m,n <= 5.10^4
1 <= si,di <= m
si != di
ti is either E, D, C or M

For 30% of the total score, m,n <=10^3
Output Format

For each case, print a single line containing n space-separated integers, where the xth integer is the minimum number of zoos that Capeta needs to reach so that she is able to transport x animals. If it is not possible to transport x animals at all, then put -1 instead.```

### Solution :

```                            ```Solution in C :

In C++ :

#include"bits/stdc++.h"
#define F(i,j,n) for(register int i=j;i<=n;i++)
#define D(i,j,n) for(register int i=j;i>=n;i--)
#define ll long long
#define lc (k<<1)
#define rc (k<<1|1)
#define N 50010
using namespace std;
namespace io{
const int L=(1<<19)+1;
char ibuf[L],*iS,*iT,c;int f;
char gc(){
if(iS==iT){
return iS==iT?EOF:*iS++;
}
return*iS++;
}
template<class I>void gi(I&x){
for(f=1,c=gc();c<'0'||c>'9';c=gc())if(c=='-')f=-1;
for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
}
};
using io::gi;
using io::gc;
int t,n,m,p,f[N],h[N];char c;
struct no{
int l,r,t;
bool operator<(const no&b)const{return r<b.r;}
}x[N];
struct qwq{
int mx[N*4],tg[N*4];
void clear(){
memset(mx,0,sizeof(mx));
memset(tg,0,sizeof(tg));
}
mx[k]+=x;tg[k]+=x;
}
void add(int k,int l,int r,int p,int q,int x){
//		printf("%d %d %d %d %d %d\n",k,l,r,p,q,x);
int m=l+r>>1;
mx[k]=max(mx[lc],mx[rc]);
}
}a;
int main(){
gi(t);
while(t--){
gi(n);gi(m);p=1;
a.clear();a.clear();
F(i,1,m){
c=0;while(c<'A'||c>'Z')c=gc();
x[i].t=(c=='E'||c=='C');
}
F(i,1,m)gi(x[i].l);
F(i,1,m)gi(x[i].r);
sort(x+1,x+m+1);
F(i,1,m)h[i]=n+1;
F(i,1,n){
while(p<=m&&x[p].r==i)
f[i]=max(a.mx,a.mx);
f[i]=max(f[i],f[i-1]);
h[f[i]]=min(h[f[i]],i);
//			printf("%d %d\n",i,f[i]);
}
D(i,m,1)h[i-1]=min(h[i-1],h[i]);
F(i,1,m)printf("%d ",h[i]<=n?h[i]:-1);puts("");
}
return 0;
}

In Java :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

static int[] minimumZooNumbers(int m, int n, char[] t, int[] s, int[] d) {
int[] counter = new int[m + 1];

for (int i = 0; i < n; i++)
if (s[i] < d[i])
counter[d[i]]++;

int[][] ind = new int[m + 1][];

for (int i = 1; i <= m; i++)
{
ind[i] = new int[counter[i]];
counter[i] = 0;
}

for (int i = 0; i < n; i++)
if (s[i] < d[i])
{
ind[d[i]][counter[d[i]]] = i;
counter[d[i]]++;
}

Node whiteRoot = buildTree(1, m);
Node blackRoot = buildTree(1, m);

int[] maxAnimals = new int[m + 1];

for (int i = 1; i <= m; i++)
{
for (int j = 0; j < counter[i]; j++)
{
int k = ind[i][j];

if (t[k] == 'E' || t[k] == 'C')
{
andOne(blackRoot, s[k]);
}
else
{
andOne(whiteRoot, s[k]);
}
}

int max =
Math.max(blackRoot.value + blackRoot.plus,
whiteRoot.value + whiteRoot.plus);

insert(blackRoot, i, max);
insert(whiteRoot, i, max);
maxAnimals[i] = max;
}

int[] res = new int[n];
int next = 0;

for (int i = 1; i <= m; i++)
{
while (next < maxAnimals[i])
{
res[next] = i;
next++;
}
}

while (next < n)
{
res[next++] = -1;
}

return res;
}

static class Node
{
Node left;
Node right;
int leftBound;
int rightBound;
int value = 0;
int plus = 0;
}

static Node buildTree(int left, int right)
{
Node node = new Node();
node.leftBound = left;
node.rightBound = right;

if (left == right)
{
return node;
}

int mid = (left + right) / 2;

node.left = buildTree(left, mid);
node.right = buildTree(mid + 1, right);

return node;
}

static void andOne(Node node, int r)
{
if (r >= node.rightBound)
{
node.plus++;
return;
}

if (node.plus > 0)
{
node.left.plus += node.plus;
node.right.plus += node.plus;
}

andOne(node.left, r);

if (r >= node.right.leftBound)
{
andOne(node.right, r);
}

updateNode(node);
}

static void updateNode(Node node)
{
node.value = Math.max(node.left.value + node.left.plus,
node.right.value + node.right.plus);
node.plus = 0;
}

static void insert(Node node, int ind, int v)
{
if (node.leftBound == ind && node.rightBound == ind)
{
node.value = v;
node.plus = 0;
return;
}

if (node.plus > 0)
{
node.left.plus += node.plus;
node.right.plus += node.plus;
}

if (node.left.rightBound >= ind)
{
insert(node.left, ind, v);
}
else
{
insert(node.right, ind, v);
}

updateNode(node);
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cases = in.nextInt();
for(int a0 = 0; a0 < cases; a0++){
int m = in.nextInt();
int n = in.nextInt();
char[] t = new char[n];
for(int t_i = 0; t_i < n; t_i++){
t[t_i] = in.next().charAt(0);
}
int[] s = new int[n];
for(int s_i = 0; s_i < n; s_i++){
s[s_i] = in.nextInt();
}
int[] d = new int[n];
for(int d_i = 0; d_i < n; d_i++){
d[d_i] = in.nextInt();
}
int[] result = minimumZooNumbers(m, n, t, s, d);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + (i != result.length - 1 ? " " : ""));
}
System.out.println("");

}
in.close();
}
}```
```

## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your \$inOrder* func

## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.