2019第十届蓝桥杯B组决赛题解第九题
8 V! {% l8 u3 \# M# U$ V3 V* h' Z8 K; y
题意: 两种操作,C x y,将x位置的数修改为y,Q x y,查询[x,y]之间的第8大值,y-x+1<= 8的话输出0 思路: 区间第8大值,线段树在时间、空间都够了(蓝桥怎么会让手写主席树。。)
* Z9 b$ N8 g7 j; J& G每个节点存储它管辖的这个区间的前8大值,修改的时候暴力merge,单次修改复杂度log(n)*8. A' `0 N& `2 d0 `
查询的时候返回含有8个值得list,并不断merge 代码:
8 Q+ _3 B' r: f#include<bits/stdc++.h>! m: W7 V) w5 T$ g8 q
#define mem(a,b) memset(a,b,sizeof(a))- ~) F& y2 E# w4 ]4 W
using namespace std; W6 z: M" _8 X; i3 x" J: ?* [
typedef long long ll;2 M7 F7 w" o! G" P/ P, _
const int inf = 0x3f3f3f3f;" a$ A: {8 A8 G- @
const int maxn = 1e5+55555;
3 Z7 L) a" c: Mconst ll mod = 998244353;
6 }, x/ Y: x3 ]$ s2 Q( h+ rconst double eps = 1e-7;
1 }1 H% x3 Q: {" \, a
3 _9 Z; |+ ? Dstruct tree {
1 s* @. q [$ H( [ int l,r;
! {( G- O9 y3 l6 e8 s& ^% u3 F int p[10];2 R6 i5 W/ z" ?/ U2 _4 C ^
} t[maxn<<2];
& \/ T! u/ h7 H& T) c
$ i4 X# O ?" x+ s. d/ Fint l,n;
4 Z4 }* N) P' o' j2 E2 Y- q @1 F& I! m M$ w3 e, q2 Z; @2 f
void build(int i,int l,int r) {# D+ y0 s/ I% y9 m; {! t2 E9 s
t.l = l;
9 V7 p& b5 m* v, G' X6 ~/ [ t.r = r;
: m4 ]' M2 W1 h- R mem(t.p,0);7 \: {) |4 C0 X5 l1 o6 K# Q
! z# T6 p+ |+ @; g/ T* O r
if(l == r) return ;4 D5 Y6 O# l+ P5 P& P Q
int mid = (l+r)>>1;
5 @7 X7 h- I# y6 u1 o4 n build(i<<1,l,mid);
7 B" W" a4 V5 I# [: Z" ^ build(i<<1|1,mid+1,r);+ W4 f b/ E. w$ i$ P! O; s
return ;
' a2 q) U+ l9 ?" N. s5 N' c3 g} t* o. U; g! X/ {( g
8 Y) ]# d. |' M' g/ t7 qvoid update(int i) {
& k7 o! q$ T$ m' D% R8 { int cnt1 = 0,cnt2 = 0;
Z# F3 n# u/ e6 k4 M for(int j = 0;j< 8;j++) {8 ^8 s3 T6 B4 q5 u X7 a, L
if(t[i<<1].p[cnt1]> t[i<<1|1].p[cnt2]) {; W; B+ ]1 g S" O% S5 B# \1 W
t.p[j] = t[i<<1].p[cnt1];
$ ]0 F9 }3 _; N& L" S4 I6 d& j5 Z cnt1++;$ Z2 D( _6 s4 h h* G
} else {! P) j/ x6 J; q: ]
t.p[j] = t[i<<1|1].p[cnt2];2 T0 q& P8 y) r- t& n4 z8 ^
cnt2++;" I6 {! r$ f1 o& Y& x4 c, M
}
9 y/ Z1 L0 [5 Z( U u }+ g, c* w! ]$ ~& Q% L2 }
return ;! q+ A' O/ H/ Q
}3 f1 n5 @. O6 F: w0 w2 W) B
+ f' _& b* ~1 G3 [8 z4 @
void modify(int i,int pos,int val) {
Z8 E4 s: ^0 ? if(t.l> pos || t.r< pos) return ;6 S. p. i4 L- h" s" _8 `$ c
if(t.l == t.r) {
8 ]) g h2 y# C1 y# Z5 e t.p[0] = val;! j3 [" ~& Q2 ~ a3 M, O
return ;
% ]5 {) k' T" U5 h }
" s1 S8 y1 W. O% n- C modify(i<<1,pos,val);
% j+ C F( @. v1 K9 `+ t modify(i<<1|1,pos,val);1 l4 m+ S/ _, S. q" s* n0 A
update(i);' c9 Q* O5 p+ O! f* A
return ;8 ^- t2 s, o& q6 ~+ J% _. X0 S# n- d
}3 f7 l3 y7 m% n" D" b2 ^
y# C; v8 \+ g' N
vector<int> merge(vector<int> ans1,vector<int> ans2) {% l8 V2 H. |4 ~; I& ~5 S
vector<int> ans;& ^% E0 `% e4 P {6 \
int cnt1 = 0,cnt2 = 0;( e& c1 u3 L% L! ^- {' I
for(int j = 0;j< 8;j++) {/ S7 q7 K% V% Z3 s2 {( b
if(ans1[cnt1]> ans2[cnt2]) {! Y* W' a6 e# D1 v: g& q
ans.push_back(ans1[cnt1]);1 s, [5 n, s ?: |/ B0 N# e, S
cnt1++;7 m5 A; ?3 L5 e# t0 B8 e
} else {2 _6 B$ ~) T3 K, O
ans.push_back(ans2[cnt2]);) O# R, [0 ~( q) v
cnt2++;
- e/ r) Z0 G. z; v9 k: q& p ]" O8 O. k }8 ?0 M; {7 b. q B' v
}
: D7 T* l& _/ W# L- L: g, ~ return ans;) V( w9 l }! f. M9 e( ]
}
+ @8 M( A, V% }& ~7 k
0 g6 I. W/ N3 y' |# R! k. Uvector<int> query(int i,int l,int r) {
! M% w8 i" ]8 s vector<int> ans;
+ M) M# D8 O" d9 \* G) o/ F if(t.l> r||t.r< l) {
4 p3 v3 _+ M0 Z* ?& } for(int j = 0;j< 8;j++) ans.push_back(0);
L/ k' D& H* T& M0 E _ return ans;
. d" j4 W* x. S" k: c6 [ }6 k5 _& ]4 i. J& e p* M' I3 o
8 o8 w" C6 P& X* W% L8 m+ a6 m if(t.l>= l&&t.r<= r) {
7 D b1 n/ S3 S, H for(int j = 0;j< 8;j++) ans.push_back(t.p[j]); }7 |" W. L3 s) ]
return ans;
x# E8 r3 S% `+ I2 X }
/ l9 S! o9 c+ k
* N, u9 ]' ]% A' { return merge(query(i<<1,l,r),query(i<<1|1,l,r));
) @& l+ e+ ^& E: C; V}# }8 |6 k! \$ @1 y/ x0 A
0 B9 }/ ~; J( o, w9 Y! T& ` d
int main() {
, n! p, |% o$ j+ I$ C cin>>l>>n;
/ x: M, }5 m; V+ J
+ m6 w' P5 a' ~: q build(1,1,l);
7 l {/ H* r$ Y6 D4 G* V- W/ x- z char c;% p _) K ^7 S
int x,y;5 n# O1 J8 ^0 `8 L+ u8 z4 w
while(n--) {, K- ~1 L' s# }% m- o
scanf(" %c %d %d",&c,&x,&y);5 u' X2 K, k; ]6 K! O! n/ e
if(c == 'C') {" F, ?5 w# k: l A. K* S
modify(1,x,y);
8 A) m$ L- G" ? } else {9 ?- r) G# R0 [7 \/ \; q1 U
if(y-x+1< 8) {- O. }& j2 i: w
printf("0\n");; O$ [+ q7 |* q+ Z/ c
continue;) H' f3 ] m( a5 V Z) v* C9 X5 O
}
3 z8 z8 e& C. z: a2 T$ | vector<int> ans = query(1,x,y);
y# Y7 x/ Y7 v( P printf("%d\n",ans[7]);; y' j0 s9 Z4 X) [7 n& K% {5 i3 N
}; P8 N$ n( p! Y" Z
}) K; ^1 Z/ a% m5 o
$ Z! D# x! t: Z, s
return 0;
8 _) Q8 t1 Z4 C7 _/ W} B2 A s5 \5 M
0 X" P: ]4 a, c$ h3 w: Z" c--------------------- $ C( a' a$ f/ x4 {- W
作者:nka_kun [6 B8 B c" z$ Z2 Q
来源:CSDN . l' w/ J* E. ~ Q+ K0 ]
) V& P2 b) q6 X0 B6 ~
1 | o* n P( k1 i! U
) K# y9 |3 \: f9 C8 G$ r |