【51nod】区间求和
LYK在研究一个有趣的东西。
第一行一个整数n(1<=n<=1000000),接下来一行n个数ai(1<=ai<=1000000)表示LYK的序列。
一行表示答案。
5
3 4 5 5 3
2 题解:每次取出一个区间[l, r],发现a[i] 产生的贡献为 2*i-l-r. 预处理一下prenum[], presum[], prenum[i]表示[1, i]有多少个和a[i]相同, presum[i]表示[1, i]与a[i]相同的数所在的下标和,同理预处理出nexnum[], nexsum[].O(n)扫一遍维护一下l, r, 2*i即可递推出各个值。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h> using namespace std;
#define ll unsigned long long
const int N = 1e6+;
int n, a[N];
int pre[N], nex[N], pos[N];
int prenum[N], nexnum[N];
int presum[N], nexsum[N];
int fl[N], fr[N], f[N]; int main(){
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", a+i); memset(pos, , sizeof(pos));
for(int i = ; i <= n; i++){
pre[i] = pos[ a[i] ];
prenum[i] = prenum[ pre[i] ]+;//前面的个数
presum[i] = presum[ pre[i] ]+i;//前面的下标和
pos[ a[i] ] = i;
}
memset(pos, , sizeof(pos));
for(int i = n; i; i--){
nex[i] = pos[ a[i] ];
nexnum[i] = nexnum[ nex[i] ]+;
nexsum[i] = nexsum[ nex[i] ]+i;
pos[ a[i] ] = i;
} for(int i = ; i <= n; i++){
fl[i] = fl[i-];
fl[i] += i*nexnum[i];
fl[i] -= presum[i-];
}
for(int i = n; i; i--){
fr[i] = fr[i+];
fr[i] += i*prenum[i];
fr[i] -= nexsum[i+];
} for(int i = ; i <= n; i++)
f[i] = f[i-]+nexnum[i]-prenum[i-];
unsigned int ans = ;
for(int i = ; i <= n; i++){
//printf("i %d: %d %d %d\n", i, fl[i], fr[i], f[i]);
ans += a[i]*(*i*f[i]-fl[i]-fr[i]);
}
printf("%u\n", ans);
return ;
}
【51nod】区间求和的更多相关文章
- POJ 2823 Sliding Window 线段树区间求和问题
题目链接 线段树区间求和问题,维护一个最大值一个最小值即可,线段树要用C++交才能过. 注意这道题不是求三个数的最大值最小值,是求k个的. 本题数据量较大,不能用N建树,用n建树. 还有一种做法是单调 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- vijos1740 聪明的质监员 (二分、区间求和)
http://www.rqnoj.cn/problem/657 https://www.vijos.org/p/1740 P1740聪明的质检员 请登录后递交 标签:NOIP提高组2011[显示标签] ...
- LightOJ 1112 Curious Robin Hood (单点更新+区间求和)
http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i 将第i个数值输出,并将第i个值清0 2 i v ...
- POJ 3468 A Simple Problem with Integers(线段树区间求和)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- poj3468树状数组的区间更新,区间求和
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 47174 ...
- D 区间求和 [数学 树状数组]
D 区间求和 题意:求 \[ \sum_{k=1}^n \sum_{l=1}^{n-k+1} \sum_{r=l+k-1}^n 区间前k大值和 \] 比赛时因为被B卡了没有深入想这道题 结果B没做出来 ...
- [用CDQ分治解决区间加&区间求和]【习作】
[前言] 作为一个什么数据结构都不会只会CDQ分治和分块的蒟蒻,面对区间加&区间求和这么难的问题,怎么可能会写线段树呢 于是,用CDQ分治解决区间加&区间求和这篇习作应运而生 [Par ...
- 线段树 区间开方区间求和 & 区间赋值、加、查询
本文同步发表于 https://www.zybuluo.com/Gary-Ying/note/1288518 线段树的小应用 -- 维护区间开方区间求和 题目传送门 约定: sum(i,j) 表示区间 ...
随机推荐
- @RequestMapping注解详解
@RequestMapping是一个用来处理请求地址映射的注解,可用于类或者方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.@RequestMapping注解有六个属性,下面进行 ...
- 很好用的request转换为实体方法还有判断实体所有参数不能为空的方法
/// <summary> /// 模型辅助处理类 /// 2016-04-18 /// </summary> public class ModelHelper { /// & ...
- xib加载的两种方式
•Xib文件的加载 Ø方法1 NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"AppView" owner:nil ...
- JAG Summer 2012 Day 4 C Connect
状压dp,由于枚举两维状态会GG,所以只枚举当前位置前m个的状态,就是这个样子大概= =: 呆马: #include <iostream> #include <cstdio> ...
- .Net发出图片Request请求
Stream reader = null; //可能是外部链接 if (imgUrl.ToLower().StartsWith("http://")) { var request ...
- centos 7 系统服务auditd kdump tuned irqbalance
tuned是红帽推出的一个动态调优方案,用户可以在不同的时间段内采用不同的调优方案.由于以服务进程形式存在,就可以很方便的和crontab结合!Tuned 是监控并收集各个系统组件用量数据的守护进程, ...
- MySQL主从同步配置(Ubuntu)
一.在两台Linux机器上安装MySQL 二.Master主服务器配置(192.168.1.3) 1.编辑my.cnf(命令查找文件位置:find / -name my.cnf) vi /etc/my ...
- LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16 Debug/firstapi.exe :
#include <windows.h> #include <iostream.h> void main() { char SourceFileName[MAX_PATH]; ...
- 调试必备---console
一.前言 可能很多前端er会习惯用 alert() 方法来进行调试,但我相信更多的人会使用 console.log(),因为它不用像 alert() 那样会有字符长度限制,也不会中止程序的进程,而且还 ...
- 我的git与github学习历程
因为想要知道如何把代码放到github上,所以就百度了一下,然后找到一个<如何从github上面拷贝源码>的文章,就先进行练习了下 1.首先到git官网下载git版本控制工具的安装包, ...