summaryrefslogtreecommitdiffstats
path: root/inf/rtk/2021-izbirno/3/prog.c
diff options
context:
space:
mode:
Diffstat (limited to 'inf/rtk/2021-izbirno/3/prog.c')
-rw-r--r--inf/rtk/2021-izbirno/3/prog.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/inf/rtk/2021-izbirno/3/prog.c b/inf/rtk/2021-izbirno/3/prog.c
new file mode 100644
index 0000000..0686c60
--- /dev/null
+++ b/inf/rtk/2021-izbirno/3/prog.c
@@ -0,0 +1,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;
+}