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 ...
随机推荐
- c# 多线程委托传参方式
1.定义一个线程调用的方法函数 private void RTPServer(object _Serverip) { IPEndPoint Serverip = _Serverip as IPEndP ...
- 树莓派Ubuntu 16.04 MATA系统 修改用户文件夹名后,提示configure it with blueman-service
自从修改了树莓派的Ubuntu 16.04 MATA 系统的 /home/ 下的用户文件夹名后,使用vncserver远程操作,看到桌面每次都提示 Configured directory for i ...
- window.open模拟表单POST提交
解决地址栏长度限制,隐藏参数,不在地址栏显示 项目 excel 导出中用到 将form的target设置成和open的name参数一样的值,通过浏览器自动识别实现了将内容post到新窗口中 var u ...
- 验证二叉搜索树的golang实现
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索树. ...
- Postman安装及入门教程
安装 本文只是基于 Chrome 浏览器的扩展插件来进行的安装,并非单独应用程序. 首先,你要台电脑,其次,安装有 Chrome 浏览器,那你接着往下看吧. 1. 官网安装(别看) 打开官网,http ...
- C#基础知识之属性
其实属性大家经常用,可以说是非常熟悉了,这里就记录一下我那天突然对属性产生的疑惑.为什么需要使用属性?属性的好处是什么? 一.什么是属性? 属性(Property) 是类(class).结构(stru ...
- vue 中使用jquery
vue-cli搭建的项目 第一种方式:npm 引包的方式 1.安装jquery npm install jquery --save 2.webpack配置 在项目根目录下的build目录下找到webp ...
- Git初始化-添加提交以及查看状态
本地库初始化 使用 git bash 打开 git 命令行窗口.这个命令行窗口兼容 Linux 命令. 命令:mkdir 当前文件下新建文件夹. 命令:git init 初始化空的本地库,.git文件 ...
- 科大讯飞语音合成api
import base64import jsonimport timeimport hashlibimport requests # API请求地址.API KEY.APP ID等参数,提前填好备用a ...
- PS制作水火相溶特效文字图片
最终效果 一.新建一个1400*900像素的画布. 二.由上到下拉一个深灰到纯黑径向渐变. 三.输入字母S,并用ctrl+t拉到适合的大小,并且降低不透明度. 四.拖入水花素材(如果大家有水花笔刷的话 ...