【cf915】E. Physical Education Lessons(线段树)
简单的线段树区间修改区间查询,但是因为数据范围过大,所以采用动态开点的方法(注意一下空间问题)。
也可以直接对询问区间的端点离散化然后建树,这种方法时间复杂度和空间复杂度都比较优秀。
给出动态开点的代码:
/*
* Author: heyuhhh
* Created Time: 2019/11/12 19:33:21
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 4e5 + 5;
int n, q;
int tot;
int rt[1];
int lc[N * 40], rc[N * 40], sum[N * 40], lz[N * 40];
void push_up(int o) {
sum[o] = sum[lc[o]] + sum[rc[o]];
}
void push_down(int o, int l, int r) {
if(lz[o] != -1) {
if(!lc[o]) lc[o] = ++tot;
if(!rc[o]) rc[o] = ++tot;
int mid = (l + r) >> 1;
sum[lc[o]] = (mid - l + 1) * lz[o];
lz[lc[o]] = lz[o];
sum[rc[o]] = (r - mid) * lz[o];
lz[rc[o]] = lz[o];
lz[o] = -1;
}
}
void upd(int &o, int l, int r, int L, int R, int v) {
if(!o) o = ++tot;
if(L <= l && r <= R) {
if(v == 0) sum[o] = lz[o] = 0;
else sum[o] = r - l + 1, lz[o] = 1;
return;
}
push_down(o, l, r);
int mid = (l + r) >> 1;
if(L <= mid) upd(lc[o], l, mid, L, R, v);
if(R > mid) upd(rc[o], mid + 1, r, L, R, v);
push_up(o);
}
void run(){
memset(lz, -1, sizeof(lz));
while(q--) {
int l, r, k; cin >> l >> r >> k;
if(k == 1) upd(rt[0], 1, n, l, r, 1);
else upd(rt[0], 1, n, l, r, 0);
int ans = n - sum[rt[0]];
cout << ans << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> q) run();
return 0;
}
【cf915】E. Physical Education Lessons(线段树)的更多相关文章
- 【CodeForces】915 E. Physical Education Lessons 线段树
[题目]E. Physical Education Lessons [题意]10^9范围的区间覆盖,至多3*10^5次区间询问. [算法]线段树 [题解]每次询问至多增加两段区间,提前括号分段后线段树 ...
- CF915E Physical Education Lessons 动态开点线段树
题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...
- Physical Education Lessons CodeForces - 915E (动态开点线段树)
Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...
- Codeforces 915E. Physical Education Lessons(动态开点线段树)
E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...
- 线段树 离散化 E. Infinite Inversions E. Physical Education Lessons
题目一:E. Infinite Inversions 这个题目没什么思维量,还比较简单,就是离散化要加上每一个值的后面一个值,然后每一个值放进去的不是1 ,而是这个值与下一个点的差值. 因为这个数代表 ...
- E. Physical Education Lessons 动态开辟线段树区间更新
E. Physical Education Lessons time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces 915E Physical Education Lessons
原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...
- CF915E Physical Education Lessons
题意: Alex高中毕业了,他现在是大学新生.虽然他学习编程,但他还是要上体育课,这对他来说完全是一个意外.快要期末了,但是不幸的Alex的体育学分还是零蛋! Alex可不希望被开除,他想知道到期末还 ...
- Codeforces 915 E Physical Education Lessons
题目描述 This year Alex has finished school, and now he is a first-year student of Berland State Univers ...
- 【题解】Luogu CF915E Physical Education Lessons
原题传送门:CF915E Physical Education Lessons 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 这道题很简单啊 每个操作就是区间赋值,顺带把总和修 ...
随机推荐
- Python—导入自定义的模块和包(指定路径下的模块和包)
模块路径如下图: import sys sys.path.append(r"E:\project\path") print "===>", sys.arg ...
- CentOS安装docker-compose
一.compose简介 compose是一个定义和运行多容器的docker应用的工具.compose 通过yaml文件配置应用服务,然后仅需一个命令就可以创建和运行所有配置中的服务. 二.compos ...
- 使用STS4新建springboot项目
1.配置maven,自定义setting文件和仓库,一定要用阿里云镜像地址下载依赖,官方太坑了,整了半天都没弄好,原来是下载太慢文件损坏 <mirror> <id>alimav ...
- [C0] 引言(Introduction)
引言(Introduction) 欢迎(Welcome) 机器学习是目前信息技术中最激动人心的方向之一.在这门课中,你将学习到这门技术的前沿,并可以自己实现学习机器学习的算法. 你或许每天都在不知不觉 ...
- webdriver浏览器版本驱动对应以及下载
对于webdriver和各个浏览器的版本的对应,我最近发现浏览器驱动的对应在selenium库的源码里都有提及,路径是:python>site-packages>selenium>w ...
- AtCoder Grand Contest 039
Preface 我发现我现在打AT真的是只会D-Before-- E,F都是抄曲明姐姐的,然后D还是几何画板猜结论做的(证明都是陈指导想的) 看来再这样下去就真的要退役了啊233 A - Connec ...
- java之运算符的优先级
优先级 运算符 结合性 1 () [] 从左往右 2 ! +(正) -(负) ++ -- 从右往左 3 * / % 从左往右 4 << >> >>> 从左往 ...
- python做中学(八)匿名函数lambda的用法
匿名函数,顾名思义即没有名称的函数,和def定义的函数的最大区别在于匿名函数创建后返回函数本身(即匿名函数不需要return来返回值),表达式本身结果就是返回值,而def创建后则赋值给一个变量名,在P ...
- tomcat运行一段时间后报错"Too many open files"
tomcat运行一段时间后报打开太多文件错误:Too many open files 查看当前进程的文件打开数: lsof -n |awk '{print $2}'|sort|uniq -c |so ...
- PHP框架 fastadmin 根据条件判断字段的显示隐藏
首先,因为fastadmin的JS里面字段不支持function函数 里面只能填false或true,不能动态判断显示隐藏, 后面通过看文档发现能在表格初始化的地方判断 如图,就可以实现根据lin ...