QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#378057#7992. 【模板】线段树gogo11TL 0ms3620kbC++173.1kb2024-04-05 23:41:292024-04-05 23:41:29

Judging History

你现在查看的是最新测评结果

  • [2024-04-05 23:41:29]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3620kb
  • [2024-04-05 23:41:29]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ui = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define lowbit(x) ((x) & (-x))
constexpr int N = 2e5 + 10;
constexpr int M = 5e5 + 10;
constexpr ll mod = (1LL<<20);
constexpr int INF = 0x3f3f3f3f;
constexpr ll Base = 131;
constexpr int MAXBIT = 19;
ui ksm(ui r1, ui r2)
{
    ui res=1;
    while(r2)
    {
        if(r2&1)
            res*=r1;
        r2>>=1;
        r1*=r1;
    }
    return res;
}
ui a[N];
ui C[N][20];
struct Node
{
    int l,r;
    ui lz;
    ui p[25];
}tr[N<<2];
void pushup(Node &u,Node &ls,Node &rs)
{
    for(int i=0;i<=19;i++)
        u.p[i]=0;
    for(int i=0;i<=19;i++)
        for(int j=0;j<=19-i;j++)
            u.p[i+j]+=ls.p[i]*rs.p[j];
}
void pushup(int p){pushup(tr[p],tr[p*2],tr[p*2+1]);}
void build(int p,int l,int r)
{
    tr[p]={l,r,0};
    if(l==r)
    {
        tr[p].p[0]=1;
        tr[p].p[1]=a[l];
        // tr[p].p[0]=a[l];
        // tr[p].p[1]=1;
        return;
    }
    int mid=(l+r)>>1;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    pushup(p);
}
void pushdown(Node &u,ui x)
{
    for(int i=19;i>=1;i--)
        for(int j=0;j<=i-1;j++)
            u.p[i]+=u.p[j]*ksm(x,i-j)*C[u.r-u.l+1-j][i-j];
	u.lz+=x;  
}
void pushdown(Node &u,Node &ls,Node &rs)
{
    if(u.lz)
    {
        pushdown(ls,u.lz),pushdown(rs,u.lz);
        u.lz=0;
    }
}
void pushdown(int p){pushdown(tr[p],tr[p*2],tr[p*2+1]);}
void update(int p,int x,int y,int z)
{
    if(x<=tr[p].l && tr[p].r<=y)
    {
        pushdown(tr[p],z);
        return;
    }
    pushdown(p);
    int mid=(tr[p].l+tr[p].r)>>1;
    if(x<=mid)
        update(p*2,x,y,z);
    if(y>mid)
        update(p*2+1,x,y,z);
    pushup(p);
}
Node query(int p,int x,int y)
{
    if(x<=tr[p].l && tr[p].r<=y)
        return tr[p];
    pushdown(p);
    int mid=(tr[p].l+tr[p].r)>>1;
    Node r1,r2,r3;
    if(y<=mid)
        return query(p*2,x,y);
    if(x>mid)
        return query(p*2+1,x,y);
    r1=query(p*2,x,y),r2=query(p*2+1,x,y);
    pushup(r3,r1,r2);
    return r3;
}
void solve()
{   
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        a[i]-=1;
    }
    C[0][0]=1;
	for(int i=1;i<=n;i++)
	{
		C[i][0]=1;
        for(int j=1;j<=19;j++)
            C[i][j]=C[i-1][j]+C[i-1][j-1];
	}
    build(1,1,n);
    for(int i=1;i<=q;i++)
    {
        int f,l,r,x;
        cin>>f>>l>>r;
        if(f==1)
        {   
            cin>>x;
            if(x==0)
                continue;
            update(1,l,r,x);
        }
        else
        {
            Node res=query(1,l,r);
			ui ans=0;
            for(int i=0;i<=19;i++)
                ans+=res.p[i];
			ans&=(1<<20)-1;
			cout<<ans<<'\n';
        }
    }
}
int main()
{
    std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    // #ifndef ONLINE_JUDGE
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    // #endif
    int _ = 1;
    // cin >> _;
    while (_--)
        solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3620kb

input:

10 10
969575 741825 24903 1047319 450475 256145 1045323 479255 810659 768323
1 5 6 3034
2 1 10
2 1 9
2 1 4
1 3 6 126904
2 5 5
2 9 9
1 7 7 853094
1 4 9 1025178
2 5 8

output:

1045541
1012343
558151
580413
810659
527353

result:

ok 6 lines

Test #2:

score: -100
Time Limit Exceeded

input:

200000 200000
496015 180543 330721 874799 740427 144379 598057 795949 323465 87657 683935 748203 748665 288301 846003 33033 746029 132621 876629 361899 701297 373189 256151 723161 377571 54947 91151 855991 433965 73347 155081 314317 790527 705555 1035217 298963 604641 203865 230029 802437 720769 843...

output:

746709
564663
426791
840425
762201
413693
881143
534387
189149
257625
60619
958793
250635
869079
383765
151047
272239
146175
46215
914259
617511
698623
381177
932779
792705
785375
1044293
202971
508317
237901
634919
646839
38501
304017
889609
214899
617927
720071
628729
202369
420511
528565
555717
7...

result: