POJ3145: Harmony Forever
来源:互联网 发布:淘宝黑号是什么意思 编辑:程序博客网 时间:2024/06/10 14:46
POJ3145: Harmony Forever
线段树·乱搞
题解:
http://blog.csdn.net/sdj222555/article/details/7383593
Code:
#include <iostream>#include <cstring>#include <cstdio>#define MP make_pair#define D(x) cout<<#x<<" = "<<x<<" "#define E cout<<endlusing namespace std;typedef pair<int,int> pii;const int mxn = 5e5;const int INF = 0x3f3f3f3f;int n,a[mxn];struct Node{ int l,r; pii d;} pool[mxn*4];void update(int x){ pool[x].d=min(pool[x*2].d,pool[x*2+1].d);}void build(int x,int l,int r){ Node &t=pool[x]; t.l=l; t.r=r; t.d=MP(INF,-1); if(l!=r){ int mid=(l+r)>>1; build(x*2,l,mid); build(x*2+1,mid+1,r); }}void insert(int x,int d,int tim){ Node &t=pool[x]; if(t.l==t.r){ t.d=MP(d,tim); } else{ int mid=(t.l+t.r)>>1; if(d<=mid) insert(x*2,d,tim); else insert(x*2+1,d,tim); update(x); }}pii query(int x,int ql,int qr){ Node &t=pool[x]; //D(x); D(t.l); D(t.r); E; if(ql<=t.l && t.r<=qr){ return t.d; } else{ int mid=(t.l+t.r)>>1; if(qr<=mid) return query(x*2,ql,qr); else if(ql>mid) return query(x*2+1,ql,qr); else return min(query(x*2,ql,qr),query(x*2+1,ql,qr)); }}void baoli(int d){ int mn=INF, pos=-1; for(int i=n;i>=1;i--){ if(a[i]%d<mn){ mn=a[i]%d; pos=i; } } printf("%d\n",pos);}void solve(int d){// D(d); E; int mn=INF, pos=-1; for(int i=0;i<=mxn;i+=d){// D(i); D(min(mxn,i+d-1)); E; pii x=query(1,i,min(mxn,i+d-1));// D(x.first); D(x.second); E; if(x.second!=-1){ if(x.first%d<mn || (x.first%d==mn && x.second>pos)){ mn=x.first%d; pos=x.second; } } } printf("%d\n",pos);}int main(){ freopen("a.in","r",stdin); freopen("a.out","w",stdout); int T,d,cas=0; char op[5]; while(scanf("%d",&T)!=EOF && T){ if(cas) puts(""); printf("Case %d:\n",++cas); n=0; build(1,1,mxn); for(int i=1;i<=T;i++){ scanf("%s%d",op,&d); if(op[0]=='A'){ if(d<=10000) baoli(d); else solve(d); } else{ a[++n]=d; insert(1,d,n); } } }}
阅读全文
0 0
- POJ3145: Harmony Forever
- poj3145 / hdu3303 Harmony Forever(树状数组 + 鸽巢原理)
- hdu 3303 Harmony Forever
- POJ 3145 Harmony Forever
- D POJ 3145 Harmony Forever
- hdu Harmony Forever (线段树 )
- POJ 3145 Harmony Forever 笔记
- poj 3145 Harmony Forever(线段树)
- HDU 3303 Harmony Forever(数学)
- poj 3145 Harmony Forever
- POJ-3145-Harmony Forever-线段树暴力
- POJ 3145 HDU 3303 Harmony Forever 线段树 + 鸽巢定理
- Forever
- forever
- POJ 3145Harmony Forever(线段树更新+分情况处理数据+区间极值查询+好题)
- poj3145(线段树)
- Harmony毕业!
- Harmony毕业!
- Others(理论知识)
- STL deque的一些函数练习
- 使用github+hexo搭建免费个人博客
- CodeChef:Magic Board(思维 & 树状数组)
- Python学习篇 之 如何实现多态机制
- POJ3145: Harmony Forever
- linux多线程之mutex
- PAT[1046]划拳
- 线性代数之行列式(1) ——行列式的定义以及二阶行列式
- MyEclipse启动Tomcat报outOfMerorgError
- html 应用记录
- 接口自动化测试:mock server之Moco工具
- 第十一天-文件File类
- grep命令中文手册(info grep翻译)