Problem Description

We believe that every inhabitant of this universe eventually will find a way to live together in harmony and peace; that trust, patience, kindness and loyalty will exist between every living being of this earth; people will find a way to appreciate and cooperate with each other instead of continuous bickering, arguing and fighting. Harmony -- the stage of society so many people dream of and yet it seems so far away from now ...
Fortunately, the method of unlocking the key to true Harmony is just
discovered by a group of philosophers. It is recorded on a strange meteorite
which has just hit the earth. You need to decipher the true meaning behind those
seemingly random symbols ... More precisely, you are to write a program which
will support the following two kinds of operation on an initially empty set S
:
1.
B X : Add number X to set S . The Kth command in the form of B X
always happens at time K , and number X does not belong to set S before this
operation.
2.
A Y : Of all the numbers in set S currently, find the one
which has the minimum remainder when divided by Y . In case a tie occurs, you
should choose the one which appeared latest in the input. Report the time when
this element is inserted.
It is said that if the answer can be given in
the minimum possible time, true Harmony can be achieved by human races. You task
is to write a program to help us.

Input

There are multiple test cases in the input file. Each
test case starts with one integer T where 1<=T<=40000 . The following T
lines each describe an operation, either in the form of ``B X " or ``A Y " where
1<=X , Y<=500000 .

T = 0 indicates the end of input file and should
not be processed by your program.

Output

Print the result of each test case in the format as
indicated in the sample output. For every line in the form of ``A Y ", you
should output one number, the requested number, on a new line; output -1 if no
such number can be found. Separate the results of two successive inputs with one
single blank line.

Sample Input

5
B 1
A 5
B 10
A 5
A 40
2
B 1
A 2
0

Sample Output

Case 1:
1
2
1
Case 2:
1
 

题意

  让你拯救世界

  给定一空集合,有两种操作,往集合里加数或者求出集合中的数在mod k意义下最小值是第几个加入的,相等的话输出最后加入的

分析

  不管怎么样先想一个暴力吧,单开一个数组记录往里边加的数,下边i表示加入时间,每次查询遍历一遍数组就行

 

 #include<cstdio>
