#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];
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;
void get_range(int s){
pair<int, int> ret = {-2e9, 2e9};
dsu.init(n);
for (int i=s+1;i<=m;i++){
dsu.merge(E[i].s, E[i].e);
if (dsu.find(E[s].s)==dsu.find(E[s].e)){
ret.second = (E[s].x + E[i].x-1)/2;
break;
}
}
dsu.init(n);
for (int i=s-1;i;i--){
dsu.merge(E[i].s, E[i].e);
if (dsu.find(E[s].s)==dsu.find(E[s].e)){
ret.first = (E[s].x + E[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, E[s].x, 1);
Ev.emplace_back(ret.second+1, -E[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;
DB.push_back(E[i].x);
}
sort(E+1, E+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:115:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
reconstruction.cpp:117:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | scanf("%d %d %d", &E[i].s, &E[i].e, &E[i].x);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:126:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
reconstruction.cpp:129:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3460 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 |
3412 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 |
2 ms |
3456 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3452 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3456 KB |
Output is correct |
17 |
Correct |
2 ms |
3456 KB |
Output is correct |
18 |
Correct |
2 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3460 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 |
3412 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 |
2 ms |
3456 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3452 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3456 KB |
Output is correct |
17 |
Correct |
2 ms |
3456 KB |
Output is correct |
18 |
Correct |
2 ms |
3456 KB |
Output is correct |
19 |
Correct |
2 ms |
3456 KB |
Output is correct |
20 |
Correct |
2142 ms |
9936 KB |
Output is correct |
21 |
Correct |
1209 ms |
9744 KB |
Output is correct |
22 |
Correct |
1301 ms |
9728 KB |
Output is correct |
23 |
Correct |
1546 ms |
9744 KB |
Output is correct |
24 |
Correct |
1959 ms |
9944 KB |
Output is correct |
25 |
Correct |
2246 ms |
9728 KB |
Output is correct |
26 |
Correct |
2149 ms |
9760 KB |
Output is correct |
27 |
Correct |
2167 ms |
9884 KB |
Output is correct |
28 |
Correct |
2132 ms |
9764 KB |
Output is correct |
29 |
Correct |
2189 ms |
6456 KB |
Output is correct |
30 |
Correct |
2161 ms |
9832 KB |
Output is correct |
31 |
Correct |
2199 ms |
9756 KB |
Output is correct |
32 |
Correct |
2183 ms |
9856 KB |
Output is correct |
33 |
Correct |
2179 ms |
9840 KB |
Output is correct |
34 |
Correct |
2114 ms |
5940 KB |
Output is correct |
35 |
Correct |
2224 ms |
9792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1628 ms |
39948 KB |
Output is correct |
5 |
Correct |
1640 ms |
39772 KB |
Output is correct |
6 |
Correct |
1652 ms |
39828 KB |
Output is correct |
7 |
Correct |
985 ms |
41664 KB |
Output is correct |
8 |
Correct |
920 ms |
41836 KB |
Output is correct |
9 |
Correct |
704 ms |
41704 KB |
Output is correct |
10 |
Correct |
1604 ms |
39784 KB |
Output is correct |
11 |
Correct |
871 ms |
41992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3460 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 |
3412 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 |
2 ms |
3456 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3452 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3456 KB |
Output is correct |
17 |
Correct |
2 ms |
3456 KB |
Output is correct |
18 |
Correct |
2 ms |
3456 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
346 ms |
35792 KB |
Output is correct |
21 |
Correct |
362 ms |
35872 KB |
Output is correct |
22 |
Correct |
360 ms |
35840 KB |
Output is correct |
23 |
Correct |
331 ms |
35736 KB |
Output is correct |
24 |
Correct |
327 ms |
35856 KB |
Output is correct |
25 |
Correct |
321 ms |
35728 KB |
Output is correct |
26 |
Correct |
332 ms |
35844 KB |
Output is correct |
27 |
Correct |
335 ms |
35732 KB |
Output is correct |
28 |
Correct |
329 ms |
35692 KB |
Output is correct |
29 |
Correct |
320 ms |
35872 KB |
Output is correct |
30 |
Correct |
323 ms |
35872 KB |
Output is correct |
31 |
Correct |
344 ms |
35772 KB |
Output is correct |
32 |
Correct |
318 ms |
36332 KB |
Output is correct |
33 |
Correct |
370 ms |
35600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3460 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 |
3412 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 |
2 ms |
3456 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3452 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3456 KB |
Output is correct |
17 |
Correct |
2 ms |
3456 KB |
Output is correct |
18 |
Correct |
2 ms |
3456 KB |
Output is correct |
19 |
Correct |
2 ms |
3456 KB |
Output is correct |
20 |
Correct |
2142 ms |
9936 KB |
Output is correct |
21 |
Correct |
1209 ms |
9744 KB |
Output is correct |
22 |
Correct |
1301 ms |
9728 KB |
Output is correct |
23 |
Correct |
1546 ms |
9744 KB |
Output is correct |
24 |
Correct |
1959 ms |
9944 KB |
Output is correct |
25 |
Correct |
2246 ms |
9728 KB |
Output is correct |
26 |
Correct |
2149 ms |
9760 KB |
Output is correct |
27 |
Correct |
2167 ms |
9884 KB |
Output is correct |
28 |
Correct |
2132 ms |
9764 KB |
Output is correct |
29 |
Correct |
2189 ms |
6456 KB |
Output is correct |
30 |
Correct |
2161 ms |
9832 KB |
Output is correct |
31 |
Correct |
2199 ms |
9756 KB |
Output is correct |
32 |
Correct |
2183 ms |
9856 KB |
Output is correct |
33 |
Correct |
2179 ms |
9840 KB |
Output is correct |
34 |
Correct |
2114 ms |
5940 KB |
Output is correct |
35 |
Correct |
2224 ms |
9792 KB |
Output is correct |
36 |
Correct |
2193 ms |
9764 KB |
Output is correct |
37 |
Correct |
1225 ms |
11544 KB |
Output is correct |
38 |
Correct |
1341 ms |
11496 KB |
Output is correct |
39 |
Correct |
1550 ms |
11620 KB |
Output is correct |
40 |
Correct |
1980 ms |
11600 KB |
Output is correct |
41 |
Correct |
2293 ms |
11596 KB |
Output is correct |
42 |
Correct |
2246 ms |
11816 KB |
Output is correct |
43 |
Correct |
2280 ms |
11520 KB |
Output is correct |
44 |
Correct |
2319 ms |
11532 KB |
Output is correct |
45 |
Correct |
2315 ms |
9308 KB |
Output is correct |
46 |
Correct |
2449 ms |
11448 KB |
Output is correct |
47 |
Correct |
2379 ms |
11424 KB |
Output is correct |
48 |
Correct |
2366 ms |
11508 KB |
Output is correct |
49 |
Correct |
2318 ms |
11532 KB |
Output is correct |
50 |
Correct |
2235 ms |
8816 KB |
Output is correct |
51 |
Correct |
2333 ms |
11448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3460 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 |
3412 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 |
2 ms |
3456 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3452 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3456 KB |
Output is correct |
17 |
Correct |
2 ms |
3456 KB |
Output is correct |
18 |
Correct |
2 ms |
3456 KB |
Output is correct |
19 |
Correct |
2 ms |
3456 KB |
Output is correct |
20 |
Correct |
2142 ms |
9936 KB |
Output is correct |
21 |
Correct |
1209 ms |
9744 KB |
Output is correct |
22 |
Correct |
1301 ms |
9728 KB |
Output is correct |
23 |
Correct |
1546 ms |
9744 KB |
Output is correct |
24 |
Correct |
1959 ms |
9944 KB |
Output is correct |
25 |
Correct |
2246 ms |
9728 KB |
Output is correct |
26 |
Correct |
2149 ms |
9760 KB |
Output is correct |
27 |
Correct |
2167 ms |
9884 KB |
Output is correct |
28 |
Correct |
2132 ms |
9764 KB |
Output is correct |
29 |
Correct |
2189 ms |
6456 KB |
Output is correct |
30 |
Correct |
2161 ms |
9832 KB |
Output is correct |
31 |
Correct |
2199 ms |
9756 KB |
Output is correct |
32 |
Correct |
2183 ms |
9856 KB |
Output is correct |
33 |
Correct |
2179 ms |
9840 KB |
Output is correct |
34 |
Correct |
2114 ms |
5940 KB |
Output is correct |
35 |
Correct |
2224 ms |
9792 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 |
1628 ms |
39948 KB |
Output is correct |
40 |
Correct |
1640 ms |
39772 KB |
Output is correct |
41 |
Correct |
1652 ms |
39828 KB |
Output is correct |
42 |
Correct |
985 ms |
41664 KB |
Output is correct |
43 |
Correct |
920 ms |
41836 KB |
Output is correct |
44 |
Correct |
704 ms |
41704 KB |
Output is correct |
45 |
Correct |
1604 ms |
39784 KB |
Output is correct |
46 |
Correct |
871 ms |
41992 KB |
Output is correct |
47 |
Correct |
2 ms |
3412 KB |
Output is correct |
48 |
Correct |
346 ms |
35792 KB |
Output is correct |
49 |
Correct |
362 ms |
35872 KB |
Output is correct |
50 |
Correct |
360 ms |
35840 KB |
Output is correct |
51 |
Correct |
331 ms |
35736 KB |
Output is correct |
52 |
Correct |
327 ms |
35856 KB |
Output is correct |
53 |
Correct |
321 ms |
35728 KB |
Output is correct |
54 |
Correct |
332 ms |
35844 KB |
Output is correct |
55 |
Correct |
335 ms |
35732 KB |
Output is correct |
56 |
Correct |
329 ms |
35692 KB |
Output is correct |
57 |
Correct |
320 ms |
35872 KB |
Output is correct |
58 |
Correct |
323 ms |
35872 KB |
Output is correct |
59 |
Correct |
344 ms |
35772 KB |
Output is correct |
60 |
Correct |
318 ms |
36332 KB |
Output is correct |
61 |
Correct |
370 ms |
35600 KB |
Output is correct |
62 |
Correct |
2193 ms |
9764 KB |
Output is correct |
63 |
Correct |
1225 ms |
11544 KB |
Output is correct |
64 |
Correct |
1341 ms |
11496 KB |
Output is correct |
65 |
Correct |
1550 ms |
11620 KB |
Output is correct |
66 |
Correct |
1980 ms |
11600 KB |
Output is correct |
67 |
Correct |
2293 ms |
11596 KB |
Output is correct |
68 |
Correct |
2246 ms |
11816 KB |
Output is correct |
69 |
Correct |
2280 ms |
11520 KB |
Output is correct |
70 |
Correct |
2319 ms |
11532 KB |
Output is correct |
71 |
Correct |
2315 ms |
9308 KB |
Output is correct |
72 |
Correct |
2449 ms |
11448 KB |
Output is correct |
73 |
Correct |
2379 ms |
11424 KB |
Output is correct |
74 |
Correct |
2366 ms |
11508 KB |
Output is correct |
75 |
Correct |
2318 ms |
11532 KB |
Output is correct |
76 |
Correct |
2235 ms |
8816 KB |
Output is correct |
77 |
Correct |
2333 ms |
11448 KB |
Output is correct |
78 |
Correct |
2644 ms |
46440 KB |
Output is correct |
79 |
Correct |
1635 ms |
48608 KB |
Output is correct |
80 |
Correct |
1744 ms |
47400 KB |
Output is correct |
81 |
Correct |
1935 ms |
47428 KB |
Output is correct |
82 |
Correct |
2284 ms |
46428 KB |
Output is correct |
83 |
Correct |
2648 ms |
46408 KB |
Output is correct |
84 |
Correct |
2592 ms |
45980 KB |
Output is correct |
85 |
Correct |
2549 ms |
45764 KB |
Output is correct |
86 |
Correct |
2463 ms |
45668 KB |
Output is correct |
87 |
Correct |
2444 ms |
44792 KB |
Output is correct |
88 |
Correct |
2496 ms |
42964 KB |
Output is correct |
89 |
Correct |
2574 ms |
42216 KB |
Output is correct |
90 |
Correct |
2478 ms |
42212 KB |
Output is correct |
91 |
Correct |
2519 ms |
42028 KB |
Output is correct |
92 |
Correct |
2452 ms |
42520 KB |
Output is correct |
93 |
Correct |
2472 ms |
43120 KB |
Output is correct |