传送门

简单的线段树区间修改区间查询,但是因为数据范围过大,所以采用动态开点的方法(注意一下空间问题)。

也可以直接对询问区间的端点离散化然后建树,这种方法时间复杂度和空间复杂度都比较优秀。

给出动态开点的代码:

/*
* 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(线段树)的更多相关文章

  1. 【CodeForces】915 E. Physical Education Lessons 线段树

    [题目]E. Physical Education Lessons [题意]10^9范围的区间覆盖,至多3*10^5次区间询问. [算法]线段树 [题解]每次询问至多增加两段区间,提前括号分段后线段树 ...

  2. CF915E Physical Education Lessons 动态开点线段树

    题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...

  3. Physical Education Lessons CodeForces - 915E (动态开点线段树)

    Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...

  4. Codeforces 915E. Physical Education Lessons(动态开点线段树)

    E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...

  5. 线段树 离散化 E. Infinite Inversions E. Physical Education Lessons

    题目一:E. Infinite Inversions 这个题目没什么思维量,还比较简单,就是离散化要加上每一个值的后面一个值,然后每一个值放进去的不是1 ,而是这个值与下一个点的差值. 因为这个数代表 ...

  6. E. Physical Education Lessons 动态开辟线段树区间更新

    E. Physical Education Lessons time limit per test 1 second memory limit per test 256 megabytes input ...

  7. Codeforces 915E Physical Education Lessons

    原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...

  8. CF915E Physical Education Lessons

    题意: Alex高中毕业了,他现在是大学新生.虽然他学习编程,但他还是要上体育课,这对他来说完全是一个意外.快要期末了,但是不幸的Alex的体育学分还是零蛋! Alex可不希望被开除,他想知道到期末还 ...

  9. Codeforces 915 E Physical Education Lessons

    题目描述 This year Alex has finished school, and now he is a first-year student of Berland State Univers ...

  10. 【题解】Luogu CF915E Physical Education Lessons

    原题传送门:CF915E Physical Education Lessons 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 这道题很简单啊 每个操作就是区间赋值,顺带把总和修 ...

随机推荐

  1. ifconfig|grep eth0|awk '{print $5}' 命令详解

    因需要将linx下获取某个网中的MAC地址,可以使用如下命令获取: ifconfig|grep eth0|awk '{print $5}' ifconfig: 输出linux下所有网口的信息(包括IP ...

  2. MySQL Explain详解 查看mysql语句详情

    在日常工作中,我们会有时会开慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,些时我们常常用到explain这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句有 ...

  3. AcWing 803. 区间合并

    网址 https://www.acwing.com/solution/AcWing/content/1590/ 题目描述给定n个区间[l, r]. 合并所有有交集的区间. 输出合并完成后的区间个数. ...

  4. bzoj5092 分割序列

    题目链接 problem 对于一个长度为n的非负整数序列\(b_1,b_2,...,b_n\),定义这个序列的能量为:\(f(b)=\max\limits_{i=0,1,...,n}(b_1 \oti ...

  5. js实现addClass方法,classList与className有什么区别?

     壹 ❀ 引 Jquery中addClass与removeClass是两个使用高频的方法,对于为dom元素增删class类非常方便:但如果不用JQ,使用js怎么模拟它们呢?本文主要记录下自己的实现思路 ...

  6. Qt Designer布局预览正常,代码调用时所有控件堆在一起

    一.实验环境 1.Windows10x64 2.anaconda4.6.9 + python3.7.1(anaconda集成,不需单独安装) 3.pyinstaller3.5 二.问题描述 1.Qt ...

  7. 实例属性和方法的动态处理(__getattr__)

    正常情况下,当调用类的方法或属性时,如果不存在,就会报错 要避免这个错误,除了可以加上那个要调用但不存在的属性外,Python还有另一个机制,那就是写一个__getattr__()方法,动态返回一个属 ...

  8. python之面向对象设计、编程

    面向对象 一.编程三个范式 1.面向过程编程 2.函数式编程 数学层面的函数 python中的函数编程 3.面向对象编程 二.面向对象设计 1.类:把一类事物共同的特征和共同的动作整合在一起就是类: ...

  9. 获取系统相关信息 (CPU使用率 内存使用率 系统磁盘大小)

    引言 在软件开个过程中,对于软件的稳定性和使用率也是我们需要关注的 .  使用sigar来监控,简单方便!  使用说明:下载sigar jar及配合sigar的dll文件来用,需要将dll文件放到JD ...

  10. idea使用lombok不生效的解决办法

    file-->setting-->plugins点击下方的 browse repositories. 搜索lombok plugin. 安装后,重启. file-->setting- ...