QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#879713 | #7471. ODT | FLY_lai | 0 | 0ms | 0kb | C++14 | 4.9kb | 2025-02-02 11:33:14 | 2025-02-02 11:33:14 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
const int INF = 2e9 + 10;
int n, q;
int fa[N], sz[N], d[N], son[N] = {}, tp[N], dfn[N], cur = 0;
vector<int> e[N];
int dfs(int x, int pr) {
d[x] = d[pr] + 1;
fa[x] = pr;
sz[x] = 1;
for (auto i: e[x]) {
if (i == pr)
continue;
sz[x] += dfs(i, x);
if (sz[i] > sz[son[x]])
son[x] = i;
}
return sz[x];
}
void srh(int x, int t) {
dfn[x] = ++cur;
tp[x] = t;
if (son[x])
srh(son[x], t);
for (auto i: e[x])
if (i != fa[x] && i != son[x])
srh(i, i);
}
struct BIT {
int tr[N];
inline void mdf(int x, int v) {
for (int i = x; i <= n; i += (i & -i))
tr[i] += v;
}
inline int qry(int x) {
int ret = 0;
for (int i = x; i; i -= (i & -i))
ret += tr[i];
return ret;
}
} bit;
int rt[N]; //rt[i]记录结点i平衡树的根结点
mt19937 myrand(time(0));
struct Node {
int l, r, sz, val, pri;
Node() {
l = r = sz = val = 0;
pri = -1;
}
Node(int v) {
l = r = 0;
sz = 1;
val = v;
pri = myrand();
}
};
struct FHQ_Treap {
queue<int> q; //储存无用结点编号
int n;
Node a[N * 2];
int newNode(int v) {
int id;
if (!q.empty()) {
id = q.front(), q.pop();
}
else
id = ++n;
a[id] = Node(v);
return id;
}
void pushup(int x) {
a[x].sz = a[a[x].l].sz + a[a[x].r].sz + 1;
}
void spt_val(int x, int v, int &L, int &R) {
if (x == 0) {
L = R = 0;
return ;
}
if (a[x].val <= v) {
L = x;
spt_val(a[x].r, v, a[L].r, R);
}
else {
R = x;
spt_val(a[x].l, v, L, a[R].l);
}
pushup(x);
}
int mrg(int L, int R) {
if (L == 0 || R == 0)
return L + R;
if (a[L].pri > a[R].pri) {
a[L].r = mrg(a[L].r, R);
pushup(L);
return L;
}
else {
a[R].l = mrg(L, a[R].l);
pushup(R);
return R;
}
}
int insrt(int x, int v) { //插入并返回新的根编号
int L, M = newNode(v), R;
spt_val(x, v, L, R);
return mrg(mrg(L, M), R);
}
int del(int x, int v) { //删除并返回新的根编号
int L, M, R;
spt_val(x, v, L, R);
spt_val(L, v - 1, L, M);
q.push(M);
return mrg(mrg(mrg(L, a[M].l), a[M].r), R);
}
int kth(int x, int k) {
if (a[x].sz < k)
return INF;
if (a[a[x].l].sz + 1 == k)
return a[x].val;
if (a[a[x].l].sz + 1 > k)
return kth(a[x].l, k);
return kth(a[x].r, k - a[a[x].l].sz - 1);
}
void srh(int x) {
if (x == 0)
return ;
srh(a[x].l);
cout << a[x].val << ' ';
srh(a[x].r);
}
} tr;
//————————————————————————————
int getv(int x) {
return bit.qry(dfn[x]);
}
void mdf(int x, int y, int v) {
while (tp[x] != tp[y]) {
if (d[tp[x]] < d[tp[y]])
swap(x, y);
bit.mdf(dfn[tp[x]], +v);
bit.mdf(dfn[x] + 1, -v);
int val = getv(tp[x]);
//fa[tp[x]]要改
if (fa[tp[x]]) {
rt[fa[tp[x]]] = tr.del(rt[fa[tp[x]]], val - v);
rt[fa[tp[x]]] = tr.insrt(rt[fa[tp[x]]], val);
}
x = fa[tp[x]];
}
if (d[x] > d[y])
swap(x, y);
bit.mdf(dfn[x], +v);
bit.mdf(dfn[y] + 1, -v);
if (x == tp[x]) {
int val = getv(x);
if (fa[x]) {
rt[fa[x]] = tr.del(rt[fa[x]], val - v);
rt[fa[x]] = tr.insrt(rt[fa[x]], val);
}
}
}
struct Node2 {
int val, cnt;
} p[10];
bool operator<(Node2 x, Node2 y) {
return x.val < y.val;
}
int tmp[4];
int qry(int x, int y) {
int v[4] = {0, getv(x), (son[x] ? getv(son[x]) : 2e9 + 10), (fa[x] ? getv(fa[x]) : 2e9 + 10)};
sort(v + 1, v + 4);
int tot = 0;
for (int i = 1; i <= 3; i++)
p[++tot] = {v[i], 1};
int lst = 0;
for (int i = max(1, y - 3); i <= y; i++)
p[++tot] = {tr.kth(rt[x], i), i - lst}, lst = i;
sort(p + 1, p + tot + 1);
int nw = 0;
for (int i = 1; i <= tot; i++) {
nw += p[i].cnt;
if(nw >= y)
return p[i].val;
}
}
inline int read()
{
int x=0,fa=1;
char ch;
while(ch>'9'||ch<'0')
{
if(ch=='-')fa=-1;
ch=getchar();
}
while('0'<=ch&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*fa;
}
inline void write(int x)
{
if(x<0)putchar('-'),x=-x;
if(x>=10)write(x/10);
putchar(x%10+'0');
}
int a[N];
int main() {
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
n = read(), q = read();
for (int i = 1; i <= n; i++)
a[i] = read();
for (int i = 1, u, v; i < n; i++) {
u = read(), v = read();
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1, 0);
srh(1, 1);
for (int i = 1; i <= n; i++) {
bit.mdf(dfn[i], a[i]);
bit.mdf(dfn[i] + 1, -a[i]);
for (auto j: e[i])
if (j != fa[i] && j != son[i]) {
rt[i] = tr.insrt(rt[i], a[j]);
}
}
while (q--) {
int op = read();
if (op == 1) {
int x = read(), y = read(), z = read();
mdf(x, y, z);
}
else {
int x = read(), k = read();
write(qry(x, k));
putchar('\n');
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
1000 1000 1221 8 716 661 1605 113 161 1921 1921 1039 676 615 1921 1165 477 1054 743 1851 87 797 1321 573 1517 1052 1232 1192 1881 1869 1141 5 1659 727 219 651 129 1178 1743 1729 1782 545 1731 178 1942 1769 1769 621 1201 826 711 1161 1026 735 943 1311 1055 1426 1559 1471 1841 1296 509 1353 481 664 18...
output:
result:
Subtask #2:
score: 0
Dangerous Syscalls
Test #2:
score: 0
Dangerous Syscalls
input:
1000000 1000000 301 421 1239 745 826 589 1490 655 119 177 1777 721 1473 834 1592 1519 441 1235 623 1633 1910 1649 565 466 934 221 109 1834 481 1341 1069 1141 1201 176 1868 1841 561 1679 1841 1610 1627 1173 1941 541 163 253 641 1575 1251 1747 715 377 911 711 816 1002 1984 301 1516 1205 1907 1445 1898...
output:
result:
Subtask #3:
score: 0
Dangerous Syscalls
Test #3:
score: 0
Dangerous Syscalls
input:
100000 100000 195 519 1801 1577 1605 921 1321 1775 1881 1123 1614 1181 572 1925 1090 1971 641 1520 1447 1114 1136 633 361 926 1055 1358 1841 929 997 1837 363 897 467 1941 1672 1696 1321 1305 433 689 983 1337 81 477 528 1185 1931 1196 1841 524 90 1615 1105 525 1837 981 901 915 647 1347 145 73 1993 16...
output:
result:
Subtask #4:
score: 0
Dangerous Syscalls
Test #6:
score: 0
Dangerous Syscalls
input:
400000 400000 1950 1675 954 1029 337 85 637 1199 982 1301 1601 1266 1627 1455 1055 792 1501 623 1203 499 315 181 571 639 1101 945 1665 653 853 161 826 771 301 353 1881 1121 555 1751 465 1875 381 1993 373 66 401 886 461 417 113 161 572 245 1449 1984 821 1321 1658 932 779 473 1515 1656 1634 1501 400 4...
output:
result:
Subtask #5:
score: 0
Dangerous Syscalls
Test #10:
score: 0
Dangerous Syscalls
input:
1000000 1000000 642 1185 1426 673 276 1001 1041 967 1078 1853 1236 1677 1681 289 591 389 525 1756 1257 756 549 1070 1331 1177 1335 401 927 1301 43 1061 693 1429 1901 949 871 1251 1608 371 1899 1781 837 1181 381 971 421 1857 1735 377 961 11 285 661 835 478 751 126 1502 993 426 1809 221 630 1581 791 1...