F. Monkeying Around
time limit per test

2.0 s

memory limit per test

256 MB

input

standard input

output

standard output

When the monkey professor leaves his class for a short time, all the monkeys go bananas. N monkeys are lined up sitting side by side on their chairs. They each have the same joke book. Before the professor returns, M jokes were heard.

Each of the M jokes are said in the order given and have the following properties:

xi - position of the monkey who said it.

li – index of the joke in the book.

ki – volume the monkey says that joke.

When the monkey at position xi says the joke li, all monkeys at a distance less than or equal to ki from that monkey (including the monkey who said the joke) will fall off their chairs in laughter if they have never heard the joke li before.

If the joke li has been heard anytime during the past before, and the monkey hears it again, then he will sit back up in his chair.

A monkey can fall off his chair more than once (every time he hears a new joke), and if he is already on the ground and hears a new joke, he will stay on the ground.

Can you figure out how many monkeys will be in their seats by the time the professor comes back?

Input

The first line of input is T – the number of test cases.

The first line of each test case is N, M (1 ≤ N ≤ 105) (1 ≤ M ≤ 105) – the number of monkeys in the class, and the number of jokes said before the professor returns.

The next M lines contain the description of each joke: xi, li, ki (1 ≤ xi ≤ N) (1 ≤ li ≤ 105) (0 ≤ ki ≤ N).

Output

For each test case, output on a line a single integer - the number of monkeys in their seats after all jokes have been said.

Example
Input
1
10 7
3 11 0
3 11 2
5 12 1
8 13 2
7 11 2
10 12 1
9 12 0
Output
3

【题解】

考虑   每一个人  站着还是坐下  取决于  最后一个他听到的笑话
这个可以线段树nlogn求出
然后
我们依次考虑每一个笑话
看他覆盖的区间
此笑话决定的人  那个人被覆盖0或>1次则坐下  否则站起来
也可以线段树
综上 复杂度nlogn

(这是我跟某dalao的聊天记录直接放这里了)

网上有用扫描线做的,不是很明白。。。

