Submission details
Task:Subarray Sums II
Sender:Isak
Submission time:2025-11-26 21:09:15 +0200
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.17 sdetails
#6ACCEPTED0.19 sdetails
#7ACCEPTED0.07 sdetails
#8ACCEPTED0.10 sdetails
#9ACCEPTED0.11 sdetails
#10ACCEPTED0.11 sdetails
#11ACCEPTED0.11 sdetails
#12ACCEPTED0.19 sdetails
#13ACCEPTED0.21 sdetails
#14ACCEPTED0.20 sdetails
#15ACCEPTED0.20 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.00 sdetails
#18ACCEPTED0.11 sdetails
#19ACCEPTED0.18 sdetails
#20ACCEPTED0.10 sdetails
#21ACCEPTED0.19 sdetails
#22ACCEPTED0.20 sdetails
#23ACCEPTED0.20 sdetails
#24ACCEPTED0.00 sdetails
#25ACCEPTED0.12 sdetails
#26ACCEPTED0.12 sdetails
#27ACCEPTED0.12 sdetails
#28ACCEPTED0.18 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:43:13: warning: variable 'v' set but not used [-Wunused-but-set-variable]
   43 |     int64_t v = 1;
      |             ^

Code

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <search.h>

#define LOOP(i,s,e) for (uint64_t i = (s); i < (e); i++)
#define SCAN(...) if (scanf(__VA_ARGS__) == 0) return EXIT_FAILURE

int main() {
    uint64_t n = 0;
    int64_t target;
    //printf("0\n");
    SCAN("%ld %ld", &n, &target);

    // main algo

    //printf("1\n");
    int64_t *tab = (int64_t *) malloc(n * sizeof(int64_t));
    //printf("2\n");
    int64_t *pref = (int64_t *) malloc(n * sizeof(int64_t));
    //printf("3\n");

    (void) hcreate(n);
    //printf("4\n");

    SCAN("%ld", tab);
    pref[0] = tab[0];
    LOOP(i, 1, n){
        SCAN("%ld", tab+i);
        //printf("add tab[%ld] : %ld", i, tab[i]);
        pref[i] = pref[i-1] + tab[i];
    }


    // ajout de 0
    // printf("ajout %ld : %ld\n", -1, 0);
    char *key = (char*) malloc(20 * sizeof(char));
    char *store_key = (char*) malloc(20 * sizeof(char));
    memcpy(store_key, "0", 2);
    int64_t v = 1;
    ENTRY e = {store_key, (void*) 1};
    hsearch(e, ENTER);
    // printf("adding %s (%ld)\n", store_key, 0);

    uint64_t acc = 0;
    LOOP(i, 0, n){
        int64_t val = pref[i] - target;
        ENTRY e;
        ENTRY *pe;
        sprintf(key, "%ld", val);
        //printf("4\n");
        e = {key, NULL};
        pe = hsearch(e, FIND);
        // printf("search : %ld\n", val);
        if (pe != NULL){
            // printf("trouvé\n");
            acc += ((int64_t)pe->data);
        }
        // printf("end of loop %ld, acc = %ld. val = %ld\n", i, acc, val);
        // printf("searching : %s\n", key);
        // if (pe == NULL)
        //     printf("found     : NULL\n");
        // else
        //     printf("found     : %s  ->  %ld\n", pe->key, (int64_t)pe->data);
        sprintf(key, "%ld", pref[i]);
        e = {key, NULL};
        pe = hsearch(e, FIND);
        if (pe == NULL){
            //printf("3a\n");
            // printf("ajout %ld : %ld\n", i, pref[i]);
            store_key = (char*) malloc(20 * sizeof(char));
            sprintf(store_key, "%ld", pref[i]);
            // printf("adding %s (%ld)\n", store_key, pref[i]);
            v = 1;
            ENTRY e = {store_key, (void *) 1};
            hsearch(e, ENTER);
        } else {
            //printf("3b\n");
            // printf("increment %ld\n", i);
            pe->data = (void*) ((int64_t)pe->data + 1);
            // printf("increment %s (%ld)\n", key, pref[i]);
        }
        //printf("5\n");
    }

    hdestroy();
    free(key);
    free(tab);
    free(pref);
    printf("%ld\n", acc);

    return EXIT_SUCCESS;
}

