51nod 小朋友的笑话
分析:
每次操作把以前没有出现这个数的设为1,有这个数的设为0。首先将当前区间设为1,考虑有set维护这个颜色出现的区间,然后把所有与当前区间相交的拿出来,修改为0。
复杂度?每次操作的线段只会加入到一次set中,从set中取出一次,只会修改一次,然后就合并成大的了,每次操作也只会加入一条线段。
代码:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#include<bitset>
#define pa pair<int,int>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
set< pa > s[N];
int n; struct SegmentTree{
int sum[N << ], tag[N << ], n;
SegmentTree() { memset(tag, -, sizeof(tag)); }
void pushdown(int rt,int len) {
tag[rt << ] = tag[rt], tag[rt << | ] = tag[rt];
sum[rt << ] = tag[rt] * (len - len / );
sum[rt << | ] = tag[rt] * (len / );
tag[rt] = -;
}
void update(int l,int r,int rt,int L,int R,int v) {
if (L > R) return ;
if (L <= l && r <= R) { tag[rt] = v, sum[rt] = v * (r - l + ); return ; }
if (~tag[rt]) pushdown(rt, r - l + );
int mid = (l + r) >> ;
if (L <= mid) update(l, mid, rt << , L, R, v);
if (R > mid) update(mid + , r, rt << | , L, R, v);
sum[rt] = sum[rt << ] + sum[rt << | ];
}
int query(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) return sum[rt];
if (~tag[rt]) pushdown(rt, r - l + );
int mid = (l + r) >> , res = ;
if (L <= mid) res = query(l, mid, rt << , L, R);
if (R > mid) res += query(mid + , r, rt << | , L, R);
return res;
}
}T;
void Insert() {
int x = read(), y = read(), k = read(), l = max(, x - k), r = min(n, x + k), nowl = l, nowr = r;
if (s[y].empty()) {
T.update(, n, , l, r, );
s[y].insert(pa(l, r)); return ;
}
set< pa > :: iterator it = s[y].lower_bound(pa(l, ));
set< pa > :: iterator pre = it; if (it != s[y].begin()) {
pre --;
if (pre->second >= l) it = pre;
}
if (it == s[y].end() || (it->first) > r) {
T.update(, n, , l, r, );
s[y].insert(pa(l, r)); return ;
}
nowl = min(nowl, it->first);
T.update(, n, , l, r, );
while (it != s[y].end() && (it->first) <= r) {
T.update(, n, , max(l, it->first), min(r, it->second), );
nowr = max(nowr, it->second);
pre = it; it ++; s[y].erase(pre);
}
s[y].insert(pa(nowl, nowr));
}
void query() {
int l = read(), r = read();
printf("%d\n", T.query(, n, , l, r));
}
int main() {
n = read();int m = read();
for (int i = ; i <= m; ++i) {
if (read() == ) Insert();
else query();
}
return ;
}
51nod 小朋友的笑话的更多相关文章
- 51nod“省选”模测第二场 C 小朋友的笑话(线段树 set)
题意 题目链接 Sol 直接拿set维护\(li\)连续段.因为set内的区间互不相交,而且每个线段会被至多加入删除一次,所以复杂度是对的. #include<bits/stdc++.h> ...
- 51nod 省选联测 R2
51nod 省选联测 R2 上场的题我到现在一道都没A,等哪天改完了再写题解吧,现在直接写第二场的. 第二场比第一场简单很多(然而这并不妨碍我不会做). A.抽卡大赛:http://www.51nod ...
- 【51Nod 1244】莫比乌斯函数之和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...
- 51Nod 1268 和为K的组合
51Nod 1268 和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...
- 51Nod 1428 活动安排问题
51Nod 1428 活动安排问题 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活 ...
- 51Nod 1278 相离的圆
51Nod 1278 相离的圆 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278 1278 相离的圆 基 ...
- 【51Nod 1501】【算法马拉松 19D】石头剪刀布威力加强版
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1501 dp求出环状不连续的前缀和,剩下东西都可以算出来,比较繁琐. 时间 ...
- 【51Nod 1622】【算法马拉松 19C】集合对
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1622 简单题..直接暴力快速幂 #include<cstdio&g ...
- 【51Nod 1616】【算法马拉松 19B】最小集合
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1616 这道题主要是查询一个数是不是原有集合的一个子集的所有数的gcd. ...
随机推荐
- 记一款bug管理系统(bugdone.cn)的开发过程(3) - 永久免费化
BugDone永久免费了! BugDone(bug管理工具)已经发布有一阵子了,自发布以来注册用户量.项目创建量稳步提升,并且得到了很多用户的好评. 在开发BugDone工具之前,我们团队也曾为找不到 ...
- python模拟自动登录网站(urllib2)
不登录打开网页: import urllib2 request = urllib2.Request('http://www.baidu.com') response = urllib2.urlopen ...
- Qt: QAction在QToolBar中快捷键行为注意事项
在QMenuBar中添加快捷键很简单,只要在text的特定字母前加&,如&k按下ALT+k即会触发(QPushButton也是一样).但在QToolBar则不然,需要调action-& ...
- MySQL基础之 存储引擎
MyISAM存储引擎 缺点:不支持事务,不支持外键.只支持表级锁. 优点:访问速度快,多用于select.insert语句的高负载操作.仅仅支持全文索引. MyISAM缓存在内存的是索引,不是数据.而 ...
- beta冲刺————第二天(2/5)
完善具体内容: 前端: (1)添加了更多设置 (2)点击后出现底栏,分别可以进行字体背景设置.收藏.分享等操作,同时可以看出对文章的排版进行了完善 后端: 对阿里云服务器中的环境进行配置,同时熟悉阿里 ...
- [python] 修改Tkinter 的默认图标
先上一个不修改的样式,如下: import easygui as g g.msgbox("hello","hi") 注意左上角的图标为红色的Tk字样 修改后: ...
- python第三十五课——生成器
1.生成器: 什么是生成器? 它内部封装了一套公式/算法,只有等到需要调用/执行数据时 --> next()函数执行 才会将公式计算得到数据结果,这就是生成器的原理(核心思想): [注意事项]: ...
- bat脚本,备份数据库并压缩
forfiles /p "D:\DBBackup" /m "*.sql" /d -08 /c "cmd /c del @path"forfi ...
- Mysql表创建外键报错
数据库表A: CREATE TABLE task_desc_tab ( id INT(11) PRIMARY KEY NOT NULL COMMENT '自增主键' AUTO_INCREMENT, t ...
- ThreadLocal解决SimpleDateFormat多线程安全问题中遇到的困惑
测试代码: public class Main { public static void main(String[] args) { for (int k = 0; k < 10; k++) { ...