待我理解理解

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b)) inline void swap(long long &x, long long &y)
{
long long tmp = x;x = y;y = tmp;
} inline void read(long long &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const long long INF = 0x3f3f3f3f;
const long long MAXN = + ; struct Node
{
long long x, l, k, t;
}node[MAXN]; long long n, m, l[MAXN], r[MAXN], tot, cnt[MAXN]; //第一颗线段树,用来维护最晚时间颜色coloe
long long color[MAXN], num[MAXN]; void pushup1(long long o, long long l, long long r)
{
long long mid = (l + r) >> ;
if(!color[o << ]) color[o << ] = color[o];
if(!color[o << | ]) color[o << | ] = color[o];
} void modify1(long long ll, long long rr,long long col, long long o = , long long l = , long long r = n)
{
pushup1(o, l, r);
if(color[o]) return;
if(ll <= l && rr >= r)
{
if(!color[o])color[o] = col;
pushup1(o, l, r);
return;
}
long long mid = (l + r) >> ;
if(mid >= ll)modify1(ll, rr, col, o << , l, mid);
if(mid < rr) modify1(ll, rr, col, o << | , mid + , r);
} void build1(long long o = , long long l = , long long r = n)
{
if(l == r)
{
num[l] = color[o];
return;
}
pushup1(o, l, r);
long long mid = (l + r) >> ;
build1(o << , l, mid);
build1(o << | , mid + , r);
} //第二颗线段树,维护特定颜色作用于某个点多少次
long long data[MAXN], lazy[MAXN]; void pushup2(long long o, long long l, long long r)
{
if(lazy[o])
{
long long mid = (l + r) >> ;
lazy[o << ] += lazy[o];
lazy[o << | ] += lazy[o];
data[o << ] += lazy[o] * (mid - l + );
data[o << | ] += lazy[o] * (r - mid);
lazy[o] = ;
}
} void modify2(long long ll, long long rr, long long x, long long o = , long long l = , long long r = n)
{
pushup2(o, l, r);
if(ll <= l && rr >= r)
{
lazy[o] += x;
data[o] += (r - l + ) * x;
return;
}
long long mid = (l + r) >> ;
if(mid >= ll)modify2(ll, rr, x, o << , l, mid);
if(mid < rr) modify2(ll, rr, x, o << | , mid + , r);
data[o] = data[o << ] + data[o << | ];
} long long ask2(long long p, long long o = , long long l = , long long r = n)
{
if(l == r && l == p)
return data[o];
pushup2(o, l, r);
long long mid = (l + r) >> ;
if(p <= mid)return ask2(p, o << , l, mid);
else return ask2(p, o << | , mid + , r);
} long long cmppp(Node a, Node b)
{
return a.l < b.l;
} long long cmp(Node a, Node b)
{
return a.t > b.t;
} long long cmpp(long long a, long long b)
{
return num[a] < num[b];
} long long ans, t; int main()
{
// freopen("data.txt", "r", stdin);
/*long long n, m,cnt[MAXN];
*/
read(t);
for(;t;-- t)
{
ans = ;tot = ;
memset(color, , sizeof(color));
memset(num, , sizeof(num));
memset(data, , sizeof(data));
memset(lazy, , sizeof(lazy));
memset(l, , sizeof(l));
memset(r, , sizeof(r));
memset(node, , sizeof(node));
memset(cnt, , sizeof(cnt));
read(n), read(m);
for(register long long i = ;i <= m;++ i)
read(node[i].x), read(node[i].l), read(node[i].k), node[i].t = i;
for(register long long i = ;i <= n;++ i) cnt[i] = i;
for(register long long i = m;i >= ;-- i)
modify1(node[i].x - node[i].k, min(n, node[i].x + node[i].k), node[i].l);
build1();
std::sort(node + , node + + m, cmppp);
std::sort(cnt + , cnt + + n, cmpp);
long long now = node[].l;tot = ;l[] = ;
for(register long long i = ;i <= m;++ i)
if(now != node[i].l)r[tot] = i - , ++ tot, now = node[i].l, l[tot] = i;
r[tot] = m;
for(register long long i = , p = ;i <= tot;++ i)
{
for(register long long j = l[i];j <= r[i];++ j)
modify2(node[j].x - node[j].k, min(n, node[j].x + node[j].k), );
long long tmp = , flag = p;
while(num[cnt[p]] == node[l[i]].l)
{
long long tmp = ask2(cnt[p]);
if(tmp == || tmp > ) ++ ans;
++ p;
}
for(register long long j = l[i];j <= r[i];++ j)
modify2(node[j].x - node[j].k, min(n, node[j].x + node[j].k), -);
}
printf("%I64d\n", ans);
}
return ;
}

GYM 101350F

GYM 101350 F. Monkeying Around的更多相关文章

  1. 组队赛Day1第一场 GYM 101350 F. Monkeying Around(线段树)

    [题目大意] 有n只猴子坐在树上,m个笑话. 给出每个讲这个笑话的猴子的编号,笑话的编号,和笑话的影响半径. 如果一个树上的猴子听了没听过的笑话,会掉到树下.如果听过并且在树下,就会爬到树上. 问最后 ...

  2. [codeforces/gym/101350/L]维护“凸包”

    题目链接:http://codeforces.com/gym/101350/problems 给定n个墙,每个墙有一个高度,要支持动态修改墙的高度和查询这个“容器”能盛多少水. (队友)观察发现,能盛 ...

  3. Gym 100637F F. The Pool for Lucky Ones

    F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  4. codeforces Gym 100187F F - Doomsday 区间覆盖贪心

    F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...

  5. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  6. Gym 100637F F. The Pool for Lucky Ones 暴力

    F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  7. Codeforces Gym 100513F F. Ilya Muromets 线段树

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

  8. Codeforces Gym 100513F F. Ilya Muromets 水题

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

  9. Gym - 100283F F. Bakkar In The Army —— 二分

    题目链接:http://codeforces.com/gym/100283/problem/F F. Bakkar In The Army time limit per test 2 seconds ...

随机推荐

  1. spark Infinate 的处理

    去掉infinity数据的方法: absperrordf_rdd = absperrordf.rdd.filter(lambda x: (np.isinf(float(x.avgperror)) == ...

  2. linux系统下重要的分区及其作用

    下面列出来的是linux系统下重要的分区及其作用/bin :bin是binary的缩写;/boot :存放启动Linux时使用的一些核心文件;/root :root(超级管理员)的用户主目录;/sbi ...

  3. 原子操作atomic

    一.原子操作:即不可再细分的操作,最小的执行单位,在操作完之前都不会被任何事件中断. 整型原子操作:对int类型的操作变成原子操作.                 int i = 0;       ...

  4. java虚拟机(十一)--GC日志分析

    GC相关:java虚拟机(六)--垃圾收集器和内存分配策略 java虚拟机(五)--垃圾回收机制GC 打印日志相关参数: -XX:+PrintGCDetails -XX:PrintGCTimestam ...

  5. Ionic 不随系统字体而变化

    1.添加插件phonegap-plugin-mobile-accessibility cordova plugin add https://github.com/phonegap/phonegap-m ...

  6. 【HTML5】如何处理HTML5新标签的浏览器兼容版问题

    HTML5规范毕竟是刚刚才定义完成的规范,还有一些浏览器并不能支持其中的新标签和新属性,尤其是IE8及以下版本浏览器.以下介绍一些在页面中使用HTML5新标签的实践方法,目的是让HTML5中的新标签在 ...

  7. vue+element-ui 使用富文本编辑器

    npm安装编辑器组件npm install vue-quill-editor –save 在components文件夹创建ue.vue组件,如下 ue.vue代码如下: <!-- 组件代码如下 ...

  8. mysql基本笔记之二

    1.查看当前编码 show variables like '%char%' 2.修改user表中id=1的name 为 A  where后面是条件,就是定位 3.符号 > //大于符号 < ...

  9. c# 将Datarow转成Datarowview

    DataRowView rowview= dataTable.DefaultView.Cast<DataRowView>().Where(a => a.Row == tmprow). ...

  10. JasperReport编译报表设计5

    我们在前面的章节中产生的JasperReport模板(JRXML文件).这个文件不能直接用于生成报告.它必须被编译成JasperReport的“本地二进制"格式,称为Jasperfile.在 ...