While practicing for The 2019 ICPC Asia Jakarta Regional Contest, Budi stumbled upon an interesting problem on data structure topic. Unfortunately, he misread the problem, but he argues that the problem he thinks of is much more interesting than the original one, thus, this problem.
Let function f(L, R) on an array of integers A 1..N be defined as incrementing every element in the subarray A i,j each by 1 for all L ≤ i ≤ j ≤ R. In other words, function f(L, R) can be written as follows (in pseudocode).
Given an array A of N elements (initially A i = 0 for all i = 1..N), your task is to perform Q queries on A of the following types.
• 1 L R — perform f(L, R) on A.
• 2 L R — output the sum of all A i where L ≤ i ≤ R.
输入
Input begins with a line containing two integers: N Q (1 ≤ N, Q ≤ 100 000) representing the size of A and the number of queries, respectively. The next Q lines each contains a query of the following types.
• 1 L R (1 ≤ L ≤ R ≤ N)
• 2 L R (1 ≤ L ≤ R ≤ N)
There is at least one query of the second type.
输出
For each query of the second type in the same order as input, output in a line an integer representing the sum of all A i where L ≤ i ≤ R. As this output can be large, you need to modulo the output by 1 000 000 007.
样例输入 Copy
9 7 1 2 5 1 4 9 2 2 7 1 3 3 2 2 7 1 1 5 2 1 9
样例输出 Copy
60 61 112
提示
暑假遗留问题属于是,没地方交了,但是样例确实过了
- #include<bits/stdc++.h>
- using namespace std;
- #define int long long
- const int N=5e5+10;
- const int mod=1e9+7;
- int n,m;
- struct node
- {
- int sum0,sum1,sum2,lz0,lz1,lz2;
- } t[N*4];
- void pushdown(int i,int l,int r)
- {
- if(t[i].lz0)
- {
- int k=t[i].lz0;
- int mid=(l+r)>>1;
- t[i<<1].sum0+=k*(mid-l+1)%mod;
- t[i<<1].sum0%=mod;
- t[i<<1|1].sum0+=k*(r-mid)%mod;
- t[i<<1|1].sum0%=mod;
- t[i<<1].lz0+=k;
- t[i<<1].lz0%=mod;
- t[i<<1|1].lz0+=k;
- t[i<<1|1].lz0%=mod;
- t[i].lz0=0;
- }
- if(t[i].lz1)
- {
- int k=t[i].lz1;
- int mid=(l+r)>>1;
- t[i<<1].sum1+=k*((mid+l)*(mid-l+1)/2%mod)%mod;
- t[i<<1].sum1%=mod;
- t[i<<1|1].sum1+=k*((r+mid+1)*(r-mid)/2%mod)%mod;
- t[i<<1|1].sum1%=mod;
- t[i<<1].lz1+=k;
- t[i<<1].lz1%=mod;
- t[i<<1|1].lz1+=k;
- t[i<<1|1].lz1%=mod;
- t[i].lz1=0;
- }
- if(t[i].lz2)
- {
- int k=t[i].lz2;
- int mid=(l+r)>>1;
- t[i<<1].sum2+=k*((mid*(mid+1)/2*(2*mid+1)/3%mod)-((l-1)*((l-1)+1)/2*(2*(l-1)+1)/3%mod)+mod)%mod%mod;
- t[i<<1].sum2%=mod;
- t[i<<1|1].sum2+=k*((r*(r+1)/2*(2*r+1)/3%mod)-((mid+1-1)*((mid+1-1)+1)/2*(2*(mid+1-1)+1)/3%mod)+mod)%mod%mod;
- t[i<<1|1].sum2%=mod;
- t[i<<1].lz2+=k;
- t[i<<1].lz2%=mod;
- t[i<<1|1].lz2+=k;
- t[i<<1|1].lz2%=mod;
- t[i].lz2=0;
- }
- }
- void build(int i,int l,int r)
- {
- t[i].lz0=t[i].lz1=t[i].lz2=0;
- if(l==r)
- {
- t[i].sum0=t[i].sum1=t[i].sum2=0;
- return ;
- }
- int mid=(l+r)>>1;
- build(i<<1,l,mid);
- build(i<<1|1,mid+1,r);
- //pushup(rt);
- }
- void update0(int rt,int l,int r,int L,int R,int k)
- {
- if(L<=l&&r<=R)
- {
- t[rt].sum0+=k*(r-l+1)%mod;
- t[rt].sum0%=mod;
- t[rt].lz0+=k;
- t[rt].lz0%=mod;
- return ;
- }
- pushdown(rt,l,r);
- int mid=(l+r)>>1;
- if(L<=mid) update0(rt<<1,l,mid,L,R,k);
- if(R>mid)update0(rt<<1|1,mid+1,r,L,R,k);
- t[rt].sum0=(t[rt<<1].sum0+t[rt<<1|1].sum0)%mod;
- return;
- }
- void update1(int rt,int l,int r,int L,int R,int k)
- {
- if(L<=l&&r<=R)
- {
- t[rt].sum1+=k*((r+l)*(r-l+1)/2%mod)%mod;
- t[rt].sum1%=mod;
- t[rt].lz1+=k;
- t[rt].lz1%=mod;
- return;
- }
- pushdown(rt,l,r);
- int mid=(l+r)>>1;
- if(L<=mid) update1(rt<<1,l,mid,L,R,k);
- if(R>mid)update1(rt<<1|1,mid+1,r,L,R,k);
- t[rt].sum1=(t[rt<<1].sum1+t[rt<<1|1].sum1)%mod;
- return;
- }
- void update2(int rt,int l,int r,int L,int R,int k)
- {
- if(L<=l&&r<=R)
- {
- t[rt].sum2+=k*((r*(r+1)/2*(2*r+1)/3%mod)-((l-1)*((l-1)+1)/2*(2*(l-1)+1)/3%mod)+mod)%mod%mod;
- t[rt].sum2%=mod;
- t[rt].lz2+=k;
- t[rt].lz2%=mod;
- return;
-
- }
- pushdown(rt,l,r);
- int mid=(l+r)>>1;
- if(L<=mid) update2(rt<<1,l,mid,L,R,k);
- if(R>mid)update2(rt<<1|1,mid+1,r,L,R,k);
- t[rt].sum2=(t[rt<<1].sum2+t[rt<<1|1].sum2)%mod;
- return;
- }
- int query0(int rt,int l,int r,int L,int R)
- {
- if(L<=l&&R>=r)
- {
- return t[rt].sum0;
- }
- pushdown(rt,l,r);
- int mid=(l+r)>>1;
- int ans=0;
- if(mid >= R) ans=ans+query0(rt<<1, l, mid, L, R),ans%=mod;
- else if(mid < L) ans=ans+query0(rt<<1|1, mid + 1, r, L, R),ans%=mod;
- else
- {
- ans=ans+query0(rt<<1, l, mid, L, mid)+query0(rt<<1|1, mid + 1, r, mid + 1, R),ans%=mod;
- }
- return ans;
- }
- int query1(int rt,int l,int r,int L,int R)
- {
- if(L<=l&&R>=r)
- {
- return t[rt].sum1;
- }
- pushdown(rt,l,r);
- int mid=(l+r)>>1;
- int ans=0;
- if(mid >= R) ans=ans+query1(rt<<1, l, mid, L, R),ans%=mod;
- else if(mid < L) ans=ans+query1(rt<<1|1, mid + 1, r, L, R),ans%=mod;
- else
- {
- ans=ans+query1(rt<<1, l, mid, L, mid)+query1(rt<<1|1, mid + 1, r, mid + 1, R),ans%=mod;
- }
- return ans;
- }
- int query2(int rt,int l,int r,int L,int R)
- {
- if(L<=l&&R>=r)
- {
- return t[rt].sum2;
- }
- pushdown(rt,l,r);
- int mid=(l+r)>>1;
- int ans=0;
- if(mid >= R) ans=ans+query2(rt<<1, l, mid, L, R),ans%=mod;
- else if(mid < L) ans=ans+query2(rt<<1|1, mid + 1, r, L, R),ans%=mod;
- else
- {
- ans=ans+query2(rt<<1, l, mid, L, mid)+query2(rt<<1|1, mid + 1, r, mid + 1, R),ans%=mod;
- }
- return ans;
- }
- signed main()
- {
-
- cin>>n>>m;
- build(1,1,n);
- for(int i=1; i<=m; i++)
- {
- int op;
- cin>>op;
- if(op==1)
- {
- int l,r;
- cin>>l>>r;
- int len=r-l+1;
- update0(1,1,n,l,r,((-(l-1)*(l-1)-(l-1)*(len+1))%mod+mod)%mod);
- update1(1,1,n,l,r,((2*l+len-1)%mod+mod)%mod);
- update2(1,1,n,l,r,-1);
- }
- else
- {
- int l,r;
- cin>>l>>r;
- cout<<(query0(1,1,n,l,r)%mod+query1(1,1,n,l,r)%mod+query2(1,1,n,l,r)%mod)%mod<<"\n";
- }
- }
- return 0;
- }