P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】
题外话:维护区间交集子集的小套路
开两个树状数组,一个维护进入区间,一个维护退出区间
$Query:$
给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数
用上面维护的信息表示,就是$r$(含)前进入的区间个数$-l$(不含)前退出的区间个数
这个题:
我们可以把它抽象为,求区间对个数,要求区间对交集非空且互不包含
尝试像上面那样解决,$l$后进入$-r$后进入保证左端点满足要求,$l$后进入$-r$前退出,右端点满足要求
但是放到一起好像就有些问题了,尝试减掉一个条件,不妨按左端点从右向左依次插入树状数组,消掉$l$后进入这个条件
这样,查询就变成了$r$前进入$-r$前退出,我们就可以顺利解决这道题了
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=;
int n,ans,a[*maxn],c[][*maxn];
vector<int>v[maxn];
int lowbit(int x)
{
return x&-x;
}
int sum(int id,int x)
{
int ret=;
while(x)
{
ret+=c[id][x];
x-=lowbit(x);
}
return ret;
}
void add(int id,int x)
{
while(x<=*n)
{
c[id][x]++;
x+=lowbit(x);
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=*n;i++)
{
scanf("%d",&a[i]);
v[a[i]].push_back(i);
}
for(int i=*n;i;i--)
if(v[a[i]][]==i)
{
int r=v[a[i]][];
ans+=sum(,r)-sum(,r);
add(,v[a[i]][]),add(,v[a[i]][]);
}
printf("%d\n",ans);
return ;
}
P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】的更多相关文章
- 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)
题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...
- P3660 [USACO17FEB]Why Did the Cow Cross the Road III G
Link 题意: 给定长度为 \(2N\) 的序列,\(1~N\) 各处现过 \(2\) 次,i第一次出现位置记为\(ai\),第二次记为\(bi\),求满足\(ai<aj<bi<b ...
- [USACO17FEB]Why Did the Cow Cross the Road III G
嘟嘟嘟 首先看到这种序列的问题,我就想到了逆序对,然后就想如何把这道题转化. 首先要满足这个条件:ai <bi.那么我们把所有数按第一次出现的顺序重新赋值,那么对于新的数列,一定满足了ai &l ...
- [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)
题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...
- 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S
P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...
- [USACO17FEB]Why Did the Cow Cross the Road III P
[USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...
- 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G
//神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...
- [Luogu3659][USACO17FEB]Why Did the Cow Cross the Road I G
题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...
- [USACO17FEB]Why Did the Cow Cross the Road III S
题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...
随机推荐
- grafana-zabbix部署和使用
grafana-zabbix安装 官网安装介绍地址:https://grafana.com/plugins/alexanderzobnin-zabbix-app/installation 下载地址:h ...
- how to calculate the best fit to a plane in 3D, and how to find the corresponding statistical parameters
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
- ext中grid根据数据不同显示不同样式
核心代码: var clsRender = function(value){ if (value == 'male') { return "<span style='color:red ...
- 树的dfs序.欧拉序
dfs序 ==先序,连续一段区间就是子树
- yolov3实践(二)
这次给大家带来一个有趣的项目,项目主要是Python写的,基于Keras,backend是tf. 首先,当我们回顾视觉目标检测这个任务时,我们可能纠结于如何使这个项目变得更加work,我理解的更加wo ...
- WPS 表格筛选两列相同数据-完美-2017年11月1日更新
应用: 1.选出A列中的数据是否在B列中出现过: 2.筛选出某一批序号在一个表格里面的位置(整批找出) 3.其实还有其他很多应用,难描述出来... ... A列中有几百的名字,本人想帅选出B列中的名字 ...
- 改变checkbox的默认样式
针对于CheckBox默认样式的改变,和选中状态的改变 <label class="checkBox"><input type="checkbox&qu ...
- vlc-android 的编译过程
参考官方文档:https://wiki.videolan.org/AndroidCompile#Get_VLC_Source 值得注意的的地方: 1.切记安装以下工具 sudo apt-get ins ...
- SpringBoot三种配置Dubbo的方式
*必须首先导入dubbo-starter (1).使用SpringBoot配置文件(application.properties或application.yml) dubbo.application. ...
- 程序执行的过程分析--【sky原创】
程序执行的过程: 比如我们要执行3 + 2 程序计数器(PC) = 指令地址 指令寄存器(IR) = 正在执行的命令 累加器(AC) = 临时存储体 那么实际上执行了三条指令 每条指令 ...