正文
增加 379 行删除 263 行
10 行未改动11121314151617181979 行未改动9910010110210310410510610710810911011114 行未改动1261271281291301311321331341351361371381395 行未改动1451461471481491501511521532 行未改动15615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120238 行未改动2412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872884 行未改动29329429529629729829930030130230330430530630730830931031131231331431531631731831919 行未改动33934034134234334434534634734834935035135235335435535635735821 行未改动3803813823833843853863873883 行未改动39239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342467 行未改动4924934944954964974984995004 行未改动5055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485497 行未改动5575585595605615625635645655665675685695705715725735745 行未改动5805815825835845855865875883 行未改动5925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386395 行未改动6456466476486496506516526533 行未改动65765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571610 行未改动7277287297307317327337347357367377382 行未改动7417427437447457467477487498 行未改动7587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957965 行未改动8028038048058068078088098105 行未改动8168178188198208218228238248258268279 行未改动8378388398408418428438448458468478488498508518528538548558568578584 行未改动86386486586686786886987087187287387487587687787887988088188288388488588688788888928 行未改动9189199209219229239249259263 行未改动9309319329339349359369379382 行未改动94194294394494594694794894913 行未改动9639649659669679689699709719729739749759769779789799809819829839846 行未改动9919929939949959969979989991000100110021003100410051006100710085 行未改动10141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110623 行未改动1066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611074 行未改动11121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511669 行未改动1176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312149 行未改动12241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129535 行未改动133113321333133413351336133713381339134013411342134313441345134613471348134913 行未改动1363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513863 行未改动1390139113921393139413951396139713986 行未改动14051406140714081409141014111412141323 行未改动143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150921 行未改动153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416257 行未改动163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716886 行未改动1695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817393 行未改动1743174417451746174717481749175017516 行未改动17581759176017611762176317641765176617671768176933 行未改动1803180418051806180718081809181018111812181318146 行未改动18211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418454 行未改动18501851185218531854185518561857185818591860186118621863186418656 行未改动1872187318741875187618771878187918801881188218831884188518861887188818894 行未改动18941895189618971898189919001901190215 行未改动191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819494 行未改动1954195519561957195819591960196119624 行未改动19671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619976 行未改动20042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720284 行未改动2033203420352036203720382039204020412042204320446 行未改动205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208410 行未改动209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213113 行未改动2145214621472148214921502151215221532154215521566 行未改动216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422157 行未改动2223222422252226222722282229223022314 行未改动2236223722382239224022412242224322442 行未改动2247224822492250225122522253225422552 行未改动22582259226022612262226322642265226622672268226922702271227222732274227522764 行未改动2281228222832284228522862287228822894 行未改动229422952296229710 行未改动```改出来的不是人看的。字数统计:61910 字符### 44粉:[Brute_Force](https://www.luogu.com.cn/user/995899)[【模板】三维偏序 / 陌上花开](https://www.luogu.com.cn/problem/P3810)79 行未改动private:int num_threads;vector<thread> workers;vector<int>& data;public:ParallelCalculator(vector<int>& d, int threads = thread::hardware_concurrency()): data(d), num_threads(threads) {}template<typename Func>void parallel_for(int start, int end, Func&& func) {int chunk = (end - start) / num_threads;for(int i = 0; i < num_threads; ++i) {14 行未改动template<typename... Args>class MultiDimensionalArray {private:tuple<vector<Args>...> data;template<size_t... Is>void print_impl(index_sequence<Is...>) {(cout << ... << get<Is>(data).size()) << endl;}public:void print_sizes() {print_impl(index_sequence_for<Args...>{});}5 行未改动private:static constexpr size_t POOL_SIZE = 1000000;T pool[POOL_SIZE];bool used[POOL_SIZE];public:T* allocate() {for(size_t i = 0; i < POOL_SIZE; ++i) {if(!used[i]) {2 行未改动}}return nullptr;}void deallocate(T* ptr) {size_t index = ptr - pool;used[index] = false;}};// 优化后的节点结构,使用移动语义和noexceptstruct Node {int a, b, c;Node() noexcept = default;Node(int a_, int b_, int c_) noexcept : a(a_), b(b_), c(c_) {}Node(const Node&) = default;Node(Node&& other) noexcept: a(exchange(other.a, 0)),b(exchange(other.b, 0)),c(exchange(other.c, 0)) {}Node& operator=(Node&& other) noexcept {if(this != &other) {a = exchange(other.a, 0);b = exchange(other.b, 0);c = exchange(other.c, 0);}return *this;}bool operator<(const Node& r) const {static constexpr auto comparator = [](int x, int y, bool odd) -> bool {return odd ? x < y : x > y;};if(bel[a] != bel[r.a]) {return bel[a] < bel[r.a];}return comparator(b, r.b, bel[a] & 1);}static inline vector<int> bel; // C++17 inline静态成员};// 常量表达式优化38 行未改动}// 查询函数 - 使用标准算法int query(int x, const vector<int>& val, const vector<int>& sum, const vector<int>& bel) {return accumulate(begin(val), begin(val) + x, 0) +accumulate(next(begin(sum)), next(begin(sum), bel[x]), 0);}signed main() {ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);// 初始化cin >> n >> k;vector<future<void>> futures;// 异步读取输入for(int i = 1; i <= n; ++i) {futures.push_back(async(launch::async, [i, &a, &b, &c]() {cin >> a[i] >> b[i] >> c[i];}));}for(auto& f : futures) f.get();// 使用并行算法parallel_for(1, n + 1, [&](int i) {++ra[a[i]];++rb[b[i]];});// 变换操作transform(ra.begin() + 1, ra.begin() + k + 1, ra.begin(),[](int x) { return x; });transform(rb.begin() + 1, rb.begin() + k + 1, rb.begin(),[](int x) { return x; });for(int i = 1; i <= k; ++i) {la[i] = ra[i - 1] + 1;ra[i] += ra[i - 1];lb[i] = rb[i - 1] + 1;rb[i] += rb[i - 1];}// 使用optional处理可能的空值optional<int> temp;for(int i = 1; i <= n; ++i) {int x = la[a[i]]++;4 行未改动ac[x] = c[i];bc[y] = c[i];temp = x; // 使用optional}// 初始化bel数组iota(bel.begin(), bel.end(), 0);transform(bel.begin(), bel.end(), bel.begin(),[](int i) { return i / B1; });Node::bel = bel;// 排序 - 使用并行排序sort(execution::par, q.begin() + 1, q.begin() + n + 1);// 重新初始化bel数组iota(bel.begin(), bel.end(), 0);transform(bel.begin(), bel.end(), bel.begin(),[](int i) { return (i - 1) / B2 + 1; });int na = 0, nb = 0;auto adder = create_adder();auto remover = create_remover();// 主循环 - 添加异常处理try {for(int i = 1; i <= n; ++i) {while(na < q[i].a) {19 行未改动remover(bc[nb], val, sum, bel);}nb--;}int result = query(q[i].c, val, sum, bel);ans[result - 1]++;}} catch(const exception& e) {cerr << "Error during processing: " << e.what() << endl;return 1;}// 输出结果copy_n(ans.begin(), n, ostream_iterator<int>(cout, "\n"));return 0;}``````21 行未改动int val, key, siz;int sum, mx, lx, rx;int tag;bool rev, same;// 新增的统计信息int mn; // 最小值int gcd_val; // 最大公约数int xor_sum; // 异或和3 行未改动int second_max; // 第二大值int second_min; // 第二小值int lazy_add; // 区间加法的懒标记bool has_add; // 是否有加法标记// 哈希相关(用于序列比对)uint64_t hash_val; // 哈希值uint64_t hash_rev; // 反转哈希值// 统计信息int sum_square; // 平方和int sum_cube; // 立方和int sum_abs; // 绝对值和// 区间最值位置int max_pos; // 最大值位置int min_pos; // 最小值位置// 优先级队列(用于维护区间最值)priority_queue<int> max_heap;priority_queue<int, vector<int>, greater<int>> min_heap;// 区间合并信息int inc_len; // 最长连续递增长度int dec_len; // 最长连续递减长度int l_val, r_val; // 区间最左和最右的值bool is_sorted_inc; // 是否升序bool is_sorted_dec; // 是否降序Node() {l = r = 0;val = key = siz = 0;sum = mx = lx = rx = 0;67 行未改动tr[p].l = tr[p].r = 0;tr[p].val = x;tr[p].key = rand();tr[p].siz = 1;// 基础信息tr[p].sum = x;tr[p].mx = x;tr[p].mn = x;4 行未改动tr[p].cnt_negative = (x < 0) ? 1 : 0;tr[p].max_abs = abs(x);tr[p].second_max = -INF;tr[p].second_min = INF;// 哈希信息tr[p].hash_val = get_hash(x, 0);tr[p].hash_rev = get_hash(x, 0);// 平方和立方tr[p].sum_square = x * x;tr[p].sum_cube = x * x * x;tr[p].sum_abs = abs(x);// 位置信息tr[p].max_pos = p;tr[p].min_pos = p;// 更新堆tr[p].max_heap.push(x);tr[p].min_heap.push(x);// 区间属性tr[p].l_val = tr[p].r_val = x;tr[p].inc_len = tr[p].dec_len = 1;tr[p].is_sorted_inc = tr[p].is_sorted_dec = true;// 懒标记tr[p].same = false;tr[p].rev = false;tr[p].has_add = false;tr[p].lazy_add = 0;tr[p].tag = 0;return p;}// 增强版的pushupvoid pushup(int p) {if(!p) return;tr[p].siz = 1;tr[p].sum = tr[p].val;tr[p].sum_square = tr[p].val * tr[p].val;tr[p].sum_cube = tr[p].val * tr[p].val * tr[p].val;7 行未改动tr[p].max_abs = abs(tr[p].val);tr[p].l_val = tr[p].r_val = tr[p].val;tr[p].inc_len = tr[p].dec_len = 1;tr[p].is_sorted_inc = tr[p].is_sorted_dec = true;// 清空堆并重新加入while(!tr[p].max_heap.empty()) tr[p].max_heap.pop();while(!tr[p].min_heap.empty()) tr[p].min_heap.pop();tr[p].max_heap.push(tr[p].val);tr[p].min_heap.push(tr[p].val);tr[p].second_max = -INF;tr[p].second_min = INF;// 合并左子树if(tr[p].l) {int lc = tr[p].l;tr[p].siz += tr[lc].siz;5 行未改动tr[p].gcd_val = gcd(tr[p].gcd_val, tr[lc].gcd_val);tr[p].cnt_positive += tr[lc].cnt_positive;tr[p].cnt_negative += tr[lc].cnt_negative;tr[p].max_abs = max(tr[p].max_abs, tr[lc].max_abs);// 合并堆priority_queue<int> temp_max = tr[lc].max_heap;while(!temp_max.empty()) {tr[p].max_heap.push(temp_max.top());3 行未改动while(!temp_min.empty()) {tr[p].min_heap.push(temp_min.top());temp_min.pop();}// 更新最值if(tr[lc].mx > tr[p].mx) {tr[p].second_max = tr[p].mx;tr[p].mx = tr[lc].mx;tr[p].max_pos = tr[lc].max_pos;} else if(tr[lc].mx < tr[p].mx) {update_second_values(p, tr[lc].mx);}if(tr[lc].mn < tr[p].mn) {tr[p].second_min = tr[p].mn;tr[p].mn = tr[lc].mn;tr[p].min_pos = tr[lc].min_pos;} else if(tr[lc].mn > tr[p].mn) {update_second_values(p, tr[lc].mn);}// 更新左右边界值tr[p].l_val = tr[lc].l_val;// 更新区间属性if(tr[lc].is_sorted_inc && tr[lc].r_val <= tr[p].val) {tr[p].is_sorted_inc = tr[lc].is_sorted_inc;} else {tr[p].is_sorted_inc = false;}if(tr[lc].is_sorted_dec && tr[lc].r_val >= tr[p].val) {tr[p].is_sorted_dec = tr[lc].is_sorted_dec;} else {tr[p].is_sorted_dec = false;}tr[p].inc_len = max(tr[lc].inc_len,(tr[lc].r_val <= tr[p].val ? tr[lc].inc_len + 1 : 1LL));tr[p].dec_len = max(tr[lc].dec_len,(tr[lc].r_val >= tr[p].val ? tr[lc].dec_len + 1 : 1LL));}// 合并右子树if(tr[p].r) {int rc = tr[p].r;tr[p].siz += tr[rc].siz;5 行未改动tr[p].gcd_val = gcd(tr[p].gcd_val, tr[rc].gcd_val);tr[p].cnt_positive += tr[rc].cnt_positive;tr[p].cnt_negative += tr[rc].cnt_negative;tr[p].max_abs = max(tr[p].max_abs, tr[rc].max_abs);// 合并堆priority_queue<int> temp_max = tr[rc].max_heap;while(!temp_max.empty()) {tr[p].max_heap.push(temp_max.top());3 行未改动while(!temp_min.empty()) {tr[p].min_heap.push(temp_min.top());temp_min.pop();}// 更新最值if(tr[rc].mx > tr[p].mx) {tr[p].second_max = tr[p].mx;tr[p].mx = tr[rc].mx;tr[p].max_pos = tr[rc].max_pos;} else if(tr[rc].mx < tr[p].mx) {update_second_values(p, tr[rc].mx);}if(tr[rc].mn < tr[p].mn) {tr[p].second_min = tr[p].mn;tr[p].mn = tr[rc].mn;tr[p].min_pos = tr[rc].min_pos;} else if(tr[rc].mn > tr[p].mn) {update_second_values(p, tr[rc].mn);}// 更新右边界值tr[p].r_val = tr[rc].r_val;// 更新区间属性if(tr[p].is_sorted_inc && tr[p].val <= tr[rc].l_val && tr[rc].is_sorted_inc) {tr[p].is_sorted_inc = true;} else {tr[p].is_sorted_inc = false;}if(tr[p].is_sorted_dec && tr[p].val >= tr[rc].l_val && tr[rc].is_sorted_dec) {tr[p].is_sorted_dec = true;} else {tr[p].is_sorted_dec = false;}int left_inc = tr[p].inc_len;int right_inc = tr[rc].inc_len;if(tr[p].val <= tr[rc].l_val) {tr[p].inc_len = max(tr[p].inc_len, left_inc + (right_inc > 0 ? 1 : 0));}int left_dec = tr[p].dec_len;int right_dec = tr[rc].dec_len;if(tr[p].val >= tr[rc].l_val) {tr[p].dec_len = max(tr[p].dec_len, left_dec + (right_dec > 0 ? 1 : 0));}}// 计算 lx, rx, mx(原有逻辑,但增强)if(tr[p].l && tr[p].r) {tr[p].lx = max(tr[tr[p].l].lx,tr[tr[p].l].sum + tr[p].val + max(0LL, tr[tr[p].r].lx));tr[p].rx = max(tr[tr[p].r].rx,tr[tr[p].r].sum + tr[p].val + max(0LL, tr[tr[p].l].rx));tr[p].mx = max(max(tr[tr[p].l].mx, tr[tr[p].r].mx),max(tr[tr[p].l].rx, 0LL) + tr[p].val + max(tr[tr[p].r].lx, 0LL));}10 行未改动else {tr[p].lx = tr[p].rx = max(0LL, tr[p].val);tr[p].mx = tr[p].val;}// 更新哈希值int left_size = tr[tr[p].l].siz;tr[p].hash_val = tr[tr[p].l].hash_val +get_hash(tr[p].val, left_size) * pow_base[left_size] +tr[tr[p].r].hash_val * pow_base[left_size + 1];tr[p].hash_rev = tr[tr[p].r].hash_rev * pow_base[left_size + 1] +get_hash(tr[p].val, tr[tr[p].r].siz) * pow_base[tr[tr[p].r].siz] +2 行未改动// 增强版的区间赋值void pushsame(int p, int c) {if(!p) return;tr[p].val = c;tr[p].sum = c * tr[p].siz;tr[p].sum_square = c * c * tr[p].siz;tr[p].sum_cube = c * c * c * tr[p].siz;8 行未改动tr[p].mx = max(c, tr[p].sum);tr[p].l_val = tr[p].r_val = c;tr[p].inc_len = tr[p].dec_len = tr[p].siz;tr[p].is_sorted_inc = tr[p].is_sorted_dec = true;// 更新堆while(!tr[p].max_heap.empty()) tr[p].max_heap.pop();while(!tr[p].min_heap.empty()) tr[p].min_heap.pop();tr[p].max_heap.push(c);tr[p].min_heap.push(c);tr[p].second_max = -INF;tr[p].second_min = INF;// 更新哈希for(int i = 0; i < tr[p].siz; i++) {tr[p].hash_val += get_hash(c, i);tr[p].hash_rev += get_hash(c, tr[p].siz - 1 - i);}tr[p].same = true;tr[p].tag = c;tr[p].has_add = false; // 区间赋值会覆盖加法标记tr[p].lazy_add = 0;}// 新增:区间加法void pushadd(int p, int c) {if(!p) return;if(tr[p].same) {// 如果已经有区间赋值标记,直接修改赋值标记pushsame(p, tr[p].tag + c);return;}tr[p].val += c;tr[p].sum += c * tr[p].siz;tr[p].sum_square += 2 * c * (tr[p].sum - c * tr[p].siz) + c * c * tr[p].siz;tr[p].sum_cube += 3 * c * tr[p].sum_square + 3 * c * c * (tr[p].sum - c * tr[p].siz) + c * c * c * tr[p].siz;5 行未改动tr[p].rx = max(0LL, tr[p].rx + c);tr[p].mx += c;tr[p].l_val += c;tr[p].r_val += c;// 更新堆priority_queue<int> new_max;priority_queue<int, vector<int>, greater<int>> new_min;while(!tr[p].max_heap.empty()) {5 行未改动tr[p].min_heap.pop();}tr[p].max_heap = new_max;tr[p].min_heap = new_min;if(tr[p].second_max != -INF) tr[p].second_max += c;if(tr[p].second_min != INF) tr[p].second_min += c;tr[p].has_add = true;tr[p].lazy_add += c;}9 行未改动// 增强版的pushdownvoid pushdown(int p) {if(!p) return;if(tr[p].has_add) {if(tr[p].l) pushadd(tr[p].l, tr[p].lazy_add);if(tr[p].r) pushadd(tr[p].r, tr[p].lazy_add);tr[p].has_add = false;tr[p].lazy_add = 0;}if(tr[p].same) {if(tr[p].l) pushsame(tr[p].l, tr[p].tag);if(tr[p].r) pushsame(tr[p].r, tr[p].tag);tr[p].same = false;}if(tr[p].rev) {if(tr[p].l) pushrev(tr[p].l);if(tr[p].r) pushrev(tr[p].r);tr[p].rev = false;4 行未改动if(!p) {x = y = 0;return;}pushdown(p);int leftsize = tr[tr[p].l].siz;if(leftsize >= k) {y = p;split(tr[p].l, k, x, tr[y].l);} else {x = p;split(tr[p].r, k - leftsize - 1, tr[x].r, y);}pushup(p);}int merge(int x, int y) {if(!x || !y) return x + y;if(tr[x].key > tr[y].key) {pushdown(x);tr[x].r = merge(tr[x].r, y);pushup(x);28 行未改动if(l > r) return;int len = r - l + 1;k = ((k % len) + len) % len;if(k == 0) return;int x, y, z, w;split(root, l - 1, x, y);split(y, len, y, z);split(y, len - k, y, w);3 行未改动// 新增功能2:获取区间中位数int get_median(int p) {if(!p) return 0;pushdown(p);// 使用对顶堆思想,这里简化处理:排序后取中间vector<int> vals;function<void(int)> dfs = [&](int u) {if(!u) return;2 行未改动vals.push_back(tr[u].val);dfs(tr[u].r);};dfs(p);sort(vals.begin(), vals.end());return vals[vals.size() / 2];}13 行未改动// 新增功能4:判断两个区间是否相等(通过哈希)bool interval_equal(int root, int l1, int r1, int l2, int r2) {if(r1 - l1 != r2 - l2) return false;int len = r1 - l1 + 1;int a, b, c, d, e, f;split(root, l1 - 1, a, b);split(b, len, b, c);uint64_t hash1 = tr[b].hash_val;root = merge(merge(a, b), c);split(root, l2 - 1, d, e);split(e, len, e, f);uint64_t hash2 = tr[e].hash_val;root = merge(merge(d, e), f);return hash1 == hash2;}// 新增功能5:区间离散化6 行未改动vals.push_back(tr[u].val);dfs(tr[u].r);};dfs(p);vector<int> sorted = vals;sort(sorted.begin(), sorted.end());sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());map<int, int> mp;for(int i = 0; i < sorted.size(); i++) {mp[sorted[i]] = i + 1;}vector<int> res;for(int v : vals) {res.push_back(mp[v]);}5 行未改动cin.tie(0);cout.tie(0);srand(time(0));init_pow();int n, m;cin >> n >> m;for(int i = 1; i <= n; i++) {cin >> a[i];}root = build(1, n);string op;while(m--) {cin >> op;operation_count++;history.push_back(operation_count);if(op == "INSERT") {int pos, tot;cin >> pos >> tot;for(int i = 1; i <= tot; i++) {cin >> a[i];value_set.insert(a[i]);freq_map[a[i]]++;}int x, y;split(root, pos, x, y);int z = build(1, tot);root = merge(merge(x, z), y);cout << "Inserted " << tot << " elements at position " << pos << endl;}else if(op == "DELETE") {int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);// 更新频率统计function<void(int)> update_freq = [&](int u) {if(!u) return;update_freq(tr[u].l);3 行未改动}update_freq(tr[u].r);};update_freq(y);del(y);root = merge(x, z);cout << "Deleted " << tot << " elements from position " << pos << endl;}else if(op == "MAKE-SAME") {int pos, tot, c;cin >> pos >> tot >> c;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);pushsame(y, c);root = merge(merge(x, y), z);cout << "Set " << tot << " elements to " << c << endl;}else if(op == "REVERSE") {int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);pushrev(y);root = merge(merge(x, y), z);cout << "Reversed " << tot << " elements from position " << pos << endl;}else if(op == "GET-SUM") {int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "Sum: " << tr[y].sum << endl;4 行未改动}else if(op == "ADD") { // 新增:区间加法int pos, tot, c;cin >> pos >> tot >> c;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);pushadd(y, c);root = merge(merge(x, y), z);cout << "Added " << c << " to " << tot << " elements" << endl;}else if(op == "MIN") { // 新增:查询最小值int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "Minimum: " << tr[y].mn << endl;root = merge(merge(x, y), z);}else if(op == "GCD") { // 新增:查询最大公约数int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "GCD: " << tr[y].gcd_val << endl;root = merge(merge(x, y), z);}else if(op == "XOR") { // 新增:查询异或和int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "XOR: " << tr[y].xor_sum << endl;root = merge(merge(x, y), z);}else if(op == "STATS") { // 新增:统计信息int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "=== Statistics ===" << endl;cout << "Size: " << tr[y].siz << endl;cout << "Sum: " << tr[y].sum << endl;cout << "Sum of squares: " << tr[y].sum_square << endl;9 行未改动cout << "Longest increasing sequence: " << tr[y].inc_len << endl;cout << "Longest decreasing sequence: " << tr[y].dec_len << endl;cout << "Is sorted ascending: " << (tr[y].is_sorted_inc ? "Yes" : "No") << endl;cout << "Is sorted descending: " << (tr[y].is_sorted_dec ? "Yes" : "No") << endl;root = merge(merge(x, y), z);}else if(op == "MEDIAN") { // 新增:查询中位数int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "Median: " << get_median(y) << endl;root = merge(merge(x, y), z);}else if(op == "MODE") { // 新增:查询众数int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);auto freq = get_mode(y);int mode_val = 0, max_freq = 0;for(auto &[val, cnt] : freq) {if(cnt > max_freq) {max_freq = cnt;mode_val = val;}}cout << "Mode: " << mode_val << " (appears " << max_freq << " times)" << endl;root = merge(merge(x, y), z);}else if(op == "ROTATE") { // 新增:区间旋转int pos, tot, k;9 行未改动}else if(op == "DISCRETE") { // 新增:区间离散化int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);auto discrete = interval_discrete(y);cout << "Discretized values: ";for(int v : discrete) {cout << v << " ";}cout << endl;root = merge(merge(x, y), z);}else if(op == "UNIQUE") { // 新增:统计不同元素个数int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);auto freq = get_mode(y);cout << "Number of distinct elements: " << freq.size() << endl;root = merge(merge(x, y), z);}else if(op == "TOP-K") { // 新增:查询前k大元素int pos, tot, k;cin >> pos >> tot >> k;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);vector<int> vals;function<void(int)> dfs = [&](int u) {if(!u) return;pushdown(u);dfs(tr[u].l);vals.push_back(tr[u].val);dfs(tr[u].r);};dfs(y);sort(vals.rbegin(), vals.rend());cout << "Top " << k << " elements: ";for(int i = 0; i < min(k, (int)vals.size()); i++) {cout << vals[i] << " ";}cout << endl;root = merge(merge(x, y), z);}}// 输出操作统计cout << "\n=== Operation Statistics ===" << endl;cout << "Total operations: " << operation_count << endl;cout << "Current tree size: " << tr[root].siz << endl;cout << "Nodes in use: " << cnt << endl;cout << "Nodes in pool: " << top << endl;cout << "Distinct values: " << value_set.size() << endl;return 0;}```35 行未改动int id;int timestamp;int priority;mutable int version;node(int v = 0, int i = 0, int t = 0, int p = 0, int ver = 0): val(v), id(i), timestamp(t), priority(p), version(ver) {}bool operator<(const node &other) const {if (val != other.val) return val < other.val;if (priority != other.priority) return priority < other.priority;if (timestamp != other.timestamp) return timestamp < other.timestamp;return id > other.id;}bool operator==(const node &other) const {return id == other.id && version == other.version;}};13 行未改动heap_base backup_heap;unordered_map<int, int> metadata;int heap_id;mutable int modification_count;extended_heap(int id = 0) : heap_id(id), modification_count(0) {}bool empty() const {return main_heap.empty() && backup_heap.empty();}size_t size() const {return main_heap.size() + backup_heap.size();}void push(const node& n) {main_heap.push(n);modification_count++;}node top() const {if (!main_heap.empty()) {node top_main = main_heap.top();if (!backup_heap.empty()) {3 行未改动return top_main;}return backup_heap.top();}void pop() {if (!main_heap.empty() && !backup_heap.empty()) {if (main_heap.top() < backup_heap.top()) {backup_heap.pop();6 行未改动backup_heap.pop();}modification_count++;}void merge(extended_heap& other) {main_heap.join(other.main_heap);backup_heap.join(other.backup_heap);modification_count += other.modification_count;23 行未改动vector<heap_base::point_iterator> pos;vector<extended_heap> heaps;vector<operation_log> op_log;int current_time;// Advanced find with path compression and union by sizeint find(int x) {if (fa[x] != x) {int root = find(fa[x]);// Path halving optimizationfa[x] = root;}return fa[x];}// Generate random priority for nodesint random_priority() {return uniform_int_distribution<int>(0, MOD)(rng);}// Validate operation before executionbool validate_operation(int op, int A, int B = -1) {if (A < 1 || A > n) return false;if (op == 4 && (B < 1 || B > n)) return false;if (op == 2 && del[A]) return false;return true;}public:GensokyoSolver() : current_time(0) {}void solve() {cin >> T >> W >> K;while (T--) {initialize_test_case();process_operations();output_results();cleanup();}}private:void initialize_test_case() {cin >> n >> m;a.resize(n + 1);fa.resize(n + 1);del.resize(n + 1);pos.resize(n + 1);heaps.clear();heaps.reserve(n + 1);op_log.clear();for (int i = 1; i <= n; i++) {cin >> a[i];fa[i] = i;del[i] = false;heaps.emplace_back(i);int priority = random_priority();pos[i] = heaps[i-1].main_heap.push({a[i], i, current_time++, priority, 0});}}void process_operations() {for (int i = 1; i <= m; i++) {int op;cin >> op;if (op == 1) {handle_undo();} else if (op == 2) {int A;21 行未改动handle_statistics();}}}void handle_delete(int A) {operation_log log;log.type = 2;log.arg1 = A;log.timestamp = current_time++;if (!del[A]) {del[A] = true;int pa = find(A);if (!heaps[pa-1].main_heap.empty()) {node current = *pos[A];log.old_state = current;log.new_state = node(0, A, current_time, 0, current.version + 1);heaps[pa-1].main_heap.erase(pos[A]);heaps[pa-1].backup_heap.push(log.new_state);op_log.push_back(log);}}}void handle_modify(int A, int B) {int pa = find(A);if (!heaps[pa-1].empty()) {node tp = heaps[pa-1].top();if (!del[tp.id]) {operation_log log;log.type = 3;log.arg1 = tp.id;log.arg2 = B;log.timestamp = current_time++;log.old_state = tp;int nv = tp.val - B;if (nv < 0) nv = 0;if (nv == 0) {del[tp.id] = true;heaps[pa-1].main_heap.erase(pos[tp.id]);log.new_state = node(0, tp.id, current_time, 0, tp.version + 1);} else {node new_node = {nv, tp.id, current_time, tp.priority, tp.version + 1};heaps[pa-1].main_heap.modify(pos[tp.id], new_node);log.new_state = new_node;}op_log.push_back(log);}}}void handle_merge(int A, int B) {int pa = find(A), pb = find(B);if (pa != pb) {operation_log log;log.type = 4;log.arg1 = pa;log.arg2 = pb;log.timestamp = current_time++;// Union by size with heap size considerationif (heaps[pa-1].size() < heaps[pb-1].size()) {swap(pa, pb);}heaps[pa-1].merge(heaps[pb-1]);fa[pb] = pa;op_log.push_back(log);}}void handle_undo() {if (!op_log.empty()) {operation_log last_op = op_log.back();op_log.pop_back();// Revert the last operationif (last_op.type == 2 || last_op.type == 3) {int id = (last_op.type == 2) ? last_op.arg1 : last_op.arg2;int pa = find(id);// Restore old stateif (last_op.old_state.val > 0) {del[id] = false;node restored = last_op.old_state;7 行未改动cout << "Undo of merge operation logged\n";}}}void handle_query(int A) {int pa = find(A);if (!heaps[pa-1].empty()) {node top_node = heaps[pa-1].top();cout << "Query result: " << top_node.val << " (from node " << top_node.id << ")\n";} else {cout << "Query result: Heap is empty\n";}}void handle_statistics() {unordered_map<int, int> heap_sizes;unordered_map<int, int> heap_modifications;for (int i = 1; i <= n; i++) {int root = find(i);heap_sizes[root]++;heap_modifications[root] += heaps[root-1].modification_count;}cout << "Statistics:\n";cout << "Total heaps: " << heap_sizes.size() << endl;cout << "Total operations: " << op_log.size() << endl;int max_heap = 0, max_heap_id = 0;for (const auto& [id, size] : heap_sizes) {if (size > max_heap) {max_heap = size;max_heap_id = id;}}if (max_heap_id > 0) {cout << "Largest heap ID: " << max_heap_id<< " (size: " << max_heap<< ", modifications: " << heap_modifications[max_heap_id] << ")\n";}}void output_results() {int tot = 0;vector<int> mx;vector<bool> vis(n + 1, false);// Calculate results with advanced statisticsvector<pair<int, int>> heap_maxima;for (int i = 1; i <= n; i++) {int rt = find(i);if (!vis[rt]) {vis[rt] = true;6 行未改动}}}}// Apply W-based modifications with additional checksif (W == 2 && !mx.empty()) {int me = *max_element(mx.begin(), mx.end());tot -= me;// Log the adjustmentcout << "W=2 adjustment: removed max value " << me << endl;} else if (W == 3 && !mx.empty()) {int me = *max_element(mx.begin(), mx.end());tot += me;// Log the adjustmentcout << "W=3 adjustment: added max value " << me << endl;}// Output final result with classificationif (tot == 0) {cout << "Gensokyo ";} else if (tot > K) {cout << "Hell ";} else {cout << "Heaven ";}cout << tot << "\n";// Additional debug information if neededif (W > 1) {cout << "Heap maxima: ";for (const auto& [id, val] : heap_maxima) {cout << "(" << id << ":" << val << ") ";}cout << endl;}}void cleanup() {for (int i = 1; i <= n; i++) {if (fa[i] == i) {while (!heaps[i-1].main_heap.empty()) {3 行未改动heaps[i-1].backup_heap.pop();}}}// Clear all containersa.clear();fa.clear();del.clear();6 行未改动signed main() {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);GensokyoSolver solver;solver.solve();return 0;}```33 行未改动const int N = 300005;const int INF = 1e18;const int MOD = 1e9 + 7;const double PI = acos(-1.0);template<typename T>T max3(T a, T b, T c) { return max({a, b, c}); }template<typename T>T min3(T a, T b, T c) { return min({a, b, c}); }}}6 行未改动// 基础属性int id, val, dis;int ls, rs;int add, mul;// 扩展属性int height; // 树高int size; // 子树大小int sum; // 子树和int max_val, min_val; // 最大值最小值int lazy_xor; // 异或懒标记bool rev; // 翻转标记// 构造函数node() : id(0), val(0), dis(0), ls(0), rs(0), add(0), mul(1),height(0), size(0), sum(0), max_val(0), min_val(0),lazy_xor(0), rev(false) {}// 带参构造函数node(int _id, int _val) : id(_id), val(_val), dis(1), ls(0), rs(0),add(0), mul(1), height(1), size(1),sum(_val), max_val(_val), min_val(_val),lazy_xor(0), rev(false) {}};4 行未改动int h[N], fa[N], a[N], v[N], s[N], c[N];int dead[N], dep[N], diedep[N];int root[N];vector<int> e[N];// 额外数据结构set<int> alive_knights[N]; // 每个城池存活的骑士map<int, int> knight_power; // 骑士力量映射queue<int> bfs_queue; // BFS队列stack<int> dfs_stack; // DFS栈priority_queue<pair<int, int>> pq; // 优先队列// 统计信息int total_operations = 0;int merge_count = 0;int pushdown_count = 0;6 行未改动// 随机数生成器random_device rd;mt19937 gen(rd());uniform_int_distribution<int> dis(1, 1000000);int random() { return dis(gen); }// 哈希函数struct Hash {size_t operator()(const pair<int, int>& p) const {return p.first * 131 + p.second;}};// 计时器类class Timer {private:chrono::high_resolution_clock::time_point start_time;4 行未改动auto end = chrono::high_resolution_clock::now();return chrono::duration<double>(end - start_time).count();}};// 调试输出(仅在DEBUG模式下)#ifdef DEBUG#define debug(x) cerr << #x << " = " << x << endl#else15 行未改动if(u == 0) {return;}pushdown_count++;// 处理基础懒标记if(tr[u].ls) {// 乘法标记tr[tr[u].ls].val = tr[tr[u].ls].val * tr[u].mul + tr[u].add;tr[tr[u].ls].add = tr[tr[u].ls].add * tr[u].mul + tr[u].add;tr[tr[u].ls].mul *= tr[u].mul;// 更新统计信息tr[tr[u].ls].sum = tr[tr[u].ls].sum * tr[u].mul + tr[u].add * tr[tr[u].ls].size;tr[tr[u].ls].max_val = tr[tr[u].ls].max_val * tr[u].mul + tr[u].add;tr[tr[u].ls].min_val = tr[tr[u].ls].min_val * tr[u].mul + tr[u].add;}if(tr[u].rs) {tr[tr[u].rs].val = tr[tr[u].rs].val * tr[u].mul + tr[u].add;tr[tr[u].rs].add = tr[tr[u].rs].add * tr[u].mul + tr[u].add;tr[tr[u].rs].mul *= tr[u].mul;tr[tr[u].rs].sum = tr[tr[u].rs].sum * tr[u].mul + tr[u].add * tr[tr[u].rs].size;tr[tr[u].rs].max_val = tr[tr[u].rs].max_val * tr[u].mul + tr[u].add;tr[tr[u].rs].min_val = tr[tr[u].rs].min_val * tr[u].mul + tr[u].add;}// 处理异或懒标记if(tr[u].lazy_xor != 0) {if(tr[u].ls) {tr[tr[u].ls].val ^= tr[u].lazy_xor;4 行未改动tr[tr[u].rs].lazy_xor ^= tr[u].lazy_xor;}tr[u].lazy_xor = 0;}// 处理翻转标记if(tr[u].rev) {if(tr[u].ls) {tr[tr[u].ls].rev ^= 1;4 行未改动swap(tr[tr[u].rs].ls, tr[tr[u].rs].rs);}tr[u].rev = false;}tr[u].add = 0;tr[u].mul = 1;}// 更新节点信息void update(int u) {if(u == 0) return;tr[u].size = 1;tr[u].sum = tr[u].val;tr[u].max_val = tr[u].val;tr[u].min_val = tr[u].val;tr[u].height = 1;if(tr[u].ls) {tr[u].size += tr[tr[u].ls].size;tr[u].sum += tr[tr[u].ls].sum;tr[u].max_val = max(tr[u].max_val, tr[tr[u].ls].max_val);tr[u].min_val = min(tr[u].min_val, tr[tr[u].ls].min_val);tr[u].height = max(tr[u].height, tr[tr[u].ls].height + 1);}if(tr[u].rs) {tr[u].size += tr[tr[u].rs].size;tr[u].sum += tr[tr[u].rs].sum;tr[u].max_val = max(tr[u].max_val, tr[tr[u].rs].max_val);6 行未改动int merge(int x, int y) {if(x == 0 || y == 0) {return x + y;}merge_count++;total_operations++;pushdown(x);pushdown(y);// 根据多种条件决定合并顺序if(tr[x].val > tr[y].val) {swap(x, y);} else if(tr[x].val == tr[y].val) {// 如果值相等,根据id和随机数决定if(tr[x].id > tr[y].id || (tr[x].id == tr[y].id && Utils::random() % 2)) {swap(x, y);}}// 随机决定合并到左边还是右边if(Utils::random() % 2) {tr[x].ls = merge(tr[x].ls, y);if(tr[tr[x].ls].dis < tr[tr[x].rs].dis) {4 行未改动if(tr[tr[x].ls].dis < tr[tr[x].rs].dis) {swap(tr[x].ls, tr[x].rs);}}tr[x].dis = min(tr[tr[x].ls].dis, tr[tr[x].rs].dis) + 1;update(x);return x;}// 深度优先搜索(递归版)6 行未改动// 复杂化的DFS(使用栈实现)void dfs(int u) {dfs_stack.push(u);while(!dfs_stack.empty()) {int cur = dfs_stack.top();dfs_stack.pop();// 处理子节点for(auto v : e[cur]) {dfs_stack.push(v);// 合并骑士root[cur] = merge(root[cur], root[v]);}// 处理当前节点的骑士while(root[cur] != 0) {pushdown(root[cur]);if(tr[root[cur]].val < h[cur]) {dead[cur]++;diedep[tr[root[cur]].id] = dep[cur];alive_knights[cur].erase(tr[root[cur]].id);root[cur] = merge(tr[root[cur]].ls, tr[root[cur]].rs);} else {break;}}// 应用城池效果if(cur != 1 && root[cur] != 0) {if(a[cur] == 0) {// 加法10 行未改动tr[root[cur]].sum *= v[cur];tr[root[cur]].max_val *= v[cur];tr[root[cur]].min_val *= v[cur];}// 随机应用一些额外效果if(Utils::random() % 100 < 5) {// 5%的概率应用异或int x = Utils::random() % 100;tr[root[cur]].val ^= x;tr[root[cur]].lazy_xor ^= x;}if(Utils::random() % 100 < 3) {// 3%的概率翻转tr[root[cur]].rev ^= 1;swap(tr[root[cur]].ls, tr[root[cur]].rs);}}// 更新统计信息update(root[cur]);}}// BFS实现(备用)void bfs(int start) {bfs_queue.push(start);while(!bfs_queue.empty()) {int u = bfs_queue.front();bfs_queue.pop();for(auto v : e[u]) {bfs_queue.push(v);}}13 行未改动signed main() {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);// 使用计时器Utils::Timer timer;// 输入优化auto read = []() {int x = 0, f = 1;char ch = getchar();6 行未改动ch = getchar();}return x * f;};n = read();m = read();FOR(i, 1, n) {h[i] = read();}FOR(i, 2, n) {fa[i] = read();a[i] = read();v[i] = read();e[fa[i]].push_back(i);}dfsdep(1, 1);FOR(i, 1, m) {s[i] = read();c[i] = read();int id = newnode(i, s[i]);root[c[i]] = merge(root[c[i]], id);alive_knights[c[i]].insert(i);knight_power[i] = s[i];}// 执行主DFSdfs(1);// 输出结果FOR(i, 1, n) {cout << dead[i] << endl;}FOR(i, 1, m) {cout << dep[c[i]] - diedep[i] << endl;}// 输出统计信息(可选)if(n > 1000) {printStatistics();}debug("Time elapsed: " << timer.elapsed() << "s");return 0;}// 额外的未使用函数,增加代码量7 行未改动b >>= 1;}return res;}// 欧拉函数int phi(int n) {int res = n;for(int i = 2; i * i <= n; i++) {4 行未改动}if(n > 1) res = res / n * (n - 1);return res;}// 扩展欧几里得int exgcd(int a, int b, int &x, int &y) {if(b == 0) {x = 1; y = 0;2 行未改动int g = exgcd(b, a % b, y, x);y -= a / b * x;return g;}// 线段树模板(未使用)class SegmentTree {private:vector<int> tree, lazy;2 行未改动SegmentTree(int _n) : n(_n) {tree.resize(4 * n);lazy.resize(4 * n);}void pushdown(int p) {if(lazy[p]) {tree[p * 2] += lazy[p];tree[p * 2 + 1] += lazy[p];lazy[p * 2] += lazy[p];lazy[p * 2 + 1] += lazy[p];lazy[p] = 0;}}void update(int p, int l, int r, int ql, int qr, int val) {if(ql <= l && r <= qr) {tree[p] += val;lazy[p] += val;4 行未改动if(ql <= mid) update(p * 2, l, mid, ql, qr, val);if(qr > mid) update(p * 2 + 1, mid + 1, r, ql, qr, val);tree[p] = max(tree[p * 2], tree[p * 2 + 1]);}int query(int p, int l, int r, int ql, int qr) {if(ql <= l && r <= qr) return tree[p];pushdown(p);int mid = (l + r) >> 1, res = -INF;4 行未改动};> }``````::::10 行未改动11121314151617181979 行未改动9910010110210310410510610710810911011114 行未改动1261271281291301311321331341351361371381395 行未改动1451461471481491501511521532 行未改动15615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120238 行未改动2412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872884 行未改动29329429529629729829930030130230330430530630730830931031131231331431531631731831919 行未改动33934034134234334434534634734834935035135235335435535635735821 行未改动3803813823833843853863873883 行未改动39239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342467 行未改动4924934944954964974984995004 行未改动5055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485497 行未改动5575585595605615625635645655665675685695705715725735745 行未改动5805815825835845855865875883 行未改动5925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386395 行未改动6456466476486496506516526533 行未改动65765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571610 行未改动7277287297307317327337347357367377382 行未改动7417427437447457467477487498 行未改动7587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957965 行未改动8028038048058068078088098105 行未改动8168178188198208218228238248258268279 行未改动8378388398408418428438448458468478488498508518528538548558568578584 行未改动86386486586686786886987087187287387487587687787887988088188288388488588688788888928 行未改动9189199209219229239249259263 行未改动9309319329339349359369379382 行未改动94194294394494594694794894913 行未改动9639649659669679689699709719729739749759769779789799809819829839846 行未改动9919929939949959969979989991000100110021003100410051006100710085 行未改动10141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110623 行未改动1066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611074 行未改动11121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511669 行未改动1176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312149 行未改动12241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129535 行未改动133113321333133413351336133713381339134013411342134313441345134613471348134913 行未改动1363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513863 行未改动1390139113921393139413951396139713986 行未改动14051406140714081409141014111412141323 行未改动143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150921 行未改动153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416257 行未改动163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716886 行未改动1695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817393 行未改动1743174417451746174717481749175017516 行未改动17581759176017611762176317641765176617671768176933 行未改动1803180418051806180718081809181018111812181318146 行未改动18211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418454 行未改动18501851185218531854185518561857185818591860186118621863186418656 行未改动1872187318741875187618771878187918801881188218831884188518861887188818894 行未改动18941895189618971898189919001901190215 行未改动191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819494 行未改动1954195519561957195819591960196119624 行未改动19671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619976 行未改动20042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720284 行未改动2033203420352036203720382039204020412042204320446 行未改动205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208410 行未改动209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213113 行未改动2145214621472148214921502151215221532154215521566 行未改动216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422157 行未改动2223222422252226222722282229223022314 行未改动2236223722382239224022412242224322442 行未改动2247224822492250225122522253225422552 行未改动22582259226022612262226322642265226622672268226922702271227222732274227522764 行未改动2281228222832284228522862287228822894 行未改动22942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241310 行未改动```改出来的不是人看的。字数统计:62030 字符### 44粉:[Brute_Force](https://www.luogu.com.cn/user/995899)[【模板】三维偏序 / 陌上花开](https://www.luogu.com.cn/problem/P3810)79 行未改动private:int num_threads;vector<thread> workers;vector<int>& data;public:ParallelCalculator(vector<int>& d, int threads = thread::hardware_concurrency()): data(d), num_threads(threads) {}template<typename Func>void parallel_for(int start, int end, Func&& func) {int chunk = (end - start) / num_threads;for(int i = 0; i < num_threads; ++i) {14 行未改动template<typename... Args>class MultiDimensionalArray {private:tuple<vector<Args>...> data;template<size_t... Is>void print_impl(index_sequence<Is...>) {(cout << ... << get<Is>(data).size()) << endl;}public:void print_sizes() {print_impl(index_sequence_for<Args...>{});}5 行未改动private:static constexpr size_t POOL_SIZE = 1000000;T pool[POOL_SIZE];bool used[POOL_SIZE];public:T* allocate() {for(size_t i = 0; i < POOL_SIZE; ++i) {if(!used[i]) {2 行未改动}}return nullptr;}void deallocate(T* ptr) {size_t index = ptr - pool;used[index] = false;}};// 优化后的节点结构,使用移动语义和noexceptstruct Node {int a, b, c;Node() noexcept = default;Node(int a_, int b_, int c_) noexcept : a(a_), b(b_), c(c_) {}Node(const Node&) = default;Node(Node&& other) noexcept: a(exchange(other.a, 0)),b(exchange(other.b, 0)),c(exchange(other.c, 0)) {}Node& operator=(Node&& other) noexcept {if(this != &other) {a = exchange(other.a, 0);b = exchange(other.b, 0);c = exchange(other.c, 0);}return *this;}bool operator<(const Node& r) const {static constexpr auto comparator = [](int x, int y, bool odd) -> bool {return odd ? x < y : x > y;};if(bel[a] != bel[r.a]) {return bel[a] < bel[r.a];}return comparator(b, r.b, bel[a] & 1);}static inline vector<int> bel; // C++17 inline静态成员};// 常量表达式优化38 行未改动}// 查询函数 - 使用标准算法int query(int x, const vector<int>& val, const vector<int>& sum, const vector<int>& bel) {return accumulate(begin(val), begin(val) + x, 0) +accumulate(next(begin(sum)), next(begin(sum), bel[x]), 0);}signed main() {ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);// 初始化cin >> n >> k;vector<future<void>> futures;// 异步读取输入for(int i = 1; i <= n; ++i) {futures.push_back(async(launch::async, [i, &a, &b, &c]() {cin >> a[i] >> b[i] >> c[i];}));}for(auto& f : futures) f.get();// 使用并行算法parallel_for(1, n + 1, [&](int i) {++ra[a[i]];++rb[b[i]];});// 变换操作transform(ra.begin() + 1, ra.begin() + k + 1, ra.begin(),[](int x) { return x; });transform(rb.begin() + 1, rb.begin() + k + 1, rb.begin(),[](int x) { return x; });for(int i = 1; i <= k; ++i) {la[i] = ra[i - 1] + 1;ra[i] += ra[i - 1];lb[i] = rb[i - 1] + 1;rb[i] += rb[i - 1];}// 使用optional处理可能的空值optional<int> temp;for(int i = 1; i <= n; ++i) {int x = la[a[i]]++;4 行未改动ac[x] = c[i];bc[y] = c[i];temp = x; // 使用optional}// 初始化bel数组iota(bel.begin(), bel.end(), 0);transform(bel.begin(), bel.end(), bel.begin(),[](int i) { return i / B1; });Node::bel = bel;// 排序 - 使用并行排序sort(execution::par, q.begin() + 1, q.begin() + n + 1);// 重新初始化bel数组iota(bel.begin(), bel.end(), 0);transform(bel.begin(), bel.end(), bel.begin(),[](int i) { return (i - 1) / B2 + 1; });int na = 0, nb = 0;auto adder = create_adder();auto remover = create_remover();// 主循环 - 添加异常处理try {for(int i = 1; i <= n; ++i) {while(na < q[i].a) {19 行未改动remover(bc[nb], val, sum, bel);}nb--;}int result = query(q[i].c, val, sum, bel);ans[result - 1]++;}} catch(const exception& e) {cerr << "Error during processing: " << e.what() << endl;return 1;}// 输出结果copy_n(ans.begin(), n, ostream_iterator<int>(cout, "\n"));return 0;}``````21 行未改动int val, key, siz;int sum, mx, lx, rx;int tag;bool rev, same;// 新增的统计信息int mn; // 最小值int gcd_val; // 最大公约数int xor_sum; // 异或和3 行未改动int second_max; // 第二大值int second_min; // 第二小值int lazy_add; // 区间加法的懒标记bool has_add; // 是否有加法标记// 哈希相关(用于序列比对)uint64_t hash_val; // 哈希值uint64_t hash_rev; // 反转哈希值// 统计信息int sum_square; // 平方和int sum_cube; // 立方和int sum_abs; // 绝对值和// 区间最值位置int max_pos; // 最大值位置int min_pos; // 最小值位置// 优先级队列(用于维护区间最值)priority_queue<int> max_heap;priority_queue<int, vector<int>, greater<int>> min_heap;// 区间合并信息int inc_len; // 最长连续递增长度int dec_len; // 最长连续递减长度int l_val, r_val; // 区间最左和最右的值bool is_sorted_inc; // 是否升序bool is_sorted_dec; // 是否降序Node() {l = r = 0;val = key = siz = 0;sum = mx = lx = rx = 0;67 行未改动tr[p].l = tr[p].r = 0;tr[p].val = x;tr[p].key = rand();tr[p].siz = 1;// 基础信息tr[p].sum = x;tr[p].mx = x;tr[p].mn = x;4 行未改动tr[p].cnt_negative = (x < 0) ? 1 : 0;tr[p].max_abs = abs(x);tr[p].second_max = -INF;tr[p].second_min = INF;// 哈希信息tr[p].hash_val = get_hash(x, 0);tr[p].hash_rev = get_hash(x, 0);// 平方和立方tr[p].sum_square = x * x;tr[p].sum_cube = x * x * x;tr[p].sum_abs = abs(x);// 位置信息tr[p].max_pos = p;tr[p].min_pos = p;// 更新堆tr[p].max_heap.push(x);tr[p].min_heap.push(x);// 区间属性tr[p].l_val = tr[p].r_val = x;tr[p].inc_len = tr[p].dec_len = 1;tr[p].is_sorted_inc = tr[p].is_sorted_dec = true;// 懒标记tr[p].same = false;tr[p].rev = false;tr[p].has_add = false;tr[p].lazy_add = 0;tr[p].tag = 0;return p;}// 增强版的pushupvoid pushup(int p) {if(!p) return;tr[p].siz = 1;tr[p].sum = tr[p].val;tr[p].sum_square = tr[p].val * tr[p].val;tr[p].sum_cube = tr[p].val * tr[p].val * tr[p].val;7 行未改动tr[p].max_abs = abs(tr[p].val);tr[p].l_val = tr[p].r_val = tr[p].val;tr[p].inc_len = tr[p].dec_len = 1;tr[p].is_sorted_inc = tr[p].is_sorted_dec = true;// 清空堆并重新加入while(!tr[p].max_heap.empty()) tr[p].max_heap.pop();while(!tr[p].min_heap.empty()) tr[p].min_heap.pop();tr[p].max_heap.push(tr[p].val);tr[p].min_heap.push(tr[p].val);tr[p].second_max = -INF;tr[p].second_min = INF;// 合并左子树if(tr[p].l) {int lc = tr[p].l;tr[p].siz += tr[lc].siz;5 行未改动tr[p].gcd_val = gcd(tr[p].gcd_val, tr[lc].gcd_val);tr[p].cnt_positive += tr[lc].cnt_positive;tr[p].cnt_negative += tr[lc].cnt_negative;tr[p].max_abs = max(tr[p].max_abs, tr[lc].max_abs);// 合并堆priority_queue<int> temp_max = tr[lc].max_heap;while(!temp_max.empty()) {tr[p].max_heap.push(temp_max.top());3 行未改动while(!temp_min.empty()) {tr[p].min_heap.push(temp_min.top());temp_min.pop();}// 更新最值if(tr[lc].mx > tr[p].mx) {tr[p].second_max = tr[p].mx;tr[p].mx = tr[lc].mx;tr[p].max_pos = tr[lc].max_pos;} else if(tr[lc].mx < tr[p].mx) {update_second_values(p, tr[lc].mx);}if(tr[lc].mn < tr[p].mn) {tr[p].second_min = tr[p].mn;tr[p].mn = tr[lc].mn;tr[p].min_pos = tr[lc].min_pos;} else if(tr[lc].mn > tr[p].mn) {update_second_values(p, tr[lc].mn);}// 更新左右边界值tr[p].l_val = tr[lc].l_val;// 更新区间属性if(tr[lc].is_sorted_inc && tr[lc].r_val <= tr[p].val) {tr[p].is_sorted_inc = tr[lc].is_sorted_inc;} else {tr[p].is_sorted_inc = false;}if(tr[lc].is_sorted_dec && tr[lc].r_val >= tr[p].val) {tr[p].is_sorted_dec = tr[lc].is_sorted_dec;} else {tr[p].is_sorted_dec = false;}tr[p].inc_len = max(tr[lc].inc_len,(tr[lc].r_val <= tr[p].val ? tr[lc].inc_len + 1 : 1LL));tr[p].dec_len = max(tr[lc].dec_len,(tr[lc].r_val >= tr[p].val ? tr[lc].dec_len + 1 : 1LL));}// 合并右子树if(tr[p].r) {int rc = tr[p].r;tr[p].siz += tr[rc].siz;5 行未改动tr[p].gcd_val = gcd(tr[p].gcd_val, tr[rc].gcd_val);tr[p].cnt_positive += tr[rc].cnt_positive;tr[p].cnt_negative += tr[rc].cnt_negative;tr[p].max_abs = max(tr[p].max_abs, tr[rc].max_abs);// 合并堆priority_queue<int> temp_max = tr[rc].max_heap;while(!temp_max.empty()) {tr[p].max_heap.push(temp_max.top());3 行未改动while(!temp_min.empty()) {tr[p].min_heap.push(temp_min.top());temp_min.pop();}// 更新最值if(tr[rc].mx > tr[p].mx) {tr[p].second_max = tr[p].mx;tr[p].mx = tr[rc].mx;tr[p].max_pos = tr[rc].max_pos;} else if(tr[rc].mx < tr[p].mx) {update_second_values(p, tr[rc].mx);}if(tr[rc].mn < tr[p].mn) {tr[p].second_min = tr[p].mn;tr[p].mn = tr[rc].mn;tr[p].min_pos = tr[rc].min_pos;} else if(tr[rc].mn > tr[p].mn) {update_second_values(p, tr[rc].mn);}// 更新右边界值tr[p].r_val = tr[rc].r_val;// 更新区间属性if(tr[p].is_sorted_inc && tr[p].val <= tr[rc].l_val && tr[rc].is_sorted_inc) {tr[p].is_sorted_inc = true;} else {tr[p].is_sorted_inc = false;}if(tr[p].is_sorted_dec && tr[p].val >= tr[rc].l_val && tr[rc].is_sorted_dec) {tr[p].is_sorted_dec = true;} else {tr[p].is_sorted_dec = false;}int left_inc = tr[p].inc_len;int right_inc = tr[rc].inc_len;if(tr[p].val <= tr[rc].l_val) {tr[p].inc_len = max(tr[p].inc_len, left_inc + (right_inc > 0 ? 1 : 0));}int left_dec = tr[p].dec_len;int right_dec = tr[rc].dec_len;if(tr[p].val >= tr[rc].l_val) {tr[p].dec_len = max(tr[p].dec_len, left_dec + (right_dec > 0 ? 1 : 0));}}// 计算 lx, rx, mx(原有逻辑,但增强)if(tr[p].l && tr[p].r) {tr[p].lx = max(tr[tr[p].l].lx,tr[tr[p].l].sum + tr[p].val + max(0LL, tr[tr[p].r].lx));tr[p].rx = max(tr[tr[p].r].rx,tr[tr[p].r].sum + tr[p].val + max(0LL, tr[tr[p].l].rx));tr[p].mx = max(max(tr[tr[p].l].mx, tr[tr[p].r].mx),max(tr[tr[p].l].rx, 0LL) + tr[p].val + max(tr[tr[p].r].lx, 0LL));}10 行未改动else {tr[p].lx = tr[p].rx = max(0LL, tr[p].val);tr[p].mx = tr[p].val;}// 更新哈希值int left_size = tr[tr[p].l].siz;tr[p].hash_val = tr[tr[p].l].hash_val +get_hash(tr[p].val, left_size) * pow_base[left_size] +tr[tr[p].r].hash_val * pow_base[left_size + 1];tr[p].hash_rev = tr[tr[p].r].hash_rev * pow_base[left_size + 1] +get_hash(tr[p].val, tr[tr[p].r].siz) * pow_base[tr[tr[p].r].siz] +2 行未改动// 增强版的区间赋值void pushsame(int p, int c) {if(!p) return;tr[p].val = c;tr[p].sum = c * tr[p].siz;tr[p].sum_square = c * c * tr[p].siz;tr[p].sum_cube = c * c * c * tr[p].siz;8 行未改动tr[p].mx = max(c, tr[p].sum);tr[p].l_val = tr[p].r_val = c;tr[p].inc_len = tr[p].dec_len = tr[p].siz;tr[p].is_sorted_inc = tr[p].is_sorted_dec = true;// 更新堆while(!tr[p].max_heap.empty()) tr[p].max_heap.pop();while(!tr[p].min_heap.empty()) tr[p].min_heap.pop();tr[p].max_heap.push(c);tr[p].min_heap.push(c);tr[p].second_max = -INF;tr[p].second_min = INF;// 更新哈希for(int i = 0; i < tr[p].siz; i++) {tr[p].hash_val += get_hash(c, i);tr[p].hash_rev += get_hash(c, tr[p].siz - 1 - i);}tr[p].same = true;tr[p].tag = c;tr[p].has_add = false; // 区间赋值会覆盖加法标记tr[p].lazy_add = 0;}// 新增:区间加法void pushadd(int p, int c) {if(!p) return;if(tr[p].same) {// 如果已经有区间赋值标记,直接修改赋值标记pushsame(p, tr[p].tag + c);return;}tr[p].val += c;tr[p].sum += c * tr[p].siz;tr[p].sum_square += 2 * c * (tr[p].sum - c * tr[p].siz) + c * c * tr[p].siz;tr[p].sum_cube += 3 * c * tr[p].sum_square + 3 * c * c * (tr[p].sum - c * tr[p].siz) + c * c * c * tr[p].siz;5 行未改动tr[p].rx = max(0LL, tr[p].rx + c);tr[p].mx += c;tr[p].l_val += c;tr[p].r_val += c;// 更新堆priority_queue<int> new_max;priority_queue<int, vector<int>, greater<int>> new_min;while(!tr[p].max_heap.empty()) {5 行未改动tr[p].min_heap.pop();}tr[p].max_heap = new_max;tr[p].min_heap = new_min;if(tr[p].second_max != -INF) tr[p].second_max += c;if(tr[p].second_min != INF) tr[p].second_min += c;tr[p].has_add = true;tr[p].lazy_add += c;}9 行未改动// 增强版的pushdownvoid pushdown(int p) {if(!p) return;if(tr[p].has_add) {if(tr[p].l) pushadd(tr[p].l, tr[p].lazy_add);if(tr[p].r) pushadd(tr[p].r, tr[p].lazy_add);tr[p].has_add = false;tr[p].lazy_add = 0;}if(tr[p].same) {if(tr[p].l) pushsame(tr[p].l, tr[p].tag);if(tr[p].r) pushsame(tr[p].r, tr[p].tag);tr[p].same = false;}if(tr[p].rev) {if(tr[p].l) pushrev(tr[p].l);if(tr[p].r) pushrev(tr[p].r);tr[p].rev = false;4 行未改动if(!p) {x = y = 0;return;}pushdown(p);int leftsize = tr[tr[p].l].siz;if(leftsize >= k) {y = p;split(tr[p].l, k, x, tr[y].l);} else {x = p;split(tr[p].r, k - leftsize - 1, tr[x].r, y);}pushup(p);}int merge(int x, int y) {if(!x || !y) return x + y;if(tr[x].key > tr[y].key) {pushdown(x);tr[x].r = merge(tr[x].r, y);pushup(x);28 行未改动if(l > r) return;int len = r - l + 1;k = ((k % len) + len) % len;if(k == 0) return;int x, y, z, w;split(root, l - 1, x, y);split(y, len, y, z);split(y, len - k, y, w);3 行未改动// 新增功能2:获取区间中位数int get_median(int p) {if(!p) return 0;pushdown(p);// 使用对顶堆思想,这里简化处理:排序后取中间vector<int> vals;function<void(int)> dfs = [&](int u) {if(!u) return;2 行未改动vals.push_back(tr[u].val);dfs(tr[u].r);};dfs(p);sort(vals.begin(), vals.end());return vals[vals.size() / 2];}13 行未改动// 新增功能4:判断两个区间是否相等(通过哈希)bool interval_equal(int root, int l1, int r1, int l2, int r2) {if(r1 - l1 != r2 - l2) return false;int len = r1 - l1 + 1;int a, b, c, d, e, f;split(root, l1 - 1, a, b);split(b, len, b, c);uint64_t hash1 = tr[b].hash_val;root = merge(merge(a, b), c);split(root, l2 - 1, d, e);split(e, len, e, f);uint64_t hash2 = tr[e].hash_val;root = merge(merge(d, e), f);return hash1 == hash2;}// 新增功能5:区间离散化6 行未改动vals.push_back(tr[u].val);dfs(tr[u].r);};dfs(p);vector<int> sorted = vals;sort(sorted.begin(), sorted.end());sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());map<int, int> mp;for(int i = 0; i < sorted.size(); i++) {mp[sorted[i]] = i + 1;}vector<int> res;for(int v : vals) {res.push_back(mp[v]);}5 行未改动cin.tie(0);cout.tie(0);srand(time(0));init_pow();int n, m;cin >> n >> m;for(int i = 1; i <= n; i++) {cin >> a[i];}root = build(1, n);string op;while(m--) {cin >> op;operation_count++;history.push_back(operation_count);if(op == "INSERT") {int pos, tot;cin >> pos >> tot;for(int i = 1; i <= tot; i++) {cin >> a[i];value_set.insert(a[i]);freq_map[a[i]]++;}int x, y;split(root, pos, x, y);int z = build(1, tot);root = merge(merge(x, z), y);cout << "Inserted " << tot << " elements at position " << pos << endl;}else if(op == "DELETE") {int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);// 更新频率统计function<void(int)> update_freq = [&](int u) {if(!u) return;update_freq(tr[u].l);3 行未改动}update_freq(tr[u].r);};update_freq(y);del(y);root = merge(x, z);cout << "Deleted " << tot << " elements from position " << pos << endl;}else if(op == "MAKE-SAME") {int pos, tot, c;cin >> pos >> tot >> c;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);pushsame(y, c);root = merge(merge(x, y), z);cout << "Set " << tot << " elements to " << c << endl;}else if(op == "REVERSE") {int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);pushrev(y);root = merge(merge(x, y), z);cout << "Reversed " << tot << " elements from position " << pos << endl;}else if(op == "GET-SUM") {int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "Sum: " << tr[y].sum << endl;4 行未改动}else if(op == "ADD") { // 新增:区间加法int pos, tot, c;cin >> pos >> tot >> c;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);pushadd(y, c);root = merge(merge(x, y), z);cout << "Added " << c << " to " << tot << " elements" << endl;}else if(op == "MIN") { // 新增:查询最小值int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "Minimum: " << tr[y].mn << endl;root = merge(merge(x, y), z);}else if(op == "GCD") { // 新增:查询最大公约数int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "GCD: " << tr[y].gcd_val << endl;root = merge(merge(x, y), z);}else if(op == "XOR") { // 新增:查询异或和int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "XOR: " << tr[y].xor_sum << endl;root = merge(merge(x, y), z);}else if(op == "STATS") { // 新增:统计信息int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "=== Statistics ===" << endl;cout << "Size: " << tr[y].siz << endl;cout << "Sum: " << tr[y].sum << endl;cout << "Sum of squares: " << tr[y].sum_square << endl;9 行未改动cout << "Longest increasing sequence: " << tr[y].inc_len << endl;cout << "Longest decreasing sequence: " << tr[y].dec_len << endl;cout << "Is sorted ascending: " << (tr[y].is_sorted_inc ? "Yes" : "No") << endl;cout << "Is sorted descending: " << (tr[y].is_sorted_dec ? "Yes" : "No") << endl;root = merge(merge(x, y), z);}else if(op == "MEDIAN") { // 新增:查询中位数int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);cout << "Median: " << get_median(y) << endl;root = merge(merge(x, y), z);}else if(op == "MODE") { // 新增:查询众数int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);auto freq = get_mode(y);int mode_val = 0, max_freq = 0;for(auto &[val, cnt] : freq) {if(cnt > max_freq) {max_freq = cnt;mode_val = val;}}cout << "Mode: " << mode_val << " (appears " << max_freq << " times)" << endl;root = merge(merge(x, y), z);}else if(op == "ROTATE") { // 新增:区间旋转int pos, tot, k;9 行未改动}else if(op == "DISCRETE") { // 新增:区间离散化int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);auto discrete = interval_discrete(y);cout << "Discretized values: ";for(int v : discrete) {cout << v << " ";}cout << endl;root = merge(merge(x, y), z);}else if(op == "UNIQUE") { // 新增:统计不同元素个数int pos, tot;cin >> pos >> tot;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);auto freq = get_mode(y);cout << "Number of distinct elements: " << freq.size() << endl;root = merge(merge(x, y), z);}else if(op == "TOP-K") { // 新增:查询前k大元素int pos, tot, k;cin >> pos >> tot >> k;int x, y, z;split(root, pos - 1, x, y);split(y, tot, y, z);vector<int> vals;function<void(int)> dfs = [&](int u) {if(!u) return;pushdown(u);dfs(tr[u].l);vals.push_back(tr[u].val);dfs(tr[u].r);};dfs(y);sort(vals.rbegin(), vals.rend());cout << "Top " << k << " elements: ";for(int i = 0; i < min(k, (int)vals.size()); i++) {cout << vals[i] << " ";}cout << endl;root = merge(merge(x, y), z);}}// 输出操作统计cout << "\n=== Operation Statistics ===" << endl;cout << "Total operations: " << operation_count << endl;cout << "Current tree size: " << tr[root].siz << endl;cout << "Nodes in use: " << cnt << endl;cout << "Nodes in pool: " << top << endl;cout << "Distinct values: " << value_set.size() << endl;return 0;}```35 行未改动int id;int timestamp;int priority;mutable int version;node(int v = 0, int i = 0, int t = 0, int p = 0, int ver = 0): val(v), id(i), timestamp(t), priority(p), version(ver) {}bool operator<(const node &other) const {if (val != other.val) return val < other.val;if (priority != other.priority) return priority < other.priority;if (timestamp != other.timestamp) return timestamp < other.timestamp;return id > other.id;}bool operator==(const node &other) const {return id == other.id && version == other.version;}};13 行未改动heap_base backup_heap;unordered_map<int, int> metadata;int heap_id;mutable int modification_count;extended_heap(int id = 0) : heap_id(id), modification_count(0) {}bool empty() const {return main_heap.empty() && backup_heap.empty();}size_t size() const {return main_heap.size() + backup_heap.size();}void push(const node& n) {main_heap.push(n);modification_count++;}node top() const {if (!main_heap.empty()) {node top_main = main_heap.top();if (!backup_heap.empty()) {3 行未改动return top_main;}return backup_heap.top();}void pop() {if (!main_heap.empty() && !backup_heap.empty()) {if (main_heap.top() < backup_heap.top()) {backup_heap.pop();6 行未改动backup_heap.pop();}modification_count++;}void merge(extended_heap& other) {main_heap.join(other.main_heap);backup_heap.join(other.backup_heap);modification_count += other.modification_count;23 行未改动vector<heap_base::point_iterator> pos;vector<extended_heap> heaps;vector<operation_log> op_log;int current_time;// Advanced find with path compression and union by sizeint find(int x) {if (fa[x] != x) {int root = find(fa[x]);// Path halving optimizationfa[x] = root;}return fa[x];}// Generate random priority for nodesint random_priority() {return uniform_int_distribution<int>(0, MOD)(rng);}// Validate operation before executionbool validate_operation(int op, int A, int B = -1) {if (A < 1 || A > n) return false;if (op == 4 && (B < 1 || B > n)) return false;if (op == 2 && del[A]) return false;return true;}public:GensokyoSolver() : current_time(0) {}void solve() {cin >> T >> W >> K;while (T--) {initialize_test_case();process_operations();output_results();cleanup();}}private:void initialize_test_case() {cin >> n >> m;a.resize(n + 1);fa.resize(n + 1);del.resize(n + 1);pos.resize(n + 1);heaps.clear();heaps.reserve(n + 1);op_log.clear();for (int i = 1; i <= n; i++) {cin >> a[i];fa[i] = i;del[i] = false;heaps.emplace_back(i);int priority = random_priority();pos[i] = heaps[i-1].main_heap.push({a[i], i, current_time++, priority, 0});}}void process_operations() {for (int i = 1; i <= m; i++) {int op;cin >> op;if (op == 1) {handle_undo();} else if (op == 2) {int A;21 行未改动handle_statistics();}}}void handle_delete(int A) {operation_log log;log.type = 2;log.arg1 = A;log.timestamp = current_time++;if (!del[A]) {del[A] = true;int pa = find(A);if (!heaps[pa-1].main_heap.empty()) {node current = *pos[A];log.old_state = current;log.new_state = node(0, A, current_time, 0, current.version + 1);heaps[pa-1].main_heap.erase(pos[A]);heaps[pa-1].backup_heap.push(log.new_state);op_log.push_back(log);}}}void handle_modify(int A, int B) {int pa = find(A);if (!heaps[pa-1].empty()) {node tp = heaps[pa-1].top();if (!del[tp.id]) {operation_log log;log.type = 3;log.arg1 = tp.id;log.arg2 = B;log.timestamp = current_time++;log.old_state = tp;int nv = tp.val - B;if (nv < 0) nv = 0;if (nv == 0) {del[tp.id] = true;heaps[pa-1].main_heap.erase(pos[tp.id]);log.new_state = node(0, tp.id, current_time, 0, tp.version + 1);} else {node new_node = {nv, tp.id, current_time, tp.priority, tp.version + 1};heaps[pa-1].main_heap.modify(pos[tp.id], new_node);log.new_state = new_node;}op_log.push_back(log);}}}void handle_merge(int A, int B) {int pa = find(A), pb = find(B);if (pa != pb) {operation_log log;log.type = 4;log.arg1 = pa;log.arg2 = pb;log.timestamp = current_time++;// Union by size with heap size considerationif (heaps[pa-1].size() < heaps[pb-1].size()) {swap(pa, pb);}heaps[pa-1].merge(heaps[pb-1]);fa[pb] = pa;op_log.push_back(log);}}void handle_undo() {if (!op_log.empty()) {operation_log last_op = op_log.back();op_log.pop_back();// Revert the last operationif (last_op.type == 2 || last_op.type == 3) {int id = (last_op.type == 2) ? last_op.arg1 : last_op.arg2;int pa = find(id);// Restore old stateif (last_op.old_state.val > 0) {del[id] = false;node restored = last_op.old_state;7 行未改动cout << "Undo of merge operation logged\n";}}}void handle_query(int A) {int pa = find(A);if (!heaps[pa-1].empty()) {node top_node = heaps[pa-1].top();cout << "Query result: " << top_node.val << " (from node " << top_node.id << ")\n";} else {cout << "Query result: Heap is empty\n";}}void handle_statistics() {unordered_map<int, int> heap_sizes;unordered_map<int, int> heap_modifications;for (int i = 1; i <= n; i++) {int root = find(i);heap_sizes[root]++;heap_modifications[root] += heaps[root-1].modification_count;}cout << "Statistics:\n";cout << "Total heaps: " << heap_sizes.size() << endl;cout << "Total operations: " << op_log.size() << endl;int max_heap = 0, max_heap_id = 0;for (const auto& [id, size] : heap_sizes) {if (size > max_heap) {max_heap = size;max_heap_id = id;}}if (max_heap_id > 0) {cout << "Largest heap ID: " << max_heap_id<< " (size: " << max_heap<< ", modifications: " << heap_modifications[max_heap_id] << ")\n";}}void output_results() {int tot = 0;vector<int> mx;vector<bool> vis(n + 1, false);// Calculate results with advanced statisticsvector<pair<int, int>> heap_maxima;for (int i = 1; i <= n; i++) {int rt = find(i);if (!vis[rt]) {vis[rt] = true;6 行未改动}}}}// Apply W-based modifications with additional checksif (W == 2 && !mx.empty()) {int me = *max_element(mx.begin(), mx.end());tot -= me;// Log the adjustmentcout << "W=2 adjustment: removed max value " << me << endl;} else if (W == 3 && !mx.empty()) {int me = *max_element(mx.begin(), mx.end());tot += me;// Log the adjustmentcout << "W=3 adjustment: added max value " << me << endl;}// Output final result with classificationif (tot == 0) {cout << "Gensokyo ";} else if (tot > K) {cout << "Hell ";} else {cout << "Heaven ";}cout << tot << "\n";// Additional debug information if neededif (W > 1) {cout << "Heap maxima: ";for (const auto& [id, val] : heap_maxima) {cout << "(" << id << ":" << val << ") ";}cout << endl;}}void cleanup() {for (int i = 1; i <= n; i++) {if (fa[i] == i) {while (!heaps[i-1].main_heap.empty()) {3 行未改动heaps[i-1].backup_heap.pop();}}}// Clear all containersa.clear();fa.clear();del.clear();6 行未改动signed main() {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);GensokyoSolver solver;solver.solve();return 0;}```33 行未改动const int N = 300005;const int INF = 1e18;const int MOD = 1e9 + 7;const double PI = acos(-1.0);template<typename T>T max3(T a, T b, T c) { return max({a, b, c}); }template<typename T>T min3(T a, T b, T c) { return min({a, b, c}); }}}6 行未改动// 基础属性int id, val, dis;int ls, rs;int add, mul;// 扩展属性int height; // 树高int size; // 子树大小int sum; // 子树和int max_val, min_val; // 最大值最小值int lazy_xor; // 异或懒标记bool rev; // 翻转标记// 构造函数node() : id(0), val(0), dis(0), ls(0), rs(0), add(0), mul(1),height(0), size(0), sum(0), max_val(0), min_val(0),lazy_xor(0), rev(false) {}// 带参构造函数node(int _id, int _val) : id(_id), val(_val), dis(1), ls(0), rs(0),add(0), mul(1), height(1), size(1),sum(_val), max_val(_val), min_val(_val),lazy_xor(0), rev(false) {}};4 行未改动int h[N], fa[N], a[N], v[N], s[N], c[N];int dead[N], dep[N], diedep[N];int root[N];vector<int> e[N];// 额外数据结构set<int> alive_knights[N]; // 每个城池存活的骑士map<int, int> knight_power; // 骑士力量映射queue<int> bfs_queue; // BFS队列stack<int> dfs_stack; // DFS栈priority_queue<pair<int, int>> pq; // 优先队列// 统计信息int total_operations = 0;int merge_count = 0;int pushdown_count = 0;6 行未改动// 随机数生成器random_device rd;mt19937 gen(rd());uniform_int_distribution<int> dis(1, 1000000);int random() { return dis(gen); }// 哈希函数struct Hash {size_t operator()(const pair<int, int>& p) const {return p.first * 131 + p.second;}};// 计时器类class Timer {private:chrono::high_resolution_clock::time_point start_time;4 行未改动auto end = chrono::high_resolution_clock::now();return chrono::duration<double>(end - start_time).count();}};// 调试输出(仅在DEBUG模式下)#ifdef DEBUG#define debug(x) cerr << #x << " = " << x << endl#else15 行未改动if(u == 0) {return;}pushdown_count++;// 处理基础懒标记if(tr[u].ls) {// 乘法标记tr[tr[u].ls].val = tr[tr[u].ls].val * tr[u].mul + tr[u].add;tr[tr[u].ls].add = tr[tr[u].ls].add * tr[u].mul + tr[u].add;tr[tr[u].ls].mul *= tr[u].mul;// 更新统计信息tr[tr[u].ls].sum = tr[tr[u].ls].sum * tr[u].mul + tr[u].add * tr[tr[u].ls].size;tr[tr[u].ls].max_val = tr[tr[u].ls].max_val * tr[u].mul + tr[u].add;tr[tr[u].ls].min_val = tr[tr[u].ls].min_val * tr[u].mul + tr[u].add;}if(tr[u].rs) {tr[tr[u].rs].val = tr[tr[u].rs].val * tr[u].mul + tr[u].add;tr[tr[u].rs].add = tr[tr[u].rs].add * tr[u].mul + tr[u].add;tr[tr[u].rs].mul *= tr[u].mul;tr[tr[u].rs].sum = tr[tr[u].rs].sum * tr[u].mul + tr[u].add * tr[tr[u].rs].size;tr[tr[u].rs].max_val = tr[tr[u].rs].max_val * tr[u].mul + tr[u].add;tr[tr[u].rs].min_val = tr[tr[u].rs].min_val * tr[u].mul + tr[u].add;}// 处理异或懒标记if(tr[u].lazy_xor != 0) {if(tr[u].ls) {tr[tr[u].ls].val ^= tr[u].lazy_xor;4 行未改动tr[tr[u].rs].lazy_xor ^= tr[u].lazy_xor;}tr[u].lazy_xor = 0;}// 处理翻转标记if(tr[u].rev) {if(tr[u].ls) {tr[tr[u].ls].rev ^= 1;4 行未改动swap(tr[tr[u].rs].ls, tr[tr[u].rs].rs);}tr[u].rev = false;}tr[u].add = 0;tr[u].mul = 1;}// 更新节点信息void update(int u) {if(u == 0) return;tr[u].size = 1;tr[u].sum = tr[u].val;tr[u].max_val = tr[u].val;tr[u].min_val = tr[u].val;tr[u].height = 1;if(tr[u].ls) {tr[u].size += tr[tr[u].ls].size;tr[u].sum += tr[tr[u].ls].sum;tr[u].max_val = max(tr[u].max_val, tr[tr[u].ls].max_val);tr[u].min_val = min(tr[u].min_val, tr[tr[u].ls].min_val);tr[u].height = max(tr[u].height, tr[tr[u].ls].height + 1);}if(tr[u].rs) {tr[u].size += tr[tr[u].rs].size;tr[u].sum += tr[tr[u].rs].sum;tr[u].max_val = max(tr[u].max_val, tr[tr[u].rs].max_val);6 行未改动int merge(int x, int y) {if(x == 0 || y == 0) {return x + y;}merge_count++;total_operations++;pushdown(x);pushdown(y);// 根据多种条件决定合并顺序if(tr[x].val > tr[y].val) {swap(x, y);} else if(tr[x].val == tr[y].val) {// 如果值相等,根据id和随机数决定if(tr[x].id > tr[y].id || (tr[x].id == tr[y].id && Utils::random() % 2)) {swap(x, y);}}// 随机决定合并到左边还是右边if(Utils::random() % 2) {tr[x].ls = merge(tr[x].ls, y);if(tr[tr[x].ls].dis < tr[tr[x].rs].dis) {4 行未改动if(tr[tr[x].ls].dis < tr[tr[x].rs].dis) {swap(tr[x].ls, tr[x].rs);}}tr[x].dis = min(tr[tr[x].ls].dis, tr[tr[x].rs].dis) + 1;update(x);return x;}// 深度优先搜索(递归版)6 行未改动// 复杂化的DFS(使用栈实现)void dfs(int u) {dfs_stack.push(u);while(!dfs_stack.empty()) {int cur = dfs_stack.top();dfs_stack.pop();// 处理子节点for(auto v : e[cur]) {dfs_stack.push(v);// 合并骑士root[cur] = merge(root[cur], root[v]);}// 处理当前节点的骑士while(root[cur] != 0) {pushdown(root[cur]);if(tr[root[cur]].val < h[cur]) {dead[cur]++;diedep[tr[root[cur]].id] = dep[cur];alive_knights[cur].erase(tr[root[cur]].id);root[cur] = merge(tr[root[cur]].ls, tr[root[cur]].rs);} else {break;}}// 应用城池效果if(cur != 1 && root[cur] != 0) {if(a[cur] == 0) {// 加法10 行未改动tr[root[cur]].sum *= v[cur];tr[root[cur]].max_val *= v[cur];tr[root[cur]].min_val *= v[cur];}// 随机应用一些额外效果if(Utils::random() % 100 < 5) {// 5%的概率应用异或int x = Utils::random() % 100;tr[root[cur]].val ^= x;tr[root[cur]].lazy_xor ^= x;}if(Utils::random() % 100 < 3) {// 3%的概率翻转tr[root[cur]].rev ^= 1;swap(tr[root[cur]].ls, tr[root[cur]].rs);}}// 更新统计信息update(root[cur]);}}// BFS实现(备用)void bfs(int start) {bfs_queue.push(start);while(!bfs_queue.empty()) {int u = bfs_queue.front();bfs_queue.pop();for(auto v : e[u]) {bfs_queue.push(v);}}13 行未改动signed main() {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);// 使用计时器Utils::Timer timer;// 输入优化auto read = []() {int x = 0, f = 1;char ch = getchar();6 行未改动ch = getchar();}return x * f;};n = read();m = read();FOR(i, 1, n) {h[i] = read();}FOR(i, 2, n) {fa[i] = read();a[i] = read();v[i] = read();e[fa[i]].push_back(i);}dfsdep(1, 1);FOR(i, 1, m) {s[i] = read();c[i] = read();int id = newnode(i, s[i]);root[c[i]] = merge(root[c[i]], id);alive_knights[c[i]].insert(i);knight_power[i] = s[i];}// 执行主DFSdfs(1);// 输出结果FOR(i, 1, n) {cout << dead[i] << endl;}FOR(i, 1, m) {cout << dep[c[i]] - diedep[i] << endl;}// 输出统计信息(可选)if(n > 1000) {printStatistics();}debug("Time elapsed: " << timer.elapsed() << "s");return 0;}// 额外的未使用函数,增加代码量7 行未改动b >>= 1;}return res;}// 欧拉函数int phi(int n) {int res = n;for(int i = 2; i * i <= n; i++) {4 行未改动}if(n > 1) res = res / n * (n - 1);return res;}// 扩展欧几里得int exgcd(int a, int b, int &x, int &y) {if(b == 0) {x = 1; y = 0;2 行未改动int g = exgcd(b, a % b, y, x);y -= a / b * x;return g;}// 线段树模板(未使用)class SegmentTree {private:vector<int> tree, lazy;2 行未改动SegmentTree(int _n) : n(_n) {tree.resize(4 * n);lazy.resize(4 * n);}void pushdown(int p) {if(lazy[p]) {tree[p * 2] += lazy[p];tree[p * 2 + 1] += lazy[p];lazy[p * 2] += lazy[p];lazy[p * 2 + 1] += lazy[p];lazy[p] = 0;}}void update(int p, int l, int r, int ql, int qr, int val) {if(ql <= l && r <= qr) {tree[p] += val;lazy[p] += val;4 行未改动if(ql <= mid) update(p * 2, l, mid, ql, qr, val);if(qr > mid) update(p * 2 + 1, mid + 1, r, ql, qr, val);tree[p] = max(tree[p * 2], tree[p * 2 + 1]);}int query(int p, int l, int r, int ql, int qr) {if(ql <= l && r <= qr) return tree[p];pushdown(p);int mid = (l + r) >> 1, res = -INF;4 行未改动};> }``````::::### 48粉:[LoyalSoldier](https://www.luogu.com.cn/user/937774)[ [JSOI2014] 学生选课](https://www.luogu.com.cn/problem/P4835)::::info[]```pythonprint(int(input())-2)`````:::::### 49粉:[ChargedCharge](https://www.luogu.com.cn/user/1051385)[「ROI 2025 Day2」沼泽里的青蛙](https://www.luogu.com.cn/problem/P12506)::::info[]```cpp#include <bits/stdc++.h>using namespace std;#define int long long#define fi first#define se secondconst int N=100005;int n,r;int x[N],y[N],fa[N<<1];map<pair<int,int>,vector<int>> mp;int getdis(int i,int j){int xi=x[i],xj=x[j],yi=y[i],yj=y[j];return (xi-xj)*(xi-xj)+(yi-yj)*(yi-yj)<=r*r;}int find(int x){if(x==fa[x]){return x;}return fa[x]=find(fa[x]);}void merge(int x,int y){fa[find(x)]=find(y+n);fa[find(y)]=find(x+n);}signed main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n>>r;for(int i=1;i<=n;i++){cin>>x[i]>>y[i];mp[{2*x[i]/r,2*y[i]/r}].push_back(i);}for(int i=1;i<=2*n;i++){fa[i]=i;}for(auto it:mp){vector<int> v=it.se;int sz=v.size();for(int i=1;i<sz;i++){merge(v[i-1],v[i]);}if(sz>=3){merge(v[0],v[2]);}if(sz<3){for(int dx=-2;dx<=2;dx++){for(int dy=-2;dy<=2;dy++){if(dx==0 and dy==0){continue;}auto key=make_pair(it.fi.fi+dx,it.fi.se+dy);auto pos=mp.find(key);if(pos!=mp.end()){for(auto p:v){for(auto q:pos->se){if(getdis(p,q)){merge(p,q);}}}}}}ava}}for(int i=1;i<=n;i++){cout<<(char)('0'+(find(i)==find(i+n)));}cout<<"\n";return 0;}```````::::