P3870 [TJOI2009] 开关(线段树)

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <unordered_map>
#include <cmath>
#include <map>
#include <cctype>
#include <cstdlib>
#include <deque>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))

int n, m;
struct tree {
	int l, r;
	int sum;
	int lz;
} tr[MAXN];
int s[MAXN];

inline void push_down(int q) {
	tr[q << 1].sum = abs((tr[q].lz % 2) * (tr[q << 1].r - tr[q << 1].l + 1) - tr[q << 1].sum);
	tr[q << 1 | 1].sum = abs((tr[q].lz % 2) * (tr[q << 1 | 1].r - tr[q << 1 | 1].l + 1) - tr[q << 1 | 1].sum);
	tr[q << 1].lz += tr[q].lz;
	tr[q << 1 | 1].lz += tr[q].lz;
	tr[q].lz = 0;
}

void build(int q, int l, int r) {
	if (l == r)
		return void(tr[q] = {l, l, 0, 0});
	tr[q] = {l, r, 0, 0};
	int mid = l + r >> 1;
	build(q << 1, l, mid);
	build(q << 1 | 1, mid + 1, r);
}

void update(int q, int l, int r) {
	if (tr[q].l >= l && tr[q].r <= r) {
		tr[q].sum = tr[q].r - tr[q].l + 1 - tr[q].sum;
		tr[q].lz++;
		return;
	}
	push_down(q);
	int mid = tr[q].l + tr[q].r >> 1;
	if (mid >= l)
		update(q << 1, l, r);
	if (mid < r)
		update(q << 1 | 1, l, r);
	tr[q].sum = tr[q << 1].sum + tr[q << 1 | 1].sum;
}

int query(int q, int l, int r) {
	if (tr[q].l >= l && tr[q].r <= r) {
		return tr[q].sum;
	}
	push_down(q);
	int mid = tr[q].l + tr[q].r >> 1;
	int ans = 0;
	if (mid >= l)
		ans = query(q << 1, l, r);
	if (mid < r)
		ans += query(q << 1 | 1, l, r);
	return ans;
}

int main() {
	scanf("%d %d", &n, &m);
	int a, b, c;
	build(1, 1, n);
	while (m--) {
		scanf("%d %d %d", &c, &a, &b);
		if (c == 0)
			update(1, a, b);
		else
			printf("%d\n", query(1, a, b));
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷P1168题目是关于中位数线段树解法的问题。中位数线段树解法可以通过维护两个堆来实现。一个是大根堆,一个是小根堆。每次插入元素时,根据一定的规则来维护这两个堆,使得大根堆的个数在一定情况下比小根堆多1或者相等。大根堆的最后一个元素即为中位数。具体的规则如下: 1. 如果大根堆和小根堆的个数相等,下一次插入的元素一定插入到大根堆。此时判断小根堆的堆顶是否大于当前元素x,如果是,则将小根堆的堆顶元素插入到大根堆,然后将x压入小根堆;否则直接将x压入大根堆。 2. 如果大根堆和小根堆的个数不相等,按照类似的规则进行操作。 通过以上规则,可以实现在每次插入元素时,维护两个堆的平衡,并且保证大根堆的最后一个元素即为中位数。 这种解法的时间复杂度为O(logN),其中N为序列的长度。 #### 引用[.reference_title] - *1* *2* [中位数(洛谷p1168)(堆/树状数组+二分/线段树+二分)](https://blog.csdn.net/qq_45604735/article/details/114382762)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [洛谷 P1168 中位数(权值线段树,离散化)](https://blog.csdn.net/qq_38232157/article/details/127594230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值