hdu4417 Super Mario
来源:互联网 发布:2016淘宝小卖家发展 编辑:程序博客网 时间:2024/04/28 00:40
题目:给你n个数,m个询问,形如(L,R,H),让你输出区间[L,R]中小于等于H的数的数目
思路:主席树,查询的时候看一下区间往左走还是
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define PI acos(-1.0)#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3f// 0x3f3f3f3fconst int maxn=1e5+50;const int maxm=maxn*30;int arr[maxn];//原数组int num[2*maxn],siz;//num离散化之后的数组int root[maxn],lson[maxm],rson[maxm],cnt[maxm];//根节点,左儿子,右儿子,区间内的数出现次数的前缀和int tot;void init(){ tot=1; root[0]=lson[0]=rson[0]=cnt[0]=0;//建一棵空树 sort(num+1,num+siz+1);//离散化 siz=unique(num+1,num+siz+1)-num-1;}int Hash(int x){ return lower_bound(num+1,num+siz+1,x)-num;}int update(int rt,int pos,int val){ int newrt=tot++,temp=newrt; cnt[newrt]=cnt[rt]+val; int l=1,r=siz; while(l<r){ int mid=(l+r)/2; if(pos<=mid){ r=mid; lson[newrt]=tot++;rson[newrt]=rson[rt]; newrt=lson[newrt];rt=lson[rt]; } else{ l=mid+1; lson[newrt]=lson[rt];rson[newrt]=tot++; newrt=rson[newrt];rt=rson[rt]; } cnt[newrt]=cnt[rt]+val; } return temp;}int ans;void query(int left_rt,int right_rt,int k){ int l=1,r=siz; while(l<r){ int mid=(l+r)/2; int temp=cnt[lson[right_rt]]-cnt[lson[left_rt]]; if(k<=mid){ r=mid; left_rt=lson[left_rt]; right_rt=lson[right_rt]; } else{ l=mid+1; ans+=temp; left_rt=rson[left_rt]; right_rt=rson[right_rt]; } } ans+=cnt[right_rt]-cnt[left_rt];}int L[maxn],R[maxn],H[maxn];int main(){ int T,cas=0,n,m; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); siz=0; for(int i=1;i<=n;i++){ scanf("%d",&arr[i]); num[++siz]=arr[i]; } for(int i=1;i<=m;i++){ scanf("%d%d%d",&L[i],&R[i],&H[i]);L[i]++;R[i]++; num[++siz]=H[i]; } init(); for(int i=1;i<=n;i++) root[i]=update(root[i-1],Hash(arr[i]),1); printf("Case %d:\n",++cas); for(int i=1;i<=m;i++){ ans=0; query(root[L[i]-1],root[R[i]],Hash(H[i])); printf("%d\n",ans); } } return 0;}
往右走进行了
阅读全文
0 0
- HDU4417 Super Mario
- hdu4417 Super Mario
- HDU4417-Super Mario
- hdu4417 Super Mario
- hdu4417 Super Mario
- HDU4417-Super Mario
- hdu4417 Super Mario
- hdu4417 Super Mario
- HDU4417 Super Mario(树状数组)
- HDU4417 Super Mario【树状数组】
- Super Mario(hdu4417,树状数组)
- hdu4417 Super Mario 线段树端点更新
- hdu4417 Super Mario (主席树+二分)
- hdu4417 Super Mario (主席树入门)
- HDU4417 Super Mario 线段树离线操作
- hdu4417- Super Mario(离线,线段树)
- 主席树总结-hdu4417 Super Mario
- HDU4417 Super Mario(划分树+二分)
- android 按两次返回,退出
- 【iOS开发】UIWebView调用JS点击事件(stringByEvaluatingJavaScriptFromString)
- pssh参数及例子
- 轻松实现dialog弹窗
- GBDT:梯度提升决策树
- hdu4417 Super Mario
- Git Unknown SSL protocol error in connection to git.oschina.net:44
- scikit-learn 实战之非监督学习 2
- js 显示时间戳转化为几分钟之前多久之前方法函数
- 理解JAVASCRIPT的闭包
- 访谈阿里孙伟光:这位技术人都经历了啥?
- 三、ScheduledThreadPoolExecutor 支持周期调度执行线程池解析
- SAP MM分割评估
- Hibernate与 MyBatis的比较