HDU-4027 Can you answer these queries?(그 동안 우리 가 밟 았 던 구덩이)

10500 단어 JavaACM선분 수
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help. You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer. Input The input contains several test cases, terminated by EOF. For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000) The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63. The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000) For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive. Output For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
Sample Input 10 1 2 3 4 5 6 7 8 9 10 5 0 1 10 1 1 10 1 1 5 0 5 8 1 4 8
Sample Output Case #1: 19 7 6
단일 업데이트, 시간 초과 후 파 지 를 잘 랐 습 니 다.토론 을 보고 나 서 야 주 는 구간 이 꼭 오름차 가 아니 라 는 것 을 알 게 되 었 다.음 아............................................................................
public class Main {
    static final int MAX_N = 100005;
    static long[] a = new long[MAX_N << 2];//   
    static boolean[] b = new boolean[MAX_N << 2];//    ,                
    static long[] evil = new long[MAX_N];

    public static void main(String[] args) {
        InputReader reader = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        int cases = 1;
        while (reader.hasNext()) {
            int n = reader.nextInt();
            for (int i = 1; i <= n; i++) {
                evil[i] = reader.nextLong();
            }
            int m = reader.nextInt();
            out.println("Case #" + (cases++) + ":");
            build(1, 1, n);
            while (m-- > 0) {
                int t = reader.nextInt();
                int x = reader.nextInt();
                int y = reader.nextInt();
                if (x > y) {
                    int temp = y;
                    y = x;
                    x = temp;
                }
                if (t == 0) {
                    update(1, x, y, 1, n);
                } else {
                    // test(1, 1, n);
                    // System.out.println();
                    // System.out.println(query(1, x, y, 1, n));
                    out.println(query(1, x, y, 1, n));
                }
            }
            out.println();
            Arrays.fill(a, 0);
            Arrays.fill(b, false);
        }
        out.close();
    }

    public static void build(int i, int l, int r) {
        if (l == r) {
            a[i] = evil[l];
            return;
        }
        int mid = (l + r) / 2;
        build(i << 1, l, mid);
        build(i << 1 | 1, mid + 1, r);
        pushUp(i);
    }

    public static void update(int i, int L, int R, int l, int r) {
        if (l == r) {
            // a[i] = Math.round(Math.sqrt(a[i]));
            a[i] = (long) Math.sqrt(a[i]);
            if (a[i] < 2) {
                b[i] = true;
            }
            return;
        }
        int mid = (l + r) / 2;
        if (L <= mid && !b[i << 1]) {
            update(i << 1, L, R, l, mid);
        }
        if (R > mid && !b[i << 1 | 1]) {
            update(i << 1 | 1, L, R, mid + 1, r);
        }
        pushUp(i);
    }

    public static long query(int i, int L, int R, int l, int r) {
        if (l >= L && r <= R) {
            return a[i];
        }
        int mid = (l + r) / 2;
        long ans = 0;
        if (L <= mid) {
            ans += query(i << 1, L, R, l, mid);
        }
        if (R > mid) {
            ans += query(i << 1 | 1, L, R, mid + 1, r);
        }
        return ans;
    }

    //      ,    
    public static void test(int i, int l, int r) {
        if (l == r) {
            System.out.print(a[i] + " ");
            return;
        }
        int mid = (l + r) / 2;
        test(i << 1, l, mid);
        test(i << 1 | 1, mid + 1, r);
    }

    public static void pushUp(int i) {
        a[i] = a[i << 1] + a[i << 1 | 1];
        b[i] = b[i << 1] && b[i << 1 | 1];
    }
}

좋은 웹페이지 즐겨찾기