QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#591215 | #8338. Quad Kingdoms Chess | Rosmontispes | RE | 0ms | 0kb | C++20 | 3.0kb | 2024-09-26 14:49:19 | 2024-09-26 14:49:20 |
answer
//14:23
#include<bits/stdc++.h>
using namespace std;
const int N = 100000 + 200;
const long long mod = 998244353;
const long long A = 131;
long long pre[N];
struct segtree{
#define ls(x) tree[x].ls
#define rs(x) tree[x].rs
struct node{
int mxlen,maxn,ls,rs;
long long hash;
};
int n,tot,rt;
vector<int>ar;
vector<node>tree;
segtree(int _n,vector<int>tmp):rt(0),tot(0),ar(tmp),n(_n),tree(4 * _n + 200){
build(1,n,rt);
}
pair<int,long long> qur(int x,int mx){
if(!x)
return {0,0};
if(tree[x].maxn < mx)
return {0,0};
if(tree[ls(x)].maxn < mx)
return qur(rs(x),mx);
if(tree[rs(x)].maxn >= mx){
pair<int,long long>ret = {tree[ls(x)].mxlen,tree[ls(x)].hash};
auto rret = qur(rs(x),mx);
ret.first += rret.first;
ret.second = (ret.second * pre[rret.first] % mod + rret.second) % mod;
} else {
return qur(ls(x),mx);
}
}
node merge(int x){
node tmp = tree[x];
if(tree[rs(x)].maxn > tree[ls(x)].maxn){
tmp.mxlen = tree[rs(x)].mxlen;
tmp.maxn = tree[rs(x)].maxn;
tmp.hash = tree[rs(x)].hash;
return tmp;
} else {
auto [len,lhash] = qur(ls(x),tree[rs(x)].maxn);
tmp.mxlen = tree[rs(x)].mxlen + len;
tmp.maxn = tree[ls(x)].maxn;
tmp.hash = (lhash * pre[len] % mod + tree[rs(x)].hash) % mod;
return tmp;
}
}
void build(int l,int r,int &x){
if(!x)
x = ++ tot;
if(l == r){
tree[x] = {1,ar[l],0,0,ar[l]};
return;
}
int mid = (l + r) / 2;
build(l,mid,ls(x)),build(mid + 1,r,rs(x));
tree[x] = merge(x);
}
void modify(int l,int r,int x,int k,int val){
if(l == r){
tree[x].maxn = tree[x].hash = val;
return;
}
int mid = (l + r) / 2;
if(k <= mid)
modify(l,mid,ls(x),k,val);
else
modify(mid + 1,r,rs(x),k,val);
tree[x] = merge(x);
}
};
void solve(){
int n,m;
cin>>n;
vector<int>p(n + 1);
for(int i = 1;i <= n;i ++)
cin>>p[i];
cin>>m;
vector<int>q(m + 1);
for(int i = 1;i <= m;i ++){
cin>>q[i];
}
segtree A(n,p),B(m,q);
int Q;
cin>>Q;
for(int i = 1;i <= Q;i ++){
int opr,k,val;
cin>>opr>>k>>val;
if(opr == 1)
A.modify(1,n,1,k,val);
else
B.modify(1,m,1,k,val);
if(A.tree[1].hash == B.tree[1].hash && A.tree[1].mxlen == B.tree[1].mxlen)
cout<<"YES\n";
else
cout<<"NO\n";
}
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
pre[0] = 1;
for(int i = 1;i <= 100000;i ++){
pre[i] = pre[i - 1] * A % mod;
}
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
5 1 2 3 4 5 5 5 4 3 2 1 8 1 1 5 1 4 2 1 2 4 1 5 1 1 5 5 2 1 4 2 3 5 2 5 5