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:

  1. \(a_L =a_{L+1} =\cdots=a_R\), and for any $ i\in [L,R], x \le a_i \le y$.
  2. 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:

  1. \(L1 \cancel= L2\)
  2. \(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]\)那么就不處理。

還有幾個需要注意的地方

  1. 如果改變了\(a[i]\)的值,記得更改\(a[i-1]\)\(a[i+1]\)
  2. 對于區間\([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_JUDGEfreopen("aa.in","r",stdin);
#endifwork();
}

轉載于:https://www.cnblogs.com/mmmqqdd/p/11508864.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/447824.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/447824.shtml
英文地址,請注明出處:http://en.pswp.cn/news/447824.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

健康丨汗從哪里出 病從哪里來

1.額頭出汗肝陽上亢如果額頭常常出很多汗&#xff0c;中醫認為可能是肝陽上亢引起的。建議你去醫院檢查一下甲狀腺激素分泌是否正常&#xff0c;因為這很可能是甲狀腺激素分泌過剩造成的。  醫師建議&#xff1a;平時盡量保持心境平和&#xff0c;少生氣&#xff0c;女人尤其…

CDN(內容分發網絡)技術原理

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 前言 Internet的高速發展&#xff0c;給人們的工作和生活帶來了極大的便利&#xff0c;對Internet的服務品質和訪問速度要求越來越高…

3.0 go mod之遠程倉庫搭建-代碼示例

注意事項 所謂的遠程倉庫指的是github&#xff0c;個人首次使用go mod在其他云倉庫上嘗試&#xff0c;并未成功&#xff0c;這浪費了我近2小時的時間&#xff1b; 如果你是初次嘗試&#xff0c;那么除了github的地址換一下之外&#xff0c;其他的都按照示例操作&#xff0c;比如…

視界云:CDN{內容分發網絡} 知識詳解

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 CDN 全稱:Content Delivery Network或Content Ddistribute Network&#xff0c;即內容分發網絡 基本思路&#xff1a; 盡可能避開互聯…

2019牛客多校第七場E Find the median 權值線段樹+離散化

Find the median題目鏈接&#xff1a; https://ac.nowcoder.com/acm/contest/887/E 題目描述 Let median of some array be the number which would stand in the middle of this array if it was sorted beforehand. If the array has even length let median be smallest of …

男人腎虛的8大表現

導語&#xff1a;腎虛是一種常見的現象。尤其是男人&#xff0c;最害怕的就是腎虛。男人的了腎虛怎么辦&#xff0c;腎虛主要都有哪些癥狀。下面專家給大家介紹一下男人腎虛的幾種表現&#xff1a; 一、畏寒肢冷 “畏寒”指有怕冷而且怕風吹的感覺。“肢冷”指四肢手足冰冷&…

更改 nginx 默認端口 ( ubuntu、linux )

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 我想讓一個demo 站點直接域名訪問&#xff0c;不帶端口&#xff0c;所以想用 80 端口啟動對應前端工程。 發現 80 被 nginx 占用&a…

怎么更改Rstudio中的默認目錄

方法一、 每次啟動Rstudio之后&#xff0c;執行代碼 setwd("F:/R/R_data")默認目錄就會修改為雙引號內的位置路徑。 方法二、 對Rstudio進行設置一次即可。 ①點擊Tools&#xff0c;打開Global Options. ②將位置設置完畢&#xff0c;點擊 Apply 確認即可。 ③Rstudi…

職場十個方法 讓專業氣質成為你的符號!

1、任何時候都要準時。   上班或是開會的時候遲到&#xff0c;都會給別人一種你對工作不夠認真的印象。所以請一定要多多注意時間的問題。當然你要注意的不僅僅是開始的時間&#xff0c;還有午休結束的時間&#xff0c;可不要貪圖幾分鐘的自由&#xff0c;棄你的專業氣質于不…

docker 虛懸鏡像 ( 懸空鏡像 ) :鏡像沒有倉庫名或沒有標簽

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 我們在build鏡像的過程中&#xff0c;可能會產生一些臨時的不具有名稱也沒有作用的鏡像他們的名稱一般都是<none>, 我們可以執…

R-apply()函數

創建一個列表變量&#xff0c;它的第一個元素包含所有從0到9的平方數&#xff0c;第二個元素為10到19之內的所有平方數&#xff0c;依此類推&#xff0c;最后一個元素為90到99之內的平方數。沒有平方數的元素也應該被包含在內&#xff01; 學習網友的解題思路&#xff0c;用的是…

編程興趣真的是由“熱情”驅動的嗎?

當我告訴人們我以寫代碼為生時&#xff0c;他們翻著白眼問我編程是不是特無聊&#xff1f;有許多編程博客告訴我們&#xff0c;如果你想要精于編程&#xff0c;那么就必須先熱愛編程。那么&#xff0c;這是不是意味著如果沒有激情&#xff0c;那你就寫不出一行代碼&#xff1f;…

心生想往 ... ...

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 連日里的忙碌 ... 終又忍不住開始想往 ... 聽著歌兒 放縱篇篇翻飛思緒 ... 拋下紛繁的朝九晚六和所有加班&#xff0c;于每一日&#…

C# 打開文件/跳轉鏈接

mark一下~ 打開文件 1.打開文件夾&#xff1a; System.Diagnostics.Process.Start(FolderPath);-- 打開文件夾 System.Diagnostics.Process.Start(FolderPath"/"FileName); -- 打開文件夾中某個文件 2.用IE打開文件: System.Diagnostics.Process.Start("Explore…

身體曲線如何反映出健康

站在鏡子前&#xff0c;看看自己的身材&#xff0c;是否勻稱優美?身體曲線不僅是美和丑的象征&#xff0c;同時還能夠反映出你的健康狀況。 1.腿細 有些人四肢纖細或運動后易酸痛&#xff0c;可能意味著肌肉少、力量弱。多項研究表明&#xff0c;肌肉與健康狀況及壽命都存在…

路的盡頭 ...

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 一直一直的 想要有一個只屬于自己的地方&#xff0c;或許可以說不只是一個地方&#xff0c;我想要的是一個叫作家的地方... 每每看到溫…

R 數據框的操作

1.插入一列 根據自帶數據集beaver 進行操作&#xff0c;比如插入一列id。 > colnames(beaver1) [1] "day" "time" "temp" "activ" > nrow(beaver1) [1] 114 方法1&#xff1a; new_beaver1$id rep(1,114)方法2 new_beaver1…

Docker 下載 JDK 鏡像(docker search 、docker pull)

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 我有一個dockerfile 中要引用 jdk。 運行腳本發現 居然沒有JDK 原始鏡像。早期是下載過的&#xff0c;不記得什么時候清掉了。 于是重新…

入夏多吃這些“殺菌菜”

天氣逐漸變熱&#xff0c;病原菌滋生快&#xff0c;肝炎、急性胃炎、急性腸炎、痢疾、霍亂等消化道疾病容易爆發。此時多吃“殺菌蔬菜”有殺滅和抑制細菌病毒的作用&#xff0c;有時甚至光靠這些殺菌菜就可以治療疾病。 專家建議&#xff0c;在炎熱的夏季為了保證胃腸道的健康&…

R 讀取excel的方法

1.加載 readxl 包&#xff0c;利用 reade_excel() 函數 install.packages("readxl") library(readxl) data read_excel("22_data.xlsx",sheet 1) read_excel函數的參數設置&#xff1a; 用法&#xff1a;read.xlsx(xlsxFile, sheet 1, startRow 1, co…