Submission #1035264

# Submission time Handle Problem Language Result Execution time Memory
1035264 2024-07-26T08:11:30 Z shenfe1 Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
2248 ms 402016 KB
#include <bits/stdc++.h>
 
#pragma GCC optimize("Ofast,unroll-loops")
 
using namespace std;
 
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(s) (int)s.size()
#define ppb pop_back
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define lb lower_bound
#define mem(a,i) memset(a,i,sizeof(a))

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int MAX=1e5+10;
 
int n,m;
 
struct DSU{
    int f[MAX];
 
    void init(int n){
        for(int i=1;i<=n;i++)f[i]=i;
    }

    int get(int v){
        return (f[v]==v?v:f[v]=get(f[v]));
    }
 
    void unite(int a,int b){
        a=get(a);
        b=get(b);
        f[a]=b;
    }
}D;


struct segtree{
    ll t[120*MAX];
    int L[120*MAX],R[120*MAX];
    int zs;

    segtree(){
        mem(t,0);
        mem(L,0);
        mem(R,0);
        zs=1;
    }
    
    void update(int v,int tl,int tr,int l,int r,int x){
        if(l>r||tl>r||l>tr)return;
        if(l<=tl&&tr<=r){
            t[v]+=x;
            return;
        }
        int tm=(tl+tr)/2;
        if(!L[v]){
            L[v]=++zs;
        }
        if(!R[v]){
            R[v]=++zs;
        }
        update(L[v],tl,tm,l,r,x);
        update(R[v],tm+1,tr,l,r,x);
    }

    ll get(int v,int tl,int tr,int pos){
        if(tl==tr)return t[v];
        int tm=(tl+tr)/2;
        if(pos<=tm){
            if(L[v])return t[v]+get(L[v],tl,tm,pos);
            return t[v];
        }
        else{
            if(R[v])return t[v]+get(R[v],tm+1,tr,pos);
            return t[v];
        }
    }
}T[2];

vector<pair<int,pii>> e;
set<pii> g[510];
int d[510];

void dfs(int v,int p=-1){
    for(auto to:g[v]){
        if(to.F==p)continue;
        d[to.F]=max(d[v],to.S);
        dfs(to.F,v);
    }
}

void dfs1(int v,int p=-1){
    for(auto to:g[v]){
        if(to.F==p)continue;
        d[to.F]=min(d[v],to.S);
        dfs1(to.F,v);
    }
}

