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. mysql localhost可以连输入本机ip地址连接不了

    Mysql 默认是没有开启这个权限的(只允许使用 host:localhost,或者 host:127.0.0.1),如果想用 host:192.168.1.* ,来访问mysql ,需要手动开启这个 ...

  2. opencv-图像类型、深度、通道

    转自:图像类型   与  opencv中图像基础(大小,深度,通道) 一.图像基本类型 在计算机中,按照颜色和灰度的多少可以将图像分为四种基本类型. 1. 二值图像 2. 灰度图像 3. 索引图像 4 ...

  3. 杂项-公司:Google

    ylbtech-杂项-公司:Google 谷歌公司(Google Inc.)成立于1998年9月4日,由拉里·佩奇和谢尔盖·布林共同创建,被公认为全球最大的搜索引擎公司.谷歌是一家位于美国的跨国科技企 ...

  4. 服务器迁移部署OmsWeb

    绑定 基本设置 高级设置

  5. 群晖引导是uefi还是传统模式的识别

     看左下角光标闪不闪,不闪的是uefi,在闪的就是传统

  6. appscan如何扫描移动应用APP

    1.前置条件:让手机和电脑处于同一WIFI下 1打开appscan,选择手动探索/外部设备. 2在弹出的对话框页面点击右上角“记录代理配置”. 3在弹出的页面选择记录代理页签,设置Appscan代理端 ...

  7. C++给组合框控件(Combo box)加变量后不能运行

    是一个BUG,找出你程序存储的位置,打开一个Debug的文件夹,将除.res文件之外的所有文件删除,然后运行,就可以了!!

  8. CF629E Famil Door and Roads【树上计数+分类讨论】

    Online Judge:Codeforces629E,Luogu-CF629E Label:树上计数,分类讨论,换根 题目描述 给出一棵n个节点的树.有m个询问,每一个询问包含两个数a.b,我们可以 ...

  9. [转]C#中用NamedPipe进程间通信

    转自:http://blog.csdn.net/jinjazz/archive/2009/02/03/3861143.aspx 本文只是一个测试例子,核心代码是kernel32.dll中的一组wind ...

  10. js算法之把一个数组按照指定的数组大小分割成若干个数组块

    题目描述:     把一个数组arr按照指定的数组大小size分割成若干个数组块. 例如:   chunk([1,2,3,4],2)=[[1,2],[3,4]];   chunk([1,2,3,4,5 ...