GYM 101350 F. Monkeying Around
2.0 s
256 MB
standard input
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?
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).
For each test case, output on a line a single integer - the number of monkeys in their seats after all jokes have been said.
1
10 7
3 11 0
3 11 2
5 12 1
8 13 2
7 11 2
10 12 1
9 12 0
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的更多相关文章
- 组队赛Day1第一场 GYM 101350 F. Monkeying Around(线段树)
[题目大意] 有n只猴子坐在树上,m个笑话. 给出每个讲这个笑话的猴子的编号,笑话的编号,和笑话的影响半径. 如果一个树上的猴子听了没听过的笑话,会掉到树下.如果听过并且在树下,就会爬到树上. 问最后 ...
- [codeforces/gym/101350/L]维护“凸包”
题目链接:http://codeforces.com/gym/101350/problems 给定n个墙,每个墙有一个高度,要支持动态修改墙的高度和查询这个“容器”能盛多少水. (队友)观察发现,能盛 ...
- 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 ...
- codeforces Gym 100187F F - Doomsday 区间覆盖贪心
F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- 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 ...
- Codeforces Gym 100513F F. Ilya Muromets 线段树
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
- Codeforces Gym 100513F F. Ilya Muromets 水题
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
- 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 ...
随机推荐
- fastjson json转linkedhashmap为null
试了几种JSONObject.parseObject的方法,返回的都是null: 使用Gson就可以转成功. LinkedHashMap<String, String> map = gso ...
- java基础之Character类概述
Character 类 在对象中包装一个基本类型 char 的值 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然 构造方法 public Cha ...
- python collections 模块 之 deque
class collections.deque(iterable[,maxlen]): 返回 由可迭代对象初始化的 从左向右的 deque 对象. maxlen: deque 的最大长度,一旦长度超出 ...
- UVA--624 CD(01背包+路径输出)
题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- js 引入Vue.js实现vue效果
拆分组件为单个js见:https://www.jianshu.com/p/2f0335818ceb 效果 html <!DOCTYPE html> <html> <hea ...
- P1249 最大乘积
打暴力找规律,都是连续自然数去掉一个 n=int(input()) a=[] cnt=0 i=2 tot=0 ans=1 while tot<=n: tot+=i cnt+=1 a.append ...
- JZOJ5894【NOIP2018模拟10.5】同余方程
题目 Description
- BZOJ 1874: [BeiJing2009 WinterCamp]取石子游戏
Time Limit: 5 Sec Memory Limit: 162 MB Submit: 957 Solved: 394 [Submit][Status][Discuss] Description ...
- ES6 class继承
ES6 class继承 class类的继承 class可以通过extends关键字实现继承,这笔ES5的通过修改原型连实现继承要清晰和方便很多. class Point{ } class ColorP ...
- dubbo入门学习(五)-----dubbo的高可用
zookeeper宕机与dubbo直连 现象 zookeeper注册中心宕机,还可以消费dubbo暴露的服务. 原因 健壮性 l 监控中心宕掉不影响使用,只是丢失部分采样数据 l 数据库宕掉后,注册中 ...