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. egret 篇——关于ios环境下微信浏览器的音频自动播放问题

    前段时间公司突然想用egret(白鹭引擎)做一个金币游戏,大半个月边看文档边写吭哧吭哧也总算是弄完了.期间遇到一个问题,那就是ios环境下微信浏览器的音频自动播放问题. 个人感觉吧,egret自己封装 ...

  2. P1985 [USACO07OPEN]翻转棋

    题目链接: 翻转棋 题目分析: 先状压/\(dfs\)枚举第一排状态,然后在每个\(1\)下面翻,即确定了第一排就确定了后面的状态 最后验证一下最后一排是不是全0即可 代码: #include< ...

  3. mysql插入数据显示:Incorrect datetime value: '0000-00-00 00:00:00'

    1. 在进行mysql数据插入的时候,由于mysql的版本为5.7.1,部分功能已经升级,导致在datetime数据类型的影响下出现错误:   数据插入: mysql>insert into j ...

  4. SQLServer中使用索引视图

    在SQL Server中,视图是一个保存的T-SQL查询.视图定义由SQL Server保存,以便它能够用作一个虚拟表来简化查询,并给基表增加另一层安全.但是,它并不占用数据库的任何空间.实际上,在你 ...

  5. STL 最大堆与最小堆

    在第一场CCCC选拔赛上,有一关于系统调度的水题.利用优先队列很容易AC. // 由于比赛时花费了不少时间研究如何定义priority_queue的比较函数,决心把STL熟练掌握... Queue 首 ...

  6. vue 组件内引入外部在线js、css

    参考:https://blog.csdn.net/u010881899/article/details/80895661 例:引入element-ui js: mounted() { const oS ...

  7. mysql下突然丢失权限

    mysql  Can't read dir of  今天打开mysql数据库突然报了这个错误 查后发现是表没权限,进行了mysql的data目录权限修改 1.show global variables ...

  8. 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter

    [题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...

  9. springboot核心技术(二)-----自动配置原理、日志

    自动配置原理 配置文件能配置的属性参照 1.自动配置原理: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration 2).@Enable ...

  10. 获取请求header的各种方法

    部分代码非原创  不定期更新 PHP function get_all_header() { // 忽略获取的header数据.这个函数后面会用到.主要是起过滤作用 $ignore = array(' ...