BZOJ4810:[YNOI2017]由乃的玉米田(莫队,bitset)
Description
由乃在自己的农田边散步,她突然发现田里的一排玉米非常的不美。这排玉米一共有N株,它们的高度参差不齐。
由乃认为玉米田不美,所以她决定出个数据结构题
这个题是这样的:
给你一个序列a,长度为n,有m次操作,每次询问一个区间是否可以选出两个数它们的差为x,或者询问一个区间是
否可以选出两个数它们的和为x,或者询问一个区间是否可以选出两个数它们的乘积为x ,这三个操作分别为操作1
,2,3选出的这两个数可以是同一个位置的数
.jpg)
Input
第一行两个数n,m
后面一行n个数表示ai
后面m行每行四个数opt l r x
opt表示这个是第几种操作,l,r表示操作的区间,x表示这次操作的x
定义c为每次的x和ai中的最大值,ai >= 0,每次的x>=2n,m,c <= 100000
Output
对于每个询问,如果可以,输出yuno,否则输出yumi
Sample Input
5 5
1 1 2 3 4
2 1 1 2
1 1 2 2
3 1 1 1
3 5 5 16
1 2 3 4
1 1 2 3 4
2 1 1 2
1 1 2 2
3 1 1 1
3 5 5 16
1 2 3 4
Sample Output
yuno
yumi
yuno
yuno
yumi
yumi
yuno
yuno
yumi
Solution
先莫个队,然后对值域开个bitsetbitset。
差相等就是ff并上ff右移xx不为00。
和相等就是ff并上翻转的ff右移N−xN−x位不为00。
积的话就sqrtsqrt枚举xx的的因子然后查询存在性就好了。
Code
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<bitset> 5 #include<cmath> 6 #include<algorithm> 7 #define N (100000) 8 using namespace std; 9 10 struct Que{int opt,l,r,x,id;}Q[N+1]; 11 int n,m,unit,opt,l,r,x,a[N+1],ID[N+1]; 12 int ans[N+9],Keg[N+9]; 13 bitset<N+1>f,g; 14 15 inline int read() 16 { 17 int x=0,w=1; char c=getchar(); 18 while (!isdigit(c)) {if (c=='-') w=-1; c=getchar();} 19 while (isdigit(c)) x=x*10+c-'0', c=getchar(); 20 return x*w; 21 22 } 23 24 void Ins(int p) 25 { 26 if (!Keg[a[p]]) f[a[p]]=1, g[N-a[p]]=1; 27 ++Keg[a[p]]; 28 } 29 30 void Del(int p) 31 { 32 --Keg[a[p]]; 33 if (!Keg[a[p]]) f[a[p]]=0, g[N-a[p]]=0; 34 } 35 36 bool check(int opt,int x) 37 { 38 if (opt==1) return (f&(f>>x)).any(); 39 if (opt==2) return (f&(g>>(N-x))).any(); 40 if (opt==3) 41 { 42 for (int i=1; i<=sqrt(x); ++i) 43 if (x%i==0 && f[i] && f[x/i]) return 1; 44 return 0; 45 } 46 } 47 48 bool cmp(Que a,Que b) 49 { 50 if (ID[a.l]==ID[b.l]) return a.r<b.r; 51 return ID[a.l]<ID[b.l]; 52 } 53 54 int main() 55 { 56 n=read(); m=read(); unit=sqrt(n); 57 for (int i=1; i<=n; ++i) ID[i]=i/unit; 58 for (int i=1; i<=n; ++i) a[i]=read(); 59 for (int i=1; i<=m; ++i) 60 { 61 opt=read(); l=read(); r=read(); x=read(); 62 Q[i]=(Que){opt,l,r,x,i}; 63 } 64 sort(Q+1,Q+m+1,cmp); 65 int l=1,r=0; 66 for (int i=1; i<=m; ++i) 67 { 68 while (l<Q[i].l) Del(l++); 69 while (l>Q[i].l) Ins(--l); 70 while (r<Q[i].r) Ins(++r); 71 while (r>Q[i].r) Del(r--); 72 ans[Q[i].id]=check(Q[i].opt,Q[i].x); 73 } 74 for (int i=1; i<=m; ++i) puts(ans[i]?"yuno":"yumi"); 75 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· 大模型工具KTransformer的安装
· [计算机/硬件/GPU] 显卡