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. ...
随机推荐
- LeetCode 题解之Reverse Words in a String
1.题目描述 2.问题分析 使用一个vector存储每个单词. 3.代码 void reverseWords(string &s) { vector<string> v; for ...
- C# 实例化的执行顺序(转)
首先进行细分1.类的成员分为:字段,属性,方法,构造函数2.成员修饰符:静态成员,实例成员不考虑继承的关系执行顺序为:1.静态字段2.静态构造方法3.实例字段4.实例构造方法其中 属性和方法只有在调用 ...
- windows系统相关命令及问题排查实践
1. 如何查看哪个端口被哪个程序占用? Netstat –ano|findstr "80" ->找到监听80端口的pid tasklist|findstr “<PID号 ...
- Oracle EBS 自治事务
自治事务程序主要是自主性,那就是,独立于主要的事务.之所以独立,或者提交之后会影响其他事务处理,本质在于它本身符合编译指令的规则,也就是说它属于在编译阶段就执行的指令,而不是在运行阶段执行的. 当自治 ...
- C# 实现 JAVA AES加密解密[原创]
以下是网上普遍能收到的JAVA AES加密解密方法. 因为里面用到了KeyGenerator 和 SecureRandom,但是.NET 里面没有这2个类.无法使用安全随机数生成KEY. 我们在接收J ...
- MySQL索引背后的数据结构及算法原理(employees实例)
摘要 http://blog.codinglabs.org/articles/theory-of-mysql-index.html 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特 ...
- MySQL出现Waiting for table metadata lock的场景浅析
MySQL版本为5.6.12. 在进行alter table操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的 ...
- Win7下设置护眼的电脑豆沙绿界面
控制面板\所有控制面板项\个性化\窗口颜色和外观 "色调"(Hue)设为85,"饱和度"(Sat)设为90,"亮度" (Lum)设为205. ...
- 第五次作业 hql查询
hql查询是基于对象的查询,不是基于表的查询. 1.hql的简单查询 @Test public void queryUsers() { //简单查询 SessionFactory sf = null; ...
- windows Server 2008R2 FTP服务器搭建详细图解
一.安装ftp服务 1.打开服务器管理器,如图: 2.右键点击角色,如图: 3.点击添加角色,会出现添加角色向导对话框,如图: 4.点击下一步,选择要添加的“web服务器(IIS)” ‘’ 5.点击下 ...