提供两种思路
一种线段树区间更新
另一种用map维护连续的区间,也是题解的思路
第二种很难写(我太渣,看了别人的代码,发现自己写的太烦了)

#include<iostream>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 6e5+5;
#define MS(x,y) memset(x,y,sizeof(x))
#define MP(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1 int main() {
int n, q;
while(~scanf("%d %d", &n, &q)) {
map<int, int> mp;
mp[-1] = -1;
mp[1] = n;
mp[n+1] = INF; int ans = n;
while(q --) {
int l, r, k; scanf("%d %d %d", &l, &r, &k);
int tt = r;
if(k == 2 && r != n) tt = r+1; auto it = mp.upper_bound(l);
auto it2 = mp.upper_bound(tt);
it --; it2 --; int head1 = it -> first; int len1 = it -> second;
int head2 = it2 -> first; int len2 = it2 -> second; while(1){
int flag = 0;
if(it == it2) flag = 1;
ans -= it->second;
auto tmp = it;
// if(it->first == n+1) while(1);
it ++;
// printf("erase: %d\n", tmp->first);
mp.erase(tmp);
if(flag) break;
}
//for(auto i = mp.begin(); i != mp.end(); ++i) printf("%d:%d ", i->first, i->second); printf("\n");
// printf("%d %d %d %d %d %d\n", head1, len1, head2, len2, l, r); if(k == 1) {
if(head1 + len1 - 1 >= l && l!=head1) {
mp[head1] = l - head1;
ans += l - head1;
} else if(l != head1){
mp[head1] = len1;
ans += len1;
} if(head2 + len2 - 1 > r) {
mp[r+1] = head2 + len2 - 1 - r;
ans += head2 + len2 - 1 - r;
}
} else {
int L = l; int R = max(r, head2 + len2 -1);
if(head1 + len1 < l) {
mp[head1] = len1;
ans += len1;
}else L = head1; mp[L] = R-L+1;
ans += R-L+1;
} // for(auto i = mp.begin(); i != mp.end(); ++i) printf("%d:%d ", i->first, i->second); printf("\n"); printf("%d\n", ans);
}
}
return 0;
}
#include<iostream>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 6e5+5;
#define MS(x,y) memset(x,y,sizeof(x))
#define MP(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1 int Q[N][3];
int has[N * 2]; int cnt; int sum[N << 2];
int lazy[N << 2]; void build(int l, int r, int rt) {
lazy[rt] = 0;
sum[rt] = has[r] - has[l-1];
if(l == r) {
return;
}
int m = (l + r) >> 1;
build(lson); build(rson);
}
void update(int ty, int L, int R, int l, int r, int rt) {
//printf("%d %d\n", l, r);
if(L <= has[l-1]+1 && has[r] <= R) {
lazy[rt] = ty == 1? -1 : 1;
sum[rt] = ty == 1? 0 : has[r] - has[l-1];
return;
} int m = (l + r) >> 1; if(lazy[rt] == 1) {
lazy[rt<<1] = 1; sum[rt<<1] = has[m] - has[l-1];
lazy[rt<<1|1] = 1; sum[rt<<1|1] = has[r] - has[m];
lazy[rt] = 0;
} else if(lazy[rt] == -1){
lazy[rt<<1] = -1; sum[rt<<1] = 0;
lazy[rt<<1|1] = -1; sum[rt<<1|1] = 0;
lazy[rt] = 0;
} if(L <= has[m-1]+1) update(ty, L, R, lson);
if(R > has[m]) update(ty, L, R, rson);
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void debug(int l, int r, int rt) {
printf("%d %d %d\n", l, r, sum[rt]);
if(l == r) return;
int m = (l + r) >> 1;
debug(lson);
debug(rson);
}
int main() {
int n;
while(~scanf("%d", &n)) {
cnt = 0; int q; scanf("%d", &q);
for(int i = 0; i < q; ++i) {
scanf("%d %d %d", &Q[i][0], &Q[i][1], &Q[i][2]);
has[cnt ++] = Q[i][0] - 1;
has[cnt ++] = Q[i][1];
}
has[cnt ++] = 0;
has[cnt ++] = n; sort(has, has+cnt);
cnt = unique(has, has + cnt) - has;
// for(int i = 0; i < cnt; ++i) printf("%d ", has[i]); printf("\n"); build(1, cnt-1, 1);
//` debug(1, cnt-1, 1);
for(int i = 0; i < q; ++i) {
update(Q[i][2], Q[i][0], Q[i][1], 1, cnt-1, 1);
printf("%d\n", sum[1]);
// debug(1, cnt-1, 1);
} }
return 0;
}

Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons的更多相关文章

  1. Educational Codeforces Round 36 (Rated for Div. 2)

    A. Garden time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  2. Educational Codeforces Round 36 (Rated for Div. 2) G. Coprime Arrays

    求a_i 在 [1,k]范围内,gcd(a_1,a_2...,a_n) = 1的a的数组个数. F(x)表示gcd(a_1,a_2,...,a_n) = i的a的个数 f(x)表示gcd(a_1,a_ ...

  3. Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题

    Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题 [Problem Description] ​ 总共两次询 ...

  4. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  5. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  6. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  7. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  8. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

随机推荐

  1. Python基础篇(四)

    Python中的字典类似于Java中的Map,数据以键值对的形式存储. 字典可以用以下的方式使用: >>> phonebook = {"alice":" ...

  2. MOBA 游戏技能系统设计 2.0

    随着游戏开发的完整度提升,技能系统的设计复杂性也越来越高,导致了用模板方式的配置方法和处理方法会导致以下几个问题: 代码冗余 排错困难 配置项冗余 熟悉业务流程时间长 扩展性低 经过我思考决定重写之. ...

  3. bzoj 4870: [Shoi2017]组合数问题 [矩阵乘法优化dp]

    4870: [Shoi2017]组合数问题 题意:求 \[ \sum_{i=0}^{n-1} \binom{nk}{ik+r} \mod p \] \(n \le 10^9, 0\le r < ...

  4. BZOJ 2743: [HEOI2012]采花 [树状数组 | 主席树]

    题意: 查询区间中出现次数$>2$的颜色个数 一眼主席树,区间中$l \le last[i] \le r$的个数减去$l \le last[last[i]] \le r$的个数,搞两颗主席树来做 ...

  5. C#中内嵌资源的读取

    起因 作为一个从Cpper转到C#并且直接从事WPF开发的萌新来说,正式编码过程中碰到了不少问题,一路上磕磕碰碰的.因为软件设计需求上的要求,需要将一些配置文件(XML.INI等)内嵌到程序中,等需要 ...

  6. 自兴人工智能------------python入门基础(2)列表和元祖

    一.通用序列操作: 列表中所有序列都可以进行特定的操作,包括索引(indexing).分片(slicing).序列相加(adding).乘法,成员资格,长度,最小值,最大值,下面会一一介绍这些操作法. ...

  7. Java并发系列[5]----ReentrantLock源码分析

    在Java5.0之前,协调对共享对象的访问可以使用的机制只有synchronized和volatile.我们知道synchronized关键字实现了内置锁,而volatile关键字保证了多线程的内存可 ...

  8. TensorFlow实战之实现自编码器过程

    关于本文说明,已同步本人另外一个博客地址位于http://blog.csdn.net/qq_37608890,详见http://blog.csdn.net/qq_37608890/article/de ...

  9. CSS3动画中的animation-timing-function效果演示

    CSS3动画(animation)属性有如下几个: 属性 值 说明 animation-name name 指定元素要使用的keyframes名称 animation-duration time(ms ...

  10. ABP框架源码学习之授权逻辑

    asp.net core的默认的几种授权方法参考"雨夜朦胧"的系列博客,这里要强调的是asp.net core mvc中的授权和asp.net mvc中的授权不一样,建议先看前面& ...