Pagini recente » Cod sursa (job #106256) | Cod sursa (job #1473243) | Cod sursa (job #2703662) | Cod sursa (job #1695735) | Cod sursa (job #3242182)
import java.io.*;
public class Main {
static final String INPUT_FILE = "aib.in";
static final String OUTPUT_FILE = "aib.out";
public static class TokenizedReader {
private final BufferedReader reader;
private int currentChar;
private String currentToken;
TokenizedReader(String filePath) throws Exception {
reader = new BufferedReader(new FileReader(filePath));
currentToken = "";
currentChar = reader.read();
}
private void skipWhitespace() {
while (currentChar != -1 && Character.isWhitespace(currentChar)) {
try {
currentChar = reader.read();
} catch (IOException e) {
throw new RuntimeException();
}
}
}
private String nextToken() {
currentToken = ""; // Clear the current token
skipWhitespace();
while (currentChar != -1 && !Character.isWhitespace(currentChar)) {
currentToken += (char) currentChar;
try {
currentChar = reader.read();
} catch (IOException e) {
throw new RuntimeException();
}
}
return currentToken;
}
private int nextInt() {
return Integer.parseInt(nextToken());
}
public void close() throws IOException {
reader.close();
}
}
public static void main(String[] args) throws Exception {
TokenizedReader reader = new TokenizedReader(INPUT_FILE);
PrintWriter writer = new PrintWriter(OUTPUT_FILE);
solve(reader, writer);
reader.close();
writer.flush();
writer.close();
}
public static class FenwickTreeSum {
private final int[] node;
private final int n;
public FenwickTreeSum(int n) {
this.n = n;
this.node = new int[n + 1];
}
public int querySum(int index) {
int sum = 0;
while (index > 0) {
sum += node[index];
index -= (index & (-index));
}
return sum;
}
public void update(int index, int val) {
while (index <= n) {
node[index] += val;
index += (index & (-index));
}
}
}
public static void solve(TokenizedReader reader,
PrintWriter writer) {
int n = reader.nextInt();
int m = reader.nextInt();
FenwickTreeSum fenwickTree = new FenwickTreeSum(n);
for (int i = 1; i <= n; ++i) {
int val = reader.nextInt();
fenwickTree.update(i, val);
}
while (m-- > 0) {
int op = reader.nextInt();
if (op == 0) {
int a = reader.nextInt();
int b = reader.nextInt();
fenwickTree.update(a, b);
} else if (op == 1) {
int a = reader.nextInt();
int b = reader.nextInt();
writer.println(fenwickTree.querySum(b) - fenwickTree.querySum(a - 1));
} else if (op == 2) {
int a = reader.nextInt();
writer.println(lowerBound(fenwickTree, a));
}
}
}
public static int lowerBound(FenwickTreeSum fenwickTreeSum, int val) {
int l = 1;
int r = fenwickTreeSum.n;
int lowerBound = -1;
while (l <= r) {
int mid = (l + r) / 2;
int currSum = fenwickTreeSum.querySum(mid);
if (val == currSum) {
lowerBound = mid;
}
if (val <= currSum) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return lowerBound;
}
}