Codeforces Gym 101138 D. Strange Queries
Description
给你一下长度为 \(n\) 的序列.
\(a_i=a_j\)
\(l_1 \leqslant i \leqslant r_1\)
\(l_2 \leqslant i \leqslant r_2\)
询问满足条件的 \((i,j)\) 对数.
Sol
分块+前缀和.
非常乱搞啊...
首先分块 \(O(\sqrt{n})\) 一块.
然后在每块里统计块中元素出现次数.
先预处理块与块之间的贡献,然后处理不足一块的.
处理块与块之间的时候,需要前缀和优化一下就可以了,枚举当前块中的元素,复杂度 \(O(n \sqrt{n})\).
处理不足一块的时候有好多细节...自己随便yy一下吧..
总复杂度 \(O(n \sqrt{n})\) .
Code
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std; typedef long long LL;
const int N = 50005;
const int M = 250;
#define debug(a) cout<<#a<<" = "<<a<<" " int n,m,tmp,q,B;
struct Block{ int a[N],b[M],c; }b[M];
int bl[N],a[N],t1[N],t2[N],t3[N],t4[N];
LL f[M][M]; inline int in(int x=0,char ch=getchar()){ while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
int GetBl(int x){ if(bl[x] == bl[x-1]) return bl[x]+1;else return bl[x]; }
int GetBr(int x){ if(bl[x] == bl[x+1]) return bl[x]-1;else return bl[x]; }
LL work(int l1,int r1,int l2,int r2){
int L1=GetBl(l1),R1=GetBr(r1),L2=GetBl(l2),R2=GetBr(r2);LL res=0; // debug(l1),debug(r1),debug(l2),debug(r2)<<endl;
// debug(L1),debug(R1),debug(L2),debug(R2)<<endl; //Block and Block
if(L2<=R2) for(int i=L1;i<=R1;i++) res+=f[i][R2]-f[i][L2-1]; // debug(res)<<endl; //count
int c1=0,c2=0;
if(bl[l1] != bl[r1]){
// cout<<"qwq"<<endl;
for(int i=l1;bl[i]<L1;i++) t1[++c1]=a[i],t2[a[i]]++;
for(int i=B*R1+1;i<=r1;i++) t1[++c1]=a[i],t2[a[i]]++;
}else{
if(bl[l1-1]==bl[l1] || bl[r1]==bl[r1+1]) for(int i=l1;i<=r1;i++) t1[++c1]=a[i],t2[a[i]]++;
} // cout<<"----------"<<endl;
// for(int i=1;i<=c1;i++) cout<<t1[i]<<" ";cout<<endl; if(bl[l2]!=bl[r2]){
for(int i=l2;bl[i]<L2;i++) t3[++c2]=a[i],t4[a[i]]++;
for(int i=B*R2+1;i<=r2;i++) t3[++c2]=a[i],t4[a[i]]++;
}else{
if(bl[l2-1]==bl[l2] || bl[r2]==bl[r2+1]) for(int i=l2;i<=r2;i++) t3[++c2]=a[i],t4[a[i]]++;
} // for(int i=1;i<=c2;i++) cout<<t3[i]<<" ";cout<<endl;
// cout<<"----------"<<endl; // leave l1r1 and [l2,r2]
for(int i=1;i<=c1;i++) res+=(L2<=R2 ? b[R2].a[t1[i]]-b[L2-1].a[t1[i]] : 0) + t4[t1[i]];
// leave l2r2 and [L1,R1]
if(L1<=R1) for(int i=1;i<=c2;i++) res+=b[R1].a[t3[i]]-b[L1-1].a[t3[i]]; //clear
for(int i=1;i<=c1;i++) t2[t1[i]]--;
for(int i=1;i<=c2;i++) t4[t3[i]]--; return res;
}
int main(){
// freopen("in.in","r",stdin);
ios::sync_with_stdio(false);
n=in(),m=1,B=sqrt(n)+1;
for(int i=1;i<=n;i++){
if(b[m].c >= B) ++m;
a[i]=in(),bl[i]=m,b[m].b[++b[m].c]=a[i];
// debug(a[i]),debug(b[m].c)<<endl;
} // debug(m)<<endl;
// for(int i=1;i<=m;i++) cout<<b[i].c<<endl; for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++) b[i].a[j]=b[i-1].a[j];
// for(int i=1;i<=n;i++) cout<<bl[i]<<" ";cout<<endl;
for(int j=1;j<=b[i].c;j++) b[i].a[b[i].b[j]]++;
// for(int i=1;i<=n;i++) cout<<bl[i]<<" ";cout<<endl;
} // for(int i=1;i<=n;i++) cout<<bl[i]<<" ";cout<<endl; for(int i=1;i<=m;i++) for(int j=1;j<=m;j++) for(int k=1;k<=b[i].c;k++) f[i][j]+=b[j].a[b[i].b[k]]; // for(int i=1;i<=n;i++) cout<<bl[i]<<" ";cout<<endl; for(q=in();q--;){
int l1=in(),r1=in(),l2=in(),r2=in();
cout<<work(l1,r1,l2,r2)<<endl;
}return 0;
}
Codeforces Gym 101138 D. Strange Queries的更多相关文章
- 【 Gym - 101138D 】Strange Queries (莫队算法)
BUPT2017 wintertraining(15) #4B Gym - 101138D 题意 a数组大小为n.(1 ≤ n ≤ 50 000) (1 ≤ q ≤ 50 000)(1 ≤ ai ≤ ...
- Codeforces Gym 101138 G. LCM-er
Description 在 \([a,b]\) 之间选择 \(n\) 个数 (可以重复) ,使这 \(n\) 个数的最小公倍数能被 \(x\) 整除,对 \(10^9+7\) 取膜. \(1\leqs ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
- Codeforces 718A Efim and Strange Grade 程序分析
Codeforces 718A Efim and Strange Grade 程序分析 jerry的程序 using namespace std; typedef long long ll; stri ...
随机推荐
- linux 查看内存的插槽数
[root@web ~]# dmidecode|grep -P -A5 "Memory\s+Device"| grep Size | grep -v Range #linux查看内 ...
- C++strng流(入门级)
/************************************************************************* * * FILENAME: stringTest. ...
- Codeforces Problem 708A Letters Cyclic Shift
题目链接: http://codeforces.com/problemset/problem/708/A 题目大意: 从字符串s中挑选出一个子串(非空),将该子串中的每个字母均替换成前一个字母,如' ...
- js与php转换时间戳
php时间:1368524732 js代码: function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleStri ...
- VS插件开发,启用实验室环境
启用外部程序: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe 命令行参数 /rootsuffix ...
- struts2基础篇(1)
一.Struts2简介Struts2以WebWork优秀的设计思想为核心,吸收了Struts1的部分优点,建立了一个基于WebWork和Struts1的MVC框架. 二.搭建Struts2开发环境2. ...
- git push.default is unset
warning: push.default is unset; its implicit value is changing inGit 2.0 from 'matching' to 'simple' ...
- jquery报错Uncaught ReferenceError: $ is not defined
- 一个Struts2的实例
对Web应用程序而言,需要跨越HTTP协议的两个障碍——无状态和基于文本. 在没有使用struts的时候,你会有一个很真切的体会,就是如何把html页面上的数据提交给后台处理,以什么格式提交? 这是个 ...
- R语言学习笔记
向量化的函数 向量化的函数 ifelse/which/where/any/all/cumsum/cumprod/对于矩阵而言,可以使用rowSums/colSums.对于“穷举所有组合问题" ...