1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
int main (int argc, char ** argv) {
char * buf = (char *) malloc(500);
fgets(buf, 500, stdin);
char * cp;
int n = strtoll(buf, &cp, 10);
cp++;
int q = strtoll(cp, NULL, 10);
int * r = (int *) calloc(n+1, sizeof(int));
int * x = (int *) calloc(n+1, sizeof(int));
realloc(buf, (n+1)*6*9);
fgets(buf, (n+1)*6*9, stdin);
cp = buf;
int i = 1; /* začnemo z delavcem 2, ker 1 nima starša */
do {
int xi = strtoll(cp, &cp, 10);
if (xi != 0)
r[++i] = xi;
cp++;
} while (cp[-1] != 0 && cp[-1] != '\n' && cp[-1] != '\r');
i = 0; /* začnemo z delavcem 1 */
fgets(buf, (n+1)*6*9, stdin);
cp = buf;
do {
int xi = strtoll(cp, &cp, 10);
if (xi != 0)
x[++i] = xi;
cp++;
} while (cp[-1] != 0 && cp[-1] != '\n' && cp[-1] != '\r');
if (n != i)
fprintf(stderr, "error %d ni %d\n", n, i);
else
fprintf(stderr, "st\n");
fgets(buf, 1000, stdin);
while (!feof(stdin)) {
cp = buf;
if (cp[0] == '1') { /* GET */
cp += 2;
int d = strtol(cp, &cp, 10);
cp++;
int t = strtol(cp, NULL, 10);
int o = INT_MAX;
for (int i = 1; i <= n; i++) {
int p = i;
int y = 0;
while (1) {
if (p == d) {
y++;
break;
}
if (p == 1)
break;
p = r[p];
}
if (!y)
continue;
else
fprintf(stderr, "GET V HIER: %d\n", i);
if (x[i] > t && x[i] < o) {
fprintf(stderr, " x[i] == %d\n", x[i]);
o = x[i];
}
}
if (o == INT_MAX)
o = -1;
fprintf(stdout, "%d\n", o);
} else { /* POST */
cp += 2;
int d = strtol(cp, &cp, 10);
cp++;
int t = strtol(cp, NULL, 10);
x[d] = t;
fprintf(stderr, "POST OK: { ");
for (int i = 1; i <= n; i++) {
fprintf(stderr, "%d:%d, ", i, x[i]);
}
fprintf(stderr, "}\n");
}
fgets(buf, 1000, stdin);
}
free(buf);
return 0;
}
|