tjut 5213
来源:互联网 发布:python 3程序开发指南 编辑:程序博客网 时间:2024/05/01 04:31
#include<stdio.h> #include<iostream> #include<string.h> #include<math.h> #include<map> #include<vector> #include<iostream> #include<stdio.h> #include<algorithm> using namespace std; #define N 30005 int a[N]; int n,k,m; int len,tot; int l1,l2,r1,r2; int ans[N],c[N*2]; struct node{ int l,r,id,p; void set(int i,int ll,int rr,int pp){ id=i;l=ll;r=rr;p=pp; } }f[N*4]; bool cmp(node a,node b){ if(a.l/len!=b.l/len) return a.l/len<b.l/len; return a.r<b.r; } void solve(){ int nl=1; int nr=0; int res=0; for(int i=0;i<tot;i++) { while(nl>f[i].l){ nl--; if(k>a[nl]) res+=c[k-a[nl]]; c[a[nl]]++; } while(nr<f[i].r){ nr++; if(k>a[nr]) res+=c[k-a[nr]]; c[a[nr]]++; } while(nl<f[i].l){ if(k>a[nl]) res-=c[k-a[nl]]; c[a[nl]]--; nl++; } while(nr>f[i].r){ if(k>a[nr]) res-=c[k-a[nr]]; c[a[nr]]--; nr--; } ans[f[i].id]+=res*f[i].p; } } int main() { while(~scanf("%d",&n)){ scanf("%d",&k); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } memset(c,0,sizeof(c)); scanf("%d",&m); tot=0; for(int i=1;i<=m;i++) { ans[i]=0; scanf("%d%d%d%d",&l1,&r1,&l2,&r2); f[tot++].set(i,l1,r2,1); f[tot++].set(i,l1,l2-1,-1); f[tot++].set(i,r1+1,r2,-1); if(r1+1<=l2-1) f[tot++].set(i,r1+1,l2-1,1); /*先求(x,y)在l1~r2区间所有解数,然后减去(x,y)在l1~l2-1,和r1+1~r2区间解数, 剩余即为满足x在l1~r1且y在l2~r2区间的解数,重复减的再加上。以此解决 莫队算法x,y属于不同区间的求解问题*/ } len=sqrt(n);//分块,len为块长度 sort(f,f+tot,cmp); solve(); for(int i=1;i<=m;i++) printf("%d\n",ans[i]); } return 0; }
0 0
- tjut 5213
- tjut 5289
- tjut 5288
- tjut 5294
- tjut 2586
- tjut 5296
- tjut 5297
- tjut 5299
- tjut 5384
- tjut 5387
- tjut 5386
- tjut 5381
- tjut 5400
- tjut 5399
- tjut 5396
- tjut 5398
- tjut 5412
- tjut 5410
- Android 自定义标题栏
- java单元测试覆盖率工具-clover
- java集合框架
- slf4j logback创建日志
- 数据结构实验之栈四:括号匹配
- tjut 5213
- 如何配置文件DSN
- 印刷电路板(PCB)的制作工艺流程
- Django上传文本文件
- thinkphp分页显示
- Android Studio 错误 com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
- JavaMail--基础类详解
- JDBC基础应用(数据库增删改查)
- 在VS2015中用C++创建MFC规则动态库DLL