QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#369929 | #8279. Segment Tree | SolitaryDream# | WA | 6ms | 34440kb | C++17 | 3.3kb | 2024-03-28 19:37:27 | 2024-03-28 19:37:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,t) for(int i=(s),_t=(t); i<=_t; ++i)
#define DOR(i,s,t) for(int i=(s),_t=(t); i>=_t; --i)
typedef long long ll;
const int N=4e5+50;
const int Mod=998244353;
int X[N];
struct node {
int L,R,ls,rs;
} T[N];
int tid,ntid;
void build(int L,int R,int &p) {
if(L+1==R) p=++ntid;
else p=++tid;
T[p].L=L;
T[p].R=R;
if(L+1==R) return ;
// cerr << L << ' ' << R << ' ' << X[p] << endl;
build(L,X[p],T[p].ls);
build(X[p],R,T[p].rs);
}
vector<int> lp[N],rp[N];
vector<int> g[N];
int n,m;
int sum[N];
void clear() {
FOR(i,0,n) sum[i]=0;
}
void add(int x) {
for(; x<=n; x+=x&-x) {
sum[x]++;
}
}
int qry(int x) {
int s=0;
for(; x; x^=x&-x) {
s+=sum[x];
}
return s;
}
int mark[N];
ll f[N][3];
void dfs(int x) {
if(!T[x].ls) {
if(mark[x]) {
f[x][0]=1;
f[x][1]=1;
f[x][2]=0;
} else {
f[x][0]=1;
f[x][1]=0;
f[x][2]=1;
}
return ;
}
int l=T[x].ls,r=T[x].rs;
dfs(l);
dfs(r);
FOR(u,0,1) FOR(i,0,2) FOR(j,0,2) {
int a=u || (i==0 && j==0);
int b=(i==0) || (u && j==0);
int c=(j==0) || (u && i==0);
// if(u==0 && i==2 && j==2) cerr << mark[x] << endl;
if(a) {
if((!b && i==1) || (!c && j==1)) ;
else {
f[x][0]=(f[x][0]+f[l][i]*f[r][j])%Mod;
}
} else {
if(mark[x] || (!b && i==1) || (!c && j==1)) {
int nb=(i==0) || (1 && j==0);
int nc=(j==0) || (1 && i==0);
if((!nb && i==1) || (!nc && j==1)) ;
else {
// if(u==0 && i==2 && j==2) cerr << f[l][i]*f[r][j] << endl;
f[x][1]=(f[x][1]+f[l][i]*f[r][j])%Mod;
}
} else {
f[x][2]=(f[x][2]+f[l][i]*f[r][j])%Mod;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
FOR(i,1,n-1) {
cin >> X[i];
}
FOR(i,1,m) {
int l,r;
cin >> l >> r;
rp[l].push_back(r);
lp[r].push_back(l);
if(l==0 && r==n) mark[1]=1;
}
int rt=0;
ntid=n-1;
build(0,n,rt);
tid=ntid;
// cerr << " -- " << endl;
FOR(i,1,tid) {
if(T[i].ls) {
g[T[i].L].push_back(i);
}
}
FOR(i,0,n-1) {
for(auto r: rp[i]) {
add(r);
}
for(auto j: g[i]) {
if(qry(T[j].R-1)-qry(T[T[j].ls].R-1)) mark[T[j].ls]=1;
}
}
// cerr << " -- " << endl;
clear();
FOR(i,0,n) g[i].clear();
FOR(i,1,tid) {
if(T[i].rs) {
g[T[i].R].push_back(i);
}
}
DOR(i,n,1) {
for(auto l: lp[i]) {
add(l+1);
}
for(auto j: g[i]) {
if(qry(T[T[j].rs].L+1)-qry(T[j].L+1)) mark[T[j].rs]=1;
}
}
// FOR(i,1,tid) if(mark[i]) cerr << i << endl;
dfs(1);
// cerr << f[2][0] << ' ' << f[2][1] << ' ' << f[2][2] << endl;
ll res=(f[1][0]+f[1][2])%Mod;
cout << res << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 34288kb
input:
2 1 1 0 2
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Accepted
time: 3ms
memory: 34348kb
input:
2 1 1 1 2
output:
5
result:
ok 1 number(s): "5"
Test #3:
score: 0
Accepted
time: 3ms
memory: 34372kb
input:
5 2 2 1 4 3 1 3 2 5
output:
193
result:
ok 1 number(s): "193"
Test #4:
score: 0
Accepted
time: 4ms
memory: 32344kb
input:
10 10 5 2 1 3 4 7 6 8 9 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10
output:
70848
result:
ok 1 number(s): "70848"
Test #5:
score: 0
Accepted
time: 3ms
memory: 34292kb
input:
2 2 1 0 1 0 2
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 0ms
memory: 32360kb
input:
3 3 1 2 0 1 0 2 0 3
output:
14
result:
ok 1 number(s): "14"
Test #7:
score: 0
Accepted
time: 3ms
memory: 34440kb
input:
4 4 1 2 3 0 1 0 2 0 3 0 4
output:
48
result:
ok 1 number(s): "48"
Test #8:
score: 0
Accepted
time: 6ms
memory: 34292kb
input:
5 5 3 1 2 4 0 1 0 2 0 3 0 4 0 5
output:
164
result:
ok 1 number(s): "164"
Test #9:
score: 0
Accepted
time: 6ms
memory: 34416kb
input:
6 6 4 2 1 3 5 0 1 0 2 0 3 0 4 0 5 0 6
output:
544
result:
ok 1 number(s): "544"
Test #10:
score: 0
Accepted
time: 4ms
memory: 34348kb
input:
7 7 3 2 1 5 4 6 0 1 0 2 0 3 0 4 0 5 0 6 0 7
output:
1856
result:
ok 1 number(s): "1856"
Test #11:
score: 0
Accepted
time: 0ms
memory: 34360kb
input:
8 8 3 1 2 4 7 5 6 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8
output:
6528
result:
ok 1 number(s): "6528"
Test #12:
score: 0
Accepted
time: 6ms
memory: 33632kb
input:
9 9 3 1 2 4 7 6 5 8 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9
output:
21520
result:
ok 1 number(s): "21520"
Test #13:
score: 0
Accepted
time: 6ms
memory: 34296kb
input:
10 10 8 2 1 3 4 6 5 7 9 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10
output:
71296
result:
ok 1 number(s): "71296"
Test #14:
score: 0
Accepted
time: 4ms
memory: 32352kb
input:
2 3 1 0 1 0 2 1 2
output:
4
result:
ok 1 number(s): "4"
Test #15:
score: 0
Accepted
time: 3ms
memory: 33696kb
input:
3 6 1 2 0 1 0 2 0 3 1 2 1 3 2 3
output:
14
result:
ok 1 number(s): "14"
Test #16:
score: 0
Accepted
time: 0ms
memory: 32300kb
input:
4 10 1 2 3 0 1 0 2 0 3 0 4 1 2 1 3 1 4 2 3 2 4 3 4
output:
48
result:
ok 1 number(s): "48"
Test #17:
score: 0
Accepted
time: 0ms
memory: 34360kb
input:
5 15 1 4 3 2 0 1 0 2 0 3 0 4 0 5 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5
output:
164
result:
ok 1 number(s): "164"
Test #18:
score: 0
Accepted
time: 3ms
memory: 34348kb
input:
6 21 5 3 1 2 4 0 1 0 2 0 3 0 4 0 5 0 6 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6
output:
544
result:
ok 1 number(s): "544"
Test #19:
score: 0
Accepted
time: 4ms
memory: 34284kb
input:
7 28 4 1 2 3 6 5 0 1 0 2 0 3 0 4 0 5 0 6 0 7 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7
output:
1912
result:
ok 1 number(s): "1912"
Test #20:
score: 0
Accepted
time: 3ms
memory: 32368kb
input:
8 36 5 2 1 3 4 7 6 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 1 2 1 3 1 4 1 5 1 6 1 7 1 8 2 3 2 4 2 5 2 6 2 7 2 8 3 4 3 5 3 6 3 7 3 8 4 5 4 6 4 7 4 8 5 6 5 7 5 8 6 7 6 8 7 8
output:
6304
result:
ok 1 number(s): "6304"
Test #21:
score: 0
Accepted
time: 0ms
memory: 32364kb
input:
9 45 6 2 1 4 3 5 7 8 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 4 3 5 3 6 3 7 3 8 3 9 4 5 4 6 4 7 4 8 4 9 5 6 5 7 5 8 5 9 6 7 6 8 6 9 7 8 7 9 8 9
output:
20736
result:
ok 1 number(s): "20736"
Test #22:
score: 0
Accepted
time: 0ms
memory: 34360kb
input:
10 55 6 3 2 1 4 5 8 7 9 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 3 4 3 5 3 6 3 7 3 8 3 9 3 10 4 5 4 6 4 7 4 8 4 9 4 10 5 6 5 7 5 8 5 9 5 10 6 7 6 8 6 9 6 10 7 8 7 9 7 10 8 9 8 10 9 10
output:
70784
result:
ok 1 number(s): "70784"
Test #23:
score: 0
Accepted
time: 5ms
memory: 34312kb
input:
2 1 1 0 2
output:
5
result:
ok 1 number(s): "5"
Test #24:
score: 0
Accepted
time: 0ms
memory: 34356kb
input:
3 1 2 1 2 3
output:
21
result:
ok 1 number(s): "21"
Test #25:
score: 0
Accepted
time: 6ms
memory: 34392kb
input:
4 1 2 1 3 0 1
output:
85
result:
ok 1 number(s): "85"
Test #26:
score: 0
Accepted
time: 0ms
memory: 34368kb
input:
5 1 4 1 3 2 0 5
output:
341
result:
ok 1 number(s): "341"
Test #27:
score: -100
Wrong Answer
time: 0ms
memory: 32316kb
input:
6 1 5 1 2 3 4 0 2
output:
1155
result:
wrong answer 1st numbers differ - expected: '1260', found: '1155'