51Nod1962 區間計數

2021-08-08 10:50:27 字數 1207 閱讀 4296

兩個數列 , ,請求出ans, ans定義如下:

ans:=σni=1σnj=i[max=max]

注:[ ]內表示式為真,則為1,否則為0.

1≤n≤3.5×1051≤ai,bi≤n

樣例解釋:

7個區間分別為:(1,4),(1,5),(2,4),(2,5),(3,3),(3,5),(4,5)

input

第一行乙個整數n

第二行n個整數ai

第三行n個整數bi

output

一行,乙個整數ans

input示例

5 1 4 2 3 4

3 2 2 4 1

output示例

7題解

維護兩個單調棧,當乙個數出棧時,以他為最大值的區間就確定了,然後去另乙個棧中找到這個值,計算重疊區間即可。

**

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define mod 1000000007

#define ll long long

#define n 350005

#define inf 0x7fffffff

using namespace std;

inline int

read()

while(ch>='0'&&ch<='9')

return

x*f;

}int n,a[n][2],q[n][2],r[n],pos[n][2];

ll ans;

ll calc(int p,int

x,int l,int r,int j)

int main()

q[++r[j]][j]=i;

pos[a[i][j]][j]=r[j];

}for(int i=1;i<=r[0];i++)ans+=calc(q[i][0],a[q[i][0]][0],q[i-1][0]+1,n,1);

printf("%lld",ans);

return

0;}

51Nod1962 區間計數

這題與之前那道區間最值的題非常類似,依舊是二分區間,然後統計跨過中間點的區間貢獻。我們要選出小於等於和小於的,這樣就可以算出相等的區間長了。複雜度o nlogn by 大奕哥 1 include2 using namespace std 3 typedef long long ll 4 ll ans...

51nod1962區間計數

考慮每個值作為最大值的區間實際上可以用單調棧求出來,即找到左邊第乙個比它大的數l i 右邊第乙個比它大的r i 那就是左端點在 l i i 右端點在 i,r i 的區間是以第i個數作為最大值。用乙個掃瞄線去做,維護當前線上矩形交的長度now。具體是這樣的 開2n個線段樹,對於每一種最大值開線段樹,分...

51nod 1712 區間求和

解法 這個題首先考慮乙個簡單情況 對於區間 x,y 權值為多少。容易寫出公式 f x y s y s x 1 sum y sum x 1 x 1 sum x 1 y x 1 其中s x 表示 從第乙個元素到第x個元素的 所有有序二元組的和 題目中定義的 sum表示字首和 這裡要求的是所有a x a ...