AC题解,代码太多,有些注释懒得写了
2023-07-16 14:53:58
发布于:广东
17阅读
0回复
0点赞
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define MAXN 200005
#define LL long long
#define ULL unsigned long long
#define ENDL putchar('\n')
#define DB double
#define lowbit(x) (-(x) & (x)) // 求负数的补码
#define FI first
#define SE second
#define PR pair<int,int>
#define UIN unsigned int
int xchar() {
static const int maxn = 1000000;
static char b[maxn];
static int pos = 0,len = 0;
if(pos == len) pos = 0,len = fread(b,1,maxn,stdin);
if(pos == len) return -1;
return b[pos ++];
}
// #define getchar() xchar()
inline LL read() {
LL f = 1,x = 0;int s = getchar();
while(s < '0' || s > '9') {if(s<0)return -1;if(s=='-')f=-f;s = getchar();}
while(s >= '0' && s <= '9') {x = (x<<1) + (x<<3) + (s^48);s = getchar();}
return f*x;
}
void putpos(LL x) {if(!x)return ;putpos(x/10);putchar((x%10)^48);}
inline void putnum(LL x) {
if(!x) {putchar('0');return ;}
if(x<0) putchar('-'),x = -x;
return putpos(x);
}
inline void AIput(LL x,int c) {putnum(x);putchar(c);}
const int MOD = 1000000007;
int n,m,s,o,k;
inline int& MD(int &x) {return x>=MOD ? x-=MOD:x;}
int a[MAXN];
void addc(int *c,int x,int y) {while(x<=m)MD(c[x]+=y),x+=lowbit(x);} // 在数组c的第x个位置加上y
void addc2(int *c,int x,int y) {while(x>0)MD(c[x]+=y),x &= (x-1);} // 在数组c的第x个位置加上y,通过位运算实现
int sum(int *c,int x) {int s=0;while(x>0)MD(s+=c[x]),x&=(x-1);return s;} // 求前缀和
int suf(int *c,int x) {int s=0;while(x<=m)MD(s+=c[x]),x+=lowbit(x);return s;} // 求后缀和
int as[MAXN],ql[MAXN],qr[MAXN];
vector<int> bu[MAXN<<2];
// 将询问添加到线段树的每个节点中
void addtree(int a,int l,int r,int al,int ar,int i) {
if(al > r || ar < l || l > r) return ;
int md = (al + ar) >> 1;
if((l <= md && r > md) || al == ar) {bu[a].push_back(i);return ;}
addtree(a<<1,l,r,al,md,i); addtree(a<<1|1,l,r,md+1,ar,i);
return ;
}
int dp[50005][23];
void solve(int x,int l,int r) {
if(l == r) {
for(auto &i:bu[x]) {
as[i] = 2;
} return ;
}
int md = (l + r) >> 1;
int dpp[23][23] = {};
for(int i = md;i >= l;i --) {
for(int j = 1;j <= m;j ++) {
int nm = suf(dpp[j],a[i]) + (j==a[i]);
addc2(dpp[j],a[i],nm);
}
dp[i][0] = 1;
for(int j = 1;j <= m;j ++) {
dp[i][j] = suf(dpp[j],1);
}
}
memset(dpp,0,sizeof(dpp)); addc(dpp[0],1,1);
for(int i = 1;i <= m;i ++) addc(dpp[i],i,1);
for(int i = md+1;i <= r;i ++) {
for(int j = 0;j <= m;j ++) {
int nm = sum(dpp[j],a[i]);
addc(dpp[j],a[i],nm);
}
for(int j = 0;j <= m;j ++) {
dp[i][j] = sum(dpp[j],m);
}
}
for(auto i:bu[x]) {
as[i] = 0;
for(int j = 0;j <= m;j ++) {
MD(as[i] += dp[ql[i]][j]*1ll*dp[qr[i]][j]%MOD);
}
}
solve(x<<1,l,md); solve(x<<1|1,md+1,r);
return ;
}
int main() {
n = read(); m = read();
for(int i = 1;i <= n;i ++) {
a[i] = read();
}
int Q = read();
for(int i = 1;i <= Q;i ++) {
ql[i] = read(); qr[i] = read();
addtree(1,ql[i],qr[i],1,n,i);
}
solve(1,1,n);
for(int i = 1;i <= Q;i ++) AIput(as[i],'\n');
return 0;
}
这里空空如也
有帮助,赞一个