hackerrank Similar Pair
Problem Statement
You are given a tree where each node is labeled from 1 to n. How many similar pairs(S) are there in this tree?
A pair (A,B) is a similar pair if the following are true:
- node A is the ancestor of node B
- abs(A−B)≤T
Input format:
The first line of the input contains two integers, n and T. This is followed by n−1 lines, each containing two integers si and ei where node si is a parent to node ei.
Output format:
Output a single integer which denotes the number of similar pairs in the tree.
Constraints:
1≤n≤100000
0≤T≤n
1≤si, ei ≤n
Sample Input:
5 2
3 2
3 1
1 4
1 5
Sample Output:
4
Explanation:
The similar pairs are: (3, 2) (3, 1) (3, 4) (3, 5).
You can have a look at the tree image here
#include <bits/stdc++.h>
using namespace std;
int T, n;
const int N(1e5+);
int bit[N];
int sum(int x){
int s=;
while(x){
s+=bit[x];
x-=x&-x;
}
return s; //error-prone
}
void add(int x){
while(x<=n){
bit[x]++;
x+=x&-x;
}
}
int get_ans(int x){
int l=max(x-T-, );
int r=min(n, x+T);
return sum(r)-sum(l);
}
int par[N];
vector<int> g[N];
long long ans;
void dfs(int u){
int tmp=get_ans(u);
for(int i=; i<g[u].size(); i++){
int &v=g[u][i];
dfs(v);
}
ans+=get_ans(u)-tmp;
add(u);
}
int main(){
//freopen("in", "r", stdin);
cin>>n>>T;
for(int i=, u, v; i<n; i++){
cin>>u>>v;
par[v]=u;
g[u].push_back(v);
}
int root=;
while(par[root])
root=par[root];
dfs(root);
cout<<ans<<endl;
}
--------------------------------------------
我们在考虑能否用C++ STL中的 set实现这个集合
显然我们需要支持3种操作
1. 插入,set OK
2.查询集合中大于x的数有多少个
3.查询集合中小于x的数有多少个
下面的资料摘自C++ Primer (5th. edition)
P. 330
Table 9.2 Contianer Operations
Type Aliases
difference_type Signed integral type big enough to hold the distance between two iterators
set<int> s;
int f(int l, int r){
return s.upper_bound(r)-s.lower_bound(l);
}
但这是行不通的,编译时报错:
:no match for ‘operator-’ (operand types are ‘std::set<int>: :iterator {aka std::_Rb_tree_const_iterator<int>}’ and ‘std::set<int>::iterator {aka std::_Rb_tree_const_iterator<int>}’)
因为set<int>::iterator不支持-(减法)
------------------------------------------------
只能写成
set<int> s;
int f(int l, int r){
auto b=s.lower_bound(l), e=s.upper_bound(r);
int res=;
while(b!=e){ //use != rather than <
++b;
++res;
}
return res;
}
但这样写复杂度是O(n),不能承受。
Bjarne Stroustrup TC++PL (4th. edition) P.954
The reason to use != rather than < for testing whether we have reached the end is partially because that is
the more precise statement of what we testing for and partially because only random-access iterators support <.
----------------------------------------------------------
hackerrank Similar Pair的更多相关文章
- R语言-混合型数据聚类
利用聚类分析,我们可以很容易地看清数据集中样本的分布情况.以往介绍聚类分析的文章中通常只介绍如何处理连续型变量,这些文字并没有过多地介绍如何处理混合型数据(如同时包含连续型变量.名义型变量和顺序型变量 ...
- 【LeetCode】734. Sentence Similarity 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...
- RFID Exploration and Spoofer a bipolar transistor, a pair of FETs, and a rectifying full-bridge followed by a loading FET
RFID Exploration Louis Yi, Mary Ruthven, Kevin O'Toole, & Jay Patterson What did you do? We made ...
- *[hackerrank]Algorithmic Crush
https://www.hackerrank.com/contests/w4/challenges/crush 第一眼觉得要用线段树,但据说会超时.其实这个可以通过生成pair排序来做. #inclu ...
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- [Functional Programming] mapReduce over Async operations and fanout results in Pair(rejected, resolved) (fanout, flip, mapReduce)
This post is similar to previous post. The difference is in this post, we are going to see how to ha ...
- Code Signal_练习题_Are Similar?
Two arrays are called similar if one can be obtained from another by swapping at most one pair of el ...
- MOSFET pair makes simple SPDT switch
With an n- and p-channel MOSFET, you can easily implement a single-pole double-throw (SPDT) switch t ...
- 2017-5-14 湘潭市赛 Similar Subsequence 分析+四维dp+一些简单优化
Similar Subsequence Accepted : Submit : Time Limit : MS Memory Limit : KB Similar Subsequence For gi ...
随机推荐
- linux 学习随笔-shell简单编写
脚本最好都放在/usr/local/sbin中 脚本的执行 sh -x 脚本.sh -x可以查看执行过程 1在脚本中使用变量 使用变量的时候,需要使用$符号: #!/bin/bash ##把命令赋 ...
- Crontab定时任务配置
CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使用它在 ...
- SQL SERVER中的OLEDB等待事件
OLEDB等待事件介绍 OLEDB等待类型是SQL SERVER 数据库中最常见的几种等待类型之一.它意味着某个会话(SPID)通过SQL Server Native Client OLEDB Pro ...
- Java 流(Stream)、文件(File)和IO
Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多种格式,比如:基本类型.对象.本地化字符集等等. 一个流可以理解为一个数据的序 ...
- 搞ACM的你伤不起[转自RoBa]------(看一次,笑一次)
RoBa原创,转载请注明出处 劳资六年前开始搞ACM啊!!!!!!!!!! 从此踏上了尼玛不归路啊!!!!!!!!!!!! 谁特么跟劳资讲算法是程序设计的核心啊!!!!!! 尼玛除了面试题就没见过用 ...
- runv nslistener源码分析
nslistener的作用实质上就是将新的namespace里的veth网卡的配置信息通过proxy进程传输出来,并且利用该信息对tap进行相同的配置,最终用tap模拟新的namespace里的vet ...
- 洛谷练习P2279 P1346
题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...
- Python版本共存之道:virtualenv和virtualenvwrapper
以前觉得根本用不着这个,但是写不同项目的时候就遇到了问题,不可能把之前的全部删掉从新安装,于是就想到了这个,终于还是要学它, 现在做一个命令的总结,方便自己和大家查询 #以下以对 test 为名的虚拟 ...
- spring有三种启动方式
spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn spring3.0及以后版本中已经删除Co ...
- Python-04-基础
一.装饰器(decorator) 装饰器本质上也是函数,目的是为其他函数添加附加功能(装饰其他函数) Python通过使用装饰器来达到代码的开放与封闭. 原则: 不能修改被装饰函数的源代码. 不能修改 ...