# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1039354 | Shadxwed | Fountain (eJOI20_fountain) | C++98 | 1570 ms | 4684 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <cstdlib>
typedef struct {
int volume;
int parent;
int i;
} treeStruct;
typedef struct {
int size;
int i;
} stackStruct;
int main() {
int n, q;
scanf("%d %d", &n, &q);
int *v = (int*)calloc(n, sizeof(int));
int *d = (int*)calloc(n, sizeof(int));
for(int i = 0; i < n; i++) {
scanf("%d %d", d+i, v+i);
}
stackStruct *stk = (stackStruct*)calloc(n+1, sizeof(stackStruct));
int lenStk = 1;
treeStruct *tree = (treeStruct*)calloc(n+1, sizeof(treeStruct));
stk[0] = (stackStruct){1000000001, 0};
tree[0] = (treeStruct){0, 0, 0};
for(int i = n-1; i >= 0; i--) {
int curr = d[i];
if(curr < stk[lenStk - 1].size) {
stk[lenStk++] = (stackStruct){curr, i+1};
} else {
while(stk[(--lenStk)-1].size < curr);
stk[lenStk++] = (stackStruct){curr, i+1};
}
tree[i+1].parent = stk[lenStk-2].i;
tree[i+1].volume = tree[tree[i+1].parent].volume + v[i];
tree[i+1].i = i+1;
}
int r, vol;
for(int p = 0; p < q; p++) {
scanf("%d %d", &r, &vol);
treeStruct node = tree[r];
if(node.volume < vol) {
printf("0\n");
continue;
}
while(true) {
treeStruct parent = tree[node.parent];
vol -= v[node.i-1];
if(vol < 0) {
printf("%d\n", node.i);
break;
}
node = parent;
}
}
free(v);
free(d);
free(stk);
free(tree);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |