[bzoj4571] [loj#2016] [Scoi2016] 美味
Description一家餐厅有 (n) 道菜,编号 (1)...(n) ,大家对第 (i) 道菜的评价值为 (ai)( (1 leq i leq n) )。有 (m) 位顾客,第 (i) 位顾客的期 Input第1行,两个整数,(n),(m),表示菜品数和顾客数。 Output输出 (m) 行,每行 1 个整数,(ymax) ,表示该位顾客选择的最美味的菜的美味值。 Sample Input4 4 1 2 3 4 1 4 1 4 2 3 2 3 3 2 3 3 4 1 2 4 Sample Output9 7 6 7 想法第一眼看,可持久化 (trie) 啊 代码#include<cstdio> #include<iostream> #include<algorithm> using namespace std; int read(){ int x=0; char ch=getchar(); while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)) x=x*10+ch-'0',ch=getchar(); return x; } const int N = 200005; const int MAX = 100000; int n,m; int root[N],cnt,ch[N*20][2],s[N*20]; void ins(int x,int y,int l,int r,int c){ s[x]=s[y]+1; if(l==r) return; int mid=(l+r)>>1; if(c<=mid) ins(ch[x][0]=++cnt,ch[y][0],l,mid,c),ch[x][1]=ch[y][1]; else ins(ch[x][1]=++cnt,ch[y][1],mid+1,r,ch[x][0]=ch[y][0]; } int num(int x,int L,int R){ if(L<=l && r<=R) return s[x]-s[y]; int mid=(l+r)>>1,ret=0; if(L<=mid) ret+=num(ch[x][0],L,R); if(R>mid) ret+=num(ch[x][1],R); return ret; } int main() { n=read(); m=read(); for(int i=1;i<=n;i++) ins(root[i]=++cnt,root[i-1],1,MAX,read()); int b,x,r; while(m--){ b=read(); x=read(); l=read(); r=read(); int cur=0,ll,rr,id; for(int i=18;i>=0;i--){ id= (b&(1<<i)) ? 1 : 0 ; if(!id) ll=cur+(1<<i)-x,rr=cur+(2<<i)-1-x; else ll=cur-x,rr=cur+(1<<i)-1-x; if(rr>0 && ll<=MAX && num(root[r],root[l-1],rr)>0) cur+=(id^1)<<i; else cur+=id<<i; } printf("%dn",b^cur); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- my.setting vb.net
- Big Txt File(一)
- delphi – 如何实现IsFirstRecord和IsLastRecord?
- Grails with ATS Transformation tutorial with a demo exa
- Bi-directional communication between Client and Server,
- Perl: 运行system `cp $file $dir`错误:perl cp missing d
- Lua学习笔记--面向对象
- 对于(1)总是与perl中的相同吗?
- Perl XML::Simple 的应用(二)
- "Can't locate CPAN.pm in @INC " 解决方案