P6327 区间加区间sin和 题解
P6327 区间加区间sin和 题解
题目描述
给出一个长度为 \(n\) 的整数序列 \(a_1,a_2,\ldots,a_n\),进行 \(m\) 次操作,操作分为两类。
操作 \(1\):给出 \(l,r,v\),将 \(a_l,a_{l+1},\ldots,a_r\) 分别加上 \(v\)。
操作 \(2\):给出 \(l,r\),询问 \(\sum\limits_{i=l}^{r}\sin(a_i)\)。
想法
考虑线段树。
对于一个节点 \([l, r]\) 它维护的应该是 \(\sin a_l + \sin a_{l + 1} +\dots+\sin a_r\)。
现在有两个问题摆在面前:
up操作down操作
up
很明显,对于这道题而言,可以直接把左右儿子维护的 \(\sin\) 和加起来。
inline void up(int k)
{
tr[k].sin = tr[k << 1].sin + tr[k << 1 | 1].sin;
tr[k].cos = tr[k << 1].cos + tr[k << 1 | 1].cos;
}
down
和角公式:
\cos(\alpha + \beta) = \cos\alpha\cos\beta-\sin\alpha\sin\beta
\]
因而直接套公式,还需要另外维护 \(\cos\) 和。
inline void add(int k, double sinv, double cosv)
{
double sal = tr[k].sin, cal = tr[k].cos; // 注意要先备份一套sin和cos,调了好久Q^Q
tr[k].sin = sal * cosv + cal * sinv; // 更新区间sin和
tr[k].cos = cal * cosv - sal * sinv; // 更新区间cos和
}
inline void down(int k)
{
int v = tr[k].tag;
double sinv = sin(v), cosv = cos(v);
add(k << 1, sinv, cosv);
add(k << 1 | 1, sinv, cosv);
tr[k << 1].tag += tr[k].tag, tr[k << 1 | 1].tag += tr[k].tag;
tr[k].tag = 0;
}
实现
剩下板
// Problem: P6327 区间加区间sin和
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P6327
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Author: Moyou
// Copyright (c) 2022 Moyou All rights reserved.
// Date: 2023-01-31 15:25:03
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#define x first
#define y second
#define speedup (ios::sync_with_stdio(0), cin.tie(0), cout.tie(0))
#define INF 0x3f3f3f3f
#define int long long
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
int n, m;
int a[N];
inline char get_char()
{
static char buf[1000000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;
}
inline int read()
{
int x = 0;
char ch = get_char();
while (ch < '0' || ch > '9')
ch = get_char();
while (ch <= '9' && ch >= '0')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = get_char();
return x;
}
double sinv, cosv;
struct owo
{
int l, r;
double sin, cos;
int tag;
} tr[N << 2];
inline void up(int k)
{
tr[k].sin = tr[k << 1].sin + tr[k << 1 | 1].sin;
tr[k].cos = tr[k << 1].cos + tr[k << 1 | 1].cos;
}
inline void add(int k, double sinv, double cosv)
{
double sal = tr[k].sin, cal = tr[k].cos; // 注意要先备份一套sin和cos,调了好久Q^Q
tr[k].sin = sal * cosv + cal * sinv; // 更新区间sin和
tr[k].cos = cal * cosv - sal * sinv; // 更新区间cos和
}
inline void down(int k)
{
int v = tr[k].tag;
double sinv = sin(v), cosv = cos(v);
add(k << 1, sinv, cosv);
add(k << 1 | 1, sinv, cosv);
tr[k << 1].tag += tr[k].tag, tr[k << 1 | 1].tag += tr[k].tag;
tr[k].tag = 0;
}
void build(int k, int l, int r)
{
tr[k] = {l, r, 0, 0};
if (l == r)
tr[k] = {l, r, sin(a[l]), cos(a[l]), 0};
else
{
int mid = l + r >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
up(k);
}
}
void update(int k, int ql, int qr, int v)
{
int l = tr[k].l, r = tr[k].r, mid = l + r >> 1;
if (ql <= l && qr >= r) // 查询区间包含当前区间
{
add(k, sinv, cosv); // 更新
tr[k].tag += v; // 打懒标记
return;
}
if (tr[k].tag)
down(k);
if(ql <= mid) update(k << 1, ql, qr, v);
if(qr > mid) update(k << 1 | 1, ql, qr, v);
up(k);
}
double query(int k, int ql, int qr)
{
int l = tr[k].l, r = tr[k].r, mid = l + r >> 1;
if(ql <= l && qr >= r)
return tr[k].sin;
if(tr[k].tag) down(k);
double tmp = 0;
if(ql <= mid) tmp += query(k << 1, ql, qr);
if(qr > mid) tmp += query(k << 1 | 1, ql, qr);
return tmp;
}
signed main()
{
n = read();
for (int i = 1; i <= n; i++)
a[i] = read();
m = read();
build(1, 1, n);
while (m--)
{
int op = read(), l = read(), r = read();
if (--op)
printf("%.1lf\n", query(1, l, r));
else
{
int v = read();
sinv = sin(v), cosv = cos(v);
update(1, l, r, v);
}
}
return 0;
}
P6327 区间加区间sin和 题解的更多相关文章
- [用CDQ分治解决区间加&区间求和]【习作】
[前言] 作为一个什么数据结构都不会只会CDQ分治和分块的蒟蒻,面对区间加&区间求和这么难的问题,怎么可能会写线段树呢 于是,用CDQ分治解决区间加&区间求和这篇习作应运而生 [Par ...
- 「模板」 线段树——区间乘 && 区间加 && 区间求和
「模板」 线段树--区间乘 && 区间加 && 区间求和 原来的代码太恶心了,重贴一遍. #include <cstdio> int n,m; long l ...
- 【BZOJ】1798: [Ahoi2009]Seq 维护序列seq 线段树多标记(区间加+区间乘)
[题意]给定序列,支持区间加和区间乘,查询区间和取模.n<=10^5. [算法]线段树 [题解]线段树多重标记要考虑标记与标记之间的相互影响. 对于sum*b+a,+c直接加上即可. *c后就是 ...
- P4315 月下“毛景树” (树链剖分+边剖分+区间覆盖+区间加+区间最大值)
题目链接:https://www.luogu.org/problem/P4315 题目大意: 有N个节点和N-1条树枝,但节点上是没有毛毛果的,毛毛果都是长在树枝上的.但是这棵“毛景树”有着神奇的魔力 ...
- COGS.1317.数列操作c(分块 区间加 区间求和)
题目链接 #include<cmath> #include<cstdio> #include<cctype> #include<algorithm> u ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- LOJ6283 数列分块入门 7 (分块 区间加/乘)题解
题意:区间加,区间乘,单点询问 思路:假设一个点为a,那么他可以表示为m * a + sum,所以区间加就变为m * a + sum + sum2,区间乘变为m * m2 * a + sum * m2 ...
- HDU 5828 Rikka with Sequence(线段树区间加开根求和)
Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...
- 区间加值,区间gcd, 牛客949H
牛客小白月赛16H 小阳的贝壳 题目链接 题意 维护一个数组,支持以下操作: 1: 区间加值 2: 询问区间相邻数差的绝对值的最大值 3: 询问区间gcd 题解 设原数组为\(a\), 用线段树维护\ ...
随机推荐
- C#中下载项目中的文件
1.将需要下载的文档添加到项目的文件夹中 2.接口部分 public IActionResult DownLoad() { var filePath = Directory.GetCurrentDir ...
- MySQL高可用集群MHA方案
MySQL高可用集群MHA方案 爱奇艺在用的数据库高可用方案 MHA 是目前比较成熟及流行的 MySQL 高可用解决方案,很多互联网公司正是直接使用或者基于 MHA 的架构进行改造实现 MySQL 的 ...
- 原生Ajax处理文件流
在通过Ajax处理请求时,可能会遇到需要下载文件的情况,这里简要的说明下处理方法. let downloadFile = document.getElementById("downloadI ...
- 46.drf过滤、搜索、排序
DRF的过滤类 drf过滤器在filters模块中,主要有四个类 BaseFilterBackend:过滤基类,留好占位方法待后续继承 SearchFilter:继承BaseFilterBackend ...
- Java中String被称为不可变字符串的原因
很多东西,看似可变,实际上不过是是新桃换旧符罢了. 代码: /** * String之所以被称为不可变字符串 */ static void testString(){ String str = &qu ...
- 数组还是HashSet?
我记得大约在半年前,有个朋友问我一个问题,现在有一个选型: 一个性能敏感场景,有一个集合,需要确定某一个元素在不在这个集合中,我是用数组直接Contains还是使用HashSet<T>.C ...
- js高级之函数高级部分
基于尚硅谷的尚硅谷JavaScript高级教程提供笔记撰写,加入一些个人理解 github源码 博客下载 原型与原型链 prototype : 显式原型属性,它默认指向一个Object空对象(即称为: ...
- PGL图学习之图神经网络GraphSAGE、GIN图采样算法[系列七]
0. PGL图学习之图神经网络GraphSAGE.GIN图采样算法[系列七] 本项目链接:https://aistudio.baidu.com/aistudio/projectdetail/50619 ...
- UBOOT编译--- include/config.h、 include/autoconf.mk、include/autoconf.mk.dep、u-boot.cfg(三)
1. 前言 UBOOT版本:uboot2018.03,开发板myimx8mmek240. 2. 概述 本节主要接上一节解析 :include/config.h. include/autoconf.mk ...
- vue-element Form表单验证没错却一直提示错误
在使用element-UI 的表单时,发生一个验证错误,已输入值但验证的时候却提示没有输入 修改前 <el-form-item>中的prop绑定的是cus_name,而item里面的控件绑 ...