Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

The Baking Business CodeChef Solution

Problem -The Baking Business CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

The Baking Business CodeChef Solution in C++14

#include<bits/stdc++.h>

#define For(i,x,y) for (int i=x;i<y;i++)

using namespace std;
 
int ch;
int IN(){
	int f,x;
	while (!isdigit(ch=getchar())&&ch!='-');ch=='-'?(f=1,x=0):(f=0,x=ch-'0');
	while (isdigit(ch=getchar())) x=(x<<1)+(x<<3)+ch-'0';return !f?x:-x;
}
 
char s[10],t[10];
int a1,a2,b1,b2,b3,c,d,e,val,l,r,res;
int f[11][4][11][21][6][2][91];
 
int main(){
	for (int Qc=IN();Qc--;){
		scanf("%s",s);
		a1=IN()+1;
		if (ch=='.') a2=IN()+1;else a2=0;
		b1=IN()+1;
		if (ch=='.'){
			b2=IN()+1;
			if (ch=='.') b3=IN()+1;else b3=0;
		} else b2=b3=0;
		scanf("%s",t);
		c=IN();
		if (s[0]=='I'){
			val=IN();
			For(A1,0,11) if (A1==0||A1==a1)
				For(A2,0,4) if (A2==0||A2==a2)
					For(B1,0,11) if (B1==0||B1==b1)
						For(B2,0,21) if (B2==0||B2==b2)
							For(B3,0,6) if (B3==0||B3==b3){
								for (int i=c;i<91;i++) f[A1][A2][B1][B2][B3][t[0]=='M'][i]+=val;
							}
		} else{
			l=c;
			if (ch=='-') r=IN();else r=l;
			res=0;
			res+=f[a1][a2][b1][b2][b3][t[0]=='M'][r];
			res-=f[a1][a2][b1][b2][b3][t[0]=='M'][l-1];
			printf("%d\n",res);
		}
	}
} 