const int N=1e5+;
int a[N];
int main(){
char ss[];
int num,t,cas=;
while(~scanf("%d",&t)){
int len=;
if(t==)return ;
if(cas)printf("\n");
printf("Case %d:\n",++cas);
while(t--){
scanf("%s%d",ss,&num);
if(ss[]=='B')
a[++len]=num;
else {
bool flag=;
int Min=num,ans;
for(int i=;i<=len;i++){
if(a[i]%num<=Min){
flag=;
Min=a[i]%num;
ans=i;
}
}
if(flag)printf("-1\n");
else printf("%d\n",ans);
}
}
}
}

  抱着试一试的心态交了上去,A了!!!没错它A了,但显然这不是正解,只是HDU数据水了,我自己手造了一组极限数据就跑了2s多,然后我又把它交到了POJ上,果然是TLE。

  那肯定是要优化咯,怎么优化呢?涉及到mod的问题,如果要mod k在连续长度大于k的区间中,mod k一定会至少有两个数相等,所以我们可以考虑将50w分块,分为0-k-1,k-2k-1…………这样遍历每一块,每一块中最小的数mod k就有可能是答案,于是这个问题就成了,在区间内找到最小的已经出现过的数字,涉及到区间问题,很容易想到线段树和树状数组,但这个问题好像没有必要用线段树,树状数组足以。

  开一棵50w的树状数组,每一个点记录到这个点一共出现了多少数字,接着就是找最小的已经出现过的数,那么不就又是暴力了吗?查找的方法除了暴力就只会二分,于是我们考虑二分查找,怎么二分呢?每次查找区间的时候,对该区间进行二分就行了,那你怎么知道是要向左分还是向右分?用树状数组很容易求得到区间左端点的点总数,取mid,如果到mid的点总数和到左端的点总数相等,说明l到mid之间没有数,改变左端点,如果有就改变右端点,更新最小值,继续二分直到l==r,树状数组的做法就是这样,线段树也大致差不多。

   还有一个问题,如果k过小了,比如k就是1,那么我们就会将区间分成50w块,50w啊!而暴力最多只枚举4w,所以我们可以设置一个值,让k过大的时候直接用暴力。

  

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int limit=;//这个数还可以换,让它别太小就行
//不要问我为什么非用这个奇怪的数
const int N=5e5+;
int q[N+],tim[N+],a[N+],len,c[N+];//多开5,防止RE
int lowbit(int i){
return i&(-i);
}
void Add(int x){
while(x<=N){
c[x]++;
x+=lowbit(x);
}
}
int n,k;
void Ins(int x){
tim[++len]=x;
a[x]=len;
for(int i=;i<=limit;i++){
if(q[i]==)q[i]=len;
else if(x%i<=tim[q[i]]%i)q[i]=len;//等于也要替换,因为优先输出后读入的
}
Add(x);//加到树状数组里统计前缀和
}
int front_sum(int x){
int sum=;
while(x){
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
int low_find(int ll,int rr){
int l,r,Min=;
if(ll==)l=;
else l=ll;
if(rr>N)r=N;
else r=rr;
int pre=front_sum(l-);
while(l<=r){
int mid=l+r>>;
int now=front_sum(mid);
if(now>pre){//说明mid到l之间出现了值,向左找
r=mid-;
Min=mid;
}else l=mid+;//没有值就向右找
}
return Min;
}
void calc(int x){
if(len==){printf("-1\n");return;}
if(x<=limit){printf("%d\n",q[x]);return;}
int l=,r=x-,ans=x-;
while(l<=N){//判断左边界,判断右边界的话取值可能不完全
int now=low_find(l,r);
if(now&&(now%x<ans%x||now%x==ans%x&&a[now]>a[ans])){
//等于和小于两种情况分开写,合在一起不行
ans=now;
}
l+=x;r+=x;
}
printf("%d\n",a[ans]);
}
int main(){
int cas=;
while(~scanf("%d",&n)){
if(n==)return ;
for(int i=;i<=N;i++){
tim[i]=a[i]=c[i]=q[i]=;
}
len=;
if(cas)printf("\n");
printf("Case %d:\n",++cas);
for(int i=;i<=n;i++){
char ss[];
scanf("%s%d",ss,&k);
if(ss[]=='A')calc(k);
else Ins(k);
}
}
}

HDU 3303 Harmony Forever 前缀和+树状数组||线段树的更多相关文章

  1. 树状数组 && 线段树应用 -- 求逆序数

    参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...

  2. hdu1394(枚举/树状数组/线段树单点更新&区间求和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...

  3. 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树

    正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...

  4. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  5. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...

  7. hdu 1166 敌兵布阵——(区间和)树状数组/线段树

    pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...

  8. HDU 1166 敌兵布阵 树状数组||线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...

  9. Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化

    题意 一个长度$n<=1e5$的数轴,$m<=1e5$个操作 有两种一些操作 $0$  $x$ 在$x$放一个食物 $1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃 ...

随机推荐

  1. 5G时代,会有什么奇葩事儿?

    ​ 在3GPP RAN第187次会议关于5G短码方案的讨论中,中国华为推荐的PolarCode方案获得认可,成为5G控制信道eMBB场景编码的最终解决方案.坦白讲,笔者在读这个新闻的时候,手里备着一本 ...

  2. Vue组件传递数据

    组件命名 1.字母全小写且必须包含一个连字符 my-componnect 2.使用 kebab-case(短横线分隔命名) 定义一个组件时,你也必须在引用这个自定义元素时使用 kebab-case,例 ...

  3. 记一个 Base64 有关的 Bug

    本文原计划写两部分内容,第一是记录最近遇到的与 Base64 有关的 Bug,第二是 Base64 编码的原理详解.结果写了一半发现,诶?不复杂的一个事儿怎么也要讲这么长?不利于阅读和理解啊(其实是今 ...

  4. 放弃了程序员互联网高薪,跑去事业单位做IT的尴尬

    “你是程序员对吧?”“是啊,怎么了?”“那你帮我修一下电脑吧.”我原来也是一个重点大学毕业,基本上事业里面搞IT就干这些事情,要是以前,我肯定会想,我是程序员和修电脑有啥关系. 但是自从进了事业单位, ...

  5. 7-49 求前n项的阶乘之和 (15 分)

    从键盘输入一个整数n,求前n项的阶乘之和,1+2!+3!+...+n!的和 输入格式: 输入一个大于1的整数.例如:输入20. 输出格式: 输出一个整数.例如:2561327494111820313. ...

  6. node跨域方法

    第一种:jsonp 参看用nodejs实现json和jsonp服务 第二种:res.wirteHeadnode部分 var http = require('http') var url = requi ...

  7. vue 动态加载图片路径报错解决方法

    最近遇到图片路径加载报错的问题 之前一直都是把图片放到assets的文件下的.总是报错,看到一些文章并且尝试成功了,特意记录下 首先先说明下vue-cli的assets和static的两个文件的区别, ...

  8. 《即时消息技术剖析与实战》学习笔记12——IM系统如何提升图片、音视频消息发送、浏览的体验

    IM系统如何提升用户发送.浏览图片和音视频消息的体验呢?一是保证图片.音视频消息发送得又快又稳,二是保证用户浏览播放图片.音视频消息时流畅不卡顿. 一.提升用户发送图片.音视频的体验 1. 多上传接入 ...

  9. MySQL5.7 数据库的my.cnf配置

    [client] port socket=/tmp/mysql.sock default-character-set=utf8 [mysql] no-auto-rehash default-chara ...

  10. 为Python安装pip

    Python及操作系统的支持 Python 2.6, 2.7, 3.2, 3.3, 3.4 Unix/Linux, OS X, 以及 Windows   默认包含 Python 2.7.9 及以后的版 ...