1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
| #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <map> #include <iostream> #include <queue> #include <set> #include <stack> #include <vector> #include <array> #include <unordered_map> #include <numeric> #include <functional> #include <iomanip> #include <chrono> #include <random> #include <ctime> using namespace std; #define ll long long #define int ll #define i128 __int128 #define ld long double #define pb push_back #define fs first #define sd second #define fast ios::sync_with_stdio(false); cin.tie(0); #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define debug(a) cout<<a<<" "; #define endl '\n'
void back_pack01(){ const ll INF=1e9; ll w[110],v[110],sum=0,n,f[100010],ans=0,W; cin>>n>>W; for(int i=1;i<=n;i++){ cin>>w[i]>>v[i]; sum+=v[i]; } fill(f,f+100005,INF); f[0]=0; for(int i=1;i<=n;i++) for(int j=sum;j>=v[i];j--) if(f[j-v[i]]!=INF) f[j]=min(f[j],f[j-v[i]]+w[i]); for(int j=sum;j>=0;j--) if(f[j]<=W){ ans=j; break; } cout<<ans<<endl; }
void val_base01(){ const ll INF=1e9; ll w[110],v[110],sum=0,n,f[100010],ans=0,W; cin>>n>>W; for(int i=1;i<=n;i++){ cin>>w[i]>>v[i]; sum+=v[i]; } fill(f,f+100005,INF); f[0]=0; for(int i=1;i<=n;i++) for(int j=sum;j>=v[i];j--) if(f[j-v[i]]!=INF) f[j]=min(f[j],f[j-v[i]]+w[i]); for(int j=sum;j>=0;j--) if(f[j]<=W){ ans=j; break; } cout<<ans<<endl; }
void bfs01(){ const int INF = 0x3f3f3f3f; const int MAXN = 1e5+1; vector<pair<int, int>> graph[MAXN]; int n, m, source; deque<int> dq; vector<int> dist(n, INF); dist[source] = 0; dq.push_front(source); while (!dq.empty()) { int u = dq.front(); dq.pop_front(); for (auto [v, w] : graph[u]) { if (w == 0 && dist[v] > dist[u]) { dist[v] = dist[u]; dq.push_front(v); } else if (w == 1 && dist[v] > dist[u] + 1) { dist[v] = dist[u] + 1; dq.push_back(v); } } } }
void way_of_back_pack(){ const ll mod=998244353; ll f[200010],c[200010]; ll n,m; cin>>n>>m; for(int i=0;i<=m;i++){ f[i]=0,c[i]=1; }
for(int i=1;i<=n;i++){ ll w,v; cin>>v>>w; for(int j=m;j>=v;j--){ if(f[j]<f[j-v]+w){ f[j]=f[j-v]+w; c[j]=c[j-v]; } else if(f[j]==f[j-v]+w){ c[j]=(c[j]+c[j-v])%mod; } } } cout<<c[m]<<endl; }
void ways_specific(){ ll f[2010][2010],v[2010],w[2010]; ll n,m,ans=0; cin>>n>>m; for(int i=1;i<=n;i++) cin>>v[i]>>w[i]; for(int i=n;i>=1;i--){ for(int j=0;j<=m;j++){ f[i][j]=f[i+1][j]; if(v[i]<=j) f[i][j]=max(f[i][j],f[i+1][j-v[i]]+w[i]); } } cout<<f[1][m]<<endl; for(int i=1,j=m;i<=n;i++){ if(j>=v[i]&&f[i][j]==f[i][j-v[i]]+w[i]){ cout<<i<<endl; j-=v[i]; } } }
struct dsu{ vector<ll> fa,sz; dsu(ll n):fa(n+1),sz(n+1,1){ for(int i=1;i<=n;++i) fa[i]=i; } ll find(ll x){return fa[x]==x?x:fa[x]=find(fa[x]);} void uni(ll x,ll y){ x=find(x); y=find(y); if(sz[x]<sz[y]) swap(x,y); fa[y]=x; sz[x]+=sz[y]; } };
void decrease_queue(){ int h=0,t=-1,q[1000010],n,a[1000010]; ll n,m; cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++){ if(h<=t && q[h]<i-m+1) h++; while(h<=t && a[i]<=a[q[t]]) t--; q[++t]=i; if(i>m-1) printf("%d ",a[q[h]]); } cout<<endl; }
void decrease_stack(){ stack<ll> s; ll N,ans[3000010],a[3000010]; cin>>N; for(int i=1;i<=N;i++){ cin>>a[i]; while(!s.empty()&&a[i]>a[s.top()]){ ans[s.top()]=i; s.pop(); } s.push(i); } for(int i=1;i<=N;i++) cout<<ans[i]<<" "; }
void DKE_tree(){ const ll N=2e5+10; ll ls[N],rs[N],top,n,a[N],stk[N]; for(int i=1;i<=n;i++) ls[i]=rs[i]=0; top=0; for(int i=1;i<=n;i++){ ll now=top; while(now>0&&a[stk[now]]>a[i]) now--; if(now) rs[stk[now]]=i; if(now<top) ls[i]=stk[now+1]; stk[++now]=i; top=now; } }
void muiltibackpack(){ ll f[200010],n,V,q[200010],g[200010]; cin>>n>>V; for(int i=1;i<=n;i++){ memcpy(g,f,sizeof(f)); ll v,w,s; cin>>w>>v>>s; for(int j=0;j<v;j++){ ll h=0,t=-1; for(int k=j;k<=V;k+=v){ if(h<=t && q[h]<k-s*v) h++; if(h<=t) f[k]=max(g[k],g[q[h]]+(k-q[h])/v*w); while(h<=t && g[k]>=g[q[t]]+(k-q[t])/v*w) t--; q[++t]=k; } } } cout<<f[V]<<endl; }
void two_dem_back_pack(){ ll f[2010][2010],V,M,n; cin>>n>>V>>M; for(int i=1;i<=n;i++){ ll v,m,w; cin>>w>>v>>m; for(int j=V;j>=v;j--) for(int k=M;k>=m;k--) f[j][k]=max(f[j-v][k-m]+w,f[j][k]); } cout<<f[V][M]<<endl; }
void fenzu_backpack(){ ll f[200010],n,V,v[200010],w[200010]; cin>>n>>V; for(int i=1;i<=n;i++){ ll s;cin>>s; for(int j=1;j<=s;j++) cin>>v[j]>>w[j]; for(int j=V;j>=1;j--){ for(int k=1;k<=s;k++){ if(j>=v[k]) f[j]=max(f[j],f[j-v[k]]+w[k]); } } } cout<<f[V]<<endl; }
struct mix_back_pack{ ll f[200010],q[200010],g[200010]; struct tree{ ll vol,worth,shu; }t[200010]; void dc(ll n,ll V){ for(int i=1;i<=n;i++){ memcpy(g,f,sizeof(f)); ll v,w,s; v=t[i].vol; w=t[i].worth; s=t[i].shu; for(int j=0;j<v;j++){ ll h=0,t=-1; for(int k=j;k<=V;k+=v){ if(h<=t && q[h]<k-s*v) h++; if(h<=t) f[k]=max(g[k],g[q[h]]+(k-q[h])/v*w); while(h<=t && g[k]>=g[q[t]]+(k-q[t])/v*w) t--; q[++t]=k; } } } } void solve(){ ll n; cin>>n; ll V; cin>>V; ll id=1; for(int i=1;i<=n;i++){ ll vol,worth,shu; cin>>vol>>worth>>shu; if(shu!=0){ t[id].vol=vol; t[id].worth=worth; t[id].shu=shu; id++; } else{ for(int j=vol;j<=V;j++){ f[j]=max(f[j],f[j-vol]+worth); } } } dc(id-1,V); cout<<f[V]<<endl; } };
const double EPS = 1e-8;
int sgn(double x) { if (fabs(x) < EPS) return 0; return x > 0 ? 1 : -1; }
struct Point { double x, y; Point(double x_ = 0, double y_ = 0) : x(x_), y(y_) {} Point operator+ (const Point& other) const { return Point(x + other.x, y + other.y); } Point operator- (const Point& other) const { return Point(x - other.x, y - other.y); } Point operator* (double k) const { return Point(x * k, y * k); } Point operator/ (double k) const { return Point(x / k, y / k); } bool operator== (const Point& other) const { return sgn(x - other.x) == 0 && sgn(y - other.y) == 0; } };
struct Segment { Point a, b; Segment() {} Segment(Point a_, Point b_) : a(a_), b(b_) {} };
struct Circle { Point o; double r; Circle() {} Circle(Point o_, double r_) : o(o_), r(r_) {} };
struct Vector{ double x,y; Vector(Point a,Point b):x(b.x-a.x),y(b.y-a.y) {} };
double dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; }
double len2(const Point& a) { return dot(a, a); }
double len(const Point& a) { return sqrt(len2(a)); }
double dist(const Point& a, const Point& b) { return len(a - b); }
double proj_len(const Point& a, const Point& b) { return dot(a, b) / len(b); }
bool is_vertical(const Point& a, const Point& b) { return sgn(dot(a, b)) == 0; }
double cross(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; }
double triangle_area(const Point& a, const Point& b, const Point& c) { return fabs(cross(b - a, c - a)) / 2.0; }
double parallelogram_area(const Point& a, const Point& b, const Point& c, const Point& d) { return fabs(cross(b - a, d - a)); }
int point_line_side(const Point& a, const Point& b, const Point& c) { return sgn(cross(b - a, c - a)); }
bool is_clockwise(const Point& a, const Point& b) { return sgn(cross(a, b)) < 0; }
bool is_counter_clockwise(const Point& a, const Point& b) { return sgn(cross(a, b)) > 0; }
double point_line_dist(const Point& a, const Point& b, const Point& p) { return fabs(cross(b - a, p - a)) / len(b - a); }
double point_segment_dist(const Point& a, const Point& b, const Point& p) { if (a == b) return dist(a, p); double t = proj_len(p - a, b - a); if (sgn(t) <= 0) return dist(p, a); if (sgn(t - len(b - a)) >= 0) return dist(p, b); return point_line_dist(a, b, p); }
Point foot_point(const Point& a, const Point& b, const Point& p) { Point ab = b - a; double t = dot(p - a, ab) / len2(ab); return a + ab * t; }
bool point_on_segment(const Point& a, const Point& b, const Point& p) { return sgn(cross(b - a, p - a)) == 0 && sgn(dot(p - a, p - b)) <= 0; }
bool rect_intersect(const Point& a1, const Point& a2, const Point& b1, const Point& b2) { double min_x1 = min(a1.x, a2.x), max_x1 = max(a1.x, a2.x); double min_y1 = min(a1.y, a2.y), max_y1 = max(a1.y, a2.y); double min_x2 = min(b1.x, b2.x), max_x2 = max(b1.x, b2.x); double min_y2 = min(b1.y, b2.y), max_y2 = max(b1.y, b2.y); return max_x1 >= min_x2 - EPS && max_x2 >= min_x1 - EPS && max_y1 >= min_y2 - EPS && max_y2 >= min_y1 - EPS; }
bool cross_stand(const Point& a, const Point& b, const Point& c, const Point& d) { return sgn(cross(d - c, a - c)) * sgn(cross(d - c, b - c)) <= 0; }
bool segment_intersect(const Segment& s1, const Segment& s2) { Point a = s1.a, b = s1.b, c = s2.a, d = s2.b; if (!rect_intersect(a, b, c, d)) return false; return cross_stand(a, b, c, d) && cross_stand(c, d, a, b); }
Point line_intersect(const Point& a, const Point& b, const Point& c, const Point& d) { double s1 = cross(d - c, a - c); double s2 = cross(d - c, b - c); return (a * s2 - b * s1) / (s2 - s1); }
Point segment_intersect_point(const Segment& s1, const Segment& s2) { return line_intersect(s1.a, s1.b, s2.a, s2.b); }
int point_circle_relation(const Point& p, const Circle& c) { double d2 = len2(p - c.o); double r2 = c.r * c.r; return sgn(d2 - r2); }
double tangent_len(const Point& p, const Circle& c) { double d = dist(p, c.o); if (sgn(d - c.r) <= 0) return 0.0; return sqrt(d * d - c.r * c.r); }
vector<Point> line_circle_intersect(const Point& a, const Point& b, const Circle& c) { vector<Point> res; Point foot = foot_point(a, b, c.o); double d = point_line_dist(a, b, c.o); if (sgn(d - c.r) > 0) return res; if (sgn(d - c.r) == 0) { res.push_back(foot); return res; } double Len = sqrt(c.r * c.r - d * d); Point dir = (b - a) / len(b - a); res.push_back(foot + dir * Len); res.push_back(foot - dir * Len); return res; }
vector<Point> segment_circle_intersect(const Point& a, const Point& b, const Circle& c) { vector<Point> res, line_inter = line_circle_intersect(a, b, c); for (auto& p : line_inter) { if (point_on_segment(a, b, p)) { res.push_back(p); } } return res; }
double polygon_area(const vector<Point>& poly) { int n = poly.size(); double area = 0.0; for (int i = 0; i < n; i++) { int j = (i + 1) % n; area += cross(poly[i], poly[j]); } return fabs(area) / 2.0; }
bool point_in_polygon(const Point& p, const vector<Point>& poly) { int n = poly.size(); int cnt = 0; for (int i = 0; i < n; i++) { Point a = poly[i], b = poly[(i + 1) % n]; if (point_on_segment(a, b, p)) return true; if (sgn(a.y - p.y) > sgn(b.y - p.y)) swap(a, b); if (sgn(b.y - p.y) <= 0) continue; if (sgn(cross(b - a, p - a)) > 0) cnt++; } return cnt % 2 == 1; }
Point rotate(const Point& a, double rad) { double c = cos(rad), s = sin(rad); return Point(a.x * c - a.y * s, a.x * s + a.y * c); }
Point rotate_point(const Point& a, const Point& p, double rad) { return rotate(a - p, rad) + p; }
Point normalize(const Point& a) { double l = len(a); if (sgn(l) == 0) return Point(0, 0); return a / l; }
void cal_martix(){ const double eps=1e-8; double a[110][110]; ll n; cin>>n; for(int i=1;i<=n;i++){ for(int j=1;j<=n+1;j++){ cin>>a[i][j]; } } for(int i=1;i<=n;i++){ ll r=i; for(int k=i;k<=n;k++){ if(fabs(a[k][i])>eps){ r=k; break; } } if(r!=i) swap(a[i],a[r]); if(fabs(a[i][i])<eps){ cout<<"No Solution"<<endl; return; } for(int k=1;k<=n;k++){ if(k==i) continue; double t=a[k][i]/a[i][i]; for(int j=i;j<=n+1;j++){ a[k][j]-=t*a[i][j]; } } } for(int i=1;i<=n;i++){ cout<<fixed<<setprecision(2)<<a[i][n+1]/a[i][i]<<endl; } }
#include <ext/rope> using namespace __gnu_cxx; void myrope(){ rope<int> a; a.insert(1,10); a.push_back(10); a.erase(0); cout<<a[1]<<a.size()<<endl; }
struct zhuxi_tree{ #define N 1000005 #define mid ((l+r)>>1)
ll ls[N*25], rs[N*25], val[N*25], lazy[N*25]; ll root[N], tot = 0, n, m, a[N];
ll newNode(ll v = 0, ll lz = 0) { ls[tot] = rs[tot] = 0; val[tot] = v; lazy[tot] = lz; return tot++; }
ll copyNode(ll u) { ll new_node = newNode(val[u], lazy[u]); ls[new_node] = ls[u]; rs[new_node] = rs[u]; return new_node; }
void pushDown(ll u, ll l, ll r) { if (lazy[u] != 0) { if (ls[u] == 0) ls[u] = newNode(0, 0); ls[u] = copyNode(ls[u]); val[ls[u]] += lazy[u] * (mid - l + 1); lazy[ls[u]] += lazy[u]; if (rs[u] == 0) rs[u] = newNode(0, 0); rs[u] = copyNode(rs[u]); val[rs[u]] += lazy[u] * (r - mid); lazy[rs[u]] += lazy[u]; lazy[u] = 0; } }
void build(ll &u, ll l, ll r) { u = newNode(); if (l == r) { val[u] = a[l]; return; } build(ls[u], l, mid); build(rs[u], mid + 1, r); val[u] = val[ls[u]] + val[rs[u]]; }
void rangeAdd(ll &u, ll v, ll l, ll r, ll ql, ll qr, ll x) { u = copyNode(v); if (ql <= l && r <= qr) { val[u] += x * (r - l + 1); lazy[u] += x; return; } pushDown(u, l, r); if (ql <= mid) rangeAdd(ls[u], ls[v], l, mid, ql, qr, x); if (qr > mid) rangeAdd(rs[u], rs[v], mid + 1, r, ql, qr, x); val[u] = val[ls[u]] + val[rs[u]]; }
ll query(ll u, ll l, ll r, ll p) { if (l == r) return val[u]; pushDown(u, l, r); if (p <= mid) return query(ls[u], l, mid, p); else return query(rs[u], mid + 1, r, p); }
void solve() { n = 5; for (int i = 1; i <= n; i++) a[i] = i; build(root[0], 1, n); rangeAdd(root[1], root[0], 1, n, 2, 4, 10); rangeAdd(root[2], root[1], 1, n, 1, 3, 5); cout << "Version 0, pos 3: " << query(root[0], 1, n, 3) << endl; cout << "Version 1, pos 3: " << query(root[1], 1, n, 3) << endl; cout << "Version 2, pos 3: " << query(root[2], 1, n, 3) << endl; return 0; } };
struct Tire{ const ll INF=1e18,N=1e5+10; char s[N]; int ch[N][26],cnt[N],idx=0; void insert(char *s){ int p=0; for(int i=0;s[i];i++){ int j=s[i]-'a'; if(!ch[p][j]) ch[p][j]=++idx; p=ch[p][j]; } cnt[p]++; } int query(char *s){ int p=0; for(int i=0;s[i];i++){ int j=s[i]-'a'; if(!ch[p][j]) return 0; p=ch[p][j]; } return cnt[p]; } };
struct C_{ const ll N=2e6+10; ll mod=998244353; ll fact[N],inv[N]; ll qpow(ll a,ll b){ ll sum=1; while(b>0){ if(b&1) sum=sum*a%mod; a=a*a%mod; b>>=1; } return sum; } void init(){ ll n=N-5; fact[0]=1; for(int i=1;i<=n;i++) fact[i]=fact[i-1]*i%mod; inv[n]=qpow(fact[n],mod-2); for(int i=n;i>=1;i--) inv[i-1]=inv[i]*i%mod; } ll C(ll n,ll i){ return fact[n]*inv[i]%mod*inv[n-i]%mod; } };
struct max_flow{ const ll MAXN=250; const ll inf=0x3f3f3f3f; struct Edge{ ll from,to,cap,flow; Edge(ll u,ll v,ll c,ll f):from(u),to(v),cap(c),flow(f){}; }; struct EK{ ll n,m; vector<Edge> edges; vector<ll> g[MAXN]; ll mf[MAXN],p[MAXN]; void AddEdge(ll from,ll to,ll cap){ edges.pb(Edge(from,to,cap,0)); edges.pb(Edge(to,from,0,0)); m=edges.size(); g[from].pb(m-2); g[to].pb(m-1); } ll Maxflow(ll s,ll t){ ll flow=0; for(;;){ memset(mf,0,sizeof(mf)); queue<ll> q; q.push(s); mf[s]=inf; while(!q.empty()){ ll x=q.front(); q.pop(); for(int i=0;i<g[x].size();i++){ Edge&e=edges[g[x][i]]; if(!mf[e.to]&&e.cap>e.flow){ p[e.to]=g[x][i]; mf[e.to]=min(mf[x],e.cap-e.flow); q.push(e.to); } } if(mf[t]) break; } if(!mf[t]) break; for(int u=t;u!=s;u=edges[p[u]].from){ edges[p[u]].flow+=mf[t]; edges[p[u]^1].flow-=mf[t]; } flow+=mf[t]; } return flow; } }; };
void max_erea(){ ll a[200010]; ll n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; ll max_ending_here=a[1]; ll max_so_far=a[1]; for(int i=2;i<=n;i++){ max_ending_here=max(a[i],max_ending_here+a[i]); max_so_far=max(max_so_far,max_ending_here); } cout<<max_so_far<<endl; ll sum=0; for(int i=1;i<=n;i++){ sum=sum+a[i]; max_so_far=max(max_so_far,sum); if(sum<0) sum=0; } }
ll B; struct Q{ll l,r,id;}; bool cmp(Q x,Q y){ if(x.l/B!=y.l/B) return x.l/B<y.l/B; if((x.l/B)%2) return x.r>y.r; return x.r<y.r; }
void qj_dp(){ ll n,a[410],ans[410][410],sum[410]; cin>>n; memset(ans,0x3f,sizeof(ans)); for(int i=1;i<=n;i++){ cin>>a[i]; sum[i]=sum[i-1]+a[i]; ans[i][i]=0; } for(int l=2;l<=n;l++){ for(int i=1;i+l-1<=n;i++){ ll j=i+l-1; for(int k=i;k<j;k++){ ans[i][j]=min(ans[i][k]+ans[k+1][j]+sum[j]-sum[i-1],ans[i][j]); } } } cout<<ans[1][n]<<endl; }
struct p_tree{ const ll N=200010; ll dfn[N],leave[N],sz[N],hson[N],top[N]; ll a[N],dep[N],fa[N]; vector<ll> g[N]; ll n,id=1,mod; struct Line_tree{ vector<ll> a,tr,tag; Line_tree(ll n):a(n+1,0),tr(n*4+10,0),tag(n*4+10,0){} void build(ll nl,ll nr,ll now){ if(nl==nr){tr[now]=a[nl];return;} ll m=nl+((nr-nl)>>1); build(nl,m,now*2); build(m+1,nr,now*2+1); tr[now]=(tr[now*2]+tr[now*2+1])%mod; } void push_down(ll nl,ll nr,ll now){ ll m=nl+((nr-nl)>>1); ll lsz=m-nl+1,rsz=nr-m; tr[now*2]=(tr[now*2]+tag[now]*lsz%mod)%mod; tr[now*2+1]=(tr[now*2+1]+tag[now]*rsz%mod)%mod; tag[now*2]=(tag[now*2]+tag[now])%mod; tag[now*2+1]=(tag[now*2+1]+tag[now])%mod; tag[now]=0; } void update_add(ll ul,ll ur,ll nl,ll nr,ll now,ll add){ if(ul<=nl&&nr<=ur){ tr[now]=(tr[now]+(nr-nl+1)*add%mod)%mod; tag[now]=(tag[now]+add)%mod; return; } ll m=nl+((nr-nl)>>1); if(tag[now]&&nl!=nr) push_down(nl,nr,now); if(m<ur) update_add(ul,ur,m+1,nr,now*2+1,add); if(m>=ul) update_add(ul,ur,nl,m,now*2,add); tr[now]=(tr[now*2]+tr[now*2+1])%mod; } ll query_sum(ll ql,ll qr,ll nl,ll nr,ll now){ ll now_ans=0; if(ql<=nl&&nr<=qr) return tr[now]; ll m=nl+((nr-nl)>>1); if(tag[now]) push_down(nl,nr,now); if(m>=ql) now_ans=(now_ans+query_sum(ql,qr,nl,m,now*2))%mod; if(m<qr) now_ans=(now_ans+query_sum(ql,qr,m+1,nr,now*2+1))%mod; return now_ans; } }; Line_tree xd(N); void dfs1(ll last,ll now,ll d){ sz[now]=1,dep[now]=d; ll p=0; for(int next:g[now]){ if(next==last) continue; dfs1(now,next,d+1); fa[next]=now; sz[now]+=sz[next]; if(sz[p]<sz[next]) p=next; } hson[now]=p; } void dfs2(ll last,ll now,ll t){ top[now]=t; xd.a[id]=a[now]; dfn[now]=id++; if(hson[now]!=0) dfs2(now,hson[now],t); for(int next:g[now]){ if(next==last) continue; if(next==hson[now]) continue; dfs2(now,next,next); } leave[now]=id-1; } void update_path(ll u,ll v,ll add){ ll l,r; while(top[u]!=top[v]){ if(dep[top[u]]<dep[top[v]]) swap(u,v); l=dfn[top[u]],r=dfn[u]; xd.update_add(l,r,1,n,1,add); u=fa[top[u]]; } l=min(dfn[u],dfn[v]),r=max(dfn[u],dfn[v]); xd.update_add(l,r,1,n,1,add); } ll query_path(ll u,ll v){ ll ans=0,l,r; while(top[u]!=top[v]){ if(dep[top[u]]<dep[top[v]]) swap(u,v); l=dfn[top[u]],r=dfn[u]; ans=(ans+xd.query_sum(l,r,1,n,1))%mod; u=fa[top[u]]; } l=min(dfn[u],dfn[v]),r=max(dfn[u],dfn[v]); ans=(ans+xd.query_sum(l,r,1,n,1))%mod; return ans; } };
bool toposort() { const ll MAXN=200010; ll n, m; vector<ll> G[MAXN]; ll in[MAXN]; vector<ll> L; queue<ll> S; for (int i = 1; i <= n; i++) if (in[i] == 0) S.push(i); while (!S.empty()) { int u = S.front(); S.pop(); L.push_back(u); for (auto v : G[u]) { if (--in[v] == 0) { S.push(v); } } } if (L.size() == n) { for (auto i : L) cout << i << ' '; return true; } return false; }
struct Point{ ld x,y; Point operator+(const Point&othor)const{return {x+othor.x,y+othor.y};} Point operator-(const Point&othor)const{return {x-othor.x,y-othor.y};} ld operator^(const Point&othor)const{return x*othor.y-y*othor.x;} ld operator*(const Point&othor)const{return x*othor.x+y*othor.y;} }; Point o; ld dis2(Point p1,Point p2){ ld x=p1.x-p2.x,y=p1.y-p2.y; return x*x+y*y; } bool cmp(Point p1,Point p2){ if(((p1-o)^(p2-o))!=0) return ((p1-o)^(p2-o))>0; return dis2(o,p1)<dis2(o,p2); } vector<Point> Graham(vector<Point> p){ ll n=p.size(),id=0; for(int i=1;i<n;i++) if(p[id].y<p[i].y||(p[id].y==p[i].y&&p[id].x<p[i].x)) id=i; swap(p[id],p[0]); o=p[0]; sort(p.begin()+1,p.end(),cmp); vector<ll> sta; sta.pb(0); for(int i=1;i<n;i++){ while(sta.size()>=2&& ((p[sta.back()]-p[sta[sta.size()-2]])^(p[i]-p[sta.back()]))<=0) sta.pop_back(); sta.pb(i); } vector<Point> ans; for(int i:sta) ans.pb(p[i]); return ans; } void solve_tb(){ ll n; cin>>n; vector<Point> a(n); for(int i=0;i<n;i++) cin>>a[i].x>>a[i].y; a=Graham(a); ld ans=0; n=a.size(); for(int i=1;i<n;i++) ans+=sqrt(dis2(a[i],a[i-1])); ans+=sqrt(dis2(a[0],a.back())); cout<<fixed<<setprecision(2)<<ans<<endl; }
struct zx_tree{ #define N 1000005 #define mid ((l+r)>>1) ll ls[N*25],rs[N*25],val[N*25]; ll root[N],tot=0,n,m,a[N]; void build(ll &u,ll l,ll r){ u=tot++; if(l==r){val[u]=a[l];return;} build(ls[u],l,mid); build(rs[u],mid+1,r); } void change(ll &u,ll v,ll l,ll r,ll p,ll x){ u=++tot; ls[u]=ls[v]; rs[u]=rs[v]; val[u]=val[v]; if(l==r){val[u]=x;return;} if(p<=mid) change(ls[u],ls[v],l,mid,p,x); else change(rs[u],rs[v],mid+1,r,p,x); } ll query(ll u,ll l,ll r,ll p){ if(l==r) return val[u]; if(p<=mid) return query(ls[u],l,mid,p); else return query(rs[u],mid+1,r,p); } };
struct MFMC{ const ll inf=0x3f3f3f3f; const ll N=2010,M=500010; struct qxx{int nex,t,v,c;}e[M]; ll h[N],cnt=1; void add_path(ll f,ll t,ll v,ll c){ e[++cnt]=qxx{h[f],t,v,c},h[f]=cnt; } void add_flow(ll f,ll t,ll v,ll c){ add_path(f,t,v,c); add_path(t,f,0,-c); } ll dis[N],pre[N],mf[N],s,t; bool vis[N]; bool spfa(){ memset(dis,0x3f,sizeof(dis)); queue<ll> q; q.push(s); dis[s]=0,mf[s]=inf,mf[t]=0; while(!q.empty()){ ll u=q.front(); q.pop(); vis[u]=0; for(int i=h[u];i;i=e[i].nex){ const ll &v=e[i].t,&w=e[i].v,&c=e[i].c; if(!w||dis[v]<=dis[u]+c) continue; dis[v]=dis[u]+c,mf[v]=min(w,mf[u]),pre[v]=i; if(!vis[v]) q.push(v),vis[v]=1; } } return mf[t]; } ll maxflow,mincost; void update(){ maxflow+=mf[t]; for(int u=t;u!=s;u=e[pre[u]^1].t){ e[pre[u]].v-=mf[t],e[pre[u]^1].v+=mf[t]; mincost+=mf[t]*e[pre[u]].c; } }
};
void dij(){ long long N; const long long INF=1e18; vector<pair<LL,LL>> g[10010]; priority_queue<pair<LL, LL>, vector<pair<LL, LL>>, greater<pair<LL, LL>>> pq;vector<bool> vis(N+1, false); vector<LL> dis(N+1, INF);
dis[1] = 0; pq.push({0, 1});
while(!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if(vis[u]) continue; vis[u] = true; for(auto [v, w] : g[u]) { if(!vis[v] && dis[v] > dis[u] + w) { dis[v] = dis[u] + w; pq.push({dis[v], v}); } } } }
struct ex_kruskal{ struct edge{ ll u,v,w; bool operator<(const edge&othor){return w<othor.w;} bool operator>(const edge&othor){return w>othor.w;} }; ll val[200010],id,fa[200010],root; ll n,m; vector<ll> g[200010]; ll find(ll x){return fa[x]==x?x:fa[x]=find(fa[x]);} ll up[500010][20],depth[500010]; void dfs(ll last,ll now,ll dep){ up[now][0]=last; depth[now]=dep; for(int next:g[now]){ if(next==last) continue; dfs(now,next,dep+1); } } void init_lca(){ dfs(0,root,1); for(int k=1;k<20;k++){ for(int i=1;i<=id;i++){ if(up[i][k-1]!=0) up[i][k]=up[up[i][k-1]][k-1]; } } } ll LCA_bz(ll u,ll v){ if(depth[u]<depth[v]) swap(u,v); ll diff=depth[u]-depth[v]; for(int i=19;i>=0;i--){ if(diff&(1ll<<i)){ u=up[u][i]; } } for(int i=19;i>=0;i--){ if(up[u][i]!=up[v][i]){ u=up[u][i]; v=up[v][i]; } } if(u==v) return u; else return up[u][0]; } void Ex_Kruskal(){ vector<edge> e(m+1); for(int i=1;i<=m;i++) cin>>e[i].u>>e[i].v>>e[i].w; sort(e.begin()+1,e.end(),greater<>()); for(int i=1;i<=2*n;i++) fa[i]=i; for(int i=1;i<=m;i++){ edge nowe=e[i]; ll fu=find(nowe.u),fv=find(nowe.v); if(fu!=fv){ val[++id]=nowe.w; fa[fu]=fa[fv]=id; g[id].pb(fu); g[id].pb(fv); } } vector<ll> roots; for(int i=1;i<=id;i++){ if(find(i)==i) roots.pb(i); } if(roots.size()>1){ id++; for(int i:roots) g[id].pb(i); } } void solve(){ cin>>n>>m; id=n; Ex_Kruskal(); root=id; init_lca(); ll q; cin>>q; while(q--){ ll x,y; cin>>x>>y; ll fx=find(x),fy=find(y); if(fx!=fy){ cout<<-1<<endl; continue; } ll lca=LCA_bz(x,y); cout<<val[lca]<<endl; } } };
void KMP(){ string t,p; cin>>t>>p; vector<ll> ans,pi(p.length()); for(int i=1;i<p.length();i++){ ll j=pi[i-1]; while(j>0&&p[j]!=p[i]) j=pi[j-1]; if(p[i]==p[j]) j++; pi[i]=j; } for(int i=0,j=0;i<t.length();i++){ while(j>0&&t[i]!=p[j]) j=pi[j-1]; if(p[j]==t[i])j++; if(j==p.length()){ ans.push_back(i-p.length()+1); j=pi[j-1]; } } for(auto &i:ans) cout<<i+1<<endl; for(int i=0;i<p.length();i++) cout<<pi[i]<<" "; cout<<endl; }
struct LCA{ vector<ll> g[500010]; ll n,m,root; ll up[500010][20],depth[500010]; void dfs(ll last,ll now,ll dep){ up[now][0]=last; depth[now]=dep; for(int next:g[now]){ if(next==last) continue; dfs(now,next,dep+1); } } ll LCA_bz(ll u,ll v){ dfs(0,root,1); for(int k=1;k<20;k++){ for(int i=1;i<=n;i++){ if(up[i][k-1]!=0) up[i][k]=up[up[i][k-1]][k-1]; } } if(depth[u]<depth[v]) swap(u,v); ll diff=depth[u]-depth[v]; for(int i=19;i>=0;i--){ if(diff&(1ll<<i)){ u=up[u][i]; } } for(int i=19;i>=0;i--){ if(up[u][i]!=up[v][i]){ u=up[u][i]; v=up[v][i]; } } if(u==v) return u; else return up[u][0]; } void solve(){ cin>>n>>m>>root; for(int i=1;i<=n-1;i++){ ll x,y; cin>>x>>y; g[x].pb(y); g[y].pb(x); } } };
void LCS(){ ll f[3010][3010],p[3010][3010]; string s,t; cin>>s>>t; ll m=s.length(),n=t.length(); for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ if(s[i-1]==t[j-1]){ f[i][j]=f[i-1][j-1]+1; p[i][j]=1; } else if(f[i][j-1]>f[i-1][j]){ f[i][j]=f[i][j-1]; p[i][j]=2; } else{ f[i][j]=f[i-1][j]; p[i][j]=3; } } } ll i,j,k; char ch[3010]; i=m,j=n,k=f[m][n]; while(i>0&&j>0){ if(p[i][j]==1){ ch[k--]=s[i-1]; i--; j--; } else if(p[i][j]==2) j--; else i--; } for(int i=1;i<=f[m][n];i++) cout<<ch[i]; cout<<endl; }
void manacher(){ const ll INF=1e18; ll p[22000010],mid=0,r=0,ans=1; string x; vector<char> s; s.push_back('~');s.push_back('#'); cin>>x; ll len=x.length()*2+1; for(int i=0;i<x.length();i++){s.push_back(x[i]); s.push_back('#');} s.push_back('!'); for(int i=2;i<len-1;i++){ if(i<=r) p[i]=min(p[mid*2-i],r-i+1); else p[i]=1; while(s[i-p[i]]==s[i+p[i]]) p[i]++; if(i+p[i]-1>r){r=p[i]+i-1;mid=i;} ans=max(p[i],ans); } cout<<ans-1<<endl; }
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll bmul(ll a,ll b,ll m){ ull c=(ull)a*(ull)b-(ull)((long double)a/m*b+0.5L)*(ull)m; if(c<(ull)m) return c; return c+m; }
ll qpow(ll x,ll p,ll mod){ ll ans=1; while(p){ if(p&1) ans=bmul(ans,x,mod); x=bmul(x,x,mod); p>>=1; } return ans; }
bool Miller_Rabin(ll p){ if(p<2) return false; if(p==2) return true; if(p==3) return true; ll d=p-1,r=0; while(!(d&1)) ++r,d>>=1; for(ll k=0;k<10;++k){ ll a=rand()%(p-2)+2; ll x=qpow(a,d,p); if(x==1||x==p-1) continue; for(int i=0;i<r-1;++i){ x=bmul(x,x,p); if(x==p-1) break; } if(x!=p-1) return false; } return true; }
ll Pollard_Rho(ll x){ ll s=0,t=0; ll c=(ll)rand()%(x-1)+1; int step=0,goal=1; ll val=1; for(goal=1;;goal*=2,s=t,val=1){ for(step=1;step<=goal;++step){ t=(bmul(t,t,x)+c)%x; val=bmul(val,abs(t-s),x); if((step%127)==0){ ll d=gcd(val,x); if(d>1) return d; } } ll d=gcd(val,x); if(d>1) return d; } } ll max_factor;
void fac(ll x){ if(x<=max_factor||x<2) return; if(Miller_Rabin(x)){ max_factor=max(max_factor,x); return; } ll p=x; while(p>=x) p=Pollard_Rho(x); while((x%p)==0) x/=p; fac(x),fac(p); }
bool prim(ll st){ const ll inf=1e9+1; ll n,m,ans=0,cnt=0; ll d[200010],vis[200010]; vector<pair<ll,ll>> g[200010]; for(int i=0;i<=n;i++) d[i]=inf; d[st]=0; priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<>> q; q.push({0,st}); while(!q.empty()){ ll u=q.top().sd,cost=q.top().fs; q.pop(); if(vis[u]) continue; vis[u]=1; ans+=d[u]; cnt++; for(auto[next,c]:g[u]){ if(d[next]>c){ d[next]=c; q.push({c,next}); } } } return cnt==n; }
void spfa(){ #define LL long long long long N; const long long INF=1e18; vector<pair<LL,LL>> g[10010]; queue<LL> q; vector<bool> inQueue(N+1, false); vector<LL> dis(N+1, INF);
dis[1] = 0; q.push(1); inQueue[1] = true;
while(!q.empty()) { LL u = q.front(); q.pop(); inQueue[u] = false; for(auto [v, w] : g[u]) { if(dis[v] > dis[u] + w) { dis[v] = dis[u] + w; if(!inQueue[v]) { q.push(v); inQueue[v] = true; } } } } }
struct splay_tree{ const int N=1e5+10; struct node{ int s[2]; int p; int v; int cnt; int size; void init(int p1,int v1){ p=p1; v=v1; cnt=size=1; } }tr[N]; int root; int idx; void pushup(int u){ tr[u].size=tr[tr[u].s[0]].size+tr[tr[u].s[1]].size+tr[u].cnt; } void rotate(int x){ int y = tr[x].p,z = tr[y].p; int k = tr[y].s[1] == x; tr[z].s[tr[z].s[1]==y] = x; tr[x].p=z; tr[y].s[k] = tr[x].s[k^1]; tr[tr[x].s[k^1]].p = y; tr[x].s[k^1] = y; tr[y].p = x; pushup(y),pushup(x); } void splay(int x,int k){ while(tr[x].p!=k){ int y=tr[x].p,z=tr[y].p; if(z!=k) (tr[y].s[0]==x)^(tr[z].s[0]==y) ? rotate(x):rotate(y); rotate(x); } if(k==0) root=x; } void find(int v){ int x=root; while(tr[x].s[v>tr[x].v]&&v!=tr[x].v) x=tr[x].s[v>tr[x].v]; splay(x,0); } int get_pre(int v){ find(v); int x=root; if(tr[x].v<v) return x; x=tr[x].s[0]; while(tr[x].s[1])x=tr[x].s[1]; splay(x,0); return x; } int get_suc(int v){ find(v); int x=root; if(tr[x].v>v) return x; x=tr[x].s[1]; while(tr[x].s[0])x=tr[x].s[0]; splay(x,0); return x; } void del(int v){ int pre=get_pre(v); int suc=get_suc(v); splay(pre,0),splay(suc,pre); int del=tr[suc].s[0]; if(tr[del].cnt>1) tr[del].cnt--,splay(del,0); else tr[suc].s[0]=0,splay(suc,0); } int get_val(int k){ int x=root; while(1){ int y=tr[x].s[0]; if(tr[y].size+tr[x].cnt<k){ k-=tr[y].size+tr[x].cnt; x=tr[x].s[1]; } else if(tr[y].size>=k) x=tr[x].s[0]; else break; } splay(x,0); return tr[x].v; } void insert(int v){ int x=root,p=0; while(x&&tr[x].v!=v) p=x,x=tr[x].s[v>tr[x].v]; if(x) tr[x].cnt++; else{ x=++idx; tr[p].s[v>tr[p].v]=x; tr[x].init(p,v); } splay(x,0); } int get_rank(int v){ insert(v); int res=tr[tr[root].s[0]].size; del(v); return res; }
};
struct ST{ const ll N=3e5+10; ll a[N],Log[N],st[N][20]; void init_st(ll n){ Log[1]=0; for(int i=2;i<=n+1;i++) Log[i]=Log[i/2]+1; for(int i=1;i<=n;i++) st[i][0]=a[i]; for(int j=1;(1ll<<j)<=n;j++) for(int i=1;i+(1ll<<j)-1<=n;i++) st[i][j]=min(st[i][j-1],st[i+(1ll<<(j-1))][j-1]); } ll query_min(ll L,ll R){ ll k=Log[R-L+1]; return min(st[L][k],st[R-(1ll<<k)+1][k]); } };
struct string_hash{ typedef unsigned long long ULL; const ll P=131; unordered_set<ll> a; ULL p[2200],h[2200]; ll init(string s){ p[0]=1,h[0]=0; for(int i=1;i<=s.length();i++){ p[i]=p[i-1]*P; h[i]=h[i-1]*P+s[i-1]; } return h[s.length()]; } ll gethash(ll l,ll r){ return h[r]-h[l-1]*p[r-l+1]; } ll sbstr(ll l1,ll r1,ll l2,ll r2){ return gethash(l1,r1)==gethash(l2,r2); } }
void prefix(){ string s; cin>>s; for(int i=1;i<s.length();i++){ ll j=pi[i-1]; while(j>0&&s[j]!=s[i]) j=pi[j-1]; if(s[i]==s[j]) j++; pi[i]=j; } for(int i=0;i<s.length();i++)cout<<pi[i]<<" "; cout<<endl; }
struct tarjan_LCA{ #define ll long long #define ld long double #define pb push_back const ll N=5e5+10; ll n,m,root; ll fa[N],vis[N],ans[N]; vector<ll> g[N]; vector<pair<ll,ll>> q[N]; ll find(ll x){return fa[x]==x?x:fa[x]=find(fa[x]);} void tarjan(ll now){ vis[now]=1; for(int next:g[now]){ if(!vis[next]){ tarjan(next); fa[next]=now; } } for(auto[othor,id]:q[now]){ if(vis[othor]) ans[id]=find(othor); } } void solve(){ cin>>n>>m>>root; for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<n;i++){ ll u,v; cin>>u>>v; g[u].pb(v); g[v].pb(u); } for(int i=1;i<=m;i++){ ll u,v; cin>>u>>v; q[v].pb({u,i}); q[u].pb({v,i}); } tarjan(root); for(int i=1;i<=m;i++) cout<<ans[i]<<endl; } };
struct fenwick_tree{ ll c[200010],n; int lowbit(int x){return x&-x;} int getsum(int x){ int ans=0; while(x>0){ ans=ans+c[x]; x=x-lowbit(x); } return ans; } void add(int x,int k){ while(x<=n){ c[x]=c[x]+k; x=x+lowbit(x); } } };
struct fenwicktree_prefix{ ll t1[1000005],t2[1000005],n; ll lowbit(int x) {return x&(-x);} void build(ll k,ll v){ ll v1=v*k; while(k<n){ t1[k]+=v;t2[k]+=v1; k+=lowbit(k); } } ll getsum(ll *t1,int k){ ll sum=0; while(k>0){ sum+=t1[k]; k-=lowbit(k); } return sum; } void add(ll k,ll v){ build(k,v); } void change(ll l,ll r,ll v){ add(l,v);add(r+1ll,-v); } ll getsum1(ll l,ll r){ return (r+1ll)*getsum(t1,r)+l*getsum(t1,l-1ll)-getsum(t2,r)-getsum(t2,l-1ll); } };
struct Trie{ ll root=0,cnt=0; vector<vector<ll>> t; vector<ll> num; Trie(){t.emplace_back(2,0);} void insert(ll x){ num.pb(x); ll p=root; for(int i=32;i>=0;i--){ if(!t[p][(x>>i)&1]){ t[p][(x>>i)&1]=++cnt; t.emplace_back(2,0); } p=t[p][(x>>i)&1]; } } ll query_min(ll x){ ll p=root,ans=0; for(int i=32;i>=0;i--){ ll u=(x>>i)&1; ans=ans*2+(t[p][u]?u:!u); p=t[p][u]?t[p][u]:t[p][!u]; } return ans; } ll query_max(ll x){ ll p=root,ans=0; for(int i=32;i>=0;i--){ ll u=(x>>i)&1; ans=ans*2+(t[p][!u]?!u:u); p=t[p][!u]?t[p][!u]:t[p][u]; } return ans; } friend void merge(Trie&x,Trie&y){ if(x.num.size()<y.num.size()) swap(x,y); for(ll i:y.num) x.insert(i); y=Trie(); } };
void Z(){ string t; cin>>t; vector<ll> zt(t.length()); zt[0]=t.length(); for(ll i=1,l=0,r=0;i<t.length();i++){ if(i<=r)zt[i]=min(zt[i-l],r-i+1); while(t[zt[i]]==t[i+zt[i]]&&i+zt[i]<t.length()) zt[i]++; if(i+zt[i]-1>r) l=i,r=zt[i]+i-1; } for(auto &i:zt) cout<<i<<" "; cout<<endl; }
|