比赛 |
2024暑假C班集训E |
评测结果 |
AAAAAAWWWWWWWWWWWWWW |
题目名称 |
相逢是问候 |
最终得分 |
30 |
用户昵称 |
liuyiche |
运行时间 |
1.985 s |
代码语言 |
C++ |
内存使用 |
3.70 MiB |
提交时间 |
2024-07-14 10:30:38 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, p;
ll c;
ll a[50005];
int L[305], R[305];
ll sum[305];
int blog[50005];
inline ll qpow(ll x, ll y)
{
if(y == 0)
return 1;
ll tmp = qpow(x,y/2)%p;
tmp = (tmp*tmp)%p;
if(y%2 != 0)
tmp = (tmp*x)%p;
return tmp;
}
inline void build()
{
int block = sqrt(n);
int num = n/block;
if(n%block) num++;
for(int i = 1; i <= num; ++i)
{
L[i] = (i-1)*block+1;
R[i] = i*block;
}
R[num] = n;
for(int i = 1; i <= n; ++i)
blog[i] = (i-1)/block+1;
for(int i = 1; i <= num; ++i)
for(int j = L[i]; j <= R[i]; ++j)
{
sum[i] += a[j];
sum[i] %= p;
}
}
inline ll query(ll l, ll r)
{
ll ans = 0;
if(blog[l] == blog[r])
for(int i = l; i <= r; ++i)
ans += a[i], ans %= p;
else
{
for(int i = l; i <= R[blog[l]]; ++i)
ans += a[i], ans%= p;
for(int i = blog[l]+1; i < blog[r]; ++i)
ans += sum[i], ans %= p;
for(int i = L[blog[r]]; i <= r; ++i)
ans += a[i], ans %= p;
}
return ans;
}
int main()
{
freopen("verbinden.in","r",stdin);
freopen("verbinden.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m >> p >> c;
for(ll i = 1; i <= n; ++i)
cin >> a[i], a[i] %= p;
build();
for(int i = 1; i <= m; ++i)
{
int s, x, y;
cin >> s >> x >> y;
if(s == 0)
{
sum[blog[x]] = ((sum[blog[x]]-a[x])%p+p)%p;
a[x] = qpow(c,a[x]);
sum[blog[x]] = (a[x]+sum[blog[x]])%p;
}
else
cout << query(x,y) << '\n';
}
return 0;
}