题面。
看了一下,好像还没有整除分块的做法。
感觉这题就是整除分块板子啊。
整除分块可以连续统计 S(近似)分解的值域,而如果 S 的可以被分解为 x×y 那么它一定能被整除分块统计过一次。
所以在整除分块的过程中去掉近似的情况就能留下所有满足 xy=S 的 (x,y) 啦。
1 2 3 4 5 6 7 8
| for(ll l=1,r=0;l<=s;l=r+1) { r=(s/(s/l)); if(s/l*r<s) continue; x[++n]=r; h[n]=-s/l; sumh[n]=sumh[n-1]+h[n]*(x[n]-x[n-1]); }
|
考虑采用纵向切分图形。
但是这题一个样例下面还有多组询问,我们对同一个询问的 S 要考虑预处理,使用前缀和的形式构成上面的 sumh。
于是问题转化为了对于确定的高值 top 和右值 rit 找到其左右下标,然后将图形分为左右两块。靠左的部分应该是被限高而构成了一个长方形,如图绿色区域;靠右的部分应该是前缀和的一部分,如图红色区域。

最恶心的地方就是一些细节处理,比如当查询矩形完全被 S 构成的区域包裹的时候,左右两块就要分类出来处理了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <iostream> #include <cstdio> #include <algorithm> #define ll long long using namespace std; template<class T> inline void read(T &x) { char c=getchar();x=0; while(!isdigit(c)) c=getchar(); while(isdigit(c)) x=x*10+c-48,c=getchar(); } int t,q,n; ll s,x[10000005],h[10000005],sumh[10000005],ans,rit,top,lft; int main() { read(t); while(t--) { read(s);read(q);n=0; for(ll l=1,r=0;l<=s;l=r+1) { r=(s/(s/l)); if(s/l*r<s) continue; x[++n]=r; h[n]=-s/l; sumh[n]=sumh[n-1]+h[n]*(x[n]-x[n-1]); } x[n+1]=h[n+1]=0; while(q--) { read(rit);read(top); ans=rit; rit=upper_bound(x+1,x+n+1,rit)-x-1; ans=(x[rit]-ans)*max(h[rit+1],-top); lft=lower_bound(h+1,h+n+1,-top)-h; ans+=x[min(lft-1,rit)]*top+max(0ll,-sumh[rit]+sumh[lft-1]); printf("%lld\n",ans); } } return 0; }
|
整除分块时间复杂度 O(S),单组数据总时间复杂度 O(S+qlogd(S)),可以通过此题。