我的代码有什么问题?怎么就 TLE 了?玄关
查看原帖
我的代码有什么问题?怎么就 TLE 了?玄关
920406
违规用户名920406楼主2024/12/26 14:41
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+1;
int n,m,siz,tot,op,x,y,a[N],l[N],r[N],ans[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)   cin>>a[i];
    siz=sqrt(n);
    tot=(n+siz-1)/siz;
    for(int i=1;i<=tot;i++)
    {
        l[i]=(i-1)*siz+1;
        r[i]=min(n,i*siz);
        for(int j=l[i];j<=r[i];j++) ans[i]+=a[j];
    }
    cin>>m;
    while(m--)
    {
        cin>>op>>x>>y;
        if(op==0)
        {
            for(int i=1;i<=tot;i++)
            {
                if(x<=l[i]&&r[i]<=y&&ans[i]!=r[i]-l[i]+1)
                {
                    for(int j=l[i];j<=r[i];j++)
                    {
                        ans[i]-=a[j];
                        a[j]=sqrt(a[j]);
                        ans[i]+=a[j];
                    }
                }
                else for(int j=max(x,l[i]);j<=min(y,r[i]);j++)
                {
                    ans[i]-=a[j];
                    a[j]=sqrt(a[j]);
                    ans[i]+=a[j];
                }
            }
        }
        else
        {
            int an=0;
            for(int i=1;i<=tot;i++)
            {
                if(x<=l[i]&&r[i]<=y)    an+=ans[i];
                else for(int j=max(x,l[i]);j<=min(y,r[i]);j++)  an+=a[j];
            }
            cout<<an<<'\n';
        }
    }
    return 0;
}
2024/12/26 14:41
加载中...