QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#819371 | #9877. Segment Tree | rqoi031 | WA | 24ms | 3664kb | C++20 | 1.9kb | 2024-12-18 15:13:04 | 2024-12-18 15:13:04 |
Judging History
answer
#include<stdio.h>
#include<algorithm>
template<int N>
struct segment_tree {
int len;
int val[1<<N+1|1];
int dis[1<<N+1|1];
void push_up(const int &x) {
dis[x]=std::min(dis[x<<1]+dis[x<<1|1],val[x]);
}
void build(const int &n) {
len=1<<n;
std::copy(val+len,val+(len<<1),dis+len);
for(int i=len-1;i!=0;i--) {
push_up(i);
}
}
void modify(int x,const int &y) {
val[x]=y;
dis[x]=x<len?std::min(dis[x<<1]+dis[x<<1|1],y):y;
while(x!=1) {
push_up(x>>=1);
}
}
int query(int x,int y) {
if(x==y) {
return 0;
}
if(x>y) {
std::swap(x,y);
}
x|=len,y|=len;
int dx[2]{0,val[x]},dy[2]{0,val[y]};
if(y==len) {
y=(len<<1)-1,dy[0]=val[y],dy[1]=0;
}
const auto go([&]()->void {
dx[x&1]=std::min(dx[x&1],dx[x&1^1]+dis[x^1]+val[x>>1]);
dx[x&1^1]=std::min(dx[x&1^1]+dis[x^1],dx[x&1]+val[x>>1]);
dy[y&1]=std::min(dy[y&1],dy[y&1^1]+dis[y^1]+val[y>>1]);
dy[y&1^1]=std::min(dy[y&1^1]+dis[y^1],dy[y&1]+val[y>>1]);
x>>=1,y>>=1;
});
while(x!=y&&(x^y)!=1) {
go();
}
int res{dx[1]+dy[0]};
while(x!=1) {
go(),res=std::min(res,dx[0]+dy[0]);
}
return res;
}
};
segment_tree<18> sgt;
int main() {
int n;
scanf("%d",&n);
for(int i=1;i!=1<<n+1;i++) {
scanf("%d",sgt.val+i);
}
sgt.build(n);
int q;
scanf("%d",&q);
for(int i=1;i<=q;i++) {
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op==1) {
sgt.modify(x,y);
}
else {
printf("%d\n",sgt.query(x,y));
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3664kb
input:
3 7 1 14 3 9 4 8 2 6 5 5 13 8 2 3 10 2 0 1 2 0 4 2 4 6 2 4 8 2 3 5 1 6 30 2 3 5 2 4 6 1 1 10000000 2 0 8
output:
2 1 4 8 17 18 13 15
result:
ok 8 tokens
Test #2:
score: -100
Wrong Answer
time: 24ms
memory: 3652kb
input:
1 7914575 2436426 4979445 199989 1 1 6190629 1 1 1407775 1 1 2804784 1 2 2631932 1 1 3078537 1 3 286918 1 2 3238506 1 3 3361868 1 2 9296263 1 3 4836991 1 3 2177068 1 3 4291757 1 1 594328 1 2 8996221 1 1 5531545 1 3 3575467 1 3 3206504 1 1 8344965 1 3 6045895 2 0 2 1 2 6248153 1 1 5797489 1 1 9766466...
output:
8344965 5684734 2756417 2512448 260252 7091295 9429341 6363152 6668726 4380822 8809904 6489233 8566868 8653391 7309148 7617913 8583126 4470761 4099069 5078402 7188565 8465921 4517278 1913351 7400947 10209488 1759308 6081288 7119110 6818224 4456186 8937580 7957006 5280672 13044542 1622556 5199610 183...
result:
wrong answer 5th words differ - expected: '130126', found: '260252'