QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#819314#9877. Segment Treerqoi031TL 0ms3684kbC++201.7kb2024-12-18 14:58:502024-12-18 14:58:53

Judging History

你现在查看的是最新测评结果

  • [2024-12-18 14:58:53]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3684kb
  • [2024-12-18 14:58:50]
  • 提交

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^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);
    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;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3684kb

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
Time Limit Exceeded

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:


result: