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 ...
随机推荐
- 清清楚楚地搭建MongoDB数据库(以搭建4.0.4版本的副本集为例)
数据的目录文件层次设计 我们一般采用多实例的方式,而不是将所有的数据库尽可能地放在一个实例中. 主要基于以下考虑: 1:不同业务线对应的数据库放在不同的实例上,部分操作的运维时间容易协调等到. 2:相 ...
- web.xml设置过滤直接访问
<security-constraint> <web-resource-collection> <web-resource-name>JSPs</web-re ...
- [20190416]11g下那些latch是Exclusive的.txt
[20190416]11g下那些latch是Exclusive的.txt --//昨天测试了11g下那些latch是共享的,链接:--//是否反过来剩下的都是Exclusive的.继续测试: 1.环境 ...
- centos下Django+uwsgi+nginx
本篇章主要讲解uwsgi和nginx的作用,并利用两者对django项目进行部署 一.概述 在开发过程中,我们一般是在该项目的虚拟环境中启用django自带的web服务:python manage.p ...
- SAP Change Request Management (ChaRM)基础教程
本文介绍了SAP Solution Manager中的变更请求管理工具(Change Request Management,以下简称ChaRM ). 最近打算写个上线前请求号检查工具,为此需要了解相关 ...
- Extending the Yahoo! Streaming Benchmark
could accomplish with Flink back at Twitter. I had an application in mind that I knew I could make m ...
- C#深度学习の接口传参(interface)-----接口的妙用
一.接口可以干嘛 我们知道,接口的本质是一个约束规范,委托是方法纵向(形式上)的封装,接口是不同方法横向(广度)的封装 接口中不能有变量,但是可以有属性方法.常见的,我们可以用接口: 1.实现需求方的 ...
- iBATIS 传MAP处理方式(value是list的方式)
1.前提条件 参数是map结构的数据 key:String 类型 value:list 集合 2.处理方式 遍历集合一般常规的方式使用iterate,这里也不例外了,如下 <iterate op ...
- 如何解决一个从SkylineGlobe5版本升级到7版本遇到的小问题
前些天,有朋友问,用Skyline5版本开发的WinForm程序,升级到7版本的时候,工程提示下面这样“创建组件AxHost失败”的错误,该如何解决呢? 后来经过百度搜索,找到了这样的答案, 测试发现 ...
- SpringCloud(5)路由网关Spring Cloud Zuul
一个简单的微服务系统如下图: 1.为什么需要Zuul Zuul很容易实现 负载均衡.智能路由 和 熔断器,可以做身份认证和权限认证,可以实现监控,在高流量状态下,对服务进行降级. 2.路由网关 继续前 ...