HDU5654xiaoxin and his watermelon candy 离线+树状数组
题意:bc 77div1 d题(中文题面),其实就是询问一个区间有多少不同的三元组,当然这个三元组要符合条件
分析(先奉上官方题解)
首先将数列中所有满足条件的三元组处理出来,数量不会超过 nn个。
设 pre[i] 为第 i 个三元组前一次出现的位置,如果在前面没有出现过则设为0,对于不合法的三元组,pre[i]设为 n。
这样对于一个查询 [L, R], 我们只不需要计算出 在这个区间内有多少个三元组的 pre 值是小于 L 的。
到这里,就可以使用可持久化线段树来计算了。
-------------------------------------------------------------华丽的分割线
然后蒟蒻表示不会这种在线的主席树做法,主席树据说很好,马上去学习
然后介绍本蒟蒻的离线做法,其实都是套路,就是求一个区间包含了,多少个小区间(即三元组)
首先和题解一样,找到符合条件的三元组,记录它的序号id,这个三元组的每个元素,他的右边界
然后按照三元组排序,以及他的id,这样处理每个三元组的pre,即在他左边,与他相同的三元组的id
然后恢复下顺序
然后就是离线的套路,把查询区间按照右端点排序,更新所有三元组右边界小于他的左端点,然后区间求和,就是答案
但是这个题有一个点,要避免查询重复,所以在当前查询的时候,要保证没有重复,所以更新三元组时,要把他的pre删掉(因为相同)
这样对于重复的三元组,只保留最靠近当前查询区间右端的
代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=2e5+;
const int INF=0x3f3f3f3f;
struct Node{
int a,b,c,id,pre,r;
bool operator==(const Node &rhs)const{
if(a==rhs.a&&b==rhs.b&&c==rhs.c)
return ;
return ;
}
}o[N];
int n,T,q;
bool cmp1(Node x,Node y){
if(x.a!=y.a)
return x.a<y.a;
if(x.b!=y.b)
return x.b<y.b;
if(x.c!=y.c)
return x.c<y.c;
return x.id<y.id;
}
bool cmp2(Node x,Node y){
return x.id<y.id;
}
int d[N],ans[N];
struct Q{
int l,r,id;
bool operator<(const Q &rhs)const{
return r<rhs.r;
}
}p[N];
int bit[N];
void add(int x,int t){
for(int i=x;i<=n;i+=i&(-i))
bit[i]+=t;
}
int get(int x){
int ans=;
for(int i=x;i>;i-=i&(-i))
ans+=bit[i];
return ans;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
int cnt=;
for(int i=;i<=n;++i){
scanf("%d",&d[i]);
if(i<=)continue;
if(d[i]>=d[i-]&&d[i-]>=d[i-]){
++cnt;
o[cnt].a=d[i-],o[cnt].b=d[i-];
o[cnt].c=d[i],o[cnt].pre=-;
o[cnt].r=i,o[cnt].id=cnt;
}
}
sort(o+,o++cnt,cmp1);
for(int i=;i<=n;++i){
if(o[i]==o[i-]){
o[i].pre=o[i-].id;
}
}
sort(o+,o++cnt,cmp2);
scanf("%d",&q);
for(int i=;i<=q;++i){
scanf("%d%d",&p[i].l,&p[i].r);
p[i].id=i;
}
sort(p+,p++q);
int pos=;
memset(bit,,sizeof(bit));
for(int i=;i<=q;++i){
for(;pos<=cnt&&o[pos].r<=p[i].r;++pos){
int pre=o[pos].pre;
if(pre!=-)
add(o[pre].r-,-);
add(o[pos].r-,);
}
ans[p[i].id]=get(p[i].r)-get(p[i].l-);
}
for(int i=;i<=q;++i)
printf("%d\n",ans[i]);
}
return ;
}
HDU5654xiaoxin and his watermelon candy 离线+树状数组的更多相关文章
- HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数
xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...
- HDU 5654 xiaoxin and his watermelon candy 离线树状数组
xiaoxin and his watermelon candy Problem Description During his six grade summer vacation, xiaoxin g ...
- 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)
pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...
- POJ 3416 Crossing --离线+树状数组
题意: 给一些平面上的点,然后给一些查询(x,y),即以(x,y)为原点建立坐标系,一个人拿走第I,III象限的点,另一个人拿II,IV象限的,点不会在任何一个查询的坐标轴上,问每次两人的点数差为多少 ...
- HDU 2852 KiKi's K-Number(离线+树状数组)
题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)
转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...
- HDU3333 Turing Tree 离线树状数组
题意:统计一段区间内不同的数的和 分析:排序查询区间,离线树状数组 #include <cstdio> #include <cmath> #include <cstrin ...
- 离线树状数组 hihocoder 1391 Countries
官方题解: // 离线树状数组 hihocoder 1391 Countries #include <iostream> #include <cstdio> #include ...
随机推荐
- Missra开源前端框架
Missra开源前端框架,官方网址:http://framework.missra.com
- PHP中使用cURL
1.cURL介绍 cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性 ...
- 写 一个PHP脚本遇到的问题总结
在项目中,因为之前的人员,基础数据没有处理好,后面需要写一个脚本来处理这个问题,经验少,总结如下: 1.在linux下直接连接跑处理MySQL数据的脚本,要用PDO的方式连接数据库,长时间在框架中处理 ...
- 【python】Windows安装Beautiful Soup
环境:win10,python 3.5,Beautiful Soup 4.1 步骤1:设定python为系统环境变量,具体设置如下图,在文本后加上";C:\Python35;C:\Pyt ...
- python with关键字学习
1.with语句时用于对try except finally 的优化,让代码更加美观, 例如常用的开发文件的操作,用try except finally 实现: f=open('file_name', ...
- PL/SQL — 变长数组
PL/SQL变长数组是PL/SQL集合数据类型中的一种,其使用方法与PL/SQL嵌套表大同小异,唯一的区别则是变长数组的元素的最大个数是有限制的.也即是说变长数组的下标固定下限等于1,上限可以扩展.下 ...
- hive 中的Sort By、 Order By、Cluster By、Distribute By 区别
Order by: order by 会对输入做全局排序,因此只有一个reducer(多个reducer无法保证全局有序)只有一个reducer,会导致当输入规模较大时,需要较长的计算时间.在hive ...
- oct(x) 将一个数字转化为8进制
>>> a = 12 >>> b 21 >>> c = oct(a) >>> d = oct(b) >>> c ...
- xxx couldn't be loaded because it has not been added to the build settings.
这个由于没有将进入场景放入Build Settings里面造成的.
- UPUPW PHP环境集成包
UPUPW PHP环境集成包 http://www.upupw.net/