void solve(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        e.pb({c,{a,b}});
    }
    sort(all(e));
    D.init(n);
    for(int i=m-1;i>=0;i--){
        if(D.get(e[i].S.F)!=D.get(e[i].S.S)){
            D.unite(e[i].S.F,e[i].S.S);
            g[e[i].S.F].insert({e[i].S.S,i});
            g[e[i].S.S].insert({e[i].S.F,i});
            // if(e[i].F<=10)cout<<i<<" "<<10-e[i].F<<"\n";
            T[0].update(1,0,1e9,e[i].F,1e9,-e[i].F);
            T[1].update(1,0,1e9,e[i].F,1e9,1);
        }
        else{
            mem(d,0);
            dfs(e[i].S.F);
            int edge=d[e[i].S.S];
            g[e[edge].S.F].erase({e[edge].S.S,edge});
            g[e[edge].S.S].erase({e[edge].S.F,edge});
            {
                int pos=(e[i].F+e[edge].F)/2;
                // if(e[i].F<=10&&10<=pos)cout<<e[i].F<<" "<<e[edge].F<<" "<<pos<<" "<<10-e[i].F<<"\n";
                T[0].update(1,0,1e9,e[i].F,pos,-e[i].F);
                T[1].update(1,0,1e9,e[i].F,pos,1);
            }
            g[e[i].S.F].insert({e[i].S.S,i});
            g[e[i].S.S].insert({e[i].S.F,i});
        }
    }
    D.init(n);
    for(int i=0;i<m;i++){
        if(D.get(e[i].S.F)!=D.get(e[i].S.S)){
            D.unite(e[i].S.F,e[i].S.S);
            g[e[i].S.F].insert({e[i].S.S,i});
            g[e[i].S.S].insert({e[i].S.F,i});
            T[0].update(1,0,1e9,0,e[i].F,e[i].F);
            T[1].update(1,0,1e9,0,e[i].F,-1);
        }
        else{
            for(int i=1;i<=n;i++)d[i]=1e9;
            dfs1(e[i].S.F);
            int edge=d[e[i].S.S];
            g[e[edge].S.F].erase({e[edge].S.S,edge});
            g[e[edge].S.S].erase({e[edge].S.F,edge});
            {
                int pos=(e[edge].F+e[i].F)/2+1;
                T[0].update(1,0,1e9,pos,e[i].F,e[i].F);
                T[1].update(1,0,1e9,pos,e[i].F,-1);
            }    
            g[e[i].S.F].insert({e[i].S.S,i});
            g[e[i].S.S].insert({e[i].S.F,i});
        }
    }
    int q;
    cin>>q;
    while(q--){
        int d;
        cin>>d;
        cout<<T[0].get(1,0,1e9,d)+T[1].get(1,0,1e9,d)*d<<"\n";
    }
}
 
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    int init=clock();
    while(t--)solve();
    #ifdef LOCAL
        cout.precision(10);
        cout<<fixed<<1.0*(clock()-init)/CLOCKS_PER_SEC<<"\n";
    #endif
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:179:9: warning: unused variable 'init' [-Wunused-variable]
  179 |     int init=clock();
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 149 ms 375996 KB Output is correct
2 Correct 150 ms 376236 KB Output is correct
3 Correct 151 ms 376144 KB Output is correct
4 Correct 150 ms 376084 KB Output is correct
5 Correct 195 ms 376072 KB Output is correct
6 Correct 152 ms 376148 KB Output is correct
7 Correct 151 ms 376144 KB Output is correct
8 Correct 152 ms 376144 KB Output is correct
9 Correct 158 ms 376216 KB Output is correct
10 Correct 151 ms 376024 KB Output is correct
11 Correct 152 ms 376160 KB Output is correct
12 Correct 157 ms 376236 KB Output is correct
13 Correct 151 ms 376144 KB Output is correct
14 Correct 152 ms 376148 KB Output is correct
15 Correct 157 ms 376140 KB Output is correct
16 Correct 154 ms 376200 KB Output is correct
17 Correct 156 ms 376144 KB Output is correct
18 Correct 157 ms 376148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 375996 KB Output is correct
2 Correct 150 ms 376236 KB Output is correct
3 Correct 151 ms 376144 KB Output is correct
4 Correct 150 ms 376084 KB Output is correct
5 Correct 195 ms 376072 KB Output is correct
6 Correct 152 ms 376148 KB Output is correct
7 Correct 151 ms 376144 KB Output is correct
8 Correct 152 ms 376144 KB Output is correct
9 Correct 158 ms 376216 KB Output is correct
10 Correct 151 ms 376024 KB Output is correct
11 Correct 152 ms 376160 KB Output is correct
12 Correct 157 ms 376236 KB Output is correct
13 Correct 151 ms 376144 KB Output is correct
14 Correct 152 ms 376148 KB Output is correct
15 Correct 157 ms 376140 KB Output is correct
16 Correct 154 ms 376200 KB Output is correct
17 Correct 156 ms 376144 KB Output is correct
18 Correct 157 ms 376148 KB Output is correct
19 Correct 158 ms 376148 KB Output is correct
20 Correct 1957 ms 377804 KB Output is correct
21 Correct 1462 ms 377804 KB Output is correct
22 Correct 1694 ms 377932 KB Output is correct
23 Correct 1822 ms 377824 KB Output is correct
24 Correct 1909 ms 377800 KB Output is correct
25 Correct 1963 ms 377804 KB Output is correct
26 Correct 1952 ms 377836 KB Output is correct
27 Correct 1997 ms 377804 KB Output is correct
28 Correct 1854 ms 377800 KB Output is correct
29 Correct 1028 ms 377804 KB Output is correct
30 Correct 2024 ms 377800 KB Output is correct
31 Correct 2053 ms 377800 KB Output is correct
32 Correct 2035 ms 377868 KB Output is correct
33 Correct 1983 ms 377800 KB Output is correct
34 Correct 1069 ms 377800 KB Output is correct
35 Correct 1975 ms 377804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 376144 KB Output is correct
2 Correct 154 ms 376028 KB Output is correct
3 Correct 157 ms 376140 KB Output is correct
4 Correct 2017 ms 391112 KB Output is correct
5 Correct 2005 ms 399816 KB Output is correct
6 Correct 2048 ms 399960 KB Output is correct
7 Correct 1302 ms 401864 KB Output is correct
8 Correct 1276 ms 402008 KB Output is correct
9 Correct 1295 ms 402016 KB Output is correct
10 Correct 1877 ms 399960 KB Output is correct
11 Correct 1108 ms 402008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 375996 KB Output is correct
2 Correct 150 ms 376236 KB Output is correct
3 Correct 151 ms 376144 KB Output is correct
4 Correct 150 ms 376084 KB Output is correct
5 Correct 195 ms 376072 KB Output is correct
6 Correct 152 ms 376148 KB Output is correct
7 Correct 151 ms 376144 KB Output is correct
8 Correct 152 ms 376144 KB Output is correct
9 Correct 158 ms 376216 KB Output is correct
10 Correct 151 ms 376024 KB Output is correct
11 Correct 152 ms 376160 KB Output is correct
12 Correct 157 ms 376236 KB Output is correct
13 Correct 151 ms 376144 KB Output is correct
14 Correct 152 ms 376148 KB Output is correct
15 Correct 157 ms 376140 KB Output is correct
16 Correct 154 ms 376200 KB Output is correct
17 Correct 156 ms 376144 KB Output is correct
18 Correct 157 ms 376148 KB Output is correct
19 Correct 147 ms 376244 KB Output is correct
20 Correct 318 ms 392272 KB Output is correct
21 Correct 329 ms 398380 KB Output is correct
22 Correct 323 ms 398160 KB Output is correct
23 Correct 329 ms 398220 KB Output is correct
24 Correct 311 ms 398192 KB Output is correct
25 Correct 323 ms 398416 KB Output is correct
26 Correct 312 ms 398160 KB Output is correct
27 Correct 294 ms 398160 KB Output is correct
28 Correct 310 ms 398164 KB Output is correct
29 Correct 316 ms 398188 KB Output is correct
30 Correct 308 ms 398416 KB Output is correct
31 Correct 292 ms 397916 KB Output is correct
32 Correct 287 ms 398596 KB Output is correct
33 Correct 300 ms 398160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 375996 KB Output is correct
2 Correct 150 ms 376236 KB Output is correct
3 Correct 151 ms 376144 KB Output is correct
4 Correct 150 ms 376084 KB Output is correct
5 Correct 195 ms 376072 KB Output is correct
6 Correct 152 ms 376148 KB Output is correct
7 Correct 151 ms 376144 KB Output is correct
8 Correct 152 ms 376144 KB Output is correct
9 Correct 158 ms 376216 KB Output is correct
10 Correct 151 ms 376024 KB Output is correct
11 Correct 152 ms 376160 KB Output is correct
12 Correct 157 ms 376236 KB Output is correct
13 Correct 151 ms 376144 KB Output is correct
14 Correct 152 ms 376148 KB Output is correct
15 Correct 157 ms 376140 KB Output is correct
16 Correct 154 ms 376200 KB Output is correct
17 Correct 156 ms 376144 KB Output is correct
18 Correct 157 ms 376148 KB Output is correct
19 Correct 158 ms 376148 KB Output is correct
20 Correct 1957 ms 377804 KB Output is correct
21 Correct 1462 ms 377804 KB Output is correct
22 Correct 1694 ms 377932 KB Output is correct
23 Correct 1822 ms 377824 KB Output is correct
24 Correct 1909 ms 377800 KB Output is correct
25 Correct 1963 ms 377804 KB Output is correct
26 Correct 1952 ms 377836 KB Output is correct
27 Correct 1997 ms 377804 KB Output is correct
28 Correct 1854 ms 377800 KB Output is correct
29 Correct 1028 ms 377804 KB Output is correct
30 Correct 2024 ms 377800 KB Output is correct
31 Correct 2053 ms 377800 KB Output is correct
32 Correct 2035 ms 377868 KB Output is correct
33 Correct 1983 ms 377800 KB Output is correct
34 Correct 1069 ms 377800 KB Output is correct
35 Correct 1975 ms 377804 KB Output is correct
36 Correct 1993 ms 377800 KB Output is correct
37 Correct 1599 ms 377756 KB Output is correct
38 Correct 1806 ms 377804 KB Output is correct
39 Correct 1862 ms 377804 KB Output is correct
40 Correct 1893 ms 377804 KB Output is correct
41 Correct 1962 ms 377784 KB Output is correct
42 Correct 1960 ms 377800 KB Output is correct
43 Correct 2040 ms 377800 KB Output is correct
44 Correct 1757 ms 377804 KB Output is correct
45 Correct 997 ms 377688 KB Output is correct
46 Correct 2007 ms 377800 KB Output is correct
47 Correct 1960 ms 377800 KB Output is correct
48 Correct 2092 ms 377804 KB Output is correct
49 Correct 1916 ms 377800 KB Output is correct
50 Correct 816 ms 377804 KB Output is correct
51 Correct 1920 ms 377804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 375996 KB Output is correct
2 Correct 150 ms 376236 KB Output is correct
3 Correct 151 ms 376144 KB Output is correct
4 Correct 150 ms 376084 KB Output is correct
5 Correct 195 ms 376072 KB Output is correct
6 Correct 152 ms 376148 KB Output is correct
7 Correct 151 ms 376144 KB Output is correct
8 Correct 152 ms 376144 KB Output is correct
9 Correct 158 ms 376216 KB Output is correct
10 Correct 151 ms 376024 KB Output is correct
11 Correct 152 ms 376160 KB Output is correct
12 Correct 157 ms 376236 KB Output is correct
13 Correct 151 ms 376144 KB Output is correct
14 Correct 152 ms 376148 KB Output is correct
15 Correct 157 ms 376140 KB Output is correct
16 Correct 154 ms 376200 KB Output is correct
17 Correct 156 ms 376144 KB Output is correct
18 Correct 157 ms 376148 KB Output is correct
19 Correct 158 ms 376148 KB Output is correct
20 Correct 1957 ms 377804 KB Output is correct
21 Correct 1462 ms 377804 KB Output is correct
22 Correct 1694 ms 377932 KB Output is correct
23 Correct 1822 ms 377824 KB Output is correct
24 Correct 1909 ms 377800 KB Output is correct
25 Correct 1963 ms 377804 KB Output is correct
26 Correct 1952 ms 377836 KB Output is correct
27 Correct 1997 ms 377804 KB Output is correct
28 Correct 1854 ms 377800 KB Output is correct
29 Correct 1028 ms 377804 KB Output is correct
30 Correct 2024 ms 377800 KB Output is correct
31 Correct 2053 ms 377800 KB Output is correct
32 Correct 2035 ms 377868 KB Output is correct
33 Correct 1983 ms 377800 KB Output is correct
34 Correct 1069 ms 377800 KB Output is correct
35 Correct 1975 ms 377804 KB Output is correct
36 Correct 161 ms 376144 KB Output is correct
37 Correct 154 ms 376028 KB Output is correct
38 Correct 157 ms 376140 KB Output is correct
39 Correct 2017 ms 391112 KB Output is correct
40 Correct 2005 ms 399816 KB Output is correct
41 Correct 2048 ms 399960 KB Output is correct
42 Correct 1302 ms 401864 KB Output is correct
43 Correct 1276 ms 402008 KB Output is correct
44 Correct 1295 ms 402016 KB Output is correct
45 Correct 1877 ms 399960 KB Output is correct
46 Correct 1108 ms 402008 KB Output is correct
47 Correct 147 ms 376244 KB Output is correct
48 Correct 318 ms 392272 KB Output is correct
49 Correct 329 ms 398380 KB Output is correct
50 Correct 323 ms 398160 KB Output is correct
51 Correct 329 ms 398220 KB Output is correct
52 Correct 311 ms 398192 KB Output is correct
53 Correct 323 ms 398416 KB Output is correct
54 Correct 312 ms 398160 KB Output is correct
55 Correct 294 ms 398160 KB Output is correct
56 Correct 310 ms 398164 KB Output is correct
57 Correct 316 ms 398188 KB Output is correct
58 Correct 308 ms 398416 KB Output is correct
59 Correct 292 ms 397916 KB Output is correct
60 Correct 287 ms 398596 KB Output is correct
61 Correct 300 ms 398160 KB Output is correct
62 Correct 1993 ms 377800 KB Output is correct
63 Correct 1599 ms 377756 KB Output is correct
64 Correct 1806 ms 377804 KB Output is correct
65 Correct 1862 ms 377804 KB Output is correct
66 Correct 1893 ms 377804 KB Output is correct
67 Correct 1962 ms 377784 KB Output is correct
68 Correct 1960 ms 377800 KB Output is correct
69 Correct 2040 ms 377800 KB Output is correct
70 Correct 1757 ms 377804 KB Output is correct
71 Correct 997 ms 377688 KB Output is correct
72 Correct 2007 ms 377800 KB Output is correct
73 Correct 1960 ms 377800 KB Output is correct
74 Correct 2092 ms 377804 KB Output is correct
75 Correct 1916 ms 377800 KB Output is correct
76 Correct 816 ms 377804 KB Output is correct
77 Correct 1920 ms 377804 KB Output is correct
78 Correct 2161 ms 398924 KB Output is correct
79 Correct 1787 ms 400840 KB Output is correct
80 Correct 1950 ms 399812 KB Output is correct
81 Correct 2029 ms 399948 KB Output is correct
82 Correct 2044 ms 399044 KB Output is correct
83 Correct 2092 ms 398928 KB Output is correct
84 Correct 2166 ms 398812 KB Output is correct
85 Correct 2248 ms 398924 KB Output is correct
86 Correct 1866 ms 398784 KB Output is correct
87 Correct 1082 ms 400580 KB Output is correct
88 Correct 2152 ms 398916 KB Output is correct
89 Correct 2152 ms 398788 KB Output is correct
90 Correct 2088 ms 398928 KB Output is correct
91 Correct 2133 ms 398664 KB Output is correct
92 Correct 948 ms 401692 KB Output is correct
93 Correct 2163 ms 400068 KB Output is correct