tyvj:1038 忠诚 线段树
2015-03-30 23:00
363 查看
tyvj:1038 忠诚
Time Limit: 1 Sec Memory Limit: 131072KiBSubmit: 9619 Solved: 3287
题目连接
http://www.tyvj.cn/p/1038Description
老管家是一个聪明能干的人。他为财主工作了整整10年,财主为了让自已账目更加清楚。要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满 意。但是由于一些人的挑拨,财主还是对管家产生了怀疑。于是他决定用一种特别的方法来判断管家的忠诚,他把每次的账目按1,2,3…编号,然后不定时的问 管家问题,问题是这样的:在a到b号账中最少的一笔是多少?为了让管家没时间作假他总是一次问多个问题。Input
输入中第一行有两个数m,n表示有m(m<=100000)笔账,n表示有n个问题,n<=100000。第二行为m个数,分别是账目的钱数
后面n行分别是n个问题,每行有2个数字说明开始结束的账目编号。
Output
输出文件中为每个问题的答案。具体查看样例。Sample Input
10 31 2 3 4 5 6 7 8 9 10
2 7
3 9
1 10
Sample Output
2 3 1HINT
[b]题解:[/b]线段树裸题~
[b]代码:[/b]
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 400001 #define mod 10007 #define eps 1e-9 //const int inf=0x7fffffff; //无限大 const int inf=0x3f3f3f3f; /* inline ll read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } */ //************************************************************************************** int n,q,a[maxn]; struct data { int l,r,mn; }tr[maxn]; void build(int k,int s,int t) { tr[k].l=s;tr[k].r=t; if(s==t) { tr[k].mn=a[s]; return; } int mid=(s+t)>>1; build(k<<1,s,mid); build(k<<1|1,mid+1,t); tr[k].mn=min(tr[k<<1].mn,tr[k<<1|1].mn); } int ask(int k,int s,int t) { int l=tr[k].l,r=tr[k].r; if(s==l&&t==r) return tr[k].mn; int mid=(l+r)>>1; if(t<=mid) return ask(k<<1,s,t); if(s>mid) return ask(k<<1|1,s,t); return min(ask(k<<1,s,mid),ask(k<<1|1,mid+1,t)); } int main() { int n,q; scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } build(1,1,n); for(int i=1;i<=q;i++) { int x,y; scanf("%d%d",&x,&y); printf("%d ",ask(1,x,y)); } return 0; }
相关文章推荐
- tyvj1038--忠诚(线段树)
- tyvj 1038 忠诚 区间最小值 线段树或者rmq
- 【Tyvj1038】忠诚 线段树
- tyvj:1038 忠诚 线段树 区间查询
- Tyvj 1038-1039 忠诚1-2 【线段树】
- tyvj1038忠诚题解
- p 1038 忠诚 线段树(水)
- 【Tyvj1038】忠诚
- TyvjOJ 题目P1039 忠诚2(线段树单点跟新,区间最小值)
- 【tyvj1038】忠诚
- tyvj P1038 忠诚 线段树
- Tyvj 1038 忠诚
- TYVJ1038 忠诚
- tyvj 1039忠诚2——初识线段树
- 【tyvj1038】忠诚
- 忠诚 && 士兵杀敌(tyvj 1038 && nyoj116)
- 线段树 TYVJ 1039 忠诚
- [Tyvj 1038] 忠诚
- 计蒜客 管家的忠诚 线段树
- [bzoj3196][Tyvj 1730][二逼平衡树] (线段树套treap)