#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
struct Seg{
pair<ll, ll> tree[200200];
int sz;
void init(int n){sz = n;}
pair<ll, ll> combine(pair<ll, ll> a, pair<ll, ll> b){
return {a.first + b.first, a.second + b.second};
}
void update(int p, int x){
p += sz;
if (x>0) tree[p].first += x, tree[p].second++;
else tree[p].first += x, tree[p].second--;
for (p>>=1;p;p>>=1) tree[p] = combine(tree[p<<1], tree[p<<1|1]);
}
pair<ll, ll> query(int l, int r){
++r;
pair<ll, ll> ret = {0, 0};
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1) ret = combine(ret, tree[l++]);
if (r&1) ret = combine(ret, tree[--r]);
}
return ret;
}
}tree;
ll ans[1001000];
vector<ll> DB;
void compress(){
sort(DB.begin(), DB.end());
DB.erase(unique(DB.begin(), DB.end()), DB.end());
}
int getidx(ll x){return lower_bound(DB.begin(), DB.end(), x) - DB.begin();}
ll x0;
ll myabs(ll x){
if (x<0) return -x;
return x;
}
struct Edge{
int s, e, x, i;
Edge(){}
Edge(int _s, int _e, int _x, int _i): s(_s), e(_e), x(_x), i(_i) {}
bool operator < (const Edge &E)const{
if (myabs(x0-x) == myabs(x0-E.x)){
if (x==E.x) return i < E.i;
return x < E.x;
}
return myabs(x0-x) < myabs(x0-E.x);
}
}E[100100], E0[100100];
struct DSU{
int path[505];
void init(int n){for (int i=1;i<=n;i++) path[i] = i;}
int find(int s){
if (s==path[s]) return s;
return path[s] = find(path[s]);
}
bool merge(int s, int v){
s = find(s), v = find(v);
if (s==v) return 0;
path[s] = v;
return 1;
}
}dsu;
struct Event{
int x, val, typ;
Event(){}
Event(int _x, int _val, int _typ): x(_x), val(_val), typ(_typ) {}
bool operator<(const Event &E) const{
if (x==E.x){
return typ < E.typ;
}
return x < E.x;
}
};
vector<Event> Ev;
int n, m;
bool solve(int s){
sort(E+1, E+m+1);
dsu.init(n);
for (int i=1;i<=m;i++){
if (E[i].i==s){
if (dsu.merge(E[i].s, E[i].e)) return 1;
return 0;
}
dsu.merge(E[i].s, E[i].e);
}
exit(1);
}
void get_range(int s){
pair<int, int> ret = {-2e9, 2e9};
dsu.init(n);
for (int i=s+1;i<=m;i++){
dsu.merge(E0[i].s, E0[i].e);
if (dsu.find(E0[s].s)==dsu.find(E0[s].e)){
ret.second = (E0[s].x + E0[i].x-1)/2;
break;
}
}
dsu.init(n);
for (int i=s-1;i;i--){
dsu.merge(E0[i].s, E0[i].e);
if (dsu.find(E0[s].s)==dsu.find(E0[s].e)){
ret.first = (E0[s].x + E0[i].x+1)/2;
break;
}
}
if (ret.first <= ret.second){
//printf("%d %d %d -> [%d, %d]\n", E0[s].s, E0[s].e, E0[s].x, ret.first, ret.second);
Ev.emplace_back(ret.first, E0[s].x, 1);
Ev.emplace_back(ret.second+1, -E0[s].x, 1);
}
}
int main(){
scanf("%d %d", &n, &m);
for (int i=1;i<=m;i++){
scanf("%d %d %d", &E[i].s, &E[i].e, &E[i].x);
E[i].i = i;
E0[i] = E[i];
DB.push_back(E[i].x);
}
sort(E0+1, E0+m+1);
compress();
int q;
scanf("%d", &q);
for (int i=1;i<=q;i++){
int x;
scanf("%d", &x);
Ev.emplace_back(x, i, 2);
}
for (int i=1;i<=m;i++) get_range(i);
sort(Ev.begin(), Ev.end());
tree.init(m+1);
for (auto &[x, val, typ]:Ev){
if (typ==1){
int pos = getidx(myabs(val));
tree.update(pos, val);
}
else if (typ==2){
int pos = getidx(x);
auto [F, fc] = tree.query(0, pos-1); auto [B, bc] = tree.query(pos, m);
ans[val] = (B - bc*x) + (fc*x - F);
}
}
for (int i=1;i<=q;i++) printf("%lld\n", ans[i]);
return 0;
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:127:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
reconstruction.cpp:129:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | scanf("%d %d %d", &E[i].s, &E[i].e, &E[i].x);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:139:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
reconstruction.cpp:142:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3448 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
1 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3444 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3452 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3444 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3448 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
1 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3444 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3452 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3444 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2150 ms |
11720 KB |
Output is correct |
21 |
Correct |
1179 ms |
12876 KB |
Output is correct |
22 |
Correct |
1290 ms |
13040 KB |
Output is correct |
23 |
Correct |
1505 ms |
12940 KB |
Output is correct |
24 |
Correct |
1953 ms |
12816 KB |
Output is correct |
25 |
Correct |
2233 ms |
12928 KB |
Output is correct |
26 |
Correct |
2155 ms |
13060 KB |
Output is correct |
27 |
Correct |
2180 ms |
12800 KB |
Output is correct |
28 |
Correct |
2160 ms |
12936 KB |
Output is correct |
29 |
Correct |
2158 ms |
9648 KB |
Output is correct |
30 |
Correct |
2185 ms |
12896 KB |
Output is correct |
31 |
Correct |
2159 ms |
12876 KB |
Output is correct |
32 |
Correct |
2173 ms |
12968 KB |
Output is correct |
33 |
Correct |
2143 ms |
12880 KB |
Output is correct |
34 |
Correct |
2110 ms |
9152 KB |
Output is correct |
35 |
Correct |
2163 ms |
12860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
1606 ms |
41904 KB |
Output is correct |
5 |
Correct |
1652 ms |
44316 KB |
Output is correct |
6 |
Correct |
1567 ms |
44324 KB |
Output is correct |
7 |
Correct |
1018 ms |
46152 KB |
Output is correct |
8 |
Correct |
852 ms |
46220 KB |
Output is correct |
9 |
Correct |
698 ms |
46244 KB |
Output is correct |
10 |
Correct |
1531 ms |
44304 KB |
Output is correct |
11 |
Correct |
846 ms |
46064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3448 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
1 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3444 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3452 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3444 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
325 ms |
37408 KB |
Output is correct |
21 |
Correct |
329 ms |
37572 KB |
Output is correct |
22 |
Correct |
314 ms |
37552 KB |
Output is correct |
23 |
Correct |
326 ms |
37448 KB |
Output is correct |
24 |
Correct |
336 ms |
37428 KB |
Output is correct |
25 |
Correct |
344 ms |
37520 KB |
Output is correct |
26 |
Correct |
320 ms |
37448 KB |
Output is correct |
27 |
Correct |
318 ms |
37444 KB |
Output is correct |
28 |
Correct |
366 ms |
37532 KB |
Output is correct |
29 |
Correct |
360 ms |
37372 KB |
Output is correct |
30 |
Correct |
330 ms |
37360 KB |
Output is correct |
31 |
Correct |
334 ms |
37052 KB |
Output is correct |
32 |
Correct |
316 ms |
37504 KB |
Output is correct |
33 |
Correct |
324 ms |
36656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3448 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
1 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3444 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3452 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3444 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2150 ms |
11720 KB |
Output is correct |
21 |
Correct |
1179 ms |
12876 KB |
Output is correct |
22 |
Correct |
1290 ms |
13040 KB |
Output is correct |
23 |
Correct |
1505 ms |
12940 KB |
Output is correct |
24 |
Correct |
1953 ms |
12816 KB |
Output is correct |
25 |
Correct |
2233 ms |
12928 KB |
Output is correct |
26 |
Correct |
2155 ms |
13060 KB |
Output is correct |
27 |
Correct |
2180 ms |
12800 KB |
Output is correct |
28 |
Correct |
2160 ms |
12936 KB |
Output is correct |
29 |
Correct |
2158 ms |
9648 KB |
Output is correct |
30 |
Correct |
2185 ms |
12896 KB |
Output is correct |
31 |
Correct |
2159 ms |
12876 KB |
Output is correct |
32 |
Correct |
2173 ms |
12968 KB |
Output is correct |
33 |
Correct |
2143 ms |
12880 KB |
Output is correct |
34 |
Correct |
2110 ms |
9152 KB |
Output is correct |
35 |
Correct |
2163 ms |
12860 KB |
Output is correct |
36 |
Correct |
2177 ms |
13048 KB |
Output is correct |
37 |
Correct |
1195 ms |
13072 KB |
Output is correct |
38 |
Correct |
1282 ms |
13144 KB |
Output is correct |
39 |
Correct |
1491 ms |
13108 KB |
Output is correct |
40 |
Correct |
1925 ms |
13076 KB |
Output is correct |
41 |
Correct |
2270 ms |
13168 KB |
Output is correct |
42 |
Correct |
2200 ms |
13064 KB |
Output is correct |
43 |
Correct |
2159 ms |
13136 KB |
Output is correct |
44 |
Correct |
2144 ms |
13100 KB |
Output is correct |
45 |
Correct |
2114 ms |
10940 KB |
Output is correct |
46 |
Correct |
2191 ms |
13280 KB |
Output is correct |
47 |
Correct |
2165 ms |
13152 KB |
Output is correct |
48 |
Correct |
2166 ms |
13120 KB |
Output is correct |
49 |
Correct |
2202 ms |
13112 KB |
Output is correct |
50 |
Correct |
2121 ms |
10388 KB |
Output is correct |
51 |
Correct |
2207 ms |
13144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3448 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
1 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3444 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3452 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3444 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2150 ms |
11720 KB |
Output is correct |
21 |
Correct |
1179 ms |
12876 KB |
Output is correct |
22 |
Correct |
1290 ms |
13040 KB |
Output is correct |
23 |
Correct |
1505 ms |
12940 KB |
Output is correct |
24 |
Correct |
1953 ms |
12816 KB |
Output is correct |
25 |
Correct |
2233 ms |
12928 KB |
Output is correct |
26 |
Correct |
2155 ms |
13060 KB |
Output is correct |
27 |
Correct |
2180 ms |
12800 KB |
Output is correct |
28 |
Correct |
2160 ms |
12936 KB |
Output is correct |
29 |
Correct |
2158 ms |
9648 KB |
Output is correct |
30 |
Correct |
2185 ms |
12896 KB |
Output is correct |
31 |
Correct |
2159 ms |
12876 KB |
Output is correct |
32 |
Correct |
2173 ms |
12968 KB |
Output is correct |
33 |
Correct |
2143 ms |
12880 KB |
Output is correct |
34 |
Correct |
2110 ms |
9152 KB |
Output is correct |
35 |
Correct |
2163 ms |
12860 KB |
Output is correct |
36 |
Correct |
2 ms |
3412 KB |
Output is correct |
37 |
Correct |
2 ms |
3412 KB |
Output is correct |
38 |
Correct |
2 ms |
3412 KB |
Output is correct |
39 |
Correct |
1606 ms |
41904 KB |
Output is correct |
40 |
Correct |
1652 ms |
44316 KB |
Output is correct |
41 |
Correct |
1567 ms |
44324 KB |
Output is correct |
42 |
Correct |
1018 ms |
46152 KB |
Output is correct |
43 |
Correct |
852 ms |
46220 KB |
Output is correct |
44 |
Correct |
698 ms |
46244 KB |
Output is correct |
45 |
Correct |
1531 ms |
44304 KB |
Output is correct |
46 |
Correct |
846 ms |
46064 KB |
Output is correct |
47 |
Correct |
2 ms |
3412 KB |
Output is correct |
48 |
Correct |
325 ms |
37408 KB |
Output is correct |
49 |
Correct |
329 ms |
37572 KB |
Output is correct |
50 |
Correct |
314 ms |
37552 KB |
Output is correct |
51 |
Correct |
326 ms |
37448 KB |
Output is correct |
52 |
Correct |
336 ms |
37428 KB |
Output is correct |
53 |
Correct |
344 ms |
37520 KB |
Output is correct |
54 |
Correct |
320 ms |
37448 KB |
Output is correct |
55 |
Correct |
318 ms |
37444 KB |
Output is correct |
56 |
Correct |
366 ms |
37532 KB |
Output is correct |
57 |
Correct |
360 ms |
37372 KB |
Output is correct |
58 |
Correct |
330 ms |
37360 KB |
Output is correct |
59 |
Correct |
334 ms |
37052 KB |
Output is correct |
60 |
Correct |
316 ms |
37504 KB |
Output is correct |
61 |
Correct |
324 ms |
36656 KB |
Output is correct |
62 |
Correct |
2177 ms |
13048 KB |
Output is correct |
63 |
Correct |
1195 ms |
13072 KB |
Output is correct |
64 |
Correct |
1282 ms |
13144 KB |
Output is correct |
65 |
Correct |
1491 ms |
13108 KB |
Output is correct |
66 |
Correct |
1925 ms |
13076 KB |
Output is correct |
67 |
Correct |
2270 ms |
13168 KB |
Output is correct |
68 |
Correct |
2200 ms |
13064 KB |
Output is correct |
69 |
Correct |
2159 ms |
13136 KB |
Output is correct |
70 |
Correct |
2144 ms |
13100 KB |
Output is correct |
71 |
Correct |
2114 ms |
10940 KB |
Output is correct |
72 |
Correct |
2191 ms |
13280 KB |
Output is correct |
73 |
Correct |
2165 ms |
13152 KB |
Output is correct |
74 |
Correct |
2166 ms |
13120 KB |
Output is correct |
75 |
Correct |
2202 ms |
13112 KB |
Output is correct |
76 |
Correct |
2121 ms |
10388 KB |
Output is correct |
77 |
Correct |
2207 ms |
13144 KB |
Output is correct |
78 |
Correct |
2548 ms |
51576 KB |
Output is correct |
79 |
Correct |
1554 ms |
53572 KB |
Output is correct |
80 |
Correct |
1654 ms |
52512 KB |
Output is correct |
81 |
Correct |
1904 ms |
52396 KB |
Output is correct |
82 |
Correct |
2293 ms |
51580 KB |
Output is correct |
83 |
Correct |
2618 ms |
51644 KB |
Output is correct |
84 |
Correct |
2574 ms |
51564 KB |
Output is correct |
85 |
Correct |
2562 ms |
51468 KB |
Output is correct |
86 |
Correct |
2504 ms |
51484 KB |
Output is correct |
87 |
Correct |
2468 ms |
50880 KB |
Output is correct |
88 |
Correct |
2587 ms |
51452 KB |
Output is correct |
89 |
Correct |
2676 ms |
51372 KB |
Output is correct |
90 |
Correct |
2643 ms |
51524 KB |
Output is correct |
91 |
Correct |
2648 ms |
51312 KB |
Output is correct |
92 |
Correct |
2528 ms |
52060 KB |
Output is correct |
93 |
Correct |
2520 ms |
52588 KB |
Output is correct |