NBUT 1457 Sona (莫队算法)
来源:互联网 发布:对json进行url编码 编辑:程序博客网 时间:2024/05/18 19:44
题意:
给你n个数字,q个询问,问[l,r]的价值,价值是每个数字出现次数的立方和
思路:
很基础的莫队,可是,这个oj有点毒
首先对于[l,r],我们转移为[l,r+1]的时候,只要加上去
错误及反思:
其实上面的都是废话,因为这就是个莫队模板题,但是!这个OJ是真的毒,首先不能用bits/stdc++.h,而且sqrt()必须在math.h里面才可以,不能是cmath,还有就是要用I64d输出,不能用lld,而且返回的错误有时候有点偏差。。
代码:
2781ms:
#include<cstdio>#include<cstring>#include<algorithm>#include<math.h>#include<vector>using namespace std;const int N = 501000;int id[N],pos[N],n,m,arr[N];long long ans[N];long long cnt[N];long long now=0;vector<int> v;struct Q{ int l,r,id;}q[N];int getid(int x){return lower_bound(v.begin(),v.end(),x)-v.begin();}bool cmp(Q a,Q b){ if(pos[a.l]!=pos[b.l]) return a.l<b.l; return a.r<b.r;}inline void gx(int y,int x){ now-=1ll*cnt[y]*cnt[y]*cnt[y]; cnt[y]+=x; now+=1ll*cnt[y]*cnt[y]*cnt[y];}int main(){ while(scanf("%d",&n)!=EOF) { memset(cnt,0,sizeof(cnt)); now=0; int block=sqrt(n); for(int i=1;i<=n;i++) { scanf("%d",&arr[i]); pos[i]=i/block; v.push_back(arr[i]); } sort(v.begin(),v.end()),v.erase(unique(v.begin(),v.end()),v.end()); for(int i=1;i<=n;i++) id[i]=getid(arr[i]); scanf("%d",&m); for(int i=0;i<m;i++) { scanf("%d%d",&q[i].l,&q[i].r); q[i].id=i; } sort(q,q+m,cmp); int l=1,r=0; for(int i=0;i<m;i++) { for(;l<q[i].l;l++) gx(id[l],-1); for(;l>q[i].l;l--) gx(id[l-1],1); for(;r<q[i].r;r++) gx(id[r+1],1); for(;r>q[i].r;r--) gx(id[r],-1); ans[q[i].id]=now; } for(int i=0;i<m;i++) printf("%I64d\n",ans[i]); }}
阅读全文
0 0
- NBUT 1457 Sona (莫队算法)
- Sona - NBUT 1457 莫队算法
- NBUT 1457Sona(莫队算法)
- NBUT 1457 Sona (莫队算法)
- NBUT 1457 Sona 莫队算法
- NBUT 1457 Sona (莫队算法)
- NBUT 1457 Sona 莫队算法 分块处理
- NBUT 1457 Sona(莫队算法+离散化)
- NBUT 1457 Sona [简单莫队]
- NBUT 1457 Sona
- 莫队算法(NBUT1457 Sona)
- nbut1457 Sona 【莫队算法+离散化】
- NBUT 1463 Sona
- NBUT 1457 (莫队算法 离散化)
- Sona
- NBUT 1641 错误的算法
- NBUT 1457 分块法
- NBUT 1457 分块
- android 的线程和线程池
- 【Android工具】19个实用的Android开发工具
- Hadoop 2.0 NameNode HA和Federation实践
- QT通过API获取天气信息
- 使用HttpClient 发送HTTP类型的 POST ,GET 的信息
- NBUT 1457 Sona (莫队算法)
- 3ds Max展UV
- android mm and ndk-build
- 第一次反编
- 过渡与动画
- COUNT()函数使用时数据溢出-->COUNT_BIG
- [资源分享][Unity好文章]实现网球乒乓球回弹的效果
- CSS可见性
- jdk源码之自动装拆箱