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. 使用UUID和int自增主键的区别

    知其然,知其所以然.在看到生成UUID的代码,后带给我的百度结合自己的经验再写下来的区别 一.UUID做主键: 优点: .保证数据在表和库都是独立的,有利于后续的分库 .合并表的时候主键不会重复 .有 ...

  2. 转-Pycharm中运行Python代码的几种方式

    转自:Pycharm中运行Python代码的几种方式 在pycharm中的Python代码运行会出现各种奇葩的问题,比如,密码输入时不显示或没有提示,给我们带来一些麻烦,下面介绍几种代码运行的几种方式 ...

  3. 杂项-SpringBoot-Jasypt:Jasypt(安全框架)

    ylbtech-杂项-SpringBoot-Jasypt:Jasypt(安全框架) 1. 使用jasypt加密Spring Boot应用中的敏感配置返回顶部 1. 本文讲述了在Spring Boot/ ...

  4. Linux 日期时间命令

    cal : 显示日历 -1 显示一个月的月历 -3 显示系统前一个月,当前月,下一个月的月历 -s  显示星期天为一个星期的第一天,默认的格式 -m 显示星期一为一个星期的第一天 -j  显示在当年中 ...

  5. elasticsearch配置优化

    调整ES内存分配有多种方式,建议调整 elasticsearch 中的设置(还可以直接修改bin下的启动脚本). # Directory where the Elasticsearch binary ...

  6. 分布式事务的2PC、3PC和TCC

    1.2PC协议 2PC 是二阶段提交(Two-phase Commit)的缩写,顾名思义,这个协议分两阶段完成.第一个阶段是准备阶段,第二个阶段是提交阶段,准备阶段和提交阶段都是由事务管理器(协调者) ...

  7. ireport 无法打开问题

    打开时闪退 ,是因为jdk版本过高的原因:https://blog.csdn.net/aust_glj/article/details/52291240 相关软件下载地址: JasperReports ...

  8. .NET Framework的属性类对控件的支持功能

     ToolBoxItem 此属性为类特性.属于工具箱属性,可以设置当前控件是否在工具箱中显示,以及所在工具箱项的类型名称等信息.默认生成的控件都显示在工具箱中. 更多设计时属性介绍: 4.3 属性的 ...

  9. vue-cli 构建项目

    1.安装vue-cli和webpack npm install webpack -g npm install vue-cli -g 2.vue-cli初始化项目 vue init webpack-si ...

  10. Leetcode322. Coin Change零钱兑换

    给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: coins = ...