Solution -「YunoOI 2016」镜中的昆虫
Description
Link.
- 区间推平;
- 区间数颜色。
Solution
考虑无修的情况,我们是采用维护每个数的 \(pre\) 来做的。具体一点就是对于每一个 \(a_{i}\) 维护 \(pre_{i}=\max\{j\in[1,i),s.t.a_{j}=a_{i}\}\)。然后数区间内 \(pre\) 严格小于左端点的元素个数。
区间推平不好做,考虑削弱这一层修改,考虑单点修改怎么做。
修改一个 \(a_{i}=x\),则受影响的下标有:
- \(j,s.t.pre_{j}=i\);
- \(i\);
- \(\min\{j\in(i,n],s.t.a_{j}=x\}\)。
这个东西套个 std::map 能直接维护。
对于区间修改,有这样的结论:
对于所有修改,\(pre\) 变化次数为 \(\mathcal{O}(n+m)\)。
做不来区间推平翻题解翻来的,至于证明不太会,摊还分析证复杂度没用过。
反正综上就能做了嘛,不知道为什么那么多人都喜欢写树套树,反正我是 CDQ。
//in the autumn sky
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
struct oper
{
int opt,opl,opr,opx;
oper(int A=0,int B=0,int C=0,int D=0){opt=A,opl=B,opr=C,opx=D;}
};
struct ST_modify
{
int ID,pos,pr,val;
ST_modify(int A=0,int B=0,int C=0,int D=0){ID=A,pos=B,pr=C,val=D;}
bool operator<(const ST_modify &ano)const{return pr<ano.pr;}
};
struct ST_query
{
int ID,l,r;
ST_query(int A=0,int B=0,int C=0){ID=A,l=B,r=C;}
bool operator<(const ST_query &ano)const{return l<ano.l;}
};
vector<int> pri;
vector<ST_modify> mod;
vector<ST_query> que;
int pre[100010],n,m,tr[100010],curInd,ans[100010];
map<int,int> mapo[200010];
map<int,pair<int,int> > exmapo; // for ODT
#define lowbit(x) ((x)&(-(x)))
void ins(int pos,int delta)
{
++pos;
while(pos<=n)
{
tr[pos-1]+=delta;
pos+=lowbit(pos);
}
}
int find(int pos)
{
int res=0;
while(pos)
{
res+=tr[pos-1];
pos^=lowbit(pos);
}
return res;
}
void ODT_split(int pos)
{
auto iter=exmapo.lower_bound(pos);
int stan=n;
if(iter!=exmapo.end()) stan=iter->fs;
if(stan^pos)
{
--iter;
exmapo.emplace(pos,iter->sc);
auto exiter=mapo[iter->sc.sc].emplace(pos,iter->sc.fs).fs;
iter->sc.fs=pos;
prev(exiter)->sc=pos;
}
}
void pushOp(int pos,int after,int ID)
{
mod.emplace_back(ST_modify(ID,pos,pre[pos],-1));
pre[pos]=after;
mod.emplace_back(ST_modify(ID,pos,after,1));
}
void rawGrass(int onel,int oner,int anol,int anor,int parl,int parr)
{
if(onel==oner || anol==anor)
{
sort(mod.begin()+onel,mod.begin()+oner);
sort(que.begin()+anol,que.begin()+anor);
}
else
{
int mid=(parl+parr)>>1,exmid1=onel,exmid2=anol;
while(exmid1<oner && mod[exmid1].ID<mid) ++exmid1;
while(exmid2<anor && que[exmid2].ID<mid) ++exmid2;
rawGrass(onel,exmid1,anol,exmid2,parl,mid);
rawGrass(exmid1,oner,exmid2,anor,mid,parr);
int ex=onel;
for(int i=exmid2;i<anor;++i)
{
while(ex<exmid1 && mod[ex].pr<que[i].l) ins(mod[ex].pos,mod[ex].val),++ex;
ans[que[i].ID]+=find(que[i].r)-find(que[i].l);
}
for(int i=onel;i<ex;++i) ins(mod[i].pos,-mod[i].val);
inplace_merge(mod.begin()+onel,mod.begin()+exmid1,mod.begin()+oner);
inplace_merge(que.begin()+anol,que.begin()+exmid2,que.begin()+anor);
}
}
int main()
{
scanf("%d %d",&n,&m);
vector<int> a(n);
vector<oper> b(m);
for(int &i:a) scanf("%d",&i),pri.emplace_back(i);
for(oper &i:b)
{
scanf("%d %d %d",&i.opt,&i.opl,&i.opr);
if(i.opt==1) scanf("%d",&i.opx),pri.emplace_back(i.opx);
--i.opl;
}
sort(pri.begin(),pri.end());
pri.erase(unique(pri.begin(),pri.end()),pri.end());
for(int &i:a) i=lower_bound(pri.begin(),pri.end(),i)-pri.begin();
for(oper &i:b)
{
if(i.opt==1) i.opx=lower_bound(pri.begin(),pri.end(),i.opx)-pri.begin();
}
curInd=0;
for(int i:a)
{
if(mapo[i].size()!=int(0)) pre[curInd]=prev(mapo[i].end())->fs;
else pre[curInd]=-1;
mod.emplace_back(ST_modify(-1,curInd,pre[curInd],1));
mapo[i].emplace(curInd,curInd+1);
exmapo.emplace(curInd,make_pair(curInd+1,i));
++curInd;
}
curInd=0;
for(oper i:b)
{
int ty=i.opt,l=i.opl,r=i.opr,x=i.opx;
if(ty==1)
{
ODT_split(l),ODT_split(r);
auto iter=mapo[x].lower_bound(l);
auto ptr=exmapo.lower_bound(l);
int rpe=(iter!=mapo[x].begin())?((iter=prev(iter))->sc-1):(-1); // for Suf
while(ptr!=exmapo.end())
{
if(ptr->fs==r) break;
pushOp(ptr->fs,rpe,curInd);
int tmp=ptr->sc.sc;
auto exiter=mapo[tmp].erase(mapo[tmp].find(ptr->fs));
if(exiter!=mapo[tmp].end())
{
if(exiter==mapo[tmp].begin()) pushOp(exiter->fs,-1,curInd);
else pushOp(exiter->fs,prev(exiter)->sc-1,curInd);
}
rpe=ptr->sc.fs-1;
ptr=exmapo.erase(ptr);
}
iter=mapo[x].lower_bound(r);
if(iter!=mapo[x].end()) pushOp(iter->fs,r-1,curInd);
exmapo.emplace(l,make_pair(r,x));
mapo[x].emplace(l,r);
}
else que.emplace_back(ST_query(curInd,l,r));
++curInd;
}
rawGrass(0,int(mod.size()),0,int(que.size()),-1,m);
curInd=0;
for(oper i:b)
{
if(i.opt==2) printf("%d\n",ans[curInd]);
++curInd;
}
return 0;
}
Solution -「YunoOI 2016」镜中的昆虫的更多相关文章
- Solution -「SDOI 2016」「洛谷 P4076」墙上的句子
\(\mathcal{Description}\) Link. (概括得说不清话了还是去看原题吧 qwq. \(\mathcal{Solution}\) 首先剔除回文串--它们一定对答案产 ...
- Solution -「ZJOI 2016」「洛谷 P3352」线段树
\(\mathcal{Descrtiption}\) 给定 \(\{a_n\}\),现进行 \(m\) 次操作,每次操作随机一个区间 \([l,r]\),令其中元素全部变为区间最大值.对于每个 \ ...
- Solution -「CERC 2016」「洛谷 P3684」机棚障碍
\(\mathcal{Description}\) Link. 给一个 \(n\times n\) 的网格图,每个点是空格或障碍.\(q\) 次询问,每次给定两个坐标 \((r_1,c_1), ...
- Solution -「NOI 2016」「洛谷 P1587」循环之美
\(\mathcal{Description}\) Link. 给定 \(n,m,k\),求 \(x\in [1,n]\cap\mathbb N,y\in [1,m]\cap \mathbb ...
- Solution -「APIO 2016」「洛谷 P3643」划艇
\(\mathcal{Description}\) Link & 双倍经验. 给定 \(n\) 个区间 \([a_i,b_i)\)(注意原题是闭区间,这里只为方便后文描述),求 \(\ ...
- Solution -「ARC 104E」Random LIS
\(\mathcal{Description}\) Link. 给定整数序列 \(\{a_n\}\),对于整数序列 \(\{b_n\}\),\(b_i\) 在 \([1,a_i]\) 中等概率 ...
- [转帖]「日常小记」linux中强大且常用命令:find、grep
「日常小记」linux中强大且常用命令:find.grep https://zhuanlan.zhihu.com/p/74379265 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍 ...
- Solution -「CF 1023F」Mobile Phone Network
\(\mathcal{Description}\) Link. 有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...
- Solution -「BZOJ 3812」主旋律
\(\mathcal{Description}\) Link. 给定含 \(n\) 个点 \(m\) 条边的简单有向图 \(G=(V,E)\),求 \(H=(V,E'\subseteq E)\ ...
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
随机推荐
- C#.NET Framework RSA 公钥加密 私钥解密 ver:20230609
C#.NET Framework RSA 公钥加密 私钥解密 ver:20230609 环境说明: .NET Framework 4.6 的控制台程序 . .NET Framework 对于RSA的支 ...
- Python-Loguru:让记录日志更装13
" 今天勇哥来介绍一款让日志记录在 Python 中变得更加轻松愉快的库--Loguru.它提供了强大的功能和简洁的接口,使我们能够以更加灵活和直观的方式记录和管理日志信息,据说比loggi ...
- 2023-06-12:如果一个正整数自身是回文数,而且它也是一个回文数的平方,那么我们称这个数为超级回文数。 现在,给定两个正整数 L 和 R (以字符串形式表示), 返回包含在范围 [L, R] 中
2023-06-12:如果一个正整数自身是回文数,而且它也是一个回文数的平方,那么我们称这个数为超级回文数. 现在,给定两个正整数 L 和 R (以字符串形式表示), 返回包含在范围 [L, R] 中 ...
- 3. IOC相关内容
1. bean 相关配置 对于 bean 的配置中,主要会讲解bean基础配置,bean的别名配置,bean的作用范围配置(重点),这三部分内容: 1.1 bean 基础配置(id 与 c ...
- CompletableFuture之批量上传
前言 最近接到一个需求,批量上传图片到服务器及实时更新上传进度.当处理大量文件上传任务时,效率是一个关键因素.传统的串行方式会导致任务耗时较长,而使用并发处理可以极大地提高上传效率.想到很久之前用Co ...
- 如何将PCM格式的原始音频采样数据编码为MP3格式或AAC格式的音频文件?
一.打开和关闭输入文件和输出文件以及判断输入文件是否读取完毕 //io_data.cpp static FILE* input_file= nullptr; static FILE* output_f ...
- 基于JavaFX的扫雷游戏实现(四)——排行榜
这期看标题已经能猜到了,主要讲的是成绩排行功能,还有对应的文件读写.那么废话不多说,让我们有请今天的主角...的设计稿: 那么主角是何方神圣呢?当然是图中的大框框--TableView.关于这 ...
- 多光源渲染方案 - Many Lights Sampling
目录 Importance Sampling(IS) Light BVH [2018~2019] 预构建 BVH 重建 BVH 基于 BVH node 的 IS Real-time Stochasti ...
- Django基本数据库操作
Django基本数据库操作 @ 目录 Django基本数据库操作 内容一:基本数据库配置 内容二:ORM基本操作 内容一:基本数据库配置 Django是一个流行的Python Web框架,它可以 ...
- Burnside 定理
Burnside 定理 问题: 给定一个 \(n\) 个点,\(n\) 条边的环,有 \(m\) 种颜色,给每个顶点染色,问有多少种本质不同的染色方案,答案对 \(10^9+7\) 取模 注意本题的本 ...