Cod sursa(job #2590387)

Utilizator benisavuSavu Beniamin benisavu Data 27 martie 2020 20:13:24
Problema Datorii Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 3.37 kb
//package com.beniamin.savu.datorii;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Hello world!
 *
 */
public class Main {
	
	/**
	 * Code to detect when the father iterogates Gigel.
	 */
	private static final int FATHER_INTEROGATION_OPERATION = 1;
	
	public static void main(String[] args) {
		FileReader fileReader = new FileReader();
		fileReader.open("datorii.in");
		
		// Read "n" and "m"
		int n = fileReader.nexInt();
		int m = fileReader.nexInt();
		
		
		
		int[] daysDebt = new int[n];
		for (int i = 0; i < n; i++) {
			daysDebt[i] = fileReader.nexInt();
		}
		
		int[][] intervals = new int[n][n];
		for (int i = 0; i < n; i++) {
			int lineTotal = 0;
			for (int j = i; j < n; j++) {
				lineTotal += daysDebt[j];
				intervals[i][j] = lineTotal;
			}
		}
		
		int[] paid = new int[n];
		
		StringBuilder result = new StringBuilder();
		for (int i = 0; i<m; i++) {
			int query = fileReader.nexInt();
			if(query == FATHER_INTEROGATION_OPERATION) {
				int dayStart = fileReader.nexInt();
				int dayEnd = fileReader.nexInt();
				int totalPaid = 0;
				for(int j = dayStart - 1; j < dayEnd - 1; j++) {
					totalPaid += paid[j];
				}
				int debt = intervals[dayStart - 1][dayEnd - 1] - totalPaid;
				result.append(debt).append("\n");
			} else {
				int day = fileReader.nexInt() - 1;
				int amountPaid = fileReader.nexInt();
				paid[day - 1] = amountPaid;
			}
		}
		fileReader.close();
		FileWriter.write(result.toString(), "datorii.out");
	}
}

class DayDebtInterval {
	private int startDay;
	
	private int endDay;
	
	private int totalDebt;
}

class DayDebt {
	
	private int day;
	
	private int debt;
	
	private DayDebt next;
	
	public DayDebt(int day, int debt, DayDebt next) {
		this.day = day;
		this.debt = debt;
		this.next = next;
	}
	
	public void setNextDebt(DayDebt next) {
		this.next = next;
	}

	public DayDebt(int day, int debt) {
		this.day = day;
		this.debt = debt;
	}

	public int findDebt(int day) {
		if(this.day == day) {
			return debt;
		} else {
			if(next != null) {
				return debt + next.findDebt(day);
			} else {
				return debt;
			}
		}
	}
	
	public void payDebt(int debt) {
		this.debt = this.debt - debt;
	}
	
}

/**
 * Reads the content of a file.
 * 
 * @author benis
 *
 */
class FileReader {

	/**
	 * The reader.
	 */
	private Scanner reader;
	
	/**
	 * Reads the contents of the file.
	 */
	public void open(String filePath) {
		try {
			reader = new Scanner(new FileInputStream(filePath));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
	}
	
	/**
	 * Reads the next integer.
	 * 
	 * @return The integer.
	 */
	public int nexInt() {
		return reader.nextInt();
	}
	
	/**
	 * Closes the connection with the file.
	 */
	public void close() {
		reader.close();
	}

}

/**
 * Writes content to a file.
 * 
 * @author benis
 *
 */
class FileWriter {

	/**
	 * Writes the given content to the given file.
	 *
	 * @param content The content to write.
	 * @param filePath The file path
	 */
	public static void write(String content, String filePath) {
		try (PrintWriter writer = new PrintWriter(filePath)) {
			writer.write(content + "\n");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}