Codeforces878E. Numbers on the blackboard
$n \leq 100000$的数列,数字范围$-1e9,1e9$,现$q \leq 1e5$个每次问在一个区间玩游戏,能得到的最大的数。“游戏”:选相邻两个数$a_x,a_y$,然后把他们删掉,变成$a_x+2a_y$,直到序列中只剩一个数。答案$\mod \ \ 1e9+7$。
单次询问可用贪心解决:首先第一个数系数一定是1,后面的数的系数是$2^k,k\geq 1$且$k$不会比上一个数的$k$多2以上。用一块表示给某个区间分配了系数2,4,8,16,。。。,也就是这个区间从左到右一个个合并,那么在数列右边加入一个新的数时,如果这个数是负数,那给他新开一块,否则合并到上一块中,若上一块的答案因此变成了正的,那就继续往前合并。
多次询问只需离线一下,记一下每个块的位置,然后按上面的方法模拟,询问时二分一下就好了。不过要注意,询问时可能左端点处不是一个完整块,这时需要把那个块的后缀单独拿出来算答案。
有个大问题:数字很大,如何判断大小以决定某个块是否要往前并?可以发现负权值最小只到2e9,因此超过2e9的正权值记成2e9+1就行了。
//#include<iostream>
#include<cstring>
#include<cstdio>
//#include<math.h>
//#include<set>
//#include<queue>
//#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std; #define LL long long
int qread()
{
char c; int s=,f=; while ((c=getchar())<'' || c>'') (c=='-') && (f=-);
do s=s*+c-''; while ((c=getchar())>='' && c<=''); return s*f;
} //Pay attention to '-' , LL and double of qread!!!! int n,m;
#define maxn 100011
const int mod=1e9+; int a[maxn],pos[maxn],bin[maxn],inv[maxn],sum[maxn],lp=,val[maxn],vv[maxn],svv[maxn];
struct Ques{int l,r,id; bool operator < (const Ques &b) const {return r<b.r;} }q[maxn];
int ans[maxn]; int WWW(LL v) {return v>?:v;} int main()
{
n=qread(); m=qread();
bin[]=inv[]=; for (int i=;i<=n;i++)
a[i]=qread(),bin[i]=(bin[i-]<<)%mod,inv[i]=1ll*inv[i-]*((mod+)>>)%mod,
sum[i]=((sum[i-]+1ll*a[i]*bin[i])%mod+mod)%mod;
for (int i=;i<=m;i++) {q[i].l=qread(); q[q[i].id=i].r=qread();}
sort(q+,q++m); pos[lp=]=; int j=; val[]=a[]*; vv[]=svv[]=((a[]*)%mod+mod)%mod;
while (j<=m && q[j].r==) ans[q[j].id]=(a[]+mod)%mod,j++;
for (int i=;i<=n;i++)
{
if (a[i]<=) pos[++lp]=i,val[lp]=*a[i],vv[lp]=(a[i]*2ll%mod+mod)%mod,svv[lp]=(svv[lp-]+vv[lp])%mod;
else
{
int cur=WWW(0ll+val[lp]+bin[pos[lp]-pos[lp-]+]*1ll*a[i]);
int cvv=(vv[lp]+bin[pos[lp]-pos[lp-]+]*1ll*a[i])%mod; lp--;
while (lp && cur>)
{
cur=WWW(0ll+val[lp]+(pos[lp]-pos[lp-]>?:bin[pos[lp]-pos[lp-]])*1ll*cur);
cvv=(vv[lp]+bin[pos[lp]-pos[lp-]]*1ll*cvv)%mod; lp--;
}
val[++lp]=cur; pos[lp]=i; vv[lp]=cvv; svv[lp]=(svv[lp-]+vv[lp])%mod;
}
while (j<=m && q[j].r==i)
{
if (q[j].l==q[j].r) {ans[q[j].id]=(a[i]+mod)%mod; j++; continue;}
int l=q[j].l+,Ans=(a[q[j].l]+mod)%mod;
int L=,R=lp;
while (L<R)
{
int mid=(L+R)>>;
if (pos[mid]>=l) R=mid; else L=mid+;
}
Ans=(Ans+svv[lp]-svv[L])%mod; Ans=(Ans+mod)%mod;
Ans=(Ans+(sum[pos[L]]-sum[l-]+mod)*1ll*inv[l-])%mod;
ans[q[j].id]=Ans;
j++;
}
} for (int i=;i<=m;i++) printf("%d\n",ans[i]);
return ;
}
Codeforces878E. Numbers on the blackboard的更多相关文章
- 【CF878E】Numbers on the blackboard 并查集
[CF878E]Numbers on the blackboard 题意:给你一个长度为n个数列,你每次可以进行如下操作: 选取两个相邻的数x,y(x在y左面),然后将这两个数去掉,用x+2y替换它. ...
- Codeforces 878 E. Numbers on the blackboard
Codeforces 878 E. Numbers on the blackboard 解题思路 有一种最优策略是每次选择最后面一个大于等于 \(0\) 的元素进行合并,这样做完以后相当于给这个元素乘 ...
- CF 878E Numbers on the blackboard 并查集 离线 贪心
LINK:Numbers on the blackboard 看完题觉得很难. 想了一会发现有点水 又想了一下发现有点困难. 最终想到了 但是实现的时候 也很难. 先观察题目中的这个形式 使得前后两个 ...
- Codeforces Beta Round #51 B. Smallest number dfs
B. Smallest number Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/pro ...
- ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)
ZOJ 3529 - A Game Between Alice and Bob Time Limit:5000MS Memory Limit:262144KB 64bit IO For ...
- ZOJ 3529 A Game Between Alice and Bob 博弈好题
A Game Between Alice and Bob Time Limit: 5 Seconds Memory Limit: 262144 KB Alice and Bob play t ...
- ural1682 Crazy Professor
Crazy Professor Time limit: 1.0 secondMemory limit: 64 MB Professor Nathan Mathan is crazy about mat ...
- [HDU - 5170GTY's math problem 数的精度类
题目链接:HDU - 5170GTY's math problem 题目描述 Description GTY is a GodBull who will get an Au in NOI . To h ...
- BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)
GTY's math problem Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
随机推荐
- JQuery EasyUI学习记录(二)
1.jquery easyUI动态添加选项卡(查看jquery easyUI手册) 1.1 用于动态添加一个选项卡 1.1.1 选中指定的选项卡和判断某个选项卡是否存在 测试代码: <a id= ...
- 字符串 -----JavaScript
本文摘要:http://www.liaoxuefeng.com/ JavaScript的字符串就是用''或""括起来的字符表示. 如果'本身也是一个字符,那就可以用"&q ...
- 小试牛刀,建立jsp网页与导出war包
一.建立jsp网页 首先创建一个动态项目(我们学习的是动态网) 二.检查编码utf-8有没错误. 如有错误就是没有设置eclipse,请按照eclipse设置 编写一段代码,进行了解 三.导出一个wa ...
- (转发)IOS高级开发~Runtime(四)
用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern ...
- c++ 定义一个结构体student,输入多个student的信息并以三种方式显示
#include <iostream> #include <string> using namespace std; const int slen = 30; struct s ...
- Bzoj 1131[POI2008]STA-Station (树形DP)
Bzoj 1131[POI2008]STA-Station (树形DP) 状态: 设\(f[i]\)为以\(i\)为根的深度之和,然后考虑从他父亲转移. 发现儿子的深度及其自己的深度\(-1\) 其余 ...
- NodeJS基础入门-Buffer
Buffer.byteLength console.log(Buffer.byteLength('test')); console.log(Buffer.byteLength('我是C语言爱好者')) ...
- LeetCode之Weekly Contest 93
第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的 ...
- jQuery 淡入淡出有png图的时候 ie8下有黑色边框
jQuery fadeTo 时ie8 png图片有黑色边框 往带有png图的样式里加 filter:progid:DXImageTransform.Microsoft.AlphaImageLoader ...
- MySQL中CONCAT()的用法
MySQL中CONCAT()的用法 在日常开发过程中,特别是在书写接口的时候,经常会遇到字符串拼接的情况,比如在返回图片数据时,数据库里往往存储的是相对路径,而接口里一般是存放绝对地址,这就需要字符串 ...