Submission #70974206


Source Code Expand

import java.io.*;
import java.util.StringTokenizer;

public class Main {

    static final int MAX_VAL = 500000;
    static final int BIT_SIZE = MAX_VAL + 2;

    static long[] countBIT = new long[BIT_SIZE];
    static long[] sumBIT = new long[BIT_SIZE];

    static int[] A;
    static int N; 

    static void update(int value, int count_delta, long sum_delta) {
        int idx = value + 1;
        while (idx < BIT_SIZE) {
            countBIT[idx] += count_delta;
            sumBIT[idx] += sum_delta;
            idx += idx & (-idx); 
        }
    }

    static long query_count(int value) {
        int idx = value + 1; 
        long count = 0;
        while (idx > 0) {
            count += countBIT[idx];
            idx -= idx & (-idx); 
        }
        return count;
    }

    static long query_sum(int value) {
        int idx = value + 1; 
        long sum = 0;
        while (idx > 0) {
            sum += sumBIT[idx];
            idx -= idx & (-idx);
        }
        return sum;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        int Q = Integer.parseInt(st.nextToken());

        A = new int[N];
        st = new StringTokenizer(br.readLine());

        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(st.nextToken());
            update(A[i], 1, A[i]);
        }

        for (int q = 0; q < Q; q++) {
            st = new StringTokenizer(br.readLine());
            int type = Integer.parseInt(st.nextToken());

            if (type == 1) {
                int x = Integer.parseInt(st.nextToken()) - 1; 
                int y = Integer.parseInt(st.nextToken());
                
                update(A[x], -1, -A[x]);
                
                update(y, 1, y);
                
                A[x] = y;

            } else {
                int l = Integer.parseInt(st.nextToken());
                int r = Integer.parseInt(st.nextToken());

                //
                // Handle the case where l > r
                // max(l, min(r, A_i)) will always be 'l' if l > r.
                // So the sum is just N * l.
                if (l > r) {
                    pw.println((long)N * l);
                } else {
                    long totalSum = 0;

                    long count_less_l = query_count(l - 1);
                    totalSum += count_less_l * l;

                    long sum_in_range = query_sum(r) - query_sum(l - 1);
                    totalSum += sum_in_range;

                    long count_greater_r = N - query_count(r);
                    totalSum += count_greater_r * r;

                    pw.println(totalSum);
                }
            }
        }

        pw.flush();
        pw.close();
        br.close();
    }
}

Submission Info

Submission Time
Task E - Clamp
User addy
Language Java24 (OpenJDK 24.0.2)
Score 450
Code Size 3118 Byte
Status AC
Exec Time 466 ms
Memory 82800 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 2
AC × 21
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 02_handmade_00.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 42 ms 46148 KiB
00_sample_01.txt AC 40 ms 45948 KiB
01_random_00.txt AC 269 ms 74632 KiB
01_random_01.txt AC 339 ms 82124 KiB
01_random_02.txt AC 326 ms 69760 KiB
01_random_03.txt AC 238 ms 67372 KiB
01_random_04.txt AC 466 ms 78352 KiB
01_random_05.txt AC 352 ms 82404 KiB
01_random_06.txt AC 404 ms 78388 KiB
01_random_07.txt AC 390 ms 78516 KiB
01_random_08.txt AC 404 ms 77288 KiB
01_random_09.txt AC 402 ms 78440 KiB
01_random_10.txt AC 395 ms 78336 KiB
01_random_11.txt AC 382 ms 78764 KiB
01_random_12.txt AC 367 ms 78028 KiB
01_random_13.txt AC 325 ms 76752 KiB
01_random_14.txt AC 408 ms 78608 KiB
01_random_15.txt AC 359 ms 78276 KiB
01_random_16.txt AC 39 ms 45828 KiB
01_random_17.txt AC 241 ms 68292 KiB
02_handmade_00.txt AC 337 ms 82800 KiB