题目链接:https://www.nowcoder.com/acm/contest/160/D

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

给出一个长度为n的整数序列a1,a2,...,an,进行m次操作,操作分为两类。
操作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

-->

示例1

输入

复制

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 很明显区间更新的题 用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 整数序列 线段树 区间更新,区间查询的更多相关文章

  1. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  2. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  3. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  4. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  5. Wannafly挑战赛22 D 整数序列 (线段树维护三角函数值)

    链接:https://ac.nowcoder.com/acm/contest/160/D 来源:牛客网 整数序列 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语 ...

  6. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

  7. CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询

    链接: I - 秋实大哥与花 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit ...

  8. HDU1698 线段树(区间更新区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  9. POJ-3468(线段树+区间更新+区间查询)

    A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...

随机推荐

  1. 【python+opencv】轮廓发现

    python+opencv---轮廓发现 轮廓发现---是基于图像边缘提取的基础寻找对象轮廓的方法, 所有边缘提取的阈值选定会影响最终轮廓发现的结果. 介绍两种API使用: -cv.findConto ...

  2. 最新版OpenWrt编译教程,解决依赖问题

    Install git , to conveniently download the OpenWrt source code, and build tools to do the cross-comp ...

  3. android 数据存储&lt;一&gt;----android短信发送器之文件的读写(手机+SD卡)

    本文实践知识点有有三: 1.布局文件,android布局有相对布局.线性布局,绝对布局.表格布局.标签布局等,各个布局能够嵌套的. 本文的布局文件就是线性布局的嵌套 <LinearLayout ...

  4. oj1500(Message Flood)字典树

    大意:输入几个字符串,然后再输入几个字符串,看第一次输入的字符串有多少没有在后面的字符串中出现(后输入的字符串不一定出现在之前的字符串中) #include <stdio.h> #incl ...

  5. RAC禁用DRM特性

    查看"_gc"开头的隐藏参数值: set linesize 333 col name for a35 col description for a66 col value for a ...

  6. file_get_post实现post请求

    function Post($url, $post = null){     $context = array();     if (is_array($post)) {       ksort($p ...

  7. Oracle 错误代码小结

    ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最大会话许可数 ORA-00020: 超出 ...

  8. Object之总结(一)

    一.Object类中一共有12个方法.一个私有方法,两个保护方法,9个公共方法.另外还有一个静态代码块. 1.registerNatives方法.私有静态本地无参数无返回值. 2.finalize方法 ...

  9. 010-centos上安装matlab

    #001-下载matlab_R2015b和破解文件(四个)到百度云盘上下载7.6g#002-上传matlab大文件先安装vm tools,然后直接复制到虚拟机桌面#003-挂载matlab镜像并安装m ...

  10. STA分析(四) lib model

    library中的一个cell可以是一个standard cell,IO buffer,或者一个complex IP.其中包含area,functionality,timing,power等相关的信息 ...