QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#311849 | #1831. Bruteforce | CrayonIsGay | RE | 0ms | 0kb | C++98 | 7.9kb | 2024-01-22 21:21:42 | 2024-01-22 21:21:43 |
answer
#include<bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,tune=native")
using namespace std;
#define int long long
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
const ll N = 1e5+100;
const ll INF = (ll)1e18;
const ll MOD = 998244353;
inline ll read(){
ll ret=0,f=1;char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f*=-1;
ch=getchar();
}
while(isdigit(ch)){
ret=(ret<<3)+(ret<<1)+ch-'0';
ch=getchar();
}
return ret*f;
}
ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
ll kpow(ll a,ll b,ll mod){
ll ret=1,base=a%mod;while(b){if(b&1) ret=ret*base%mod;base=base*base%mod;b>>=1;}return ret;
}
inline ll upd(ll x){return (x%MOD+MOD)%MOD;}
ll C[10][10];
inline ll get_d(ll k,ll c,ll w){
return C[k][c]*kpow(w,k-c,MOD);
}
ll n,m,k,W,a[N];
const ll V = 1e5+2;
struct segment_tree0{
ll mod;
int cnt[N*4],sum[N*4],sum_k[6][N*4];
int tg[N*4];
segment_tree0(){memset(cnt,0,sizeof(cnt));memset(sum,0,sizeof(sum));memset(sum_k,0,sizeof(sum_k));memset(tg,0,sizeof(tg));}
inline void push_up(int rt){
sum[rt]=upd(sum[rt<<1]+sum[rt<<1|1]);
for(int i=0;i<=k;++i)
sum_k[i][rt]=upd(sum_k[i][rt<<1]+sum_k[i][rt<<1|1]);
cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1];
}
inline void modify(int rt,int v){
tg[rt]+=v;
for(int i=k;i>=0;i--)
for(int j=i-1;j>=0;j--)
sum_k[i][rt]=upd(sum_k[i][rt]+1ll*sum_k[j][rt]*get_d(i,j,v));
sum[rt]=sum_k[k][rt];
}
inline void push_down(int rt){
if(tg[rt]!=0){
modify(rt<<1,tg[rt]);
modify(rt<<1|1,tg[rt]);
tg[rt]=0;
}
}
inline int query_sz(int rt,int l,int r,int idx){
if(r<=idx) return cnt[rt];
if(l>idx) return 0;
int mid=(l+r)>>1;
return query_sz(rt<<1,l,mid,idx)+query_sz(rt<<1|1,mid+1,r,idx);
}
inline void update_v(int rt,int l,int r,int w,int c){
if(r<w||l>w) return;
if(l==r){
if(c==1){
ll id=query_sz(1,0,V,w)+1;
cnt[rt]++;
for(ll i=0;i<=k;++i)
sum_k[i][rt]=upd(1ll*sum_k[i][rt]+1ll*w*kpow(id,i,MOD)%MOD);
}else{
ll id=query_sz(1,0,V,w);
cnt[rt]--;
for(ll i=0;i<=k;++i)
sum_k[i][rt]=upd(1ll*sum_k[i][rt]-1ll*w*kpow(id,i,MOD)%MOD);
}
sum[rt]=sum_k[k][rt];
return ;
}
int mid=l+r>>1;
push_down(rt);
update_v(rt<<1,l,mid,w,c);
update_v(rt<<1|1,mid+1,r,w,c);
push_up(rt);
}
inline void update_i(int rt,int l,int r,int ql,int qr,int v){
if(r<ql||l>qr) return;
if(ql<=l&&r<=qr){
if(v==1){
for(int i=k;i>=0;i--)
for(int j=i-1;j>=0;j--)
sum_k[i][rt]=upd(1ll*sum_k[i][rt]+1ll*sum_k[j][rt]*get_d(i,j,v));
}else{
for(int i=k;i>=0;i--)
for(int j=i-1;j>=0;j--)
sum_k[i][rt]=upd(1ll*sum_k[i][rt]+1ll*sum_k[j][rt]*get_d(i,j,v));
}
sum[rt]=sum_k[k][rt];
tg[rt]+=v;
return ;
}
int mid=(l+r)>>1;
push_down(rt);
update_i(rt<<1,l,mid,ql,qr,v);
update_i(rt<<1|1,mid+1,r,ql,qr,v);
push_up(rt);
}
}sg0;
ll kpw[20][20];
struct segment_tree1{
int cnt[N*4],sum[N*4],sum_k[6][6][N*4];
int tg[N*4];
segment_tree1(){memset(cnt,0,sizeof(cnt));memset(sum,0,sizeof(sum));memset(sum_k,0,sizeof(sum_k));memset(tg,0,sizeof(tg));}
inline void push_up(int rt){
sum[rt]=upd(sum[rt<<1]+sum[rt<<1|1]);
for(int i=0;i<W;++i)
for(int j=0;j<W;++j)
sum_k[i][j][rt]=upd(sum_k[i][j][rt<<1]+sum_k[i][j][rt<<1|1]);
cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1];
}
inline void modify(int rt,int v){
v=(v%W+W)%W;
ll ret[6][6];
sum[rt]=0;
for(int j=0;j<W;++j){
for(int i=0;i<W;++i)
ret[j][i]=sum_k[j][(i-v+W*2)%W][rt];
for(int i=0;i<W;++i)
sum_k[j][i][rt]=ret[j][i];
for(int i=0;i<W;++i)
sum[rt]=upd(sum[rt]+1ll*j*kpw[i][k]%W*sum_k[j][i][rt]%MOD);
}
}
inline void push_down(int rt){
if(tg[rt]!=0){
tg[rt<<1]+=tg[rt];tg[rt<<1|1]+=tg[rt];
modify(rt<<1,tg[rt]);
modify(rt<<1|1,tg[rt]);
tg[rt]=0;
}
}
inline ll query_sz(int rt,int l,int r,int idx){
if(r<=idx) return cnt[rt];
if(l>idx) return 0;
int mid=(l+r)>>1;
return query_sz(rt<<1,l,mid,idx)+query_sz(rt<<1|1,mid+1,r,idx);
}
inline void update_v(int rt,int l,int r,int w,int c){
if(r<w||l>w) return;
if(l==r){
if(c==1){
ll id=query_sz(1,0,V,w)+1;
cnt[rt]++;
sum_k[w%W][id%W][rt]=upd(sum_k[w%W][id%W][rt]+1);
}else{
ll id=query_sz(1,0,V,w);
cnt[rt]--;
sum_k[w%W][id%W][rt]=upd(sum_k[w%W][id%W][rt]-1);
}
sum[rt]=0;
for(int j=0;j<W;++j)
for(int i=0;i<W;++i)
sum[rt]=upd(sum[rt]+1ll*j*kpw[i][k]%W*sum_k[j][i][rt]%MOD);
return ;
}
int mid=l+r>>1;
push_down(rt);
update_v(rt<<1,l,mid,w,c);
update_v(rt<<1|1,mid+1,r,w,c);
push_up(rt);
}
inline void update_i(int rt,int l,int r,int ql,int qr,int v){
if(r<ql||l>qr) return;
if(ql<=l&&r<=qr){
modify(rt,v);
tg[rt]+=v;
return ;
}
int mid=(l+r)>>1;
push_down(rt);
update_i(rt<<1,l,mid,ql,qr,v);
update_i(rt<<1|1,mid+1,r,ql,qr,v);
push_up(rt);
}
}sg1;
ll b[N];
void multi_test(){
for(int i=0;i<=6;++i){
C[i][0]=C[i][i]=1;
for(int j=1;j<i;++j)
C[i][j]=C[i-1][j-1]+C[i-1][j];
}
n=read(),k=read(),W=read();
for(int i=0;i<=10;++i)
for(int j=0;j<=10;++j)
kpw[i][j]=kpow(i,j,W);
sg0.mod=998244353;//sg1.mod=W;
for(int i=1;i<=n;++i){
a[i]=read();
b[i]=a[i];
}
sort(b+1,b+n+1);
for(int i=1;i<=n;++i){
sg0.update_v(1,0,V,b[i],1);
sg0.update_i(1,0,V,b[i]+1,V,1);
sg1.update_v(1,0,V,b[i],1);
sg1.update_i(1,0,V,b[i]+1,V,1);
}
// cout<<sg0.sum[1]<<endl;
// cout<<sg1.sum[1]<<endl<<endl;
ll q=read();
while(q--){
ll pos=read(),x=read();
sg0.update_v(1,0,V,a[pos],-1);
sg0.update_i(1,0,V,a[pos]+1,V,-1);
sg0.update_v(1,0,V,x,1);
sg0.update_i(1,0,V,x+1,V,1);
// cout<<sg0.sum[1]<<endl;
sg1.update_v(1,0,V,a[pos],-1);
sg1.update_i(1,0,V,a[pos]+1,V,-1);
sg1.update_v(1,0,V,x,1);
sg1.update_i(1,0,V,x+1,V,1);
// cout<<sg1.sum[1]<<endl<<endl;
assert(upd(sg0.sum_k[k][1])==upd(sg0.sum[1]));
printf("%lld\n",(ll)1ll*upd(sg0.sum_k[k][1]-sg1.sum[1])*kpow(W,MOD-2,MOD)%MOD);
a[pos]=x;
}
return ;
}
/*
Things to notice:
1. do not calculate useless values
2. do not use similar names
Things to check:
1. submit the correct file
2. time (it is log^2 or log)
3. memory
4. prove your naive thoughts
5. long long
6. corner case like n=0,1,inf or n=m
7. fileio in some oi-contest
8. module on time
9. the number of a same divisor in a math problem
10. multi-information and queries
*/
signed main(){
freopen("data.in","r",stdin);
freopen("my.out","w",stdout);
ll T=1;
while(T--) multi_test();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
3 1 1 2 2 8 2 2 5 3 6