题外话:维护区间交集子集的小套路

开两个树状数组,一个维护进入区间,一个维护退出区间

$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】的更多相关文章

  1. 洛谷 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 ...

  2. P3660 [USACO17FEB]Why Did the Cow Cross the Road III G

    Link 题意: 给定长度为 \(2N\) 的序列,\(1~N\) 各处现过 \(2\) 次,i第一次出现位置记为\(ai\),第二次记为\(bi\),求满足\(ai<aj<bi<b ...

  3. [USACO17FEB]Why Did the Cow Cross the Road III G

    嘟嘟嘟 首先看到这种序列的问题,我就想到了逆序对,然后就想如何把这道题转化. 首先要满足这个条件:ai <bi.那么我们把所有数按第一次出现的顺序重新赋值,那么对于新的数列,一定满足了ai &l ...

  4. [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)

    题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...

  5. 洛谷 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 ...

  6. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

  7. 洛谷 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Tomcat服务启动成功,但访问index.jsp出错 (jspInit)【转】

    本文引用自 --> http://zhouhaitao.iteye.com/blog/1164736 Tomcat服务启动成功,但访问index.jsp出错 环境:Tomcat6 + jdk6 ...

  2. Codeforces 954 G. Castle Defense

    http://codeforces.com/problemset/problem/954/G 二分答案 检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越 ...

  3. Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程

    待解决的问题 Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程 解决办法 为spring session添加spr ...

  4. phpexcel 导出到xls文件的时候出现乱码解决

    在header() 前面加上ob_end_clean() 函数, 清除缓冲区, 这样就不会乱码了! <?php include 'global.php'; $ids = $_GET['ids'] ...

  5. 六、uboot 代码流程分析---start.S

    6.1 _start 入口函数 6.1.1 vectors.S (arch\arm\lib) 从上一节可以知道,uboot 的入口函数为 _start .此 函数定义在 vectors.S (arch ...

  6. sql 分页row_number() over(order by key)

    select * from ( select row_number() over(order by BD008_001) as row ,* from (select * from bd008)t ) ...

  7. WinRar 压缩接压缩文件

    windows  WinRAR 定时压缩文件 命名当天时间 设置时间格式: set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%" 指定 WinRAR ...

  8. Managers经理/代理形式的数据共享

    代理方式实现进程间共享字典和列表形式的数据, 代码块: import os from multiprocessing import Process,Manager def f(d,l,normalLi ...

  9. MSSQL-SELECT&UPDATE动作要申请的锁

    最近在学习[MySQL事务&锁]这块知识,一不留神和MSSQL乱窜了~.~ 文章最初是想查看MySQL vs MSSQL在下面环境产生的阻塞现象会话1开启事务更新数据尚未提交->会话2读 ...

  10. python - classs内置方法 solt

    solt # __solt__ # 是一个类变量,变量值可以是列表.元组或者是可迭代对象,也可以是一个字符串 # (以为这所有实例只有一种数据属性) # # 作用:(为了节省内存空间,减少过多的实例属 ...