传送门

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

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

给出动态开点的代码:

/*
* 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. RMAN 'Duplicate From Active Database' Feature in Oracle11g (Doc ID 452868.1)

    RMAN 'Duplicate From Active Database' Feature in Oracle11g (Doc ID 452868.1) APPLIES TO: Oracle Data ...

  2. 利用Flask中的werkzeug.security模块加密

    1.这种加密方式的原理:加密时混入一段"随机"字符串(盐值)再进行哈希加密.即使 密码相同,如果盐值不同,那么哈希值也是不一样的.现在网站开发中主要是运 用这种加密方法. 2.这个 ...

  3. fuse3 编译相关简要记录 与 fuse3 系统调优;

    下面是在使用fuse3 编译bbfs 过程中一些参数,用于备忘: FUSE_CFLAGS="-I/usr/local/include/fuse3" FUSE_LIBS=" ...

  4. 【1期】mysql必知必会

    再有人问你为什么MySQL用B+树做索引,就把这篇文章发给她

  5. 发送get请求接口

    一.简介 python做接口测试,我们需要了解和学习第三方库requests.python内置的urllib模块,也用于访问网络资源,但是使用较麻烦,而且缺少很多实用的高级功能.这里推荐使用reque ...

  6. Mybatis----传入参数parameterType类型详解

    Mybatis----传入参数parameterType类型详解 前言 Mybatis的Mapper文件中的select.insert.update.delete元素中有一个parameterType ...

  7. SpringBoot实现登陆

    1.依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:/ ...

  8. ImportError: unable to find Qt5Core.dll on PATH

    一.实验环境 1.Windows7x32_SP1 2.python3.7.4 3.pyinstaller3.5 二.问题描述 1.一直都是在Windows10x64上使用pyinstaller打包ex ...

  9. 可迭代对象(__iter__()和__next__())

    如果一个类想被用于for ... in循环,类似list或tuple那样,就必须实现一个__iter__()和__next__()方法,该方法返回一个迭代对象 然后,Python的for循环就会不断调 ...

  10. IT兄弟连 Java语法教程 算符运算符

    Java提供了丰富的运算符环境.可以将大部分Java运算符划分为4组:算术运算符.位运算符.关系运算符以及逻辑运算符.Java还定义了一些用于处理某些特定情况的附加运算符.本章将介绍除类型比较运算符i ...