Test details

Test 1

Verdict: ACCEPTED

input
100 50
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
51

user output
51

Test 2

Verdict: ACCEPTED

input
100 1000000000
1000000000 1000000000 10000000...

correct output
100

user output
100

Test 3

Verdict: ACCEPTED

input
100 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

correct output
5050

user output
5050

Test 4

Verdict: ACCEPTED

input
100 4
2 1 -3 2 -7 7 -2 6 9 -4 10 -6 ...

correct output
53

user output
53

Test 5

Verdict: ACCEPTED

input
200000 100000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
100001

user output
100001

Test 6

Verdict: ACCEPTED

input
200000 1000000000
1000000000 1000000000 10000000...

correct output
200000

user output
200000

Test 7

Verdict: ACCEPTED

input
200000 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

correct output
20000100000

user output
20000100000

Test 8

Verdict: ACCEPTED

input
200000 39
44 -62 -3 23 11 -68 42 69 -22 ...

correct output
903601

user output
903601

Test 9

Verdict: ACCEPTED

input
131072 199999
199999 199999 199999 199999 19...

correct output
131072

user output
131072

Test 10

Verdict: ACCEPTED

input
131072 107897
107897 107897 107897 107897 10...

correct output
131072

user output
131072

Test 11

Verdict: ACCEPTED

input
131072 126271
126271 126271 126271 126271 12...

correct output
131072

user output
131072

Test 12

Verdict: ACCEPTED

input
200000 107897
107897 107897 107897 107897 10...

correct output
199999

user output
199999

Test 13

Verdict: ACCEPTED

input
200000 100000
1056323 1056323 1056323 105632...

correct output
0

user output
0

Test 14

Verdict: ACCEPTED

input
200000 100000
2144977 2144977 2144977 214497...

correct output
0

user output
0

Test 15

Verdict: ACCEPTED

input
200000 100000
65536 65536 65536 65536 65536 ...

correct output
0

user output
0

Test 16

Verdict: ACCEPTED

input
5 0
0 0 0 0 0

correct output
15

user output
15

Test 17

Verdict: ACCEPTED

input
20 536870912
268435456 268435456 268435456 ...

correct output
19

user output
19

Test 18

Verdict: ACCEPTED

input
131072 136607
136607 136607 136607 136607 13...

correct output
131072

user output
131072

Test 19

Verdict: ACCEPTED

input
200000 562841
562841 562841 562841 562841 56...

correct output
200000

user output
200000

Test 20

Verdict: ACCEPTED

input
107897 107897
107897 107897 107897 107897 10...

correct output
107897

user output
107897

Test 21

Verdict: ACCEPTED

input
200000 202409
101204 101205 101204 101205 10...

correct output
199998

user output
199998

Test 22

Verdict: ACCEPTED

input
200000 202409
138630 138631 138630 138631 13...

correct output
0

user output
0

Test 23

Verdict: ACCEPTED

input
200000 10273
410857 410857 410857 410857 41...

correct output
0

user output
0

Test 24

Verdict: ACCEPTED

input
5 2
1 -1 1 -1 2

correct output
3

user output
3

Test 25

Verdict: ACCEPTED

input
200000 1
1048577 -1048570 29 145 725 36...

correct output
104671

user output
104671

Test 26

Verdict: ACCEPTED

input
200000 1
1048577 -1048571 25 125 625 31...

correct output
104859

user output
104859

Test 27

Verdict: ACCEPTED

input
200000 0
5334500 -3502392 3421268 -2064...

correct output
6341575890

user output
6341575890

Test 28

Verdict: ACCEPTED

input
200000 0
172933 172933 172933 172933 17...

correct output
0

user output
0