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 ...
随机推荐
- Linux(DeepInOS) 下 mysql 的安装与基本配置
索引: 目录索引 参看代码 GitHub: DeepIn(GNU/Linux) MySQL 一.安装 sudo apt-get install mysql-server 期间需要输入两次密码,root ...
- AES 加密与解密
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Securit ...
- android SDK 无法更新
android-windows-sdk无法更新解决办法: 1.在host文件新增如下配置 (host文件位置:c:\Windows\System32\drivers\etc文件夹下面,用文本编辑器 ...
- chrome总是提示“请停用开发者模式运行的扩展程序”
方法1:通过组策略的扩展白名单.要下载一个组策略管理模板 1.开始 -> 运行 -> 输入gpedit.msc -> 回车确定打开计算机本地组策略编辑器(通过Win + R快捷键可以 ...
- Nginx 配置 https
从云服务提供商处申请证书 申请 https 证书教程-百度经验 申请下来的证书目录结构 . ├── Apache │ ├── 1_root_bundle.crt │ ├── 2_website ...
- (生活)Photoshop入门(不定时更新)
我可能是想找个工作以外的事情做一下. 目标:我要自学网PhotoShop商业修图. 笔记: .图层 .1总结: 1.1.1图层就好像画画的一张纸,但是每一层又互不影响. 1.1.2图层蒙版(覆盖一层玻 ...
- 读写锁ReentrantReadWriteLock的使用
package com.thread.test.Lock; import java.util.Random; import java.util.concurrent.locks.Lock; impor ...
- MySQL之库相关操作
一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...
- k-近邻(KNN)算法改进约会网站的配对效果[Python]
使用Python实现k-近邻算法的一般流程为: 1.收集数据:提供文本文件 2.准备数据:使用Python解析文本文件,预处理 3.分析数据:可视化处理 4.训练算法:此步骤不适用与k——近邻算法 5 ...
- luogu P4842 城市旅行
嘟嘟嘟 好题,好题 刚开始突发奇想写了一个\(O(n ^ 2)\)暴力,结果竟然过了?!后来才知道是上传题的人把单个数据点开成了10s-- 不过不得不说我这暴力写的挺好看的.删边模仿链表删边,加边的时 ...