bzoj2223 [Coci 2009]PATULJCI (模板)(主席树)

2223: [Coci 2009]PATULJCI

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 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

时间: 2024-08-10 01:43:27

bzoj2223 [Coci 2009]PATULJCI (模板)(主席树)的相关文章

BZOJ2223 [Coci 2009]PATULJCI

求区间内个数大于rank的一个数 主席树求一下就好啦! 1 /************************************************************** 2 Problem: 2223 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:704 ms 7 Memory:54712 kb 8 ********************************************************

【莫队算法】【权值分块】bzoj2223 [Coci 2009]PATULJCI

不带修改主席树裸题<=>莫队+权值分块裸题. 复杂度O(m*sqrt(n)). P.S.题目描述坑爹,第二个数是权值的范围. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; #define N 300001 #define M 10001 int f,c; inline void R(int &x){ c=0;f=1; for(;c<'0'||c

【BZOJ2223/3524】[Coci 2009]PATULJCI

Description Input Output 10 3 1 2 1 2 1 2 3 2 3 3 8 1 2 1 3 1 4 1 5 2 5 2 6 6 9 7 10 Sample Input no yes 1 no yes 1 no yes 2 no yes 3 Sample Output HINT Notice:输入第二个整数是序列中权值的范围Lim,即1<=ai(1<=i<=n)<=Lim. 依旧主席树模板,无需离散化. 1<=Lim<=10000 Source

bzoj3524 [Poi2014]Couriers/2223 [Coci 2009]PATULJCI

题目链接1 题目链接2 主席树模板题 两题有细节不同 1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<queue>

【bzoj5178】[Jsoi2011]棒棒糖 主席树

题目描述 Coffee的世界里也是有棒棒糖卖的,Coffee买了N(1≤N≤50000)只连着的.这N只棒棒糖包裹在小塑料袋中,排成一列,相邻的两只棒棒糖的塑料袋是接起来的.为了方便,我们把棒棒糖从左到右编号为1..N.每只棒棒糖有一种口味.第i只的口味是ci(1≤ci≤50000).两只棒棒糖i,j的口味相同,当且仅当ci=cj.Coffee对m只棒棒糖总体口味的评价比较奇怪.如果这m只棒棒糖中,有一种口味c0的数量严格大于总数的一半m/2,那么Coffee认为这m只棒棒糖主要是c0口味的.C

BZOJ_2223_[Coci 2009]PATULJCI_主席树

Description Input 10 3 1 2 1 2 1 2 3 2 3 3 8 1 2 1 3 1 4 1 5 2 5 2 6 6 9 7 10 Output no yes 1 no yes 1 no yes 2 no yes 3 区间众数,可以用主席树求. 查询时判断那边大走哪边. 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #def

【POJ 2104】 K-th Number 主席树模板题

达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没有评测,但我立下flag这个代码一定能A.我的同学在自习课上考语文,然而机房党都跑到机房来避难了\(^o^)/~ #include<cstdio> #include<cstring> #include<algorithm> #define for1(i,a,n) for(i

【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题

达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状数组,而我一开始根本不知道该怎么套,, 学习吧,,, 然后我自己脑补如果不套会如何?后来想到是查询O(logn),修改是O(nlogn),很明显修改的复杂度太大了,为了降低修改的复杂度,我们只得套上树状数组来维护前缀和使它的n的复杂度降低为logn,从而修改的复杂度变为O(log2n).但因为我们套

主席树模板

#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int M = 50006; int w[M], a[M], root[M]; struct Tree{ int ls, rs, w; } tr[M << 4] ; int tot ; void insert(int &x, int l, int r, int d){ tr[++tot]