题目链接: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. AndroidStudio 3.3+ ButterKnife R2 红名问题

    如果你一直用着ButterKnife,但是用的低版本(比如用得很多的8.4.0),然后在多module项目中一定知道要把R改成R2 然后最近如果升级AndroidStudio了,升到3.3以上(201 ...

  2. mysql 数据操作 多表查询 子查询 带IN关键字的子查询

    1 带IN关键字的子查询 #查询平均年龄在25岁以上的部门名关键点部门名 以查询员工表的dep_id的结果 当作另外一条sql语句查询条件使用 in (sql语句) mysql ; +-------- ...

  3. 编译错误 ----- /usr/bin/ld: cannot find -lc

    yum install glibc-static glib-static是Gcc链接时使用到的库.

  4. Mysql的group by语句

    如上图查询结果,因为group by后面的字段是的分组的依据字段,所以select子句中只有是group by后面的字段,或者是聚集函数才有意义.然而mysql居然可以在select子句中出现不在gr ...

  5. X-UA-Compatible

    X-UA-Compatible是神马? X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中 ...

  6. Linux root用户下强制静音的问题

    解决方法 pulseaudio --start --log-target=syslog suorce /etc/profile

  7. Python: 正则表达式匹配反斜杠 "\"

    Python正则表达式匹配反斜杠 "\" eg: >>>a='w\w\w' 'w\\w\\w' #  打印出来的 "\\" 被转义成 一个反斜 ...

  8. Zookeeper使用实例——分布式共享锁

    前一讲中我们知道,Zookeeper通过维护一个分布式目录数据结构,实现分布式协调服务.本文主要介绍利用Zookeeper有序目录的创建和删除,实现分布式共享锁. 举个例子,性能管理系统中,告警规则只 ...

  9. linux常用命令:date 命令

    在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...

  10. leetcode_目录

    3Sum Closest 3Sum 4Sum Add Binary Add Two Numbers Anagrams Balanced Binary Tree Best Time to Buy and ...