AC题解
2023-08-25 14:43:15
发布于:广东
1阅读
0回复
0点赞
#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
using namespace std;
typedef long long ll;
template<class T>
il void rd(T &x){
char ch;x=0;bool fl=false;
while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*10+numb);
(fl==true)&&(x=-x);
}
template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');}
template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');}
namespace Solution{
const int N=1e5+5;
const int mod=1e9+7;
int n;
int u[N],d[N];
ll a[N],sum[N];
int h[N];
vector<int>dele[N];
int main(){
rd(n);
int x,y;
for(reg i=1;i<=n;++i){
rd(x);rd(y);
++x;++y;h[x]=y;
}
int mx=0;
for(reg i=n;i>=1;--i){
mx=max(mx,h[i]);
u[i]=mx;
}
int mi=n+233;
for(reg i=1;i<=n;++i){
mi=min(mi,h[i]);
d[i]=mi;dele[d[i]-1].push_back(i);
}
for(reg i=1;i<=n;++i){
++a[d[i]];--a[u[i]+1];
}
for(reg i=1;i<=n;++i){
a[i]+=a[i-1];
}
for(reg i=n;i>=1;--i){
sum[i]=sum[i+1]+a[i];
}
ll ans=0,tot=0;
ll len=0;
for(reg i=n;i>=1;--i){
for(reg j=0;j<(int)dele[i].size();++j){
int now=dele[i][j];
len+=u[now]-d[now]+1;
tot-=sum[i+1]-len;
}
ans=(ans+tot)%mod;
tot=tot+(a[i]-1)*a[i]/2;
}
cout<<ans;
return 0;
}
}
signed main(){
Solution::main(); // Leetcode做过,CV过来的框架,懒得改了
return 0;
}
这里空空如也
有帮助,赞一个