Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons

61 篇文章 0 订阅
27 篇文章 0 订阅

题目链接

大意:初始n天都是工作日,给你q个操作,分别是把l,r变成工作日或者非工作日。让你求出工作日的天数。

思路:n很大,但操作数不大,所以我们要动态开点线段数来解决这个问题,算是裸题了。

记录每个点和左右儿子节点的编号,如果是新的点就新开一个节点。其他的操作都是根普通线段树一样拉
细节见代码:

#include<bits/stdc++.h>
 
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define LL long long
#define SZ(X) X.size()
#define pii pair<int,int>
#define ALL(X) X.begin(),X.end()
 
using namespace std;
 
LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}
LL powmod(LL a, LL b, LL MOD) {LL ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
const int N = 1e7 + 5e6;
int n, q, cnt = 1;
int ls[N], rs[N], t[N];
int s[N];
#define mid (l+r>>1)
void push_down(int o, int l, int r) {
	if (!ls[o])ls[o] = ++cnt;
	if (!rs[o])rs[o] = ++cnt;
	t[ls[o]] = t[o];
	t[rs[o]] = t[o];
	if (t[o] == 2) {
		s[ls[o]] = mid - l + 1;
		s[rs[o]] = r - mid;
		s[o] = 0;
	} else if (t[o] == 1) {
		s[ls[o]] = s[rs[o]] = 0;
		s[o] = 0;
	}
	t[o] = 0;
	return ;
}
void up(int &o, int l, int r, int x, int y, int Q) {
	if (!o) o = ++cnt;
	if (x <= l && y >= r) {
		t[o] = Q;
		if (Q == 2) {
			s[o] = r - l + 1;
		} else s[o] = 0;
		return ;
	}
	if (t[o])push_down(o, l, r);
	if (x <= mid && t[ls[o]] != Q)up(ls[o], l, mid, x, y, Q);
	if (y > mid && t[rs[o]] != Q)up(rs[o], mid + 1, r, x, y, Q);
	if (t[ls[o]] == t[rs[o]])t[o] = t[ls[o]];
	else t[o] = 0;
	s[o] = s[ls[o]] + s[rs[o]];
	return ;
}
int main() {
	// cout << (double)log2(1000000000) * 300000 << endl;
	scanf("%d%d", &n, &q);
	t[1] = 1;
	int root = 1;
	up(root, 1, n, 1, n, 2);
	for (int i = 1; i <= q; i++) {
		int l, r, p;
		scanf("%d%d%d", &l, &r, &p);
		if (p == 1) {
			up(root, 1, n, l, r, 1);
		} else {
			up(root, 1, n, l, r, 2);
		}
		printf("%d\n", s[1] );
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 887是一个程序设计竞赛活动,由Codeforces组织举办。根据引用中的代码,该竞赛的题目要求解决一个序列操作的问题。给定一个长度为n的序列,通过执行一系列操作,使得序列变得非sorted,即非严格递增。具体操作是将序列中[1, i]范围内的数字全部加一,同时将[i+1, n]范围内的数字全部减一。问题要求求解最少需要执行多少次操作才能达到要求。 引用中的代码给出了解决这个问题的实现。代码首先读入序列的长度n和序列a。然后通过判断序列是否已经是非sorted,如果是则直接输出0。接下来,代码遍历序列,求出相邻两个数字的差的最小值。最后,计算出最少需要执行的操作次数,并输出结果。 需要注意的是,引用中的代码只是给出了解决问题的一种实现方式,并不代表Codeforces Round 887的具体题目和解答。要了解该竞赛的具体信息,需要参考Codeforces官方网站或相关资料。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Codeforces Round 887 (Div. 2)](https://blog.csdn.net/qq_36545889/article/details/131905067)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值