Atcoder regular Contest 073(C - Sentou)
传送门
每个人对开关的影响区间为a[i]--a[i]+t,因此此题即为将所有区间离散化后求所有独立区间的长度和
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=2e5+10;
pair<ll,ll> B[N*2];
int main()
{
// while(true)
{
int n=read(),t=read();
int top=0;
ll x;
for(int i=0;i<n;i++)
{
x=read();
B[top].first=x;
B[top].second=-1;
top++;
// cout<<x+t<<endl;
B[top].first=x+t;
B[top].second=1;
top++;
}
sort(B,B+top);
int count=0;
ll pre=0,ans=0;
bool first=1;
for(int i=0;i<top;i++)
{
if(B[i].second==1){
count++;
if(count==0){
ans+=B[i].first-pre;
}
}
if(B[i].second==-1){
count--;
if(count==-1){
pre=B[i].first;
}
}
}
printf("%I64d
",ans);
}
return 0;
}