Wannafly 挑战赛22 D 整数序列 线段树 区间更新,区间查询
题目链接:https://www.nowcoder.com/acm/contest/160/D
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
操作1:给出l,r,v,将al,al+1,...,ar分别加上v;
操作2:给出l,r,询问
输入描述:
n个数,m次操作
op=1, l,r,v 区间[l,r] 加v
op=2, l,r 区间查询上式
n,m,val[i] <= 2e5
输出描述:
对每个操作2,输出一行,表示答案,四舍五入保留一位小数
保证答案的绝对值大于0.1,且答案的准确值的小数点后第二位不是4或5
数据随机生成(n,m人工指定,其余整数在数据范围内均匀选取),并去除不满足条件的操作2
输入例子:
4
1 2 3 4
5
2 2 4
1 1 3 1
2 2 4
1 2 4 2
2 1 3
输出例子:
0.3
-1.4
-0.3
-->
输出
0.3
-1.4
-0.3 很明显区间更新的题 用lazy维护某一段区间的 sin(val[i])和 cos(val[i])值
根据下面的式子 可知道 要保证区间加和性,就要先要 维护sin(x) 区间加和的性质
so 看代码 和 公式 应该差不多能理解的
// sin(x + v) = sinx*cosv+sinvcosx
// cos(x + v) = cosx*cosv-sinx*sinv // sin(x1 + v) + sin(x2 + v) = cosv*(sinx1+sinx2) + sinv*(cosx1+cosx2)
// cos(x1 + v) + cos(x1 + v) = cosv*(cosx1+cosx2) - sinv*(sinx1+sinx2)
#include<bits/stdc++.h>
using namespace std; const int N = +;
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll; ll val[N], lz[N<<];
double ssin[N<<],ccos[N<<]; void up(int rt) {
ssin[rt] = ssin[ls] + ssin[rs];
ccos[rt] = ccos[ls] + ccos[rs];
} void down(int rt) {
if(lz[rt]) {
ll v = lz[rt];
lz[ls] += v;
lz[rs] += v; double tsin=ssin[ls], tcos=ccos[ls];
ssin[ls]=tsin*cos(v) + tcos*sin(v);
ccos[ls]=tcos*cos(v) - tsin*sin(v); tsin=ssin[rs],tcos=ccos[rs];
ssin[rs]=tsin*cos(v) + tcos*sin(v);
ccos[rs]=tcos*cos(v) - tsin*sin(v);
lz[rt]=;
}
} void build(int rt,int l,int r) {
if(l==r) {
ssin[rt]=sin(val[l]);
ccos[rt]=cos(val[l]);
return ;
}
int m = (l+r)/;
build(ls,l,m);
build(rs,m+,r);
up(rt);
} void update(int rt,int l,int r,int L,int R,ll v) {
if(L <= l && r <= R) {
lz[rt] += v;
double tsin=ssin[rt], tcos=ccos[rt];
ssin[rt]=tsin*cos(v) + tcos*sin(v);
ccos[rt]=tcos*cos(v) - tsin*sin(v);
return ;
}
down(rt);
int m = (l+r)/;
if(L <= m)
update(ls,l,m,L,R,v);
if(m < R)
update(rs,m+,r,L,R,v);
up(rt);
} double query(int rt,int l,int r,int L,int R) {
if(L<=l && r<=R) {
return ssin[rt];
}
down(rt);
int m = (l+r)/;
double res = ;
if(L <= m)
res += query(ls,l,m,L,R);
if(m < R)
res += query(rs,m+,r,L,R);
return res;
} int n,m;
int main ()
{
//freopen("in.txt","r",stdin);
while (scanf("%d",&n)!=EOF) {
memset(lz,,sizeof(lz));
for(int i=;i<=n;i++) {
scanf("%lld", &val[i]);
}
build(,,n);
scanf("%d", &m);
while (m--){
int op,l,r;
scanf("%d %d %d",&op,&l,&r);
if(op==) {
ll v; scanf("%lld",&v);
update(,,n,l,r,v);
}else {
printf("%.1f\n",query(,,n,l,r));
}
}
}
return ;
}
Wannafly 挑战赛22 D 整数序列 线段树 区间更新,区间查询的更多相关文章
- codevs 1690 开关灯 线段树区间更新 区间查询Lazy
题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- Wannafly挑战赛22 D 整数序列 (线段树维护三角函数值)
链接:https://ac.nowcoder.com/acm/contest/160/D 来源:牛客网 整数序列 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询
链接: I - 秋实大哥与花 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
- HDU1698 线段树(区间更新区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- POJ-3468(线段树+区间更新+区间查询)
A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...
随机推荐
- 【CSS3】CSS3自学
CSS3学习网址:http://www.runoob.com/css3/css3-tutorial.html
- minus查找两张表的不同项
minus关键字的使用: select * from A minus select * from B; 上面的SQL语句返回的是表A中存在,表B中不存在的数据: 注意:1.区分不同的规则是查询的所有字 ...
- keras搭建密集连接网络/卷积网络/循环网络
输入模式与网络架构间的对应关系: 向量数据:密集连接网络(Dense层) 图像数据:二维卷积神经网络 声音数据(比如波形):一维卷积神经网络(首选)或循环神经网络 文本数据:一维卷积神经网络(首选)或 ...
- Leetcode: Repeated DNA Sequence
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- OAuth 白话简明教程 3.客户端模式(Client Credentials)
转自:http://www.cftea.com/c/2016/11/6704.asp OAuth 白话简明教程 1.简述 OAuth 白话简明教程 2.授权码模式(Authorization Code ...
- mysql的Navicat查看数据库的ER图
1.mysql数据库表间的关系图可以通过navicat查看.
- mysql参数配置文件
(1)参数配置文件中的内容以键值对形式存在. (2)如何查看键值对?show variables like '%name%';或者查看information_schema库下的global_varia ...
- 谷歌插件--Advanced REST client
早上在测试调用服务去获取数据的时候,因为自己的单元测试不是很熟悉,问了同事,同事给我介绍了一个插件Advanced REST client,这个可以在谷歌的“扩展与应用”中找打,使用 安装之后会提示要 ...
- Python 迭代器切片
函数itertools.islice() 正好适用于在迭代器和生成器上做切片操作 >>> def count(n): ... while True: ... yield n ... ...
- linux常用命令:sudo 命令
sudo命令用来以其他身份来执行命令,预设的身份为root. 1.命令格式: sudo [参数] [命令] 2.命令功能: 功能: sudo可以针对单个命令授予临时权限.用户也可以通过su切换到 ...