#include #include #include #include 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; }