Tang-Dreamoon's Blog

tvyj 1039 忠诚2

单点修改 区间查询

  题目链接:tyvj 忠诚2

  模板题目,感觉单点修改还不是太难,没啥好注意的地方,自己Y Y就挺好的。直接粘代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
int map[100100];
int now[100100*4];
int min(int a,int b)
{
if(a<=b) return a;
else return b;
}
void build(int l,int r,int pos)
{
if(l==r) now[pos]=map[l];
else
{
int mid=(l+r)/2,lson=pos*2,rson=pos*2+1;
build(l,mid,lson),build(mid+1,r,rson);
now[pos]=min(now[lson],now[rson]);
}
}
void add(int l,int r,int pos,int where,int w)
{
if(l==r) now[pos]=w;
else
{
int mid=(l+r)/2,lson=pos*2,rson=pos*2+1;
if(mid<where)
{
add(mid+1,r,rson,where,w);
}
else
{
add(l,mid,lson,where,w);
}
now[pos]=min(now[lson],now[rson]);
}
}
int ask(int l,int r,int pos,int fr,int to)
{
if(l==fr&&r==to) return now[pos];
else
{
int mid=(l+r)/2,lson=pos*2,rson=pos*2+1;
int ans=247483647;
if(mid>=to)
{
ans=ask(l,mid,lson,fr,to);
}
else if(mid<fr)
{
ans=ask(mid+1,r,rson,fr,to);
}
else
{
ans=min(ask(l,mid,lson,fr,mid),ask(mid+1,r,rson,mid+1,to));
}
return ans;
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&map[i]);
}
build(1,n,1);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&z,&x,&y);
if(z==1)
{
printf("%d ",ask(1,n,1,x,y));
}
else
{
add(1,n,1,x,y);
}
}
return 0;
}