QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#133571 | #4940. Token Distance | Delay_for_five_minutes# | RE | 2ms | 7600kb | C++14 | 4.6kb | 2023-08-02 11:21:15 | 2023-08-02 11:21:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n , q;
typedef long long ll;
const int mod = 998244353;
int a[100005];
template<int K>
struct Seg {
int sum[100005 * 4][K + 1];
int mn[100005] , mx[100005];
int pos[100005];
int c[K + 1][K + 1];
int pre[100005][K + 1];
int fp(int a,int b) {
int ans = 1;
while(b) {
ans = 1LL*ans*a%mod;b--;
}
return ans;
}
void init () {
c[0][0] = 1;
memset(pre,0,sizeof(pre));
for(int i = 1;i <= K;i++) {
c[i][0] = 1;
for(int j = 1;j <= i;j++) c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}
for(int i = 1;i <= n;i++) {
for(int j = 0;j <= K;j++) pre[i][j] = (pre[i - 1][j] + fp(i , j)) % mod;
}
}
void rec(int u) {
for(int i = 0;i <= K;i++) sum[u][i] = (sum[u<<1][i] + sum[u<<1|1][i]) % mod;
mn[u] = min(mn[u << 1 | 1] , mn[u << 1]);
mx[u] = max(mx[u << 1] , mx[u << 1 | 1]);
}
void build(int u,int l,int r) {
if(l == r) {
sum[u][0] = 1; pos[l] = u;
mn[u] = mx[u] = a[l];
for(int i = 1;i <= K;i++) sum[u][i] = 1LL*sum[u][i - 1]*a[l]% mod;
return;
}
build(u<<1 , l , (l + r >> 1));
build(u<<1|1 , (l + r >> 1) + 1 , r);
rec(u);
}
pair<int,int> qdif(int u,int l,int r,int ql,int qr) {
if(ql <= l && qr >= r) {
return pair<int,int>{mn[u] , mx[u]};
}
pair<int,int> ans = pair<int,int>{1e9 , 0};
int md = (l + r >> 1);
if(ql <= md) ans = qdif(u<<1 , l , md , ql , qr);
if(qr > md) {
auto d = qdif(u<<1|1 , md + 1 , r , ql , qr);
ans.first = min(ans.first , d.first);
ans.second = max(ans.second , d.second);
}
//printf("u %d , %d %d , %d %d\n",u,l,r,ans.first,ans.second);
return ans;
}
array<int,K > query(int u,int l,int r,int ql,int qr) {
if(ql <= l && qr >= r) {
array<int,K> ans;
for(int i = 1;i <= K;i++) ans[i - 1] = sum[u][i];
// printf("Q %d %d , %d %d , %d\n",l,r,ql,qr,ans[0]);
return ans;
}
array<int,K> ans;
for(int i = 0;i < K;i++) ans[i] = 0;
int md = (l + r >> 1);
if(ql <= md) ans = query(u<<1 , l , md , ql , qr);
if(qr > md) {
auto d = query(u<<1|1 , md + 1 , r , ql , qr);
for(int i = 0;i < K;i++) ans[i] = (ans[i] + d[i]) % mod;
}
// printf("Q %d %d , %d %d , %d\n",l,r,ql,qr,ans[0]);
return ans;
}
void upd(int p)
{
int u = pos[p]; mn[u] = mx[u] = a[p];
for(int i = 1;i <= K;i++) sum[u][i] = 1LL*sum[u][i - 1] * a[p] % mod;
u >>= 1;
while(u) {
rec(u) ;
u >>= 1;
}
}
int get(int a1,int d,int len,int k) {
int ans = 0;
for(int j = 0;j <= k;j++) {
int u = 1LL*c[k][j]*fp(a1 , k - j) % mod * fp(d , j) % mod;
ans = (ans + 1LL*u*pre[len][j]) % mod;
}
// printf("%d %d %d , %d\n",a1,d,len,ans);
return ans;
}
};
Seg<3> seg;
const int K = 3;
int main()
{
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);
cin >> n >> q;
for(int i =1 ;i <= n;i++) cin >> a[i];
seg.init();
seg.build(1 ,1 , n);
while(q--) {
int op;cin >> op;
if(op == 1) {
int x , y;cin >> x >> y;
a[x] = y;
seg.upd(x);
}
else {
int l , r;cin >> l >> r;
if(r - l + 1 <= 2) {
cout << "YES\n" ; continue;
}
int len = r- l + 1;
pair<int,int> d = seg.qdif(1 , 1 , n , l , r);
int dif = d.second - d.first ;
if(dif % (len - 1) != 0) {
cout << "NO\n" ; continue;
}
dif /= (len - 1);
// printf("A1 %d dif %d\n",d.first - dif , dif);
array<int,K> ar = seg.query(1 , 1 , n , l , r) ;
// printf("%d %d %d\n",ar[0],ar[1],ar[2]);
// printf("%d %d %d\n",seg.get(-2 , 3 , 4 , 1) , seg.get(-2 , 3 , 4 , 2) , seg.get(-2 , 3 , 4 , 3)) ;
bool ff = 1;
for(int j = 0;j < K;j++) {
if(ar[j] != seg.get(d.first - dif , dif , len , j + 1)) {
ff = 0;break;
}
}
if(ff) cout << "YES\n";
else cout << "NO\n";
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 6320kb
input:
5 7 1 1 1 10 1 2 1 3 2 1 5 1 5 4 1 3 7 2 2 4 2 2 5 2 4 5
output:
YES NO NO YES YES
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 7600kb
input:
2 1 0 1000000000 2 1 2
output:
YES
result:
ok single line: 'YES'
Test #3:
score: -100
Runtime Error
input:
81473 13549 972586683 972586964 972587245 972587526 972587807 972588088 972588369 972588650 972588931 972589212 972589493 972589774 972590055 972590336 972590617 972590898 972591179 972591460 972591741 972592022 972592303 972592584 972592865 972593146 972593427 972593708 972593989 972594270 97259455...