CF911G Mass Change Queries(线段树+暴力)
cf机子真的快。
其实这个题的维护的信息还是很巧妙的。
首先,观察到题目中涉及到,区间修改这个操作,然后最后只查询一次,我们不妨用线段树来维护这个过程。
但是貌似直接维护每个位置的值可能不太好维护。
这时候我们考虑
每一个节点维护一个\(to\)数组,其中\(to[i]\)表示这个节点对应的区间里面,\(i\)这个值将会变成哪个值。
一开始,每个节点的\(to[i]=i\)
由于最后是单点询问,所以不用\(up\)操作。
现在考虑\(pushdown\)应该怎么写。
对于当前节点的\(to[i]\)应该等于他的父亲的\(to[to[i]]\),就是他原本的i指向的点,在父亲里会指向哪里。
void pushdown(int root,int l,int r)
{
for (rint i=1;i<=100;++i)
{
f[root<<1].to[i]=f[root].to[f[root<<1].to[i]];
f[root<<1|1].to[i]=f[root].to[f[root<<1|1].to[i]];
}
for (rint i=1;i<=100;++i)
f[root].to[i]=i;
}
然后\(update\)的时候,直接暴力扫一遍即可。
将所有\(to[i]=x\)的to,都修改成\(y\).
询问的时候,直接询问\(to\)就可以,直接给代码了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define mk make_pair
#define ll long long
#define rint register int
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int maxn = 2e5+1e2;
int n;
int val[maxn];
struct Node{
int to[102];
};
Node f[4*maxn];
void pushdown(int root,int l,int r)
{
for (rint i=1;i<=100;++i)
{
f[root<<1].to[i]=f[root].to[f[root<<1].to[i]];
f[root<<1|1].to[i]=f[root].to[f[root<<1|1].to[i]];
}
for (rint i=1;i<=100;++i)
f[root].to[i]=i;
}
void build(int root,int l,int r)
{
for (rint i=1;i<=100;++i) f[root].to[i]=i;
if (l==r)
{
return;
}
int mid = l+r >>1;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
}
void update(int root,int l,int r,int x,int y,int p,int pp)
{
if (x<=l && r<=y)
{
for (int i=1;i<=100;i++)
{
if (f[root].to[i]==p) f[root].to[i]=pp;
}
return;
}
pushdown(root,l,r);
int mid = l+r >>1;
if (x<=mid) update(root<<1,l,mid,x,y,p,pp);
if (y>mid) update(root<<1|1,mid+1,r,x,y,p,pp);
}
int query(int root,int l,int r,int x,int p)
{
if (l==r)
{
return f[root].to[p];
}
pushdown(root,l,r);
int mid = l+r >> 1;
if (x<=mid) return query(root<<1,l,mid,x,p);
if (x>mid) return query(root<<1|1,mid+1,r,x,p);
}
int main()
{
n=read();
for (rint i=1;i<=n;++i) val[i]=read();
build(1,1,n);
// for (rint i=1;i<=n;++i)
//{
// cout<<query(1,1,n,i,val[i])<<" ";
//}
// cout<<endl;
int q=read();
for (rint i=1;i<=q;++i)
{
int l=read(),r=read(),x=read(),y=read();
update(1,1,n,l,r,x,y);
// cout<<i<<":"<<endl;
// for (rint i=1;i<=n;++i)
// {
// cout<<query(1,1,n,i,val[i])<<" ";
//}
// cout<<endl;
}
for (rint i=1;i<=n;++i)
{
cout<<query(1,1,n,i,val[i])<<" ";
}
return 0;
}
CF911G Mass Change Queries(线段树+暴力)的更多相关文章
- [CF911G]Mass Change Queries
题目大意: 给你一个长度为n的数列a,按顺序进行以下m次操作,每次将区间[l,r]中的所有x变成y,问最后数列是怎样的. 思路: 线段树. 每个线段树结点上维护当前区间每个数分别会变成多少.时间复杂度 ...
- Mass Change Queries CodeForces - 911G (线段树合并)
链接 大意: 给定序列, 每次操作将区间[l,r]中的x全改为y, 最后输出序列 权值范围比较小, 对每个权值开一颗线段树, 每次将x合并到y上即可 #include <iostream> ...
- V - Can you answer these queries? HDU - 4027 线段树 暴力
V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开 ...
- hdu 4288 线段树 暴力 **
题意: 维护一个有序数列{An},有三种操作: 1.添加一个元素. 2.删除一个元素. 3.求数列中下标%5 = 3的值的和. 解题思路: 看的各种题解,今天终于弄懂了. 由于线段树中不支持添加.删除 ...
- 【BZOJ】3038: 上帝造题的七分钟2(线段树+暴力)
http://www.lydsy.com:808/JudgeOnline/problem.php?id=3038 这题我就有得吐槽了,先是线段树更新写错,然后不知哪没pushup导致te,精度问题sq ...
- codefroces 911G Mass Change Queries
题意翻译 给出一个数列,有q个操作,每种操作是把区间[l,r]中等于x的数改成y.输出q步操作完的数列. 输入输出格式 输入格式: The first line contains one intege ...
- HDU 4027 Can you answer these queries? (线段树区间修改查询)
描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...
- hdu 6430 线段树 暴力维护
Problem E. TeaTree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Oth ...
- hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和
Can you answer these queries? Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...
随机推荐
- Git最强总结!
本文已经收录到Github仓库,欢迎大家围观.star.此仓库用于分享Java核心知识,包括Java基础.MySQL.SpringBoot.Mybatis.Redis.RabbitMQ等等,面试必备. ...
- Kafka详细教程加面试题
一.部署kafka集群 启动zookeeper服务: zkServer.sh start 修改配置文件config/server.properties #broker 的全局唯一编号,不能重复 bro ...
- Django+Ansible构建任务中心思路
Ansible作为老牌的自动化运维工具,由Python开发,应用广泛,但其默认只提供了命令行下的使用方式,好在提供有完善的API支持二次开发,可以很方便的集成到我们的自动化运维系统中 最近一个朋友跳槽 ...
- “ShardingCore”是如何针对分表下的分页进行优化的
分表情况下的分页如何优化 首先还是要给自己的开原框架打个广告 sharding-core 针对efcore 2+版本的分表组件,首先我们来快速回顾下目前市面上分表下针对分页常见的集中解决方案 分表解决 ...
- Docker小白到实战之Dockerfile解析及实战演示,果然顺手
前言 使用第三方镜像肯定不是学习Docker的最终目的,最想要的还是自己构建镜像:将自己的程序.文件.环境等构建成自己想要的应用镜像,方便后续部署.启动和维护:而Dockerfile就是专门做这个事的 ...
- Linux--MySQL 日志管理、备份与恢复
MySQL 日志管理.备份与恢复一.MySQL 日志管理二.数据库备份的重要性与分类 1.数据备份的重要性 2.从物理与逻辑的角度,备份分为 3.从数据库的备份策略角度,备份可分为三.常见的备份 ...
- 生成随机uuid
/** * 生成随机uuid */ export function uuid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.repla ...
- nohup命令的用法
在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/local/mysql/bin/my ...
- 洛谷P1583——魔法照片(结构体排序)
https://www.luogu.org/problem/show?pid=1583#sub 题目描述 一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人. ...
- python+echarts+flask实现对全国疫情数据的爬取并可视化展示
用Python进行数据爬取并存储到数据库,3.15学习总结(Python爬取网站数据并存入数据库) - 天岁 - 博客园 (cnblogs.com) 通过echarts+flask实现数据的可视化展示 ...