답안 #44629

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
44629 2018-04-04T02:28:03 Z imaxblue Construction of Highway (JOI18_construction) C++17
100 / 100
520 ms 25904 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define p3i pair<pii, int>
#define pll pair<ll, ll>
#define p3l pair<pll, ll>
#define vi vector<int>
#define vpii vector<pii>
#define vp3i vector<p3i>
#define vpll vector<pll>
#define vp3l vector<p3l>
#define lseg L, (L+R)/2, N*2+1
#define rseg (L+R)/2+1, R, N*2+2
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define MN 1000000007
#define fox(k, x) for (int k=0; k<x; ++k)
#define fox1(k, x) for (int k=1; k<=x; ++k)
#define foxr(k, x) for (int k=x-1; k>=0; --k)
#define fox1r(k, x) for (int k=x; k>0; --k)
#define ms multiset
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
#define rng() ((rand() << 14)+rand())
#define scan(X) do{while((X=getchar())<'0'); for(X-='0'; '0'<=(_=getchar()); X=(X<<3)+(X<<1)+_-'0');}while(0)
char _;
#define pi 3.14159265358979323846

int n, a[100005], b[100005], c[100005];
vector<int> v[100005];
int sub[100005], bit[100005];

void add(int P, int V){
    for(; P<=100002; P+=P&-P){
        bit[P]+=V;
    }
}
int sum(int P, int C=0){
    for(; P>0; P-=P&-P) C+=bit[P];
    return C;
}
struct line{
    set<pii> s;
    int par;
    line(int P){
        par=P;
        s=set<pii>();
    }
};
line*lines[100005];
int pos[100005];
set<pii>::iterator i, j;
void mod(int N, int V){
    if (N==-1) return;
    line* L=lines[N];
    //cout << "*" << pos[N] << ' '<< N << ' ' << V << endl;
    while(1){
        i=L->s.ub(mp(pos[N], (1<<30)));
        if (i==L->s.begin()) break;
        --i;
        L->s.erase(i);
    }
    L->s.insert(mp(pos[N], V));
    mod(L->par, V);
}
int dis=0, sz;
vector<pii> res;
void query(int N){
    if (N==-1) return;
    line* L=lines[N];
    i=L->s.lb(mp(pos[N], 0));
    if (i!=L->s.begin()){
        j=i; --j;
        sz=pos[N]-j->x;
        if (res.size() && res.back().x==i->y) res.back().y+=sz;
        else res.push_back(mp(i->y, sz));
        --i;
        for (; ; --i){
            if (i==L->s.begin()){
                sz=i->x;
            } else {
                j=i; --j;
                sz=i->x-j->x;
            }
            if (res.size() && res.back().x==i->y) res.back().y+=sz;
            else res.push_back(mp(i->y, sz));
            if (i==L->s.begin()) break;
        }
    } else {
        sz=pos[N];
        if (res.size() && res.back().x==i->y) res.back().y+=sz;
        else res.push_back(mp(i->y, sz));
    }
    query(L->par);
}
void get(int A, int B){
    ll ans=0;
    res.clear();
    query(A);
    //cout << A << ' ' << B << endl;
    fox(l, res.size()){
        ans+=1LL*sum(res[l].x-1)*res[l].y;
        add(res[l].x, res[l].y);
        //cout << res[l].x << ':' << res[l].y << ' ';
    }
    fox(l, res.size()){
        add(res[l].x, -res[l].y);
    }
    //cout << endl;
    mod(B, c[B]);
    printf("%lli\n", ans);
}
void dfs0(int N){
    sub[N]=1;
    fox(l, v[N].size()){
        dfs0(v[N][l]);
        sub[N]+=sub[v[N][l]];
    }
}
void dfs(int N, line*L, int P){
    int best=0;
    pos[N]=P; lines[N]=L;
    fox(l, v[N].size()){
        if (sub[v[N][l]]>sub[v[N][best]]) best=l;
    }
    fox(l, v[N].size()){
        if (l!=best){
            line*tmp=new line(N);
            dfs(v[N][l], tmp, 1);
        } else {
            dfs(v[N][l], L, P+1);
        }
    }
}
vector<pii> com;
int main(){
    scanf("%i", &n);
    fox1(l, n){
        scanf("%i", c+l);
        com.pb(mp(c[l], l));
    }
    sort(com.begin(), com.end());
    int p=0;
    fox(l, n){
        if (l==0 || com[l].x!=com[l-1].x) ++p;
        c[com[l].y]=p;
    }
    fox(l, n-1){
        scanf("%i%i", a+l, b+l);
        v[a[l]].pb(b[l]);
    }
    dfs0(1);
    line*tmp=new line(-1);
    dfs(1, tmp, 1);
    mod(1, c[1]);
    fox(l, n-1){
        //cout << a[l] << ' '<< b[l] << ' ' << c[b[l]] << endl;
        get(a[l], b[l]);
        //if (l==20) break;
    }
    return 0;
}
/*
15
1 1 1 2 1 2 2 1 2 2 2 2 1 2 2
1 2
1 3
2 4
4 5
1 6
4 7
2 8
4 9
5 10
9 11
8 12
5 13
10 14
9 15

10
1 1 1 2 1 2 2 1 2 2
1 2
1 3
2 4
4 5
1 6
4 7
2 8
4 9
5 10
*/

