传送门

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

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

给出动态开点的代码:

/*
* 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. mysql5.7 Multiple-Column Indexes 多列索引(二)

    场景一: 复合索引的替代方法,对多列字段拼接做hash,引入一个hashed 字段,对此字段添加索引,可以做到复合索引查询速度快,例: SELECT * FROM tbl_name WHERE has ...

  2. alluxio 安装记录及相关信息

    最近要尝试探究一下alluxio相关的知识,本博客进行对alluxio的安装过程进行备忘: 单例安装过程: https://docs.alluxio.io/os/user/stable/cn/cont ...

  3. Linux自动同步时间

    一.安装时间同步工具 yum -y install ntp 二.同步时间 1.修改时区 cp -y /usr/share/zoneinfo/Asia/Shanghai /etc/localtime v ...

  4. 一、itk在VS2019上面的安装 和例子(HelloWorld)运行

    一.Itk简介 vtk是专门用于医疗图像处理的函数库,类似opencv. 这篇博客主要是讲解安装vtk之后的例子的运行,即如何构建自己的第一个ITK例子 二.Itk安装 Itk安装参考这篇博客: ht ...

  5. win7+vim搭建+verilog HDL IDE

    参考地址:http://www.huangdc.com/421 参考文章为2016年,部分更新贴于文章内了 安装下载vim vim在win下叫gvim,下载地址:https://www.vim.org ...

  6. POI2008 CLO-Toll

    [POI2008]CLO-Toll 题意描述 Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 你要把其中一些road ...

  7. 详解 Redis 应用场景及原理

    本文转自https://blog.csdn.net/niucsd/article/details/50966733,描述了redis实现原理和应用场景,篇幅较长,有意学习redis的同学可耐心阅读. ...

  8. [ Python入门教程 ] Python文件基本操作_shutil模块

    shutil模块是对os模块中文件操作的补充,提供文件和目录的移动.复制.打包.压缩.解压等功能 shutil常用函数   shutil.copyfile(src, dst)   复制文件, 如果ds ...

  9. thinkphp5.1单模块设置

    thinkphp5.1单模块 1. // 是否支持多模块'app_multi_module' => false, // 自动搜索控制器'controller_auto_search' => ...

  10. html公用头部和尾部

    这个方式比较简单,样式和js也有效果,还有object和iframe方式 效果图,可以看出公共的样式对于引入的文件也有效果,在加载完文件后js也是有效果的 index.html header.html ...