inline LL read(){ registerchar c; for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar()); registerbool f = c == '-'; register LL s = f ? 0 : c - '0'; for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) { s = (s << 3) + (s << 1) + c - '0'; } return f ? -s : s; }
voidinsert(LL p){ nowid++; node[nowid].p = p; LL newtop = top; while (newtop && node[stk[newtop]].p > node[nowid].p) newtop--; if (newtop) node[stk[newtop]].rs = nowid; if (newtop < top) node[nowid].ls = stk[newtop + 1]; stk[++newtop] = nowid; top = newtop; }
intmain(){ n = read(); LL p; for (LL i = 0; i < n; i++) { p = read(); insert(p);/*这里 insert 的数字的值(p)没有单调的要求, 但数字的标号(id)要求单增,若不单增要先排序*/ } for (LL i = 1; i <= n; i++) { printf("%lld %lld %lld\n", node[i].p, node[i].ls, node[i].rs); } }