Pagini recente » Cod sursa (job #1911113) | Cod sursa (job #866763) | Cod sursa (job #1922558) | Cod sursa (job #2935345) | Cod sursa (job #2590251)
package com.beniamin.savu.datorii;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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) throws IOException {
FileReader fileReader = new FileReader();
fileReader.open("datorii.in");
// Read "n" and "m"
int n = fileReader.nexInt();
int m = fileReader.nexInt();
// Read A1, A2, .... An
int[] customersDebts = new int[n];
int totalCustomerDebt = 0;
for (int i = 0; i<n; i++) {
customersDebts[i] = fileReader.nexInt();
totalCustomerDebt += customersDebts[i];
}
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();
if(dayEnd - dayStart + 1 == m) {
result.append(totalCustomerDebt).append("\n");
} else if (dayEnd - dayStart + 1 < m/2) {
int totalDebt = 0;
for(int j = dayStart - 1; j<dayEnd; j++) {
totalDebt += customersDebts[j];
}
result.append(totalDebt).append("\n");
} else {
int totalDebt = 0;
for(int j = 0; j<dayStart-1; j++) {
totalDebt += customersDebts[j];
}
if(dayEnd != m) {
for(int j = dayEnd; j<m; j++) {
totalDebt += customersDebts[j];
}
}
result.append(totalCustomerDebt - totalDebt).append("\n");
}
} else {
int day = fileReader.nexInt() - 1;
int amountPaid = fileReader.nexInt();
customersDebts[day] -= amountPaid;
totalCustomerDebt -= amountPaid;
}
}
fileReader.close();
FileWriter.write(result.toString(), "datorii.out");
}
}
/**
* 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) throws IOException {
reader = new Scanner(new FileInputStream(filePath));
}
/**
* 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) throws IOException {
PrintWriter writer = new PrintWriter(filePath);
writer.write(content + "\n");
writer.close();
}
}