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 ...
随机推荐
- python中实现mysql连接池
python中实现mysql连接池 import pymysql from DBUtils.PooledDB import PooledDB MYSQL_HOST = 'localhost' USER ...
- 002-nginx-在 nginx 反向代理中使用域名,配置动态域名解析
一.概述 代理(proxy),即中间人,它代替客户端发送请求给服务器,收到响应后再转给客户端.通常意义上的代理是从用户的角度讲的,用户通过某个代理可以访问多个网站,这个代理是靠近用户的,比如某些公司可 ...
- Java-idea-设置类头注释和方法注释
一.文件级别的注释 主要是通过File-->Setting-->Editor→File and Code Template中来设置 可以再右侧include中设置File ...
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- JS 转整型
1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4, ...
- linux lvs
- C#中NPOI操作excel之读取和写入excel数据
一.下载引用 下载需要引用的dll,即:NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,ICSharpCode.SharpZipLib.dll(office2 ...
- 017-linux正则表达式
一.单字符表示:1.特定字符:某个具体的字符. '1' 'a' '\.'2.范围内单个字符:单个字符[] [0-9] [259] [a-z] [abc] [A-Z] [ABC] [a-zA-Z] [, ...
- python之路 socket、socket server
一.socket socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意思.通常也 称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,可 ...
- linux常用命令:grep 命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...