Hdu-5919 Sequence II(主席树在线求区间不同数)
Description
In the i-th query, you are given two integers li and ri. Consider the subsequence ali,ali+1,ali+2,⋯,ari.
We can denote the positions(the positions according to the original sequence) where an integer appears first in this subsequence as p(i)1,p(i)2,⋯,p(i)ki (in ascending order, i.e.,p(i)1<p(i)2<⋯<p(i)ki).
Note that ki is the number of different integers in this subsequence. You should output p(i)⌈ki2⌉for the i-th query.
Input
Each test case starts with two integers n (n≤2×105) and m (m≤2×105). There are n integers in the next line, which indicate the integers in the sequence(i.e., a1,a2,⋯,an,0≤ai≤2×105).
There are two integers li and ri in the following m lines.
However, Mr. Frog thought that this problem was too young too simple so he became angry. He modified each query to l‘i,r‘i(1≤l‘i≤n,1≤r‘i≤n). As a result, the problem became more exciting.
We can denote the answers as ans1,ans2,⋯,ansm. Note that for each test case ans0=0.
You can get the correct input li,ri from what you read (we denote them as l‘i,r‘i)by the following formula:
Output
For each test case, output one line “Case #x: p1,p2,⋯,pm”, where x is the case number (starting from 1) and p1,p2,⋯,pm is the answer.
Sample Input
2 5 2 3 3 1 5 4 2 2 4 4 5 2 2 5 2 1 2 2 3 2 4
Sample Output
Case #1: 3 3 Case #2: 3 1
题意:给一个数列,有m个询问,每次问[l,r]区间中所有数的第一次出现的位置的中位数是多少(有点绕),强制在线。
分析:比赛时队友黄大爷临时想出了一个分块的做法卡过了这题Orz,这题的一般做法是在主席树上做二分,倒着插点建树,然后对于每个询问的l,我们都可以得到一个从l开始的权值线段树,表示每个位置上的数是否是第一次出现,然后我们对于每个询问l,r,就可以二分它的中位数的位置了直接在权值线段树中递归下去就可以了。
#include <cstdio>
#include <iostream>
#include <algorithm>3e
#include <cstdlib>
#include <cstdio>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <cstring>
#define MOD 1000000007
#define N 200005
using namespace std;
typedef long long ll;
int T,n,m,cnt,pos,a[N],b[N],rt[N],f[N];
struct Tree
{int ls,rs,sum;
}tr[N*40];
void Build(int &node,int l,int r)
{node = ++cnt;tr[node].sum = 0;if(l == r) return;int mid = (l+r)>>1;Build(tr[node].ls,l,mid);Build(tr[node].rs,mid+1,r);
}
void Insert(int pre,int &node,int x,int l,int r)
{node = ++cnt;tr[node] = tr[pre];tr[node].sum += x;if(l == r) return;int mid = (l + r) >> 1;if(pos <= mid) Insert(tr[pre].ls,tr[node].ls,x,l,mid);else Insert(tr[pre].rs,tr[node].rs,x,mid+1,r);
}
int Ask(int node,int x,int l,int r)
{if(l == r) return tr[node].sum;int mid = (l+r)>>1;if(x <= mid) return Ask(tr[node].ls,x,l,mid);else return tr[tr[node].ls].sum + Ask(tr[node].rs,x,mid+1,r);
}
int Query(int node,int k,int l,int r)
{if(l == r) return l;int mid = (l+r)>>1;if(tr[tr[node].ls].sum >= k) return Query(tr[node].ls,k,l,mid);else return Query(tr[node].rs,k-tr[tr[node].ls].sum,mid+1,r);
}
int main()
{scanf("%d",&T);for(int t = 1;t <= T;t++){cnt = 0;memset(f,0,sizeof(f));scanf("%d%d",&n,&m);for(int i = 1;i <= n;i++) scanf("%d",&a[i]);printf("Case #%d:",t);Build(rt[n+1],1,n);for(int i = n;i;i--){pos = f[a[i]];if(pos){Insert(rt[i+1],rt[i],-1,1,n);pos = i;Insert(rt[i],rt[i],1,1,n);}else{pos = i;Insert(rt[i+1],rt[i],1,1,n);}f[a[i]] = i;}int pre = 0;for(int i = 1;i <= m;i++){int l,r;scanf("%d%d",&l,&r);l = (l + pre) % n + 1,r = (r + pre) % n + 1;if(l > r) swap(l,r);int k = Ask(rt[l],r,1,n);pre = Query(rt[l],(k+1)/2,1,n);printf(" %d",pre);}printf("\n");}
}
Hdu-5919 Sequence II(主席树在线求区间不同数)相关推荐
- HDU - 5919 Sequence II——主席树+区间种类++逆序建树
[题目描述] HDU - 5919 Sequence II [题目分析] 题目给定一个数组,每次查询一个区间,找出区间内不同数字的个数x,然后输出按出现顺序第x/2向上取整个数字的位置. 按照要求,我 ...
- HDU 5919 Sequence II 主席树
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5919 Sequence II Time Limit: 9000/4500 MS (Java/Othe ...
- HDU - 5919 Sequence II(主席树+思维)
题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a ,再给出 m 次询问,每次询问给出一个区间 [ l , r ] ,问区间 [ l , r ] 内首次出现的数字的位置的中位数 题目分析:题 ...
- HDU - 5919 Sequence II
题意: 给定长度为n的序列和q次询问.每次询问给出一个区间(L,R),求出区间内每个数第一次出现位置的中位数,强制在线. 题解: 用主席树从右向左的插入点.对于当前点i,如果a[i]出现过,则把原位置 ...
- zoj 2112 树状数组 套主席树 动态求区间 第k个数
总算是把动态求区间第k个数的算法看明白了. 在主席树的基础上,如果有修改操作,则要通过套树状数组来实现任意区间求第k小的问题. 刚开始看不明白什么意思,现在有一点理解.树状数组的每个元素是一个线段树, ...
- hdu 3397 Sequence operation(线段树,lazy,区间合并)
hdu 3397 Sequence operation 线段树lazy和区间合并结合的一个题,相当于几个题集中到一起嘛,分开想就好了 0,1,2操作都要lazy,2的异或操作找到每一只含1或只含0的区 ...
- HDU - 4417 Super Mario(主席树/线段树+离线)
题目链接:点击查看 题目大意:给出由 n 个数的数列,再给出 m 次查询,每次查询需要输出 [ l , r ] 内小于等于 h 的数有多少个 题目分析:大晚上睡不着觉随便做做题,发现这个题目原来可以用 ...
- [蓝桥杯][2016年第七届真题]压缩变换(主席树求区间不同数的个数)
题目描述 小明最近在研究压缩算法. 他知道,压缩的时候如果能够使得数值很小,就能通过熵编码得到较高的压缩比. 然而,要使数值很小是一个挑战. 最近,小明需要压缩一些正整数的序列,这些序列的特点是,后面 ...
- #HDU 4417 Super Mario (主席树 + 二分)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
最新文章
- FusionCharts简明教程(一)---建立FusionCharts图形
- 解决Linux下inode使用率100%的问题
- MyBatis开发Dao的方法
- SVN中“txn-current-lock:拒绝访问”错误
- acwing2058. 笨拙的手指(进制转换)
- 最新的Windows phone Developer tools RTW 发布了
- mysql applier_MySQL推出Applier,可实时复制数据到Hadoop-阿里云开发者社区
- JS实现鼠标点击展开/隐藏表格行
- IOS 自定义导航栏标题和返回按钮标题
- php 时间戳存储 原因,将php文件中的unix时间戳存储到mysql中(store unix timestamp from php file into mysql)...
- 【路径规划】基于matlab灰狼算法求解机器人栅格地图路径规划问题【含Matlab源码 1761期】
- virtualbox虚拟机下的cdlinux找不到无线网卡的解决方法
- 【bat】bat批处理安装与卸载软件,自动安装
- 单基因gsea_零代码5分+的单基因综合分析
- SSL双向认证和SSL单向认证的流程和区别
- mybatis SQL打印插件
- 微信一次性订阅消息流程
- 实验一 机械臂正逆运动学
- 计算机键盘gt,雷神(Thunderobot)911GT-Y1笔记本电脑键盘评测-ZOL中关村在线
- 市场营销的角度探讨企业网站建设的营销策略
热门文章
- Linux音乐相册代码,小年糕音乐相册制作下载-小年糕视频编辑appv1.5.8-Linux公社
- HP 惠普笔记本电脑 禁用触摸板 在插入鼠标后
- linux硬盘速度测试方法
- mysql报错error2002_mysql中异常出错ERROR:2002的处理办法分享
- Java入门教程笔记07(面向对象基础)
- 吾生也有涯,而知也无涯。
- html是什么意思网络用语,网络用语内涵是什么意思
- SCD-缓慢变化维-拉链表
- 多条ADSL线路合并叠加效果测试,多WAN口聚合
- 1146 mysql_MySQL--ERROR 1146 (42S02):table doesn’t exist