NBUT 1457 B - Sona
同小Z的袜子,只是需要离散化,平方变成了立方
//#include<bits/stdc++.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#define N 100010
#define LL long longusing namespace std;
struct node
{int l,r,id;
};int n,m,t;
int pos[N],c[N],a[N];
LL s[N],ans[N];
node q[N];void init()
{memset(s,0,sizeof(s));int unit=int(sqrt(n));for (int i=1;i<=n;i++)if (i%unit==0) pos[i]=i/unit; else pos[i]=i/unit+1;
}bool cmp(node a, node b)
{if (pos[a.l]==pos[b.l]) return a.r<b.r;else return pos[a.l]<pos[b.l];
}LL gcd(LL a, LL b)
{if (b==0) return a;return gcd(b,a%b);
}int main()
{while(scanf("%d",&n)!=EOF){init();for (int i=1;i<=n;i++){scanf("%d",&a[i]);c[i]=a[i];}sort(a+1,a+n+1);t=unique(a+1,a+n+1)-a-1;for (int i=1;i<=n;i++) c[i]=lower_bound(a+1,a+t+1,c[i])-a;scanf("%d",&m);for (int i=1;i<=m;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].id=i;}sort(q+1,q+m+1,cmp);int l=1,r=1;LL tmp=1;s[c[1]]=1;for (int i=1;i<=m;i++){while(r<q[i].r){r++;tmp=tmp-s[c[r]]*s[c[r]]*s[c[r]];s[c[r]]++;tmp=tmp+s[c[r]]*s[c[r]]*s[c[r]];}while(r>q[i].r){tmp=tmp-s[c[r]]*s[c[r]]*s[c[r]];s[c[r]]--;tmp=tmp+s[c[r]]*s[c[r]]*s[c[r]];r--;}while(l<q[i].l){tmp=tmp-s[c[l]]*s[c[l]]*s[c[l]];s[c[l]]--;tmp=tmp+s[c[l]]*s[c[l]]*s[c[l]];l++;}while(l>q[i].l){l--;tmp=tmp-s[c[l]]*s[c[l]]*s[c[l]];s[c[l]]++;tmp=tmp+s[c[l]]*s[c[l]]*s[c[l]];}ans[q[i].id]=tmp;}for (int i=1;i<=m;i++) printf("%I64d\n",ans[i]);}return 0;
}
转载于:https://www.cnblogs.com/wzb-hust/p/4863672.html
NBUT 1457 B - Sona相关推荐
- (NBUT - 1457)Sona(莫队+离散化)
题目链接:Sona - NBUT 1457 - Virtual Judge (ppsucxtt.cn) 题意比较简单:有N个数,有M个询问,求每次询问的区间[L,R]中,每种数字出现次数的立方和. 在 ...
- NBUT 1457 Sona(莫队算法+离散化)
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...
- NBUT - 1457 Sona 莫队
NBUT - 1457 Sona 题意: 计算一个区间内每种数出现次数的立方和,那么转移的话,假设一个区间是[L,R],如果向左转移,用ans保存区间的答案,cnt[i]记录第 i 种数出现的次数,那 ...
- NBUT 1457 Sona (莫队算法)
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...
- NBUT 1457 Sona 莫队算法 分块处理
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...
- Sona - NBUT 1457 莫队算法
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...
- NBUT - 1457 Sona (莫队)
题链:https://vjudge.net/problem/NBUT-1457/origin 题意:求区间内,每个数出现次数x的立方和. 思路:模板题.添加时,由k^3->(k+1)^3,多了3 ...
- NBUT 1457 Sona
莫队算法+离散化 1.map会TLE,必须离散化做 2.long long会WA,__int64定义 %I64d输出输出能AC 3.注意输入的序列会爆int #include<cstdio> ...
- NBUT - 1457 Sona
题目:给你n个数字,m个区间询问,问你区间内每个数出现的次数的立方和 思路:直接莫队搞就好了.输出用%lld会WA,要用%I64d才能A 代码: #pragma comment(linker, &qu ...
最新文章
- 聚合复合_【专家视觉】聚合物接枝多壁碳纳米管及其聚氨酯复合材料
- gradle仓库配置
- java解惑你知道多少_Java解惑
- captcha.js一个生成验证码的插件,使用js和canvas生成
- dubbo使用简单说明
- 当重复调用addsubview时出现显示重叠问题
- 函数求值需要运行所有线程_精读《深度学习 - 函数式之美》
- 【转载】LinkedHashMap和HashMap区别
- 证明一下拉普拉斯的《概率分析论》观点
- RNNnoise知识整理
- VisualStudio2022创建.ASP.NET应用程序
- java entries_Enumerationlt;? extends ZipEntrygt; entries()_学习Java Zip|WIKI教程
- 突发!人人影视字幕组14人被抓,用户超800万,涉案金额超1600万!网友炸了:免费的片没了...
- 论文精读2: Ground-to-Aerial Image Geo-LocalizationWith a Hard Exemplar Reweighting Triplet Loss
- 这些前后端性能指标,面试一问我就懵了
- OpenCV-Python图形图像处理:制作雪花飘落特效
- 210127 课内整理
- 列举3种强制类型转换和2种隐式类型转换?
- 软件工程新生需要注意什么?
- VMware 安装 Windows11