The Baking Business CodeChef Solution in JAVA


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
    private RootNode rootNode = new RootNode();
    

    private void processInput(int productId,int sizeId,int provinceId,int cityId,int regionId,int gender,int age, int unitsSold){
        age = age-1;
    	ProductNode productNode = rootNode.products.get(productId);
    	if(productNode == null){
    		productNode = new ProductNode();
    		productNode.id = productId;
    		rootNode.products.put(productId, productNode);
    	}
    	
    	ProvinceNode province = null;
    	if(sizeId == -1){
    		province = productNode.productsWithoutSize.get(provinceId);
    		if(province == null){
    			province = new ProvinceNode();
    			province.id = provinceId;
    			productNode.productsWithoutSize.put(province.id, province);
    		}
    	}else{
    		Map<Integer,ProvinceNode> provinceIdToProviceMap = productNode.productSizes.get(sizeId);
    		if(provinceIdToProviceMap == null){
    			provinceIdToProviceMap = new HashMap<Integer, ProvinceNode>(5);
    			productNode.productSizes.put(sizeId, provinceIdToProviceMap);	
    		}
    		province = provinceIdToProviceMap.get(provinceId);
    		if(province == null){
    			province = new ProvinceNode();
    			province.id = provinceId;
    			provinceIdToProviceMap.put(province.id, province);
    		}
    	}
    	
    	processProvince(province, cityId, regionId, gender, age, unitsSold);
    	productNode.salesCount[gender][age] = productNode.salesCount[gender][age]+unitsSold;
    	
    	
    }
    
    private void processProvince(ProvinceNode province,int cityId,int regionId,int gender,int age, int unitsSold){
    	if(cityId != -1){
    		CityNode node = province.cities.get(cityId);
    		if(node == null){
    			node = new CityNode();
    			node.id = cityId;
    			province.cities.put(cityId, node);
    		}
    		processCity(node, regionId, gender, age, unitsSold);
    		
    	}
    	province.salesCount[gender][age] = province.salesCount[gender][age]+unitsSold;
    	
    }
    
    private void processCity(CityNode city,int regionId,int gender,int age, int unitsSold){
    		
    	if(regionId != -1 ){
    		RegionNode node = city.regions.get(regionId);
    		if(node == null){
    			node = new RegionNode();
    			node.id = regionId;
    			city.regions.put(regionId, node);
    		}
    		processRegion(node, gender, age, unitsSold);
    		
    	}
    	city.salesCount[gender][age] = city.salesCount[gender][age]+unitsSold;
    	
    }
    
    private void processRegion(RegionNode region,int gender,int age, int unitsSold){
    	region.salesCount[gender][age] = region.salesCount[gender][age]+unitsSold;
    }
    
    private long processQuery(int productId,int sizeId,int provinceId,int cityId,int regionId,int gender,int startAge, int endAge){
    	long totalSales = 0;
    	startAge = startAge -1;
    	if(endAge != -1){
    		endAge = endAge -1;
    	}
    	if(productId == -1){
    		for(ProductNode node:rootNode.products.values()){
    			totalSales = totalSales+processQuery(node, sizeId, provinceId, cityId, regionId, gender, startAge, endAge);
    		}
    	}else if(rootNode.products.containsKey(productId)){
    		totalSales = totalSales + processQuery(rootNode.products.get(productId), sizeId, provinceId, cityId, regionId, gender, startAge, endAge);
    	}
    	return totalSales;
    }
    
   private long processQuery(ProductNode node,int sizeId,int provinceId,int cityId,int regionId,int gender,int startAge, int endAge){
	   long totalSales = 0;
	   
	   if(sizeId == -1){
		   if(provinceId == -1){
			   	totalSales = processQuery(node, gender, startAge, endAge);
		   }else{
			   ProvinceNode provinceNode = node.productsWithoutSize.get(provinceId);
			   totalSales = totalSales+processQuery(provinceNode, cityId, regionId, gender, startAge, endAge);
			   for(Map<Integer,ProvinceNode> provinceIdToNodes:node.productSizes.values()){
				   totalSales = totalSales + processQuery(provinceIdToNodes.get(provinceId), cityId, regionId, gender, startAge, endAge);
			   }
		   }
	   }else{
		   if(provinceId == -1){
			   Map<Integer,ProvinceNode> proviceIdToNodes = node.productSizes.get(sizeId);
			   if(proviceIdToNodes != null){
				   for(ProvinceNode provinceNode:proviceIdToNodes.values()){
					   totalSales = totalSales+processQuery(provinceNode, cityId, regionId, gender, startAge, endAge);
				   }
			   }
		   }else{
			   ProvinceNode provinceNode = node.productSizes.get(sizeId)!=null?node.productSizes.get(sizeId).get(provinceId):null;
			   totalSales = totalSales+processQuery(provinceNode, cityId, regionId, gender, startAge, endAge);
		   }
	   }
	   
	   return totalSales;
    	
    }
   
   private long processQuery(ProvinceNode node,int cityId,int regionId,int gender,int startAge, int endAge){
	   if(node == null) return 0;
	   
	   long totalSales = 0;
	   
	   if(cityId == -1){
			   	totalSales = processQuery(node, gender, startAge, endAge);
	   }else{
		   totalSales =  processQuery(node.cities.get(cityId),regionId, gender, startAge, endAge);
	   }
	   
	   return totalSales;
    	
    }
   
   private long processQuery(CityNode node,int regionId,int gender,int startAge, int endAge){
	   if(node == null) return 0;
	   
	   long totalSales = 0;
	   
	   if(regionId == -1){
			   	totalSales = processQuery(node, gender, startAge, endAge);
	   }else{
		   totalSales =  processQuery(node.regions.get(regionId), gender, startAge, endAge);
	   }
	   
	   return totalSales;
    	
    }
   
   private long processQuery(Node node,int gender,int startAge, int endAge){
	   if(node == null)
	   {
		   return 0;
	   }else if(endAge == -1){
	    	return node.salesCount[gender][startAge];
	    }
	    	
	   long totalSales = 0;
	   
		   for(int age = startAge;age <= endAge ;age++){
			   totalSales = totalSales+node.salesCount[gender][age];
		   }
		   
	   return totalSales;
	    	
	    
	   
	  
   }
    
    
    
    private void processInput(){
          Scanner scanner = new Scanner(System.in);
          int numberOfInputLines = scanner.nextInt();
          //sop(numberOfInputLines+"\n");
          for(;numberOfInputLines > 0;numberOfInputLines--){
        	  boolean isInput = "I".equals(scanner.next("[I|Q]"))?true:false;
        	  int productId = getId(scanner);
        	  int sizeId = getSubId(scanner,true);
        	  int provinceId = getId(scanner);
        	  int cityId = getSubId(scanner,false);
        	  int regionId = -1;
        	  if(cityId != -1){
        		  regionId = getSubId(scanner,true);
        	  }
        	  boolean isMale = "M".equals(scanner.next("[M|F]"))?true:false;
        	  
        	  if(isInput){
        		  int age = scanner.nextInt();scanner.findInLine(" ");
        		  int unitsSold = scanner.nextInt();
        		  
        		//  sop((isInput?"I":"Q")+" "+productId+"."+sizeId+" "+provinceId+"."+cityId+"."+regionId +" "+(isMale?"M":"F")+" "+age+" "+unitsSold);
        		  processInput(productId, sizeId, provinceId, cityId, regionId, isMale?0:1, age, unitsSold);
        	  }else
        	  {
        		  scanner.findInLine(" ");
        		  StringTokenizer tokenizer= new StringTokenizer(scanner.findInLine(".*"),"-");
        		  int startAge = Integer.valueOf(tokenizer.nextToken());
        		  int endAge = -1;
        		  if(tokenizer.hasMoreTokens()){
        			  endAge = Integer.valueOf(tokenizer.nextToken());
        		  }
        		  //sop((isInput?"I":"Q")+" "+productId+"."+sizeId+" "+provinceId+"."+cityId+"."+regionId +" "+(isMale?"M":"F")+" "+startAge+"-"+endAge);
        		 sop(""+processQuery(productId, sizeId, provinceId, cityId, regionId, isMale?0:1, startAge, endAge));
        	  }
        	  
          }
          scanner.close();
          
          
    }
    private int getId(Scanner scanner){
    	return Integer.valueOf(scanner.findInLine("[-]?[0-9]+"));
    }
    
    private int getSubId(Scanner scanner, boolean ignoreNextSpace){
    	if(scanner.findInLine("[.| ]").equals(" ")){
    		return -1;
    	}
    	int sizeId =  Integer.valueOf(scanner.findInLine("[0-9]+"));
    	
    	if(ignoreNextSpace){
    	   scanner.findInLine(" ");
    	}
    	return sizeId;
    }
    private void sop(String message){
    	System.out.println(message);
    }
    
    
	public static void main(String[] args) {
		Main bake = new Main();
		bake.processInput();
		
	}
	
	private static class Node{
		int id;
		long[][] salesCount = new long[2][90];
		
	}
	
	private static class RootNode{
		Map<Integer,ProductNode> products = new HashMap<Integer, Main.ProductNode>(5);
	}
	
	private static class ProductNode extends Node{
		Map<Integer,ProvinceNode> productsWithoutSize = new HashMap<Integer, Main.ProvinceNode>(5);
		Map<Integer,Map<Integer,ProvinceNode>> productSizes = new HashMap<Integer, Map<Integer,ProvinceNode>>(3);
	}
	private static class ProvinceNode extends Node{
		Map<Integer,CityNode> cities = new HashMap<Integer, Main.CityNode>(5);
		
	}
	private static class CityNode extends Node{
		Map<Integer,RegionNode> regions = new HashMap<Integer, Main.RegionNode>(3);
		
	}
	private static class RegionNode extends Node{
	}

}

