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 ...
随机推荐
- shell环境
1 引言 一个进程运行在shell环境中,理解进程运行的环境是十分重要的.环境影响着进程的行为,利用环境提供的便利,可以极大地提高开发效率.本节深入讨论shell中与进程有关的环境问题,包括命令行参数 ...
- 排序算法THREE:归并排序MergeSort
/** *归并排序思路:分治法思想 O(nlogn) * 把数组一分为二,二分为四 * 四和为二,二和为一 * */ /** * 归并排序主方法 *@params 待排序的数组 *@params 初始 ...
- jquery <li>标签 隔若干行 加空白或者加虚线
$(function () { $('ul li').addClass(function (i) { return i % 6 == 5 ? "ab" : "" ...
- 高性能IO设计的Reactor和Proactor模式(转)
在高性能的I/O设计中,有两个比较著名的模式Reactor和Proactor模式,其中Reactor模式用于同步I/O,而Proactor运用于异步I/O操作. 在比较这两个模式之前,我们首先的搞明白 ...
- C#细节忽略的问题:int 与 int?
int 与 int? 天天都在看,却不知道这2有什么区别呢? 首先说明下这个?的由来吧:C#值类型使不可谓null的,但是sql server的 int 确是可以为null的. 废话不多说直接上代码 ...
- Python Geospatial Development reading note(1)
chapter 1, Summary: In this chapter, we briefly introduced the Python programming language and the m ...
- python eval函数
eval()函数十分强大,官方demo解释为:将字符串str当成有效的表达式来求值并返回计算结果. 可以把字符串转为list.tuple .dict 等数据类型 1.把字符串转为字典 ####### ...
- 转 JavaScript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
收藏一下 1.判断select选项中 是否存在Value=”paraValue”的Item2.向select选项中 加入一个Item3.从select选项中 删除一个Item4.删除select中选中 ...
- java.sql.SQLException: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=186646784)(ERR=12505)(ERR
dbc 链接orcal出错 java.sql.SQLException: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=186646784)( ...
- redis info 各信息意义
redis_version:2.4.16 # Redis 的版本redis_git_sha1:00000000redis_git_dirty:0arch_bits:64multiplexing_api ...