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 ...
随机推荐
- hadoop1.0 和 Hadoop 2.0 的区别
1.Hadoop概述 在Google三篇大数据论文发表之后,Cloudera公司在这几篇论文的基础上,开发出了现在的Hadoop.但Hadoop开发出来也并非一帆风顺的,Hadoop1.0版本有诸多局 ...
- 重置Visual Studio 2017的配置
1,从命令行进入VS 2017安装目录下面的Common7\IDE文件夹. 例如,Windows 10系统中 VS 2017 企业版的默认安装目录如下: C:\Program Files (x86)\ ...
- CSS---选择器种类 | 层叠性权重
一.css选择器种类 1.1,ID选择器 1.2,类选择器 1.3,标签选择器 1.4,后代选择器 1.5,子代选择器 1.6,交集选择器 1.7,并集选择器 1.8,通配符选择器 1.9,属性选择器 ...
- 大数据平台Lambda架构详解
Lambda架构由Storm的作者Nathan Marz提出.旨在设计出一个能满足.实时大数据系统关键特性的架构,具有高容错.低延时和可扩展等特. Lambda架构整合离线计算和实时计算,融合不可变( ...
- Mac下使用国内镜像安装Homebrew
First MBP上的brew很老了,就想把brew更新一下,顺便安装一下NodeJs.无奈更新的过程一直卡在网络下载,毫不动弹.想想,应该是Repo访问不到的原因,于是重装brew. 根据官网上的方 ...
- STM32 FSMC使用笔记
最近在使用STM32的FSMC与FPGA做并行通信总线控制,做一下总结 1,利用FSMC读取写入16位数据时的封装函数如下,不这样使用的话在与FPGA进行通信的过程中可能会出现不可预知的错误. #de ...
- Mac下MySql初始密码设置及mysql数据库操作
1. 首先 点击系统偏好设置 -> 点击MySQL, 在弹出的页面中,关闭服务.2. 进入终端命令输出: cd /usr/local/mysql/bin/ 命令,回车.3. 回车后,输入命令:s ...
- TabBar用到bottomNavigationBar
import 'package:flutter/material.dart';import 'homepage.dart';import 'lastpage.dart';import 'secondp ...
- 使用Mycat构建MySQL读写分离、主从复制、主从高可用
数据库读写分离对于大型系统或者访问量很高的互联网应用来说,是必不可少的一个重要功能. 从数据库的角度来说,对于大多数应用来说,从集中到分布,最基本的一个需求不是数据存储的瓶颈,而是在于计算的瓶颈,即S ...
- 使用Kernel NetEm和tc模拟复杂网络环境
关键词:netem(Network Emulator).tc(Traffic Control). 大部分局域网环境良好,但是产品实际网络环境可能千差万别,为了对产品进行各种情况测试就需要模拟网络环境. ...