筱玛爱线段树 差分

筱玛爱线段树

思路:两次差分。倒着来,从后往前,对操作的次数进行差分,最后把操作一的累加次数进行差分。

垃圾题,我说怎么一直找不到错,明明写的对的,造的数据也过了,就因为模mod前没加mod?行吧,垃圾题。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
const int mod=1e9+7;
int c1[maxn];
int d1[maxn];
int c2[maxn];
int d2[maxn];
int op[maxn],lt[maxn],rt[maxn];
int op1[maxn];
int opc[maxn];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=m; i++)
        scanf("%d%d%d",&op[i],&lt[i],&rt[i]);
    int cnt1=0;
    for(int i=m; i>=1; i--)
    {
        d1[i]=(d1[i+1]+c1[i])%mod;
        opc[i]=(d1[i]+1)%mod;
        if(op[i]==2)
        {
            c1[rt[i]]=(c1[rt[i]]+opc[i])%mod;
            c1[lt[i]-1]=(c1[lt[i]-1]-opc[i]+mod)%mod;
        }
        else
            op1[++cnt1]=i;

    }
    for(int i=1; i<=cnt1; i++)
    {
        int op=op1[i];
        c2[lt[op]]=(c2[lt[op]]+opc[op])%mod;
        c2[rt[op]+1]=(c2[rt[op]+1]-opc[op]+mod)%mod;
    }
    for(int i=1; i<=n; i++)
    {
        d2[i]=(d2[i-1]+c2[i])%mod;
        if(i!=n) printf("%d ",d2[i]);
        else printf("%d",d2[i]);
    }
}

猜你喜欢

转载自www.cnblogs.com/dongdong25800/p/11143003.html