Compilation message

construction.cpp: In function 'void get(int, int)':
construction.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fox(k, x) for (int k=0; k<x; ++k)
construction.cpp:107:9:
     fox(l, res.size()){
         ~~~~~~~~~~~~~             
construction.cpp:107:5: note: in expansion of macro 'fox'
     fox(l, res.size()){
     ^~~
construction.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fox(k, x) for (int k=0; k<x; ++k)
construction.cpp:112:9:
     fox(l, res.size()){
         ~~~~~~~~~~~~~             
construction.cpp:112:5: note: in expansion of macro 'fox'
     fox(l, res.size()){
     ^~~
construction.cpp: In function 'void dfs0(int)':
construction.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fox(k, x) for (int k=0; k<x; ++k)
construction.cpp:121:9:
     fox(l, v[N].size()){
         ~~~~~~~~~~~~~~            
construction.cpp:121:5: note: in expansion of macro 'fox'
     fox(l, v[N].size()){
     ^~~
construction.cpp: In function 'void dfs(int, line*, int)':
construction.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fox(k, x) for (int k=0; k<x; ++k)
construction.cpp:129:9:
     fox(l, v[N].size()){
         ~~~~~~~~~~~~~~            
construction.cpp:129:5: note: in expansion of macro 'fox'
     fox(l, v[N].size()){
     ^~~
construction.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fox(k, x) for (int k=0; k<x; ++k)
construction.cpp:132:9:
     fox(l, v[N].size()){
         ~~~~~~~~~~~~~~            
construction.cpp:132:5: note: in expansion of macro 'fox'
     fox(l, v[N].size()){
     ^~~
construction.cpp: In function 'int main()':
construction.cpp:143:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i", &n);
     ~~~~~^~~~~~~~~~
construction.cpp:145:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i", c+l);
         ~~~~~^~~~~~~~~~~
construction.cpp:155:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i%i", a+l, b+l);
         ~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 3 ms 2792 KB Output is correct
3 Correct 4 ms 2792 KB Output is correct
4 Correct 4 ms 2864 KB Output is correct
5 Correct 6 ms 2892 KB Output is correct
6 Correct 5 ms 2892 KB Output is correct
7 Correct 4 ms 2944 KB Output is correct
8 Correct 5 ms 2944 KB Output is correct
9 Correct 5 ms 2944 KB Output is correct
10 Correct 4 ms 2992 KB Output is correct
11 Correct 5 ms 2992 KB Output is correct
12 Correct 4 ms 3004 KB Output is correct
13 Correct 4 ms 3132 KB Output is correct
14 Correct 4 ms 3132 KB Output is correct
15 Correct 4 ms 3132 KB Output is correct
16 Correct 4 ms 3132 KB Output is correct
17 Correct 6 ms 3132 KB Output is correct
18 Correct 5 ms 3132 KB Output is correct
19 Correct 4 ms 3132 KB Output is correct
20 Correct 4 ms 3132 KB Output is correct
21 Correct 4 ms 3132 KB Output is correct
22 Correct 4 ms 3132 KB Output is correct
23 Correct 5 ms 3132 KB Output is correct
24 Correct 5 ms 3132 KB Output is correct
25 Correct 5 ms 3132 KB Output is correct
26 Correct 4 ms 3132 KB Output is correct
27 Correct 4 ms 3132 KB Output is correct
28 Correct 5 ms 3132 KB Output is correct
29 Correct 4 ms 3156 KB Output is correct
30 Correct 5 ms 3156 KB Output is correct
31 Correct 4 ms 3156 KB Output is correct
32 Correct 4 ms 3156 KB Output is correct
33 Correct 4 ms 3156 KB Output is correct
34 Correct 5 ms 3156 KB Output is correct
35 Correct 4 ms 3156 KB Output is correct
36 Correct 6 ms 3156 KB Output is correct
37 Correct 4 ms 3156 KB Output is correct
38 Correct 4 ms 3156 KB Output is correct
39 Correct 4 ms 3156 KB Output is correct
40 Correct 5 ms 3156 KB Output is correct
41 Correct 5 ms 3156 KB Output is correct
42 Correct 4 ms 3156 KB Output is correct
43 Correct 5 ms 3156 KB Output is correct
44 Correct 4 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 3 ms 2792 KB Output is correct
3 Correct 4 ms 2792 KB Output is correct
4 Correct 4 ms 2864 KB Output is correct
5 Correct 6 ms 2892 KB Output is correct
6 Correct 5 ms 2892 KB Output is correct
7 Correct 4 ms 2944 KB Output is correct
8 Correct 5 ms 2944 KB Output is correct
9 Correct 5 ms 2944 KB Output is correct
10 Correct 4 ms 2992 KB Output is correct
11 Correct 5 ms 2992 KB Output is correct
12 Correct 4 ms 3004 KB Output is correct
13 Correct 4 ms 3132 KB Output is correct
14 Correct 4 ms 3132 KB Output is correct
15 Correct 4 ms 3132 KB Output is correct
16 Correct 4 ms 3132 KB Output is correct
17 Correct 6 ms 3132 KB Output is correct
18 Correct 5 ms 3132 KB Output is correct
19 Correct 4 ms 3132 KB Output is correct
20 Correct 4 ms 3132 KB Output is correct
21 Correct 4 ms 3132 KB Output is correct
22 Correct 4 ms 3132 KB Output is correct
23 Correct 5 ms 3132 KB Output is correct
24 Correct 5 ms 3132 KB Output is correct
25 Correct 5 ms 3132 KB Output is correct
26 Correct 4 ms 3132 KB Output is correct
27 Correct 4 ms 3132 KB Output is correct
28 Correct 5 ms 3132 KB Output is correct
29 Correct 4 ms 3156 KB Output is correct
30 Correct 5 ms 3156 KB Output is correct
31 Correct 4 ms 3156 KB Output is correct
32 Correct 4 ms 3156 KB Output is correct
33 Correct 4 ms 3156 KB Output is correct
34 Correct 5 ms 3156 KB Output is correct
35 Correct 4 ms 3156 KB Output is correct
36 Correct 6 ms 3156 KB Output is correct
37 Correct 4 ms 3156 KB Output is correct
38 Correct 4 ms 3156 KB Output is correct
39 Correct 4 ms 3156 KB Output is correct
40 Correct 5 ms 3156 KB Output is correct
41 Correct 5 ms 3156 KB Output is correct
42 Correct 4 ms 3156 KB Output is correct
43 Correct 5 ms 3156 KB Output is correct
44 Correct 4 ms 3156 KB Output is correct
45 Correct 6 ms 3156 KB Output is correct
46 Correct 15 ms 3580 KB Output is correct
47 Correct 11 ms 3580 KB Output is correct
48 Correct 11 ms 3580 KB Output is correct
49 Correct 12 ms 3868 KB Output is correct
50 Correct 8 ms 3884 KB Output is correct
51 Correct 11 ms 3884 KB Output is correct
52 Correct 12 ms 3884 KB Output is correct
53 Correct 8 ms 3884 KB Output is correct
54 Correct 9 ms 3884 KB Output is correct
55 Correct 8 ms 3884 KB Output is correct
56 Correct 8 ms 3884 KB Output is correct
57 Correct 13 ms 3884 KB Output is correct
58 Correct 13 ms 3884 KB Output is correct
59 Correct 13 ms 3884 KB Output is correct
60 Correct 13 ms 3884 KB Output is correct
61 Correct 9 ms 3884 KB Output is correct
62 Correct 11 ms 3884 KB Output is correct
63 Correct 9 ms 3884 KB Output is correct
64 Correct 13 ms 3884 KB Output is correct
65 Correct 11 ms 3884 KB Output is correct
66 Correct 16 ms 3884 KB Output is correct
67 Correct 11 ms 3884 KB Output is correct
68 Correct 11 ms 3884 KB Output is correct
69 Correct 12 ms 3924 KB Output is correct
70 Correct 11 ms 4016 KB Output is correct
71 Correct 12 ms 4016 KB Output is correct
72 Correct 19 ms 4016 KB Output is correct
73 Correct 18 ms 4016 KB Output is correct
74 Correct 12 ms 4016 KB Output is correct
75 Correct 8 ms 4016 KB Output is correct
76 Correct 8 ms 4016 KB Output is correct
77 Correct 9 ms 4016 KB Output is correct
78 Correct 11 ms 4016 KB Output is correct
79 Correct 8 ms 4016 KB Output is correct
80 Correct 8 ms 4016 KB Output is correct
81 Correct 9 ms 4016 KB Output is correct
82 Correct 9 ms 4016 KB Output is correct
83 Correct 12 ms 4016 KB Output is correct
84 Correct 12 ms 4016 KB Output is correct
85 Correct 8 ms 4016 KB Output is correct
86 Correct 9 ms 4016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 3 ms 2792 KB Output is correct
3 Correct 4 ms 2792 KB Output is correct
4 Correct 4 ms 2864 KB Output is correct
5 Correct 6 ms 2892 KB Output is correct
6 Correct 5 ms 2892 KB Output is correct
7 Correct 4 ms 2944 KB Output is correct
8 Correct 5 ms 2944 KB Output is correct
9 Correct 5 ms 2944 KB Output is correct
10 Correct 4 ms 2992 KB Output is correct
11 Correct 5 ms 2992 KB Output is correct
12 Correct 4 ms 3004 KB Output is correct
13 Correct 4 ms 3132 KB Output is correct
14 Correct 4 ms 3132 KB Output is correct
15 Correct 4 ms 3132 KB Output is correct
16 Correct 4 ms 3132 KB Output is correct
17 Correct 6 ms 3132 KB Output is correct
18 Correct 5 ms 3132 KB Output is correct
19 Correct 4 ms 3132 KB Output is correct
20 Correct 4 ms 3132 KB Output is correct
21 Correct 4 ms 3132 KB Output is correct
22 Correct 4 ms 3132 KB Output is correct
23 Correct 5 ms 3132 KB Output is correct
24 Correct 5 ms 3132 KB Output is correct
25 Correct 5 ms 3132 KB Output is correct
26 Correct 4 ms 3132 KB Output is correct
27 Correct 4 ms 3132 KB Output is correct
28 Correct 5 ms 3132 KB Output is correct
29 Correct 4 ms 3156 KB Output is correct
30 Correct 5 ms 3156 KB Output is correct
31 Correct 4 ms 3156 KB Output is correct
32 Correct 4 ms 3156 KB Output is correct
33 Correct 4 ms 3156 KB Output is correct
34 Correct 5 ms 3156 KB Output is correct
35 Correct 4 ms 3156 KB Output is correct
36 Correct 6 ms 3156 KB Output is correct
37 Correct 4 ms 3156 KB Output is correct
38 Correct 4 ms 3156 KB Output is correct
39 Correct 4 ms 3156 KB Output is correct
40 Correct 5 ms 3156 KB Output is correct
41 Correct 5 ms 3156 KB Output is correct
42 Correct 4 ms 3156 KB Output is correct
43 Correct 5 ms 3156 KB Output is correct
44 Correct 4 ms 3156 KB Output is correct
45 Correct 6 ms 3156 KB Output is correct
46 Correct 15 ms 3580 KB Output is correct
47 Correct 11 ms 3580 KB Output is correct
48 Correct 11 ms 3580 KB Output is correct
49 Correct 12 ms 3868 KB Output is correct
50 Correct 8 ms 3884 KB Output is correct
51 Correct 11 ms 3884 KB Output is correct
52 Correct 12 ms 3884 KB Output is correct
53 Correct 8 ms 3884 KB Output is correct
54 Correct 9 ms 3884 KB Output is correct
55 Correct 8 ms 3884 KB Output is correct
56 Correct 8 ms 3884 KB Output is correct
57 Correct 13 ms 3884 KB Output is correct
58 Correct 13 ms 3884 KB Output is correct
59 Correct 13 ms 3884 KB Output is correct
60 Correct 13 ms 3884 KB Output is correct
61 Correct 9 ms 3884 KB Output is correct
62 Correct 11 ms 3884 KB Output is correct
63 Correct 9 ms 3884 KB Output is correct
64 Correct 13 ms 3884 KB Output is correct
65 Correct 11 ms 3884 KB Output is correct
66 Correct 16 ms 3884 KB Output is correct
67 Correct 11 ms 3884 KB Output is correct
68 Correct 11 ms 3884 KB Output is correct
69 Correct 12 ms 3924 KB Output is correct
70 Correct 11 ms 4016 KB Output is correct
71 Correct 12 ms 4016 KB Output is correct
72 Correct 19 ms 4016 KB Output is correct
73 Correct 18 ms 4016 KB Output is correct
74 Correct 12 ms 4016 KB Output is correct
75 Correct 8 ms 4016 KB Output is correct
76 Correct 8 ms 4016 KB Output is correct
77 Correct 9 ms 4016 KB Output is correct
78 Correct 11 ms 4016 KB Output is correct
79 Correct 8 ms 4016 KB Output is correct
80 Correct 8 ms 4016 KB Output is correct
81 Correct 9 ms 4016 KB Output is correct
82 Correct 9 ms 4016 KB Output is correct
83 Correct 12 ms 4016 KB Output is correct
84 Correct 12 ms 4016 KB Output is correct
85 Correct 8 ms 4016 KB Output is correct
86 Correct 9 ms 4016 KB Output is correct
87 Correct 25 ms 4284 KB Output is correct
88 Correct 115 ms 6584 KB Output is correct
89 Correct 314 ms 14896 KB Output is correct
90 Correct 358 ms 15004 KB Output is correct
91 Correct 307 ms 15004 KB Output is correct
92 Correct 207 ms 25900 KB Output is correct
93 Correct 180 ms 25904 KB Output is correct
94 Correct 153 ms 25904 KB Output is correct
95 Correct 187 ms 25904 KB Output is correct
96 Correct 225 ms 25904 KB Output is correct
97 Correct 230 ms 25904 KB Output is correct
98 Correct 205 ms 25904 KB Output is correct
99 Correct 160 ms 25904 KB Output is correct
100 Correct 500 ms 25904 KB Output is correct
101 Correct 453 ms 25904 KB Output is correct
102 Correct 517 ms 25904 KB Output is correct
103 Correct 520 ms 25904 KB Output is correct
104 Correct 201 ms 25904 KB Output is correct
105 Correct 197 ms 25904 KB Output is correct
106 Correct 179 ms 25904 KB Output is correct
107 Correct 300 ms 25904 KB Output is correct
108 Correct 332 ms 25904 KB Output is correct
109 Correct 285 ms 25904 KB Output is correct
110 Correct 153 ms 25904 KB Output is correct
111 Correct 205 ms 25904 KB Output is correct
112 Correct 227 ms 25904 KB Output is correct
113 Correct 178 ms 25904 KB Output is correct
114 Correct 370 ms 25904 KB Output is correct
115 Correct 481 ms 25904 KB Output is correct
116 Correct 188 ms 25904 KB Output is correct
117 Correct 140 ms 25904 KB Output is correct
118 Correct 140 ms 25904 KB Output is correct
119 Correct 200 ms 25904 KB Output is correct
120 Correct 136 ms 25904 KB Output is correct
121 Correct 129 ms 25904 KB Output is correct
122 Correct 143 ms 25904 KB Output is correct
123 Correct 156 ms 25904 KB Output is correct
124 Correct 187 ms 25904 KB Output is correct
125 Correct 228 ms 25904 KB Output is correct
126 Correct 166 ms 25904 KB Output is correct
127 Correct 216 ms 25904 KB Output is correct
128 Correct 192 ms 25904 KB Output is correct