跳到正文

个人记录

题解:AT_abc462_d [ABC462D] Accomplice

yiming1123约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年6月15日
收录
2026年6月15日
更新
2026年11月9日
篇幅
约 2 分钟

版本

  1. v1当前

思路

考虑差分、枚举结束点,看该区间内有多少个人然后算贡献并加入。但显然若我们直接用原来的 sis_i 和 tit_i 是没办法分辨一个人是否能持续 DD 个时间的,所以在读入时将末尾的一段时间直接不要,等于将 tit_i 往前移。

Code

C++
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
#define lint long long
#define line inline

namespace OIfast{
	#define endl putchar('\n')
	
	line lint read(){
		lint x=0;int f=1;char c=getchar();
		while(c<'0' or c>'9'){if(c=='-')f=-1;c=getchar();}
		while(c>='0' and c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
		return x*f;
	}
	
	line void wte(lint x){
		static int sta[40],top=0;
		if(x<0) putchar('-'),x=-x;
		do{sta[++top]=x%10;x/=10;}while(x);
		while(top) putchar(sta[top--]+48);
		return putchar(' '),void();
	}
}using namespace OIfast;

const int N=2e5+5,M=1e6+5;
int n,D,s[N],t[N];
lint ans,a[M];

line lint calc(lint x){
	return x*(x-1)/2;
}

int main(){
	n=read(),D=read();
	int m=0;
	for(int i=1;i<=n;++i){
		s[i]=read(),t[i]=read()-D;
		m=max(m,t[i]);
		if(s[i]>t[i]) continue;
		a[s[i]]+=1,a[t[i]+1]-=1;
	}
	for(int i=1;i<=m;++i){
		a[i]+=a[i-1];
		ans+=calc(a[i]);
	}
	wte(ans),endl;
	return 0;
}
原文