每多一粉写一道紫
写完之后会让 AI 改代码,防抄袭。(我也不知道对不对)
提示词:
下面是一段C++代码,随便改,尽量复杂一些。
(代码)
(题目)改出来的不是人看的。
字数统计:61910 字符
44粉:Brute_Force
INFO
代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
// 添加各种设计模式和模板元编程
template<typename T, typename U>
struct TypeTraits {
static constexpr bool is_integral = false;
};
template<>
struct TypeTraits<int, void> {
static constexpr bool is_integral = true;
using type = int;
};
// CRTP模式基类
template<typename Derived>
class QueryProcessor {
public:
void process() {
static_cast<Derived*>(this)->process_impl();
}
protected:
~QueryProcessor() = default;
};
// 策略模式
class AddStrategy {
public:
virtual void execute(int x, vector<int>& val, vector<int>& sum) = 0;
virtual ~AddStrategy() = default;
};
class SimpleAddStrategy : public AddStrategy {
public:
void execute(int x, vector<int>& val, vector<int>& sum) override {
++val[x];
++sum[belong[x]];
}
private:
static inline vector<int> belong; // C++17 inline变量
};
class LoggingAddStrategy : public AddStrategy {
public:
void execute(int x, vector<int>& val, vector<int>& sum) override {
cout << "[LOG] Adding element: " << x << endl;
++val[x];
++sum[belong[x]];
}
private:
static inline vector<int> belong;
};
// 工厂模式
template<typename T>
class StrategyFactory {
public:
static unique_ptr<AddStrategy> create() {
if constexpr (is_same_v<T, SimpleAddStrategy>) {
return make_unique<SimpleAddStrategy>();
} else if constexpr (is_same_v<T, LoggingAddStrategy>) {
return make_unique<LoggingAddStrategy>();
} else {
return nullptr;
}
}
};
// 多线程计算框架
class ParallelCalculator {
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) {
int chunk_start = start + i * chunk;
int chunk_end = (i == num_threads - 1) ? end : chunk_start + chunk;
workers.emplace_back([&, chunk_start, chunk_end]() {
for(int j = chunk_start; j < chunk_end; ++j) {
func(j);
}
});
}
for(auto& w : workers) w.join();
workers.clear();
}
};
// 变长模板参数元组展开
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...>{});
}
};
// 内存池分配器
template<typename T>
class PoolAllocator {
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]) {
used[i] = true;
return &pool[i];
}
}
return nullptr;
}
void deallocate(T* ptr) {
size_t index = ptr - pool;
used[index] = false;
}
};
// 优化后的节点结构,使用移动语义和noexcept
struct 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静态成员
};
// 常量表达式优化
constexpr int N = 100005;
constexpr int K = 200005;
constexpr int B1 = 1000;
constexpr int B2 = 300;
int n, k;
array<int, N> a, b, c;
array<int, K> la, ra, lb, rb;
array<int, N> ab, ba, ac, bc;
vector<int> bel(K), val(K), sum(K), ans(N);
vector<Node> q(N);
// RAII资源管理类
template<typename T>
class ResourceGuard {
T* resource;
public:
ResourceGuard(T* res) : resource(res) {}
~ResourceGuard() { delete resource; }
ResourceGuard(const ResourceGuard&) = delete;
ResourceGuard& operator=(const ResourceGuard&) = delete;
};
// 函数式编程风格的添加操作
auto create_adder() {
return [](int x, auto& val, auto& sum, auto& bel) {
val[x]++;
sum[bel[x]]++;
return val[x];
};
}
auto create_remover() {
return [](int x, auto& val, auto& sum, auto& bel) {
val[x]--;
sum[bel[x]]--;
return val[x];
};
}
// 查询函数 - 使用标准算法
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]]++;
int y = lb[b[i]]++;
q[i] = Node(ra[a[i]], rb[b[i]], c[i]);
ab[x] = y;
ba[y] = x;
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) {
na++;
if(ab[na] <= nb) {
adder(ac[na], val, sum, bel);
}
}
while(na > q[i].a) {
if(ab[na] <= nb) {
remover(ac[na], val, sum, bel);
}
na--;
}
while(nb < q[i].b) {
nb++;
if(ba[nb] <= na) {
adder(bc[nb], val, sum, bel);
}
}
while(nb > q[i].b) {
if(ba[nb] <= na) {
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;
}45粉:孟令然2013
INFO
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 500005;
const int INF = 0x3f3f3f3f3f3f3f3f;
// 增加更多结构体成员,支持更多操作
struct Node {
int l, r;
int val, key, siz;
int sum, mx, lx, rx;
int tag;
bool rev, same;
// 新增的统计信息
int mn; // 最小值
int gcd_val; // 最大公约数
int xor_sum; // 异或和
int cnt_positive; // 正数个数
int cnt_negative; // 负数个数
int max_abs; // 最大绝对值
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;
mn = INF;
gcd_val = 0;
xor_sum = 0;
cnt_positive = cnt_negative = 0;
max_abs = 0;
second_max = -INF;
second_min = INF;
lazy_add = 0;
has_add = false;
rev = same = false;
tag = 0;
hash_val = hash_rev = 0;
sum_square = sum_cube = sum_abs = 0;
max_pos = min_pos = 0;
inc_len = dec_len = 1;
l_val = r_val = 0;
is_sorted_inc = is_sorted_dec = true;
}
} tr[N];
// 增加更多全局变量
int nodes[N], root, cnt, top;
int a[N];
uint64_t base = 131; // 哈希基数
uint64_t pow_base[N]; // 预计算的幂
map<int, int> freq_map; // 频率统计
set<int> value_set; // 值集合
vector<int> history; // 操作历史
int operation_count = 0; // 操作计数
// 初始化幂数组
void init_pow() {
pow_base[0] = 1;
for(int i = 1; i < N; i++) {
pow_base[i] = pow_base[i-1] * base;
}
}
// 新增的辅助函数
int gcd(int a, int b) {
while(b) {
int t = b;
b = a % b;
a = t;
}
return abs(a);
}
// 计算哈希值
uint64_t get_hash(int val, int pos) {
return val * pow_base[pos];
}
// 更新第二最值
void update_second_values(int p, int val) {
// 更新第二大值
if(val > tr[p].second_max && val < tr[p].mx) {
tr[p].second_max = val;
}
// 更新第二小值
if(val < tr[p].second_min && val > tr[p].mn) {
tr[p].second_min = val;
}
}
int newnode(int x) {
int p = top ? nodes[top--] : ++cnt;
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;
tr[p].lx = tr[p].rx = max(0LL, x);
tr[p].gcd_val = abs(x);
tr[p].xor_sum = x;
tr[p].cnt_positive = (x > 0) ? 1 : 0;
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;
}
// 增强版的pushup
void 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;
tr[p].sum_abs = abs(tr[p].val);
tr[p].xor_sum = tr[p].val;
tr[p].gcd_val = abs(tr[p].val);
tr[p].mx = tr[p].val;
tr[p].mn = tr[p].val;
tr[p].cnt_positive = (tr[p].val > 0) ? 1 : 0;
tr[p].cnt_negative = (tr[p].val < 0) ? 1 : 0;
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;
tr[p].sum += tr[lc].sum;
tr[p].sum_square += tr[lc].sum_square;
tr[p].sum_cube += tr[lc].sum_cube;
tr[p].sum_abs += tr[lc].sum_abs;
tr[p].xor_sum ^= tr[lc].xor_sum;
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());
temp_max.pop();
}
priority_queue<int, vector<int>, greater<int>> temp_min = tr[lc].min_heap;
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;
tr[p].sum += tr[rc].sum;
tr[p].sum_square += tr[rc].sum_square;
tr[p].sum_cube += tr[rc].sum_cube;
tr[p].sum_abs += tr[rc].sum_abs;
tr[p].xor_sum ^= tr[rc].xor_sum;
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());
temp_max.pop();
}
priority_queue<int, vector<int>, greater<int>> temp_min = tr[rc].min_heap;
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));
}
else if(tr[p].l) {
tr[p].lx = max(tr[tr[p].l].lx, tr[tr[p].l].sum + tr[p].val);
tr[p].rx = max(tr[p].val, tr[tr[p].l].rx + tr[p].val);
tr[p].mx = max(tr[tr[p].l].mx, max(tr[tr[p].l].rx, 0LL) + tr[p].val);
}
else if(tr[p].r) {
tr[p].lx = max(tr[p].val, tr[p].val + tr[tr[p].r].lx);
tr[p].rx = max(tr[tr[p].r].rx, tr[tr[p].r].sum + tr[p].val);
tr[p].mx = max(tr[tr[p].r].mx, tr[p].val + max(tr[tr[p].r].lx, 0LL));
}
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] +
tr[tr[p].l].hash_rev;
}
// 增强版的区间赋值
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;
tr[p].sum_abs = abs(c) * tr[p].siz;
tr[p].xor_sum = (tr[p].siz % 2) ? c : 0;
tr[p].gcd_val = abs(c);
tr[p].mx = tr[p].mn = c;
tr[p].cnt_positive = (c > 0) ? tr[p].siz : 0;
tr[p].cnt_negative = (c < 0) ? tr[p].siz : 0;
tr[p].max_abs = abs(c);
tr[p].lx = tr[p].rx = max(0LL, tr[p].sum);
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;
tr[p].sum_abs = 0; // 需要重新计算,暂时不处理
tr[p].xor_sum ^= (tr[p].siz % 2) ? c : 0;
tr[p].mx += c;
tr[p].mn += c;
tr[p].lx = max(0LL, tr[p].lx + c); // 简化处理
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()) {
new_max.push(tr[p].max_heap.top() + c);
tr[p].max_heap.pop();
}
while(!tr[p].min_heap.empty()) {
new_min.push(tr[p].min_heap.top() + c);
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;
}
void pushrev(int p) {
if(!p) return;
swap(tr[p].l, tr[p].r);
swap(tr[p].lx, tr[p].rx);
swap(tr[p].l_val, tr[p].r_val);
swap(tr[p].is_sorted_inc, tr[p].is_sorted_dec);
swap(tr[p].hash_val, tr[p].hash_rev);
tr[p].rev ^= 1;
}
// 增强版的pushdown
void 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;
}
}
void split(int p, int k, int &x, int &y) {
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);
return x;
} else {
pushdown(y);
tr[y].l = merge(x, tr[y].l);
pushup(y);
return y;
}
}
int build(int l, int r) {
if(l > r) return 0;
int mid = (l + r) >> 1;
int p = newnode(a[mid]);
tr[p].l = build(l, mid - 1);
tr[p].r = build(mid + 1, r);
pushup(p);
return p;
}
void del(int p) {
if(!p) return;
nodes[++top] = p;
if(tr[p].l) del(tr[p].l);
if(tr[p].r) del(tr[p].r);
}
// 新增功能1:区间旋转(把区间内元素循环右移k位)
void rotate_interval(int &root, int l, int r, int k) {
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);
root = merge(merge(merge(x, w), y), z);
}
// 新增功能2:获取区间中位数
int get_median(int p) {
if(!p) return 0;
pushdown(p);
// 使用对顶堆思想,这里简化处理:排序后取中间
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(p);
sort(vals.begin(), vals.end());
return vals[vals.size() / 2];
}
// 新增功能3:区间众数
map<int, int> get_mode(int p) {
map<int, int> freq;
function<void(int)> dfs = [&](int u) {
if(!u) return;
pushdown(u);
dfs(tr[u].l);
freq[tr[u].val]++;
dfs(tr[u].r);
};
dfs(p);
return freq;
}
// 新增功能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:区间离散化
vector<int> interval_discrete(int p) {
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(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]);
}
return res;
}
signed main() {
ios::sync_with_stdio(0);
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);
freq_map[tr[u].val]--;
if(freq_map[tr[u].val] == 0) {
value_set.erase(tr[u].val);
}
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;
root = merge(merge(x, y), z);
}
else if(op == "MAX-SUM") {
cout << "Maximum subarray sum: " << tr[root].mx << endl;
}
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;
cout << "Sum of cubes: " << tr[y].sum_cube << endl;
cout << "Sum of absolute: " << tr[y].sum_abs << endl;
cout << "Positive count: " << tr[y].cnt_positive << endl;
cout << "Negative count: " << tr[y].cnt_negative << endl;
cout << "Maximum: " << tr[y].mx << endl;
cout << "Minimum: " << tr[y].mn << endl;
cout << "Max absolute: " << tr[y].max_abs << endl;
cout << "Second maximum: " << (tr[y].second_max == -INF ? 0 : tr[y].second_max) << endl;
cout << "Second minimum: " << (tr[y].second_min == INF ? 0 : tr[y].second_min) << endl;
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;
cin >> pos >> tot >> k;
rotate_interval(root, pos, pos + tot - 1, k);
cout << "Rotated " << tot << " elements by " << k << " positions" << endl;
}
else if(op == "EQUAL") { // 新增:比较两个区间
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
bool eq = interval_equal(root, l1, r1, l2, r2);
cout << "Intervals are " << (eq ? "equal" : "not equal") << endl;
}
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;
}46粉:hali13
INFO
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <chrono>
#include <random>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define endl '\n'
const int N = 2000005;
const int MOD = 1e9 + 7;
const int INF = 1e18;
// Custom hash function for pairs
struct pair_hash {
template <class T1, class T2>
std::size_t operator() (const std::pair<T1, T2> &pair) const {
return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
}
};
// Advanced node structure with additional metadata
struct node {
int val;
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;
}
};
// Custom comparator for different heap behaviors
template<typename T>
struct custom_heap_cmp {
bool operator()(const T& a, const T& b) const {
return a < b;
}
};
// Multi-layer heap structure
typedef __gnu_pbds::priority_queue<node, custom_heap_cmp<node>, pairing_heap_tag> heap_base;
struct extended_heap {
heap_base main_heap;
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()) {
node top_backup = backup_heap.top();
return (top_main < top_backup) ? top_backup : top_main;
}
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();
} else {
main_heap.pop();
}
} else if (!main_heap.empty()) {
main_heap.pop();
} else if (!backup_heap.empty()) {
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;
heap_id = min(heap_id, other.heap_id);
}
};
// Thread-safe random number generator
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// Persistent data structure for undo operations
struct operation_log {
int type;
int arg1, arg2, arg3;
int timestamp;
node old_state;
node new_state;
};
// Main solution class
class GensokyoSolver {
private:
int T, W, K, n, m;
vector<int> a;
vector<int> fa;
vector<bool> del;
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 size
int find(int x) {
if (fa[x] != x) {
int root = find(fa[x]);
// Path halving optimization
fa[x] = root;
}
return fa[x];
}
// Generate random priority for nodes
int random_priority() {
return uniform_int_distribution<int>(0, MOD)(rng);
}
// Validate operation before execution
bool 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;
cin >> A;
if (validate_operation(op, A)) {
handle_delete(A);
}
} else if (op == 3) {
int A, B;
cin >> A >> B;
if (validate_operation(op, A)) {
handle_modify(A, B);
}
} else if (op == 4) {
int A, B;
cin >> A >> B;
if (validate_operation(op, A, B)) {
handle_merge(A, B);
}
} else if (op == 5) {
int A;
cin >> A;
handle_query(A);
} else if (op == 6) {
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 consideration
if (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 operation
if (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 state
if (last_op.old_state.val > 0) {
del[id] = false;
node restored = last_op.old_state;
restored.version++;
restored.timestamp = current_time++;
heaps[pa-1].main_heap.modify(pos[id], restored);
}
} else if (last_op.type == 4) {
// Complex merge undo would require maintaining additional state
// Simplified version - just log that undo happened
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 statistics
vector<pair<int, int>> heap_maxima;
for (int i = 1; i <= n; i++) {
int rt = find(i);
if (!vis[rt]) {
vis[rt] = true;
if (!heaps[rt-1].empty()) {
int mv = heaps[rt-1].top().val;
if (mv > 0) {
mx.push_back(mv);
tot += mv;
heap_maxima.emplace_back(rt, mv);
}
}
}
}
// Apply W-based modifications with additional checks
if (W == 2 && !mx.empty()) {
int me = *max_element(mx.begin(), mx.end());
tot -= me;
// Log the adjustment
cout << "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 adjustment
cout << "W=3 adjustment: added max value " << me << endl;
}
// Output final result with classification
if (tot == 0) {
cout << "Gensokyo ";
} else if (tot > K) {
cout << "Hell ";
} else {
cout << "Heaven ";
}
cout << tot << "\n";
// Additional debug information if needed
if (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()) {
heaps[i-1].main_heap.pop();
}
while (!heaps[i-1].backup_heap.empty()) {
heaps[i-1].backup_heap.pop();
}
}
}
// Clear all containers
a.clear();
fa.clear();
del.clear();
pos.clear();
heaps.clear();
op_log.clear();
}
};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
GensokyoSolver solver;
solver.solve();
return 0;
}47粉:yjh20130105
INFO
#include <bits/stdc++.h>
using namespace std;
// 超级复杂化版本 - 左偏树骑士远征问题
// 增加了大量无关但有趣的特性
// 使用宏定义实现各种奇技淫巧
#define int long long
#define _CRT_SECURE_NO_WARNINGS
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for(int i = (a); i >= (b); --i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define endl '\n'
#define __builtin_popcountll __popcnt64
// 命名空间嵌套
namespace Global {
namespace Constants {
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}); }
}
}
using namespace Global::Constants;
// 复杂化的节点结构
struct node
{
// 基础属性
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) {}
};
// 全局变量,使用命名空间封装
namespace Data {
node tr[N * 2]; // 扩大数组
int n, m;
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;
}
using namespace Data;
// 工具函数命名空间
namespace Utils {
// 随机数生成器
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;
public:
Timer() { reset(); }
void reset() { start_time = chrono::high_resolution_clock::now(); }
double elapsed() {
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
#else
#define debug(x)
#endif
}
// 节点操作函数
int newnode(int id, int val) {
static int cnt = 0;
cnt++;
tr[cnt] = node(id, val);
debug(cnt << " " << id << " " << val);
return cnt;
}
// 复杂化的pushdown
void pushdown(int u) {
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;
tr[tr[u].ls].lazy_xor ^= tr[u].lazy_xor;
}
if(tr[u].rs) {
tr[tr[u].rs].val ^= tr[u].lazy_xor;
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;
swap(tr[tr[u].ls].ls, tr[tr[u].ls].rs);
}
if(tr[u].rs) {
tr[tr[u].rs].rev ^= 1;
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);
tr[u].min_val = min(tr[u].min_val, tr[tr[u].rs].min_val);
tr[u].height = max(tr[u].height, tr[tr[u].rs].height + 1);
}
}
// 复杂化的合并操作
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) {
swap(tr[x].ls, tr[x].rs);
}
} else {
tr[x].rs = merge(tr[x].rs, y);
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;
}
// 深度优先搜索(递归版)
void dfsdep(int u, int d) {
dep[u] = d;
for(auto v : e[u]) {
dfsdep(v, d + 1);
}
}
// 复杂化的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) {
// 加法
tr[root[cur]].val += v[cur];
tr[root[cur]].add += v[cur];
tr[root[cur]].sum += v[cur] * tr[root[cur]].size;
tr[root[cur]].max_val += v[cur];
tr[root[cur]].min_val += v[cur];
} else {
// 乘法
tr[root[cur]].val *= v[cur];
tr[root[cur]].add *= v[cur];
tr[root[cur]].mul *= v[cur];
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);
}
}
}
// 输出统计信息
void printStatistics() {
cout << "\n=== Statistics ===\n";
cout << "Total operations: " << total_operations << endl;
cout << "Merge count: " << merge_count << endl;
cout << "Pushdown count: " << pushdown_count << endl;
cout << "Memory usage: " << sizeof(tr) / 1024 / 1024 << " MB\n";
cout << "==================\n";
}
// 复杂化的主函数
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();
while(ch < '0' || ch > '9') {
if(ch == '-') f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
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];
}
// 执行主DFS
dfs(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;
}
// 额外的未使用函数,增加代码量
namespace Unused {
// 快速幂
int qpow(int a, int b, int mod = MOD) {
int res = 1;
while(b) {
if(b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
// 欧拉函数
int phi(int n) {
int res = n;
for(int i = 2; i * i <= n; i++) {
if(n % i == 0) {
res = res / i * (i - 1);
while(n % i == 0) n /= i;
}
}
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;
return a;
}
int g = exgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
// 线段树模板(未使用)
class SegmentTree {
private:
vector<int> tree, lazy;
int n;
public:
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;
return;
}
pushdown(p);
int mid = (l + r) >> 1;
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;
if(ql <= mid) res = max(res, query(p * 2, l, mid, ql, qr));
if(qr > mid) res = max(res, query(p * 2 + 1, mid + 1, r, ql, qr));
return res;
}
};
> }