两次差分.第一次统计下某个操作要操作多少次,第二次差分就是直接统计答案了.注意要从后往前算.
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+5; const int mod=1e9+7; ll op[N],l[N],r[N],ans[N],b[N]; int main() { int n,m; cin>>n>>m; for(int i=1;i<=m;i++) { scanf("%lld%lld%lld",&op[i],&l[i],&r[i]); } for(int i=m;i>=1;i--)//从后往前统计就无后效性. { b[i]=(b[i]+b[i+1]+mod)%mod; if(op[i]==1) { b[i]++; b[i-1]--; ans[l[i]]=(ans[l[i]]+b[i]+mod)%mod; ans[r[i]+1]=(ans[r[i]+1]-(b[i])+mod)%mod; } else { b[l[i]-1]=(b[l[i]-1]-(b[i]+1)+mod)%mod; b[r[i]]=(b[r[i]]+b[i]+1+mod)%mod; } } for(int i=1;i<=n;i++) { ans[i]=(ans[i]+ans[i-1]+mod)%mod; cout<<ans[i]<<' '; }puts(""); return 0; }