2019南昌网络赛 I. Yukino With Subinterval 树状数组套线段树
I. Yukino With Subinterval
题目链接:
Problem Descripe
Yukino has an array \(a_1, a_2 \cdots a_n\). As a tsundere girl, Yukino is fond of studying subinterval.
Today, she gives you four integers $l, r, x, y $, and she is looking for how many different subintervals \([L, R]\) are in the interval \([l, r]\)that meet the following restraints:
- \(a_L =a_{L+1} =\cdots=a_R\), and for any $ i\in [L,R], x \le a_i \le y$.
- The length of such a subinterval should be maximum under the first restraint.
Note that two subintervals \([L_1,R_1] , [L_2,R_2]\) are different if and only if at least one of the following formulas is true:
- \(L1 \cancel= L2\)
- \(R1 \cancel= R2\)
Yukino, at the same time, likes making tricks. She will choose two integers \(pos,v\), and she will change \(a_{pos}\) to \(v\).
Now, you need to handle the following types of queries:
- \(1 \ pos \ v\) : change \(a_{pos}\) to $v $
- \(2\) \(l \ r \ x \ y\): print the number of legal subintervals in the interval \([l, r]\)
Input
The first line of the input contains two integers \(n, m (1 \le n, m \le 2 \times 10^5)\)– the numbers of the array and the numbers of queries respectively.
The second line of the input contains nnn integers \(a_i (1 \le a_i \le n)\).
For the next mmm line, each containing a query in one of the following queries:
- \(1\) \(pos\) \(v \ (1 \le pos, v \le n)\): change \(a_{pos}\) to \(v\)
- \(2 \ l \ r \ x \ y (1 \le l \le r \le n) (1 \le x \le y \le n)\): print the number of legal subintervals in the interval \([l,r]\)
Output
For each query of the second type, you should output the number of legal subintervals in the interval \([l, r]\).
样例输入
6 3
3 3 1 5 6 5
2 2 3 4 5
1 3 2
2 1 6 1 5
样例输出
0
4
样例解释
For the first operations, there are \(3\) different subintervals \(([2, 2],[3, 3],[2,3])\)in the interval \([2, 3]\), but none of them meets all the restraints.
For the third operations, the legal subintervals in interval \([1, 6]\) are: \([1, 2], [3, 3], [4, 4], [6, 6]\)
Notes that although subintervals \([1,1]\) and \([2,2]\) also meet the first restraint, we can extend them to subinterval \([1, 2]\). So the length of them is not long enough, which against the second one.
题意
给你一个序列,提供两种操作
- \(1\) \(pos\) \(v \ (1 \le pos, v \le n)\): 将 \(a_{pos}\) 改为 \(v\)
- \(2 \ l \ r \ x \ y (1 \le l \le r \le n) (1 \le x \le y \le n)\): 输出\([l,r]\) 中权值\(\in [x,y]\) 的个数。特别注意一段连续相同的数只算一次
题解
树套树\(n\)年前打的,早就忘了,于是直接跳过,其实这就是一道可修改区间第k大模板题吧,如果不会的可以去luogu学习一下。
模板传送门:https://www.luogu.org/problem/P3380
这题唯一要解决的就是怎么处理连续段只算一次的问题了。我是树状数组套线段树,于是如果\(a[i]=a[i-1]\)那么就不处理。
还有几个需要注意的地方
- 如果改变了\(a[i]\)的值,记得更改\(a[i-1]\)和\(a[i+1]\)
- 对于区间\([l,r]\),记得特判\(a[l]\),可能\(a[l]=a[l-1]\),但是这时也要算
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 200050
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int n,m,treeNode;
int a[N],ql[20],qr[20];
struct Tree{int ls,rs,sum;}tr[N*150];
void update(int&x,int p,int tt,int l,int r)
{
if (x==0)x=++treeNode;
tr[x].sum+=tt;
if (l==r)return;
int mid=(l+r)>>1;
if (p<=mid)update(tr[x].ls,p,tt,l,mid);
else update(tr[x].rs,p,tt,mid+1,r);
}
void change(int x,int p,int tt)
{while(x<=n)update(x,p,tt,1,n+1),x+=x&-x;}
void getRt(int l,int r)
{
ql[0]=qr[0]=0;
while(l)ql[++ql[0]]=l,l-=l&-l;
while(r)qr[++qr[0]]=r,r-=r&-r;
}
int getSum()
{
int ans=0;
for(int i=1;i<=ql[0];i++)ans-=tr[tr[ql[i]].ls].sum;
for(int i=1;i<=qr[0];i++)ans+=tr[tr[qr[i]].ls].sum;
return ans;
}
void move_L()
{
for(int i=1;i<=ql[0];i++)ql[i]=tr[ql[i]].ls;
for(int i=1;i<=qr[0];i++)qr[i]=tr[qr[i]].ls;
}
void move_R()
{
for(int i=1;i<=ql[0];i++)ql[i]=tr[ql[i]].rs;
for(int i=1;i<=qr[0];i++)qr[i]=tr[qr[i]].rs;
}
int _Rank(int p,int l,int r)
{
if (l==r)return 0;
int mid=(l+r)>>1,tp=getSum();
if (p<mid){move_L();return _Rank(p,l,mid);}
move_R(); return tp+_Rank(p,mid+1,r);
}
int Rank(int l,int r,int k)
{
getRt(l-1,r);
return _Rank(k-1,1,n+1);
}
void work()
{
int id,pos,v,l,r,x,y;
read(n); read(m);
treeNode=n;
for(int i=1;i<=n;i++)read(a[i]);
for(int i=1;i<=n;i++)if (a[i]!=a[i-1])change(i,a[i],1);
for(int i=1;i<=m;i++)
{
read(id);
if (id==1)
{
read(pos); read(v);
if (a[pos]!=a[pos-1])change(pos,a[pos],-1);
if (v!=a[pos-1])change(pos,v,1);
if (a[pos]==a[pos+1])change(pos+1,a[pos+1],1);
if (v==a[pos+1])change(pos+1,a[pos+1],-1);
a[pos]=v;
}
if (id==2)
{
read(l); read(r); read(x); read(y);
int ans=-Rank(l,r,x)+Rank(l,r,y+1);
if (a[l]==a[l-1]&&x<=a[l]&&a[l]<=y)ans++;
printf("%d\n",ans);
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
work();
}
2019南昌网络赛 I. Yukino With Subinterval 树状数组套线段树的更多相关文章
- 2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)
题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1. 有单点修改和区间查询. 思路:46min交了第一发树套树,T了. 稍加优化多交几次就过了. 不难想到,除了L这个点, ...
- 2019南昌网络赛-I. Yukino With Subinterval 线段树套树状数组,CDQ分治
TMD...这题卡内存卡的真优秀... 所以以后还是别用主席树的写法...不然怎么死的都不知道... 树套树中,主席树方法开权值线段树...会造成空间的浪费...这道题内存卡的很紧... 由于树套树已 ...
- 2019 ICPC 南昌网络赛I:Yukino With Subinterval(CDQ分治)
Yukino With Subinterval Yukino has an array a_1, a_2 \cdots a_na1,a2⋯*a**n*. As a tsundere girl, Yuk ...
- HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Tota ...
- 2019icpc徐州现场赛 H Yuuki and a problem (树状数组套主席树)
题意 2e5的数组,q个操作 1.将\(a[x]\)改为y 2.求下标l到r内所有的\(a[i]\)通过加法不能构成的最小的值 思路 通过二操作可以知道需要提取l到r内的值及其数量,而提取下标为l到r ...
- ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval
ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...
- ACM-ICPC 2019南昌网络赛F题 Megumi With String
ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...
- 计蒜客 1460.Ryuji doesn't want to study-树状数组 or 线段树 (ACM-ICPC 2018 徐州赛区网络预赛 H)
H.Ryuji doesn't want to study 27.34% 1000ms 262144K Ryuji is not a good student, and he doesn't wa ...
- 2019南昌网络赛 hello 2019
这道题和一道2017,2016的类似. A string t is called nice if a string “2017” occurs in t as a subsequence but a ...
随机推荐
- 【概率论】3-5:边缘分布(Marginal Distribution)
title: [概率论]3-5:边缘分布(Marginal Distribution) categories: Mathematic Probability keywords: Marginal p. ...
- 元祖(tuple)
元祖和列表几乎是一样的: 列表是可以进行修改的,它可以填加一个元素,也可以移除一个元素,但元祖是不能修改的 如果我们以后想导一个集合,这个集合以后不允许修改,我们用元祖:如何我们想让别人进行修改,我们 ...
- 【做题记录】AtCoder AGC做题记录
做一下AtCoder的AGC锻炼一下思维吧 目前已做题数: 75 总共题数: 239 每一场比赛后面的字母是做完的题,括号里是写完题解的题 AGC001: ABCDEF (DEF) AGC002: A ...
- Reconnect due to socket error java.nio.channels.ClosedChannelException
storm整合kafka后出现如下异常: 错误原因:有部分kafka服务器连接不上导致,检查一下是不是每个kafka都能连接到(有的kafka配置使用的是host,记得配置相同的环境) 造成异常代码段 ...
- ubuntu dnsmasq问题
在很多ubuntu开启wifi热点的教程中,配置比较繁琐的是hostapd+dnsmasq,很多教程都给出了dnsmasq的安装过程,其实在ubuntu桌面版系统下,已经集成到NetworkManag ...
- @Autowired注解与@Resource注解的区别与用法
Spring不但支持自己定义的@Autowired注解,还支持JSR-250规范定义的几个注解.如:@Resource.@PostConstruct及@PreDestroy 1. @Autowired ...
- Qt编写数据可视化大屏界面电子看板8-调整间距
一.前言 在数据可视化大屏界面电子看板系统中,前期为了使用目标客户机,调整间距是必不可少的工作,QMainWindow中的QDockWidget,会默认生成布局和QSplitter调整宽高大小,鼠标移 ...
- MySQL 查询某个数据库中所有包含数据记录的表名
MySQL 查询某个数据库中所有包含数据记录的表名 有时根据实际应用需要,需要对数据进行备份. 如果一个数据库中有很多数据表,但是只想备份包含数据记录的那些表数据(空表不做数据备份). 如果通过如下S ...
- python之selenium多窗口切换
前提: 在页面操作过程中有时候点击某个链接会弹出新的窗口,这就需要主机切换到新打开的窗口上.WebDriver提供了switch_to.window()方法,可以实现在不同的窗口之间切换. 内容: 以 ...
- 鸟哥私房菜基础篇:程序管理与 SELinux 初探习题
猫宁!!! 参考:http://cn.linux.vbird.org/linux_basic/0440processcontrol.php 1-简单说明什么是程序 (program) 而什么是程序 ( ...