QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#879717 | #7471. ODT | FLY_lai | 100 ✓ | 2470ms | 208640kb | C++14 | 4.9kb | 2025-02-02 11:38:03 | 2025-02-02 11:38:04 |
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);
int val = getv(tp[x]);
bit.mdf(dfn[tp[x]], +v);
bit.mdf(dfn[x] + 1, -v);
//fa[tp[x]]要改
if (fa[tp[x]]) {
rt[fa[tp[x]]] = tr.del(rt[fa[tp[x]]], val);
rt[fa[tp[x]]] = tr.insrt(rt[fa[tp[x]]], val + v);
}
x = fa[tp[x]];
}
if (d[x] > d[y])
swap(x, y);
int val = getv(x);
bit.mdf(dfn[x], +v);
bit.mdf(dfn[y] + 1, -v);
if (x == tp[x] && fa[x]) {
rt[fa[x]] = tr.del(rt[fa[x]], val);
rt[fa[x]] = tr.insrt(rt[fa[x]], val + v);
}
}
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: 20
Accepted
Test #1:
score: 20
Accepted
time: 8ms
memory: 79616kb
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:
1087 1789 708 779 1301 17 1399 111 628 1245 63 1246 1841 2302 1266 1329 88 399 1041 1276 959 9887 241 278 675 1862 251 1721 427 10480 1385 797 3958 3471 1937 481 1080 16248 817 1895 755 1065 1236 493 3534 1400 533 1619 1161 561 2041 289 823 5645 1709 823 404 3792 233 1 2410 251 1371 3841 1449 49 915...
result:
ok 472 numbers
Subtask #2:
score: 10
Accepted
Test #2:
score: 10
Accepted
time: 415ms
memory: 208640kb
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:
1871 375 1770 2286 4098 1062 3143 1137 834 995 5633 362 5836 298 7501 4586 1754 5970 2687 13269 9842 11461 6170 14192 12539 15684 593 418 16611 13241 4243 4961 14837 12826 16861 17471 17922 18988 18681 23561 10825 20318 21794 20065 28739 28908 15346 24298 8784 31565 27055 24886 22647 9570 23800 1296...
result:
ok 497095 numbers
Subtask #3:
score: 20
Accepted
Test #3:
score: 20
Accepted
time: 82ms
memory: 86136kb
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:
47 1793 441 1611 1533 1878 241 1667 93 697 309 1565 505 844 861 1093 251 185 449 521 1453 879 1792 871 501 1671 1725 126 1949 801 883 1611 1522 6160 1051 1186 1639 535 1985 501 79 1033 405 1279 1819 12232 1362 1826 250 1752 161 869 12147 1246 1824 1131 1490 161 1681 707 939 297 1694 563 944 1661 100...
result:
ok 70062 numbers
Test #4:
score: 20
Accepted
time: 82ms
memory: 82048kb
input:
100000 100000 1874 1653 1258 763 361 1954 1325 609 119 1515 1519 576 285 229 1016 1 321 248 689 1881 129 1763 493 1579 994 331 9 1226 1320 1221 779 278 1107 1795 1363 881 251 841 193 1299 1089 1390 133 151 1528 1329 1147 151 441 31 1001 1337 707 1225 81 481 1866 441 892 931 1729 37 1466 1085 1825 73...
output:
203 1947 893 763 257 815 1620 1849 1433 125 453 1835 2734 687 1959 465 4021 1166 433 801 1619 1251 757 328 552 209 1271 917 165 746 1746 521 1957 457 1589 139 9747 165 1036 403 923 1157 4420 601 676 1386 1845 916 1541 401 1768 1539 1345 870 126 401 1288 601 1049 13469 49 15049 1733 1905 1505 1345 99...
result:
ok 70051 numbers
Test #5:
score: 20
Accepted
time: 89ms
memory: 91768kb
input:
100000 100000 1227 1506 479 1568 1241 310 1821 858 1278 181 527 1461 1237 148 551 1539 1807 561 553 17 1251 1957 1601 1949 501 1189 322 1405 281 369 549 1381 1589 986 379 353 1165 325 636 18 665 13 1031 1289 101 1521 1269 1295 943 1861 1848 1409 842 356 29 1316 281 977 1461 331 260 886 1621 918 544 ...
output:
642 337 1617 1028 1320 83 1037 1025 929 1001 796 1379 493 3291 1311 131 1381 1551 24 482 1301 976 929 607 1201 1401 6296 579 36 232 913 761 1229 825 1457 1694 1131 1646 1220 961 6113 856 995 1626 1261 129 1825 1314 981 894 1245 753 1417 536 621 1815 10507 16438 1421 562 1228 1103 459 1275 1129 1401 ...
result:
ok 59863 numbers
Subtask #4:
score: 30
Accepted
Test #6:
score: 30
Accepted
time: 237ms
memory: 127604kb
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:
1524 1930 361 2804 3698 2403 545 1101 2784 609 6687 793 1367 2234 493 8557 2720 6006 9681 804 1286 1786 51 889 229 521 1797 1669 3427 397 3459 1911 9882 243 22597 1267 22474 20149 18288 23985 25213 1320 3829 1521 17 1942 25319 1377 1502 683 21974 333 10380 1605 1914 16443 1501 22645 726 10068 24412 ...
result:
ok 198805 numbers
Test #7:
score: 30
Accepted
time: 358ms
memory: 120644kb
input:
400000 400000 939 449 561 501 34 1283 1705 308 911 977 1030 798 173 1960 1756 614 119 1915 1784 1917 415 1797 301 1589 315 919 983 1849 1069 1916 1864 905 561 1741 895 795 1509 613 1294 1222 1090 1433 1334 1520 631 171 436 1635 1125 1653 1603 1073 1825 982 837 54 1941 213 401 380 407 1690 518 1963 1...
output:
1396 1313 915 1132 520 993 1857 1969 401 1711 107 181 1391 650 734 1444 1999 1006 1038 586 1394 1017 651 581 336 879 151 1817 1966 1052 1991 105 1635 1059 806 813 629 1201 1726 1531 651 1001 1676 1941 206 1115 1025 241 1315 175 1252 853 1399 2327 1398 657 1051 719 1607 1177 363 924 286 1399 1517 189...
result:
ok 359996 numbers
Test #8:
score: 30
Accepted
time: 419ms
memory: 113300kb
input:
400000 400000 1894 969 1486 1816 1165 1571 1429 17 129 1510 1022 321 813 1 1997 737 1001 941 1865 275 1121 885 969 1302 1 429 221 1496 1101 1686 252 297 1047 1246 1774 746 291 219 1825 1896 1101 49 539 896 433 689 88 1063 131 1968 1693 1 1681 1684 518 441 1882 1923 1569 1896 1071 1951 681 569 1586 8...
output:
1299 729 1009 1763 952 293 1315 122 935 1841 1096 1137 901 1703 776 7895 625 876 571 837 1593 1296 1503 1361 701 1295 691 1326 360 57 1881 1592 833 201 1536 316 1201 1137 1471 436 1579 274 301 601 1201 92 8786 1801 438 1210 26 1627 419 834 1247 1969 991 1551 1553 1265 1122 1965 5 1181 73 295 1071 34...
result:
ok 239883 numbers
Test #9:
score: 30
Accepted
time: 545ms
memory: 111932kb
input:
400000 400000 741 1225 181 1070 1691 965 1147 115 1783 621 1231 1257 365 91 649 1863 910 1717 1579 1601 251 314 1054 1381 1361 49 1841 809 1369 1735 369 1324 491 404 828 614 751 721 1867 901 1769 161 489 1105 1714 1803 1241 1741 952 1107 1601 1667 1301 1633 1471 1189 1873 1257 38 1483 170 855 1857 1...
output:
231 480 865 943 535 711 373 1824 293 1609 1181 1061 1885 481 1137 985 1509 129 1133 737 1417 1322 1617 373 451 151 91 1537 776 207 914 613 1715 1616 1909 1060 789 364 1450 473 11426 337 1861 1098 1194 965 981 1731 1361 97 1665 1013 1448 948 225 177 309 1102 881 58 1263 585 1358 1993 939 180 806 1920...
result:
ok 239940 numbers
Subtask #5:
score: 20
Accepted
Test #10:
score: 20
Accepted
time: 1973ms
memory: 150212kb
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...
output:
1165 1825 1777 1123 61 915 619 1059 448 1587 1025 773 1302 999 753 1793 1473 295 1516 81 595 887 1108 445 673 1005 1766 1509 1601 1596 356 1316 1123 412 879 945 1119 625 831 1457 1290 1025 1445 1345 1038 1776 1996 1921 705 1965 1811 1057 1145 751 801 1735 513 1259 1232 1288 178 601 537 985 1580 509 ...
result:
ok 800509 numbers
Test #11:
score: 20
Accepted
time: 1544ms
memory: 144588kb
input:
1000000 1000000 1449 1821 302 1617 1571 1426 1009 1495 1389 1673 706 1297 697 831 202 1501 522 1631 207 228 1661 1466 833 1915 1 1388 81 366 771 1857 1321 77 301 296 1461 274 1988 989 1306 253 338 640 205 1685 1801 1949 1843 209 1841 1353 1092 871 385 833 1889 1721 1141 697 731 1440 540 858 655 705 ...
output:
1689 69 1937 1 979 1581 1323 1781 99 1505 381 195 1485 937 1361 781 1719 591 1877 921 241 281 233 1555 1545 1419 521 126 651 1711 1891 940 1326 1544 105 41 1875 1776 1776 342 207 72 417 691 851 1655 118 1146 12 377 59 33 82 804 470 13859 1452 1133 573 269 1903 1142 1238 1661 1140 811 1861 1491 985 8...
result:
ok 699952 numbers
Test #12:
score: 20
Accepted
time: 1109ms
memory: 159168kb
input:
1000000 1000000 226 918 1263 29 698 477 1163 48 881 705 1593 1851 1411 1309 893 1065 1061 1237 765 1071 1441 1241 799 1685 1496 1891 1893 569 1041 1689 321 689 519 1262 1883 236 288 871 1341 1765 877 1362 1281 1777 1825 1226 736 1283 1959 1911 1029 615 1735 1341 1265 1520 274 263 373 1726 1819 633 1...
output:
753 1803 563 1858 45 297 1209 842 1001 77 1746 1313 1633 1851 865 401 559 3647 1603 1496 205 1881 961 385 1749 622 1101 174 1806 1150 485 507 125 1936 1823 1030 1081 573 961 1181 657 1445 808 941 1427 191 333 43 1161 1086 638 999 1426 10760 1701 1749 1613 1729 1638 1622 224 1351 991 10025 821 1096 1...
result:
ok 700254 numbers
Test #13:
score: 20
Accepted
time: 2470ms
memory: 146856kb
input:
1000000 1000000 1376 961 1545 311 1017 1137 1125 771 693 1607 1236 1273 1624 641 1137 130 979 1721 501 1971 1577 1725 1187 688 1921 1608 533 260 1758 1052 1126 806 741 363 1344 1401 1871 1326 1901 153 1294 1587 1549 527 1521 1301 545 1323 101 789 151 766 1555 1995 1739 1701 351 1661 1966 1773 1541 4...
output:
313 1696 225 49 1636 1210 1028 1153 340 1025 1649 223 19 1847 1926 401 523 355 1207 1029 327 314 1889 529 1481 353 1993 361 54 261 97 1016 1639 1865 1075 615 277 539 595 1426 1847 1613 781 43 593 681 1929 1651 1735 1751 1317 437 186 559 1905 306 729 446 1905 699 61 754 1300 616 887 1605 701 1794 87 ...
result:
ok 600200 numbers