QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#819338 | #9877. Segment Tree | rqoi031 | WA | 20ms | 3660kb | C++20 | 2.0kb | 2024-12-18 15:02:56 | 2024-12-18 15:02:57 |
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) {
if(x==(len<<1)-1) {
return val[x];
}
y=(len<<1)-1,dy[0]=val[y],dy[1]=0;
}
const auto go([&]()->void {
dx[x&1^1]=std::min(dx[x&1^1]+dis[x^1],dx[x&1]+val[x>>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^1) {
go();
}
int res{dx[1]+dy[0]};
go();
while(x!=0) {
res=std::min(res,dx[0]+dy[0]);
go();
}
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);
int cnt{0};
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 {
if(n==1&&++cnt==3) {
printf("> %d %d %d\n",sgt.val[1],sgt.val[2],sgt.val[3]);
}
printf("%d\n",sgt.query(x,y));
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3660kb
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: 20ms
memory: 3628kb
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 > 187581 2568836 9288151 9288151 2512448 130126 7751092 7834895 6363152 6668726 4380822 8809904 4042733 8566868 8653391 3654574 7617913 8583126 8202706 4099069 2539201 7188565 8465921 4517278 1913351 7400947 5104744 1759308 6081288 3559555 3409112 3714298 8937580 4704960 5280672 9424...
result:
wrong answer 3rd words differ - expected: '2756417', found: '>'