The Baking Business CodeChef Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BAKE
{
    class Program
    {
        public static Dictionary<string, Dictionary<string, Dictionary<string, Data>>> dict = new Dictionary<string, Dictionary<string, Dictionary<string, Data>>>();
        public static Dictionary<string, Dictionary<string, Data>> productMinus = new Dictionary<string, Dictionary<string, Data>>();
        public static Dictionary<string, Data> provinceMinus = new Dictionary<string, Data>();
        static void init()
        {
            //Initialize the Dictionary
            //Product Data contains Product Id + size Id
            List<string> productData = new List<string>();
            //10 Products
            for (int i = 0; i < 10; i++)
            {
                //3 Sizes {0,1,2}
                for (int j = -1; j < 3; j++)
                {
                    productData.Add(i.ToString() + "." + j.ToString());
                }
            }
            //Province Data
            List<string> provinceData = new List<string>();
            for (int i = 0; i < 10; i++)
            {
                provinceData.Add(i.ToString());
            }
            //City Data consists of City + Region
            List<string> cityData = new List<string>();
            for (int i = -1; i < 20; i++)
            {
                for (int j = -1; j < 5; j++)
                {
                    cityData.Add(i.ToString() + "." + j.ToString());
                }
            }
            //Load the Dictionary
            for (int i = 0; i < productData.Count; i++)
            {
                Dictionary<string, Dictionary<string, Data>> provinceDict = new Dictionary<string, Dictionary<string, Data>>();
                for (int j = 0; j < provinceData.Count; j++)
                {
                    Dictionary<string,Data>cityDict= new Dictionary<string,Data>();
                    for (int k = 0; k < cityData.Count; k++)
                    {
                        cityDict.Add(cityData[k], new Data());
                    }
                    provinceDict.Add(provinceData[j], cityDict);

                }
                dict.Add(productData[i], provinceDict);
            }

            //Load Province Minus
            for (int i = 0; i < productData.Count; i++)
            {
                provinceMinus.Add(productData[i], new Data());
            }
            
            //Load Product Minus
            provinceData.Add("-1");
            for (int j = 0; j < provinceData.Count; j++)
            {
                Dictionary<string, Data> cityDict = new Dictionary<string, Data>();
                for (int k = 0; k < cityData.Count; k++)
                {
                    cityDict.Add(cityData[k], new Data());
                }
                productMinus.Add(provinceData[j], cityDict);
            }
        }
        static void Main(string[] args)
        {
            int count;
            init();

            string input = Console.ReadLine();

            count = Int32.Parse(input);
            
            while (count > 0)
            {
                input = Console.ReadLine();
                string[] temp = input.Split(' ');
                //Input
                //temp[1] -->Product.size
                string[] product = temp[1].Split('.');
                string productStr = "";
                string provinceStr = "";
                string cityRegionStr = "";
                string cityRegionMinus = "";
                string cityMinus = "";
                string productMinusStr = "";
                if (product.Length == 1)
                {

                    productStr = temp[1] + ".-1";
                    productMinusStr = temp[1] + ".-1";
                }
                else
                {
                    productStr = temp[1];
                    productMinusStr = product[0] + ".-1";
                }
                //temp[2] --> Province.City.Region
                string[] province = temp[2].Split('.');
                provinceStr = province[0];

                if (province.Length == 1)
                {
                    cityRegionStr = "-1.-1";
                    cityRegionMinus = "-1.-1";
                    cityMinus = "-1.-1";
                }
                else if (province.Length == 2)
                {
                    cityRegionStr = province[1] + ".-1";
                    cityMinus = "-1.-1";
                    cityRegionMinus = province[1] + ".-1";
                }
                else if (province.Length == 3)
                {
                    cityRegionStr = province[1] + "."+ province[2];
                    cityRegionMinus = province[1] + ".-1";
                    cityMinus = "-1.-1";
                }

                if (temp[0] == "I")
                {
                    int age = Int32.Parse(temp[4]);
                    int units = Int32.Parse(temp[5]);
                    if (temp[3] == "M")
                    {
                        dict[productStr][provinceStr][cityRegionStr].maleData[age]+=units;
                        if (productStr != productMinusStr)
                        {
                            dict[productMinusStr][provinceStr][cityRegionStr].maleData[age] += units;
                            provinceMinus[productMinusStr].maleData[age] += units;
                            if (cityRegionStr != cityMinus)
                            {
                                dict[productMinusStr][provinceStr][cityMinus].maleData[age] += units;

                            }
                            if (cityRegionStr != cityRegionMinus)
                            {
                                dict[productMinusStr][provinceStr][cityRegionMinus].maleData[age] += units;
                            }
                        }
                        //Add to ProductMinus
                        productMinus[provinceStr][cityRegionStr].maleData[age] += units;
                        productMinus["-1"][cityRegionStr].maleData[age] += units;
                        //Add to ProvinceMinus
                        provinceMinus[productStr].maleData[age] += units;
                        //Add to Region -1
                        if (cityRegionMinus != cityRegionStr)
                        {
                            dict[productStr][provinceStr][cityRegionMinus].maleData[age] += units;
                            productMinus[provinceStr][cityRegionMinus].maleData[age] += units;
                            productMinus["-1"][cityRegionMinus].maleData[age] += units;
                        }
                        //Add to City.-1
                        if (cityMinus != cityRegionStr)
                        {
                            dict[productStr][provinceStr][cityMinus].maleData[age] += units;
                            productMinus[provinceStr][cityMinus].maleData[age] += units;
                            productMinus["-1"][cityMinus].maleData[age] += units;
                        }

                        //Add to -1.-1 for Province
                    }
                    else
                    {
                        Data d = (Data)(dict[productStr][provinceStr][cityRegionStr]);
                        dict[productStr][provinceStr][cityRegionStr].femaleData[age] += units; 
                        if (productStr != productMinusStr)
                        {
                            dict[productMinusStr][provinceStr][cityRegionStr].femaleData[age] += units;
                            provinceMinus[productMinusStr].femaleData[age] += units;

                            if (cityRegionStr != cityMinus)
                            {
                                dict[productMinusStr][provinceStr][cityMinus].femaleData[age] += units;
                            }
                            if (cityRegionStr != cityRegionMinus)
                            {
                                dict[productMinusStr][provinceStr][cityRegionMinus].femaleData[age] += units;
                            }
                        }


                        //Add to ProductMinus
                        productMinus[provinceStr][cityRegionStr].femaleData[age] += units;
                        productMinus["-1"][cityRegionStr].femaleData[age] += units;
                        //Add to ProvinceMinus
                        provinceMinus[productStr].femaleData[age] += units;
                        if (cityRegionMinus != cityRegionStr)
                        {
                            dict[productStr][provinceStr][cityRegionMinus].femaleData[age] += units;
                            productMinus[provinceStr][cityRegionMinus].femaleData[age] += units;
                            productMinus["-1"][cityRegionMinus].femaleData[age] += units;
                        }
                        //Add to City.-1
                        if (cityMinus != cityRegionStr)
                        {
                            dict[productStr][provinceStr][cityMinus].femaleData[age] += units;
                            productMinus[provinceStr][cityMinus].femaleData[age] += units;
                            productMinus["-1"][cityMinus].femaleData[age] += units;
                        }
                    }
                }
                //Query
                else
                {
                    int minAge = 0, maxAge = 0;
                    int total = 0;
                    string[] ageRange = temp[4].Split('-');
                    minAge = Int32.Parse(ageRange[0]);
                    if (ageRange.Length == 2)
                    {
                        maxAge = Int32.Parse(ageRange[1]);
                    }
                    else
                    {
                        maxAge = minAge;
                    }
                    if (productStr == "-1.-1")
                    {
                        //Print Value from the Product -1
                        
                        Data d = productMinus[provinceStr][cityRegionStr];
                        if (temp[3] == "M")
                        {
                            for (int age = minAge; age <= maxAge; age++)
                            {
                                total += d.maleData[age];
                            }
                        }
                        else
                        {
                            for (int age = minAge; age <= maxAge; age++)
                            {
                                total += d.femaleData[age];
                            }
                        }
                    }
                    else if (provinceStr == "-1")
                    {
                        //Print from Province Data
                        Data d = provinceMinus[productStr];
                        if (temp[3] == "M")
                        {
                            for (int age = minAge; age <= maxAge; age++)
                            {
                                total += d.maleData[age];
                            }
                        }
                        else
                        {

                            for (int age = minAge; age <= maxAge; age++)
                            {
                                total += d.femaleData[age];
                            }
                        }
                    }
                    else
                    {
                        Data d = dict[productStr][provinceStr][cityRegionStr];

                        if (temp[3] == "M")
                        {
                            for (int age = minAge; age <= maxAge; age++)
                            {
                                total += d.maleData[age];
                            }
                        }
                        else
                        {
                            for (int age = minAge; age <= maxAge; age++)
                            {
                                total += d.femaleData[age];
                            }
                        }

                    }
                    Console.WriteLine(total);
                }
                count--;
            }
            
        }

    }
    public class Data
    {
        public int[] maleData = new int[91];
        public int[] femaleData = new int[91];
    }
}
The Baking Business CodeChef Solution Review:

In our experience, we suggest you solve this The Baking Business CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the The Baking Business CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this The Baking Business CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *