# 牛客練習賽66 E、騷區間

``````//p[i][j]表示p[i][j]爲左端點可行右端點的區間的端點，如果爲正，表示剛加入進來，爲負就消除。
for(auto it:p[i]){
if(it>0)update(it,1);
else update(-it,-1);
}
``````

``````#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int man = 1e6+10;
#define IOS ios::sync_with_stdio(0)
#define ull unsigned ll
#define uint unsigned
#define pai pair<int,int>
#define pal pair<ll,ll>
#define IT iterator
#define pb push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);++i)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);--i)
#define endl '\n'
#define ll long long
const ll mod = 1e9+7;
int a[man];
int c[man],n;
#define lowbit(x) x&(-x)
void update(int pos,int va){
for(int i = pos;i <= n;i += lowbit(i)){
c[i] += va;
}
}

int query(int pos){
int ans = 1;
for(int i = pos;i >= 1;i -= lowbit(i)){
ans += c[i];
}
return ans;
}

int minn[man<<2],maxx[man<<2];

void build(int l,int r,int rt){
minn[rt] = 1e9;
maxx[rt] = 0;
if(l==r){
minn[rt] = maxx[rt] = a[l];
return;
}
int m = l +r >>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
maxx[rt] = max(maxx[rt<<1],maxx[rt<<1|1]);
minn[rt] = min(minn[rt<<1],minn[rt<<1|1]);
}

void query_min(int l,int r,int L,int R,int rt,int va,int &ans){
if(r<L||l>R||r<l)return;
if(minn[rt]>=va)return;
if(l==r){
ans = l;
return;
}
int m = l + r >>1;
if(minn[rt<<1]<va)query_min(l,m,L,R,rt<<1,va,ans);
if(ans==n+1)query_min(m+1,r,L,R,rt<<1|1,va,ans);
}

void query_max(int l,int r,int L,int R,int rt,int va,int &ans){
if(r<L||l>R||r<l)return;
if(maxx[rt]<=va)return;
if(l==r){
ans = l;
return;
}
int m = l + r >>1;
if(maxx[rt<<1|1]>va)query_max(m+1,r,L,R,rt<<1|1,va,ans);
if(ans==0)query_max(l,m,L,R,rt<<1,va,ans);
}
int vis[man];
vector<int>p[man];

signed main() {
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
scanf("%d",&n);
for(int i = 1;i <= n;i++){
scanf("%d",a+i);
}
build(1,n,1);
ll ans = 0;
for(int i = 1;i <= n;i++){
//if(i>1)continue;
int l = n+1,r = n+1;
query_min(1,n,i+1,n,1,a[i],l);
query_min(1,n,l+1,n,1,a[i],r);
if(l!=n+1)p[l].emplace_back(i);
if(r!=n+1)p[r].emplace_back(-i);
// cout << l <<" " <<r <<endl;
for(auto it:p[i]){
if(it>0)update(it,1);
else{
it *= -1;
update(it,-1);
}
}
l = r = 0;
query_max(1,n,1,i-1,1,a[i],r);
query_max(1,n,1,r-1,1,a[i],l);
ans += query(r) - query(l);
}
printf("%lld\n",ans);
return 0;
}
``````

set寫法

``````#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int man = 1e6+10;
#define IOS ios::sync_with_stdio(0)
#define ull unsigned ll
#define uint unsigned
#define pai pair<int,int>
#define pal pair<ll,ll>
#define IT iterator
#define pb push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);++i)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);--i)
#define endl '\n'
#define ll long long
const ll mod = 1e9+7;
int a[man];
int c[man],n;
#define lowbit(x) x&(-x)
void update(int pos,int va){
for(int i = pos;i <= n;i += lowbit(i)){
c[i] += va;
}
}

int query(int pos){
int ans = 1;
for(int i = pos;i >= 1;i -= lowbit(i)){
ans += c[i];
}
return ans;
}
int pos[man],l[man],r[man];
vector<int>p[man];

signed main() {
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
scanf("%d",&n);
for(int i = 1;i <= n;i++){
scanf("%d",a+i);
pos[a[i]] = i;
}
ll ans = 0;
set<int>s;
s.clear();
s.insert(n+1);
s.insert(n+2);
for(int i = 1;i <= n;i++){
auto it = s.upper_bound(pos[i]);
p[*it].emplace_back(pos[i]);
it++;
p[*it].emplace_back(-pos[i]);
s.insert(pos[i]);
}
s.clear();s.insert(0),s.insert(-1),s.insert(n+1);
for(int i = n;i >= 1;i--){
auto it = s.upper_bound(pos[i]);
it--;
r[pos[i]] = *it;
it--;
l[pos[i]] = *it;
s.insert(pos[i]);
}
for(int i = 1;i <= n;i++){
for(auto it:p[i]){
if(it>0)update(it,1);
else update(-it,-1);
}
//cout << l[i] << " " <<r[i] << endl;
ans += query(r[i]) - query(l[i]);
}
printf("%lld\n",ans);
return 0;
}
``````