luogu P5444 [APIO2019]奇怪装置
题面传送门
看上去统计\((x,y)\)这样的二元组一脸不可做,可以改成统计\((x-y,y)\)这样的二元组。
\(x-y=(t+\lfloor \frac{t}{B}\rfloor-t\bmod B)\bmod A=(\lfloor \frac{t}{B}\rfloor\times (B+1))\bmod A\)
也就是说,对于相同的\(y\),\(x\)会在\(\frac{A}{\gcd(B+1,A)}\)循环。
所以可以将每个线段拆开,然后跑线段覆盖即可。时间复杂度瓶颈在于排序\(O(n\log n)\)
code:
#include<bits/stdc++.h>
#define I inline
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define ll long long
#define db double
#define lb long db
#define N (2000000+5)
#define M ((1<<16)+5)
#define K (1000+5)
#define mod 998244353
#define Mod (mod-1)
#define eps (1e-9)
#define ull unsigned ll
#define it iterator
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) (n*(x-1)+(y))
#define R(n) (rand()*rand()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define PB push_back
using namespace std;
int n,H;ll Ans,Mx=-1,A,B,x,y,z;
struct Ques{ll l,r;}S[N];I bool cmp(Ques x,Ques y){return x.l<y.l;}
int main(){
freopen("1.in","r",stdin);
int i,j;scanf("%d%lld%lld",&n,&A,&B);z=A/__gcd(A,B+1);for(i=1;i<=n;i++){
scanf("%lld%lld",&x,&y);if(x/B/z+1<y/B/z) {printf("%lld\n",B*z);return 0;}if(x/B/z+1==y/B/z) S[++H]=(Ques){x%(B*z),B*z-1},S[++H]=(Ques){0,y%(B*z)};
else if(x/B/z) S[++H]=(Ques){x%(B*z),y%(B*z)};else S[++H]=(Ques){x,y};
} sort(S+1,S+H+1,cmp);for(i=1;i<=H;i++) S[i].r>Mx&&(Ans+=S[i].r-max(S[i].l-1,Mx),Mx=S[i].r);printf("%lld\n",Ans);
};