Gym - 101350F Monkeying Around(线段树+树状数组)
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
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
题意:
一坨猴子坐在椅子上讲笑话,要是某只猴子听到的笑话它没听过,它就会坐到地上去,否则就坐在椅子上,问最后有多少个猴子坐在椅子上。
思路:
显而易见,猴子最后的状态之和猴子听到的最后一个笑话有关。
所以当前问题就是,对于某一只猴子,它听到的最后一个笑话是几号笑话?这个笑话它之前有没有听过?
对于第一个问题,用线段树区间染色。
对于第二个问题,记录下每个笑话对应的猴子(就是猴子和它最后听的笑话相对应),每个笑话对应的区间。
先区间更新笑话对应的区间,在查询对应的猴子听过这个笑话的次数。
代码:
我的代码是错的。
但是仍然能够AC,因为数据没有存在某一个猴子没有听见笑话的情况。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); struct node{
int l,r;
int num;
}a[maxn<<]; void build(int t,int l,int r){
a[t].l=l;a[t].r=r;
a[t].num=;
if(l==r){a[t].num=-;return;}
int mid=(l+r)>>;
build(ls,l,mid);
build(rs,mid+,r);
} void push_down(int t){
a[ls].num=a[rs].num=a[t].num;
a[t].num=;
} void update(int t,int l,int r,int num){
if(a[t].num){push_down(t);}
if(a[t].l==l&&a[t].r==r){
a[t].num=num;
return;
}
int mid=(a[t].l+a[t].r)>>;
if(mid>=r){update(ls,l,r,num);}
else if(mid<l){
update(rs,l,r,num);
}
else{
update(ls,l,mid,num);
update(rs,mid+,r,num);
}
} int query(int t,int x){
if(a[t].num||a[t].l==a[t].r){return a[t].num;}
int mid=(a[t].l+a[t].r)>>;
if(x<=mid){return query(ls,x);}
else{query(rs,x);}
} int bit[maxn],n;
int lowbit(int x){
return x&(-x);
} void update(int x,int num){
while(x<maxn){
bit[x]+=num;
x+=lowbit(x);
}
}
int query(int x){
int ans=;
while(x>){
ans+=bit[x];
x-=lowbit(x);
}
return ans;
}
int m; vector<pair<int,int> >g1[maxn];
vector<int>g2[maxn]; int main()
{
int T;
scanf("%d",&T);
while(T--){
for(int i=;i<maxn;i++){
g1[i].clear();
g2[i].clear();
}
scanf("%d%d",&n,&m);
int x,l,k;
build(,,n);
for(int i=;i<=m;i++){
scanf("%d%d%d",&x,&l,&k);
int L=max(,x-k);
int R=min(n,x+k);
update(,L,R,l);
g1[l].push_back(make_pair(L,R));
}
for(int i=;i<=n;i++){
int k=query(,i);
g2[k].push_back(i);
}
int ans=;
for(int i=;i<maxn;i++){
int sz=g1[i].size();
if(sz==){continue;}
for(int j=;j<sz;j++){
update(g1[i][j].first,);
update(g1[i][j].second+,-);
}
sz=g2[i].size();
for(int j=;j<sz;j++){
if(query(g2[i][j])>=){ans++;}
}
sz=g1[i].size();
for(int j=;j<sz;j++){
update(g1[i][j].first,-);
update(g1[i][j].second+,);
}
}
printf("%d\n",ans);
}
return ;
}
Gym - 101350F Monkeying Around(线段树+树状数组)的更多相关文章
- CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)
The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...
- [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- HDU 1556 线段树或树状数组,插段求点
1.HDU 1556 Color the ball 区间更新,单点查询 2.题意:n个气球,每次给(a,b)区间的气球涂一次色,问最后每个气球各涂了几次. (1)树状数组 总结:树状数组是一个查 ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings
谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
- POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树
题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...
- Turing Tree_线段树&树状数组
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
- HDU 1166 敌兵布阵 (数状数组,或线段树)
题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
随机推荐
- python 迭代器协议和生成器
一.什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个stoplteration异常,以终止迭代(只能往后走,不能往前退) 2.可迭代 ...
- c/c++ 继承与多态 继承时如何改变个别成员的访问属性
问题1:若类B以private的方式继承类A,但还想让类A的某些个别成员,保持public或者protected的访问属性,这时应该怎么办? 使用using,去改变访问属性. #include < ...
- js学习之路1: 初识js函数
1. 简单的函数: <html> <head> <script type="text/javascript"> function myfunct ...
- Chinese word segment based on character representation learning 论文笔记
论文名和编号 摘要/引言 相关背景和工作 论文方法/模型 实验(数据集)及 分析(一些具体数据) 未来工作/不足 是否有源码 问题 原因 解决思路 优势 基于表示学习的中文分词 编号:1001-908 ...
- 0109 ubuntu nginx ssl
1. sudo apt-get install openssl libssl-dev # ./configure --with-http_stub_status_module --with-http_ ...
- 2018/05/14 03:56:10 [error] 12959#0: *42285845507 client intended to send too large body: 1664288 bytes
Syntax: client_max_body_size size; Default: client_max_body_size 1m; Context: http, server, location ...
- [LeetCode] 4. 寻找两个有序数组的中位数
题目链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 ...
- keras 中模型的保存
参考:https://www.cnblogs.com/weiyinfu/p/9788179.html#0 1.model.summary() 这个函数会打印模型结构,但是仅仅是打印到控制台,不能保存 ...
- Python排序算法——冒泡排序
有趣的事,Python永远不会缺席! 如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10786904.html 一.冒泡排序(Bubb ...
- React-propsType和defaultProps
TodoItem.propTypes={ content:PropTypes.string, text:PropTypes.string.isRequired, handleDeleteItem:Pr ...