AT_abc442_d 学习笔记

大冤种。

第一眼拿到题目:这不一眼前缀和吗?

结果没看时限&没分析时间复杂度的我:前缀和肯定会 T 飞,需要优化。

怎么优化呢?这不一眼线段树吗,于是我写了 \(100\) 多行线段树。

这里的 \(1\) 操作是让我们交换 \(a_x\)\(a_{x+1}\) 的值,那么这里的 update 函数我们可以这么写:

void update(ll d, ll pl, ll pr, ll p){
	if (pl == pr){
		tree[p] = a[pl];
		return;
	}
	pushdown(p, pl, pr);
	ll mid = (pl + pr) >> 1;
	if (d <= mid)
		if (d + 1 <= mid)//都在左子树
			update(d, pl, mid, ls(p));
		else //一左一右
			update(d, pl, mid, ls(p)), update(d + 1, mid + 1, pr, rs(p));
	else //都在右子树
		update(d, mid + 1, pr, rs(p));
	pushup(p);
}

\(2\) 操作就是个区间和(前缀和)板子,不多讲了,直接上冤种代码。

/**********************************************************
 * Author        : dingziyang888
 * Website       : https://www.luogu.com.cn/problem/
 * Created Time  :
 * FileName      :
 * Warning!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 * 1.MLE?
 * 2.array size enough?
 * 3.long long?
 * 4.overflow long long?
 * 5.multiple task cleaned?
 * 6.freopen?
 * 7.TLE?
 * *******************************************************/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#define DEBUG
#define Ofile(s) freopen(s".in", "r", stdin), freopen (s".out", "w", stdout)
#define Cfile(s) fclose(stdin), fclose(stdout)
#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;

using ll = long long;
using ull = unsigned long long;
using lb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;

constexpr int mod = 998244353;
constexpr int maxk = 200005;

ll n, q, L, R, x, opt;

ll a[maxk], tree[maxk << 2], tag[maxk << 2];

ll ls(ll p){
	return p << 1;
}

ll rs(ll p){
	return p << 1 | 1;
}

void pushup(int p){
	tree[p] = tree[ls(p)] + tree[rs(p)];
}

void build(ll p, ll pl, ll pr){
	tag[p] = 0;
	if (pl == pr){
		tree[p] = a[pl];
		return;
	}
	ll mid = (pl + pr) >> 1;
	build(ls(p), pl, mid);
	build(rs(p), mid + 1, pr);
	pushup(p);
}

void addtag(ll p, ll pl, ll pr, ll d){
	tag[p] += d;
	tree[p] += d * (pr - pl + 1);
}

void pushdown(ll p, ll pl, ll pr){
	if (tag[p]){
		ll mid = (pl + pr) >> 1;
		addtag(ls(p), pl, mid, tag[p]);
		addtag(rs(p), mid + 1, pr, tag[p]);
		tag[p] = 0;
	}
}

ll query(ll L, ll R, ll p, ll pl, ll pr){
	if (L <= pl && pr <= R)
		return tree[p];
	pushdown(p, pl, pr);
	ll mid = (pl + pr) >> 1, res = 0;
	if (L <= mid)
		res += query(L, R, ls(p), pl, mid);
	if (mid < R)
		res += query(L, R, rs(p), mid + 1, pr);
	return res;
}

void update(ll d, ll pl, ll pr, ll p){
	if (pl == pr){
		tree[p] = a[pl];
		return;
	}
	pushdown(p, pl, pr);
	ll mid = (pl + pr) >> 1;
	if (d <= mid)
		if (d + 1 <= mid)
			update(d, pl, mid, ls(p));
		else 
			update(d, pl, mid, ls(p)), update(d + 1, mid + 1, pr, rs(p));
	else 
		update(d, mid + 1, pr, rs(p));
	pushup(p);
}

int main() {
	fast;
	cin >> n >> q;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	build(1, 1, n);
	while (q--){
		cin >> opt;
		if (opt == 1){
			cin >> x;
			swap(a[x], a[x + 1]);
			update(x, 1, n, 1);
			update(x + 1, 1, n, 1);
		}
		else {
			cin >> L >> R;
			cout << query(L, R, 1, 1, n) << endl;
		}
	}
	return 0;
}
posted @ 2026-02-02 19:16  constexpr_ll  阅读(0)  评论(0)    收藏  举报