跳到正文

题解

题解:CF2179H Blackslex and Plants

LJQGO约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年7月20日
收录
2026年7月28日
更新
2026年11月17日
篇幅
约 6 分钟
题目
CF2179H

版本

  1. v2当前
  2. v1

考虑寻找 f(i)⁡=i×\operatorname{f(i)}=i\times lowbit(i) 的规律,以操作 (1,5)(1,5) 为例:

f(1)⁡=1×1\operatorname{f(1)}=1\times 1

f(2)⁡=2×2\operatorname{f(2)}=2\times 2

f(3)⁡=3×1\operatorname{f(3)}=3\times 1

f(4)⁡=4×2\operatorname{f(4)}=4\times 2

f(5)⁡=5×1\operatorname{f(5)}=5\times 1

我们发现,对于lowbit为 2k2^k 的一次修改,修改的点下标之间的差值为 2k+12^{k+1},修改的值除以 2k2^k 之后之间的差值也为 2k+12^{k+1},那么可以想到适当修改差分的定义:对于lowbit为 2k2^k 的查分数组,a[i]=a[i]+a[i−2k+1]a[i]=a[i]+a[i-2^{k+1}]。再二阶差分消除公差即可。

代码如下:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long 
int t;
const int N=2e5+25;
int n,q;
int p2[25];
int d[25][N];
// int d1[18][N];
int d2[25][N];
int a[N];
void init()
{
    p2[0]=1;
    for(int i=1;i<=24;i++)
    {
        p2[i]=p2[i-1]*2;
    }
    return;
}
void chafen(int L,int R)
{
//     cout<<L<<" "<<R<<endl;
    int l=R-L+1;
    for(int i=0;(1<<i)<=(l);i++)
    {
//    	 cout<<i<<endl;
        //L+p2[i+1]*((R-L)/p2[i+1])
//        cout<<"first:"<<L+p2[i]-1<<endl;
//        cout<<"firstx:"<<pow(p2[i],2)<<endl;
        d2[i][L+p2[i]-1]+=pow(p2[i],2);
//        cout<<"a"<<endl;
        if(L+p2[i]-1+p2[i+1]<=n)
        {
            d2[i][L+p2[i]-1+p2[i+1]]+=p2[i]*p2[i];
        }
        int lstp=(L+p2[i]-1)+(R-(L+p2[i]-1))/p2[i+1]*p2[i+1];
        //         初项                    项数->末位置
        // cout<<"b"<<endl;
        int lstx=pow(p2[i],2)+(R-(L+p2[i]-1))/p2[i+1]*p2[i+1]*p2[i];
//        cout<<"last:"<<lstp<<"\nlastx:"<<lstx<<endl;        
		if(lstp+p2[i+1]<=n)
        {
            d2[i][lstp+p2[i+1]]-=lstx+p2[i+1]*p2[i];
        }
        // cout<<"c"<<endl;
        if(lstp+p2[i+2]<=n)
        {
            d2[i][lstp+p2[i+2]]+=lstx;
        }
    }                                       
    return;
}
void solve()
{
    cin>>n>>q;
//    cout<<"X"<<endl;
    for(int i=1;i<=n;i++)
    {
        a[i]=0;
        for(int j=0;j<=17;j++)
        {
            d[j][i]=0;
            d2[j][i]=0;
        }
    }
    for(int i=1;i<=q;i++)
    {
        int l,r;
        cin>>l>>r;
        // cout<<"S"<<endl;
        chafen(l,r);
    }
    // cout<<"A"<<endl;
//    for(int i=0;i<=2;i++)
//    {
//    	for(int j=1;j<=n;j++)
//    	{
//    		cout<<d2[i][j]<<" ";
//		}
//		cout<<endl;
//	}
    for(int i=0;(1<<i)<=n;i++)
    {
//    	cout<<"i:"<<i<<endl;
    	for(int k=1;k<=p2[i+1];k++)
    	{
	    	for(int j=k;j<=n;j+=p2[i+1])
	        {
//	        	cout<<"j:"<<j<<endl;
//				cout<<d2[i][j]<<" "<<d2[i][max(0LL,j-p2[i+1])]<<endl;
	            d2[i][j]+=d2[i][max(0LL,j-p2[i+1])];
	            d[i][j]=d[i][max(0LL,j-p2[i+1])]+d2[i][j];
	//            cout<<d2[i][j]<<" "<<d[i][j]<<endl;
	            a[j]+=d[i][j];
	        }	
		}
    }    
    for(int i=1;i<=n;i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
}
signed main()
{
    cin>>t;
    init();
    for(int i=1;i<=t;i++)                  
    {
        solve();
    }
	return 0;
}
原文