bzoj2223 [Coci 2009]PATULJCI (模板)(主席樹)
阿新 • • 發佈:2017-09-12
int log ref style hide ide hid names num
Submit: 1245 Solved: 530
[Submit][Status][Discuss]
2223: [Coci 2009]PATULJCI
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1245 Solved: 530
[Submit][Status][Discuss]
Description
HINT
輸入第二個整數是序列中權值的範圍Lim,即1<=ai(1<=i<=n)<=Lim,1<=Lim<=10000。
主席樹模板dearu;
蒟蒻只能碼板子了啊(攤);
↓代碼
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<string>
5 #include<cmath>
6 #include<algorithm>
7 #include<vector>
8 #include<queue>
9 #include<map>
10 #define N 300039
11 #define inf 0x3f3f3f3f
12 #define ll long long
13 using namespace std;
14 struct number
15 {
16 int n,r;
17 }num[N];
18 struct seg
19 {
20 int ls,rs,cnt;
21 }tree[N<<4];
22 int val[N],tot,root[N];
23 bool cmpn(number a,number b)
24 {
25 return a.n==b.n ? a.r<b.r : a.n<b.n;
26 }
27 bool cmpr(number a,number b)
28 {
29 return a.r<b.r;
30 }
31 void build(int k,int l,int r)
32 {
33 tree[k].cnt=0;
34 if(l==r)
35 {
36 return;
37 }
38 int mid=(l+r)>>1;
39 build(tree[k].ls=++tot,l,mid);
40 build(tree[k].rs=++tot,mid+1,r);
41 }
42 void update(int las,int now,int l,int r,int v)
43 {
44 if(l==r)
45 {
46 tree[now].cnt=tree[las].cnt+1;
47 return;
48 }
49 int mid=(l+r)>>1;
50 if(mid>=v)
51 {
52 tree[now].rs=tree[las].rs;
53 update(tree[las].ls,tree[now].ls=++tot,l,mid,v);
54 }
55 else
56 {
57 tree[now].ls=tree[las].ls;
58 update(tree[las].rs,tree[now].rs=++tot,mid+1,r,v);
59 }
60 tree[now].cnt+=tree[tree[now].ls].cnt+tree[tree[now].rs].cnt;
61 }
62 int query(int las,int now,int k,int l,int r)
63 {
64 if(l==r)
65 {
66 return l;
67 }
68 int mid=(l+r)>>1;
69 if(tree[tree[now].ls].cnt-tree[tree[las].ls].cnt>k)
70 {
71 return query(tree[las].ls,tree[now].ls,k,l,mid);
72 }
73 else if(tree[tree[now].rs].cnt-tree[tree[las].rs].cnt>k)
74 {
75 return query(tree[las].rs,tree[now].rs,k,mid+1,r);
76 }
77 else
78 {
79 return -1;
80 }
81 }
82 int main()
83 {
84 int n,m,lim,a,b,c;
85 scanf("%d%d",&n,&lim);
86 build(root[0]=++tot,1,lim);
87 for(a=1;a<=n;a++)
88 {
89 scanf("%d",&b);
90 update(root[a-1],root[a]=++tot,1,lim,b);
91 }
92 scanf("%d",&m);
93 while(m--)
94 {
95 scanf("%d%d",&a,&b);
96 c=query(root[a-1],root[b],(b-a+1)/2,1,lim);
97 if(c==-1)
98 {
99 printf("no\n");
100 }
101 else
102 {
103 printf("yes %d\n",c);
104 }
105 }
106 return 0;
107 }
bzoj2223
bzoj2223 [Coci 2009]PATULJCI (模板)(主席樹)