Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fenwicktree;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.StringTokenizer;
- public class Main {
- private int N;
- ArrayList<Integer> fenwick_tree = new ArrayList<Integer>();
- public void addition(int k, int value) {
- for (; k < fenwick_tree.size(); k |= (k + 1)) {
- fenwick_tree.set(k, fenwick_tree.get(k) + value);
- }
- }
- public int getQueryResult(int a, int b) {
- if (a == 0) {
- int sum = 0;
- for (; b >= 0; b = (b & (b + 1)) - 1) {
- sum += fenwick_tree.get(b);
- }
- return sum;
- } else {
- return getQueryResult(0, b) - getQueryResult(0, a - 1);
- }
- }
- public int nextInt(String s) {
- return Integer.valueOf(s);
- }
- public void run() {
- try {
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- StringTokenizer st;
- N = nextInt(bf.readLine());
- for (int i = 0; i <= N; i++) {
- fenwick_tree.add(0);
- }
- for (int i = 1; i <= N; i++) {
- addition(i, nextInt(bf.readLine()));
- }
- N = nextInt(bf.readLine());
- for (int i = 1; i <= N; i++) {
- st = new StringTokenizer(bf.readLine());
- System.out.println(getQueryResult(nextInt(st.nextToken()), nextInt(st.nextToken())));
- }
- } catch (IOException e) {
- }
- }
- public static void main(String[] args) {
- new Main().run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement