51nod 1680区间求和 (dp+树状数组/线段树)
不妨考虑已知一个区间[l,r]的k=1、k=2....k=r-l+1这些数的答案ans(只是这一个区间,不包含子区间)
那么如果加入一个新的数字a[i](i = r+1)
则新区间[l, i]的答案为ans + (c+1)*a[i] + s ,c为[l,r]中小于等于a[i]的数的个数,s为大于它的树的和
接下来考虑一个区间组,区间组i表示的是以i为结尾的所有区间
另dp[i]表示[1,i], [2,i] .... [i-1, i],[i, i]这些区间的答案和
那么dp[i+1] = dp[i] + ∑(j*a[j])(if a[j] >= a[i]) + ∑k*a[i](if a[k] < a[i]) + i*a[i]
这样的话,最后只需要把dp[1~n]加起来就构成了答案
上面的值可以使用树状数组或者线段树维护
这里还使用了hashmap来进行离散化
(线段树日常卡常数,优化很久才过)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <ext/hash_map>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
using namespace __gnu_cxx;
const int maxn = 1e6 + ;
typedef long long LL;
typedef pair<int, int> PII;
vector<int> V(maxn, );
const int MOD = (1e9 + ) + 0.5;
hash_map <int, int> H;
PII tree[maxn*];
PII v;
int k, L, R;
inline PII Add(PII a, PII b){
return {(a.fi + b.fi)%MOD, (a.se + b.se)%MOD};
}
void Insert(int o, int l, int r){
if(l == r){
tree[o] = Add(tree[o], {v.fi, (LL)v.fi*v.se%MOD});
return;
}
int mid = (l+r)>>;
if(k <= mid) Insert(o<<, l, mid);
else Insert((o<<)+, mid+, r);
tree[o] = Add(tree[o<<], tree[(o<<)+]);
}
PII Query(int o, int l, int r){
if(tree[o].fi == ) return mp(, );
if(L <= l && r <= R){
return tree[o];
}
int mid = (l+r)>>;
return Add((L <= mid ? Query(o*, l, mid) : mp(, )),
(R > mid ? Query(o*+, mid+, r) : mp(, )));
}
int a[maxn], A, B, C;
int n; void I_AM_ANGRY(){
for(int i = ; i <= n; i++){
a[i] = ((LL)a[i-]*A + B)%C;
V[i] = a[i];
}
/*
for(int i = 2; i <= n; i += 2){
a[i] = ((LL)a[i-1]*A + B)%C;
a[i+1] = ((LL)a[i]*A + B)%C;
V[i] = a[i];
V[i+1] = a[i+1];
}
*/
/*
for(int i = 2; i <= n; i += 4){
a[i] = ((LL)a[i-1]*A + B)%C;
a[i+1] = ((LL)a[i]*A + B)%C;
a[i+2] = ((LL)a[i+1]*A + B)%C;
a[i+3] = ((LL)a[i+2]*A + B)%C; V[i] = a[i];
V[i+1] = a[i+1];
V[i+2] = a[i+2];
V[i+3] = a[i+3];
}*/
V[] = a[];
} int main()
{
cin>>n>>a[]>>A>>B>>C;
I_AM_ANGRY();
sort(V.begin(), V.end());
int tot = ;
for(auto x : V) if(!H[x]) H[x] = ++tot;
LL dp = a[], ans = dp;
int N = n; n = tot;
v = {, a[]}; k = H[a[]];
Insert(, , n);
for(int i = ; i <= N; i++){
int t = H[a[i]];
L = t; R = n;
PII x = Query(, , n);
dp = (dp + x.se + (LL)(tree[].fi - x.fi+i)*a[i])%MOD;
v = {i, a[i]}; k = t;
Insert(, , n);
ans += dp;
}
cout<<(ans%MOD + MOD)%MOD<<endl;
return ;
}
51nod 1680区间求和 (dp+树状数组/线段树)的更多相关文章
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- 树状数组 && 线段树应用 -- 求逆序数
参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...
- 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树
正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- hdu 1166 敌兵布阵——(区间和)树状数组/线段树
pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...
- 区间操作---树状数组&&线段树
涉及区间操作的一些套路必须要会呀 区间加减为了偷懒能不写线段树so我选择树状数组!! 但是区间乘除,最大值我想了想还是用线段树分块吧. 树状数组: 这里用网上的一张图: 这里灰色数组是原本的数组(a[ ...
- 第十四个目标(dp + 树状数组 + 线段树)
Problem 2236 第十四个目标 Accept: 17 Submit: 35 Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
随机推荐
- Delphi初始化与结束化
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 误删 EhCache 中的数据?
最近遇到一个问题:在使用ehcache时,通过CacheManager.getCache(chachename).get(key),获取相应的缓存内对象(当时这个对象是个list), 有个同事写个方法 ...
- QOS-QOS(服务质量)概述
QOS-QOS(服务质量)概述 2018年7月7日 20:29 概述及背景: 1. 引入: 传统IP网络仅提供“尽力而为”的传输服务,网络有可用资源就转发,资源不足时就丢弃 新一代IP网络承载了 ...
- 获取单片机唯一id(stm32获取单片机唯一id)
stm32唯一id: 不同型号的stm32单片机,id不在同一地址上!具体地址可以通过用户手册中的Device electronic signature>Unique device ID reg ...
- Python的logging模块、os模块、commands模块与sys模块
一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message ...
- 使用apache的ab压力测试时失败请求原因
只要出现 Failed requests 就会多出现一行要求失败的各原因的数据统计,分别有 Connect, Length,与 Exception 三种,分别代表的意义为:Connect 无 ...
- [转] Bash脚本:怎样一行行地读文件(最好和最坏的方法)
用bash脚本读文件的方法有很多.请看第一部分,我使用了while循环及其后的管道命令(|)(cat $FILE | while read line; do … ),并在循环当中递增 i 的值,最后, ...
- Apache 服务器性能评估
1 查看当前并发连接数 netstat -an | grep ESTABLISHED | wc -l 2 查看当前进程数 ps aux|grep httpd|wc -l
- iOS中如何根据UIView获取所在的UIViewController
原理 Responder Chain 事件的响应者链 大概的传递规则就是从视图顶层的UIView向下到UIViewController再到RootViewController再到Window最后到Ap ...
- Less入门教程
http://www.cnblogs.com/fsjohnhuang/p/4187675.html