#include <bits/stdc++.h>
#define pii pair<int, int>
#define ti3 tuple<int, int, int>
#define ti4 tuple<int, int, int, int>
#define int long long
// lalalalalalala, you flood into me, slowly from over the horizon ~ Seunghee, The Fifth Season (SSFWL)
using namespace std;
struct dat{
int lTime, rTime, timeUsed, powers;
dat(): lTime(0), rTime(0), timeUsed(0), powers(0){}
dat(int a, int b, int c, int d): lTime(a), rTime(b), timeUsed(c), powers(d){}
dat operator +(const dat &oth){
dat ans;
ans.timeUsed = timeUsed + oth.timeUsed;
ans.powers = powers + oth.powers;
int lrch = lTime + timeUsed, rrch = rTime + timeUsed;
if (max(lrch, oth.lTime) <= min(rrch, oth.rTime)){
// in some interval on left node, we can reach the right node on time.
ans.lTime = max(lrch, oth.lTime) - timeUsed;
ans.rTime = min(rrch, oth.rTime) - timeUsed;
} else if (oth.lTime > rrch){
// right node opens too late, need to wait
// better for interval to start as late as possible, bcos waiting better than using powers
ans.lTime = rTime;
ans.rTime = rTime;
ans.timeUsed += oth.lTime - rrch;
} else {
// right node closes too early, need to use powers
// better for interval to start as early as possible to use less powers
ans.powers += lrch - oth.rTime;
ans.timeUsed -= lrch - oth.rTime;
ans.lTime = ans.rTime = lTime;
}
return ans;
}
};
vector<pii> v;
struct node{
int s, e, m;
dat lft, rgt;
node *l, *r;
node(int s, int e): s(s), e(e), m((s+e)/2){
if (s == e){
lft = dat(v[s].first, v[s].second, 1, 0);
rgt = dat(v[s].first, v[s].second, 1, 0);
} else {
l = new node(s, m);
r = new node(m+1, e);
lft = l->lft + r->lft;
rgt = r->rgt + l->rgt;
}
}
void upd(int idx){
if (s == e){
lft = dat(v[s].first, v[s].second, 1, 0);
rgt = dat(v[s].first, v[s].second, 1, 0);
return;
} else if (idx <= m) l->upd(idx);
else r->upd(idx);
lft = l->lft + r->lft;
rgt = r->rgt + l->rgt;
}
dat query(int x, int y, bool islft){
if (s==x && e==y) return (islft?lft:rgt);
else if (y <= m) return l->query(x, y, islft);
else if (x > m) return r->query(x, y, islft);
else {
if (islft) return l->query(x, m, islft) + r->query(m+1, y, islft);
else return r->query(m+1, y, islft) + l->query(x, m, islft);
}
}
} *root;
main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n, q; cin >> n >> q;
v.push_back({69, 420}); // dummy
for (int i = 1; i < n; i++){
int a, b; cin >> a >> b;
v.push_back({a, b-1});
}
if (n > 1) root = new node(1, n-1);
while (q--){
int t; cin >> t;
if (t == 1){
int x, a, b; cin >> x >> a >> b;
v[x] = {a, b-1};
if (n > 1) root->upd(x);
} else {
int st, l, en, r; cin >> st >> l >> en >> r;
if (st == en){
if (r < l) cout << l-r << '\n';
else cout << "0\n";
continue;
}
bool islft = true;
if (en < st){
swap(en, st); //swap(l, r);
islft = false;
}
dat ans = root->query(st, en-1, islft);
int st_time = max(ans.lTime, l);
//cout << ans.timeUsed << endl;
if (ans.rTime < l){
ans.powers += l-ans.rTime;
st_time = ans.rTime;
}
int end_time = st_time + ans.timeUsed;
if (r < end_time){
ans.powers += end_time - r;
}
cout << ans.powers << '\n';
}
}
}
Compilation message
timeleap.cpp:73:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
73 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
2 ms |
560 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
480 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
500 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
500 KB |
Output is correct |
28 |
Correct |
1 ms |
580 KB |
Output is correct |
29 |
Correct |
1 ms |
572 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
1 ms |
588 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
2 ms |
588 KB |
Output is correct |
36 |
Correct |
2 ms |
584 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
2 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
580 KB |
Output is correct |
41 |
Correct |
0 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
663 ms |
84500 KB |
Output is correct |
2 |
Correct |
625 ms |
80404 KB |
Output is correct |
3 |
Correct |
611 ms |
81184 KB |
Output is correct |
4 |
Correct |
614 ms |
79024 KB |
Output is correct |
5 |
Correct |
671 ms |
84272 KB |
Output is correct |
6 |
Correct |
727 ms |
83024 KB |
Output is correct |
7 |
Correct |
742 ms |
85324 KB |
Output is correct |
8 |
Correct |
702 ms |
88764 KB |
Output is correct |
9 |
Correct |
674 ms |
79736 KB |
Output is correct |
10 |
Correct |
718 ms |
85124 KB |
Output is correct |
11 |
Correct |
706 ms |
84452 KB |
Output is correct |
12 |
Correct |
744 ms |
89792 KB |
Output is correct |
13 |
Correct |
740 ms |
91032 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
2 ms |
560 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
480 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
500 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
500 KB |
Output is correct |
28 |
Correct |
1 ms |
580 KB |
Output is correct |
29 |
Correct |
1 ms |
572 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
1 ms |
588 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
2 ms |
588 KB |
Output is correct |
36 |
Correct |
2 ms |
584 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
2 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
580 KB |
Output is correct |
41 |
Correct |
0 ms |
312 KB |
Output is correct |
42 |
Correct |
663 ms |
84500 KB |
Output is correct |
43 |
Correct |
625 ms |
80404 KB |
Output is correct |
44 |
Correct |
611 ms |
81184 KB |
Output is correct |
45 |
Correct |
614 ms |
79024 KB |
Output is correct |
46 |
Correct |
671 ms |
84272 KB |
Output is correct |
47 |
Correct |
727 ms |
83024 KB |
Output is correct |
48 |
Correct |
742 ms |
85324 KB |
Output is correct |
49 |
Correct |
702 ms |
88764 KB |
Output is correct |
50 |
Correct |
674 ms |
79736 KB |
Output is correct |
51 |
Correct |
718 ms |
85124 KB |
Output is correct |
52 |
Correct |
706 ms |
84452 KB |
Output is correct |
53 |
Correct |
744 ms |
89792 KB |
Output is correct |
54 |
Correct |
740 ms |
91032 KB |
Output is correct |
55 |
Correct |
0 ms |
204 KB |
Output is correct |
56 |
Correct |
581 ms |
80712 KB |
Output is correct |
57 |
Correct |
546 ms |
75572 KB |
Output is correct |
58 |
Correct |
575 ms |
82644 KB |
Output is correct |
59 |
Correct |
571 ms |
83216 KB |
Output is correct |
60 |
Correct |
554 ms |
77728 KB |
Output is correct |
61 |
Correct |
573 ms |
85552 KB |
Output is correct |
62 |
Correct |
558 ms |
85116 KB |
Output is correct |
63 |
Correct |
562 ms |
84812 KB |
Output is correct |
64 |
Correct |
573 ms |
85632 KB |
Output is correct |
65 |
Correct |
559 ms |
81556 KB |
Output is correct |
66 |
Correct |
556 ms |
81984 KB |
Output is correct |
67 |
Correct |
576 ms |
85304 KB |
Output is correct |
68 |
Correct |
556 ms |
78512 KB |
Output is correct |
69 |
Correct |
573 ms |
87468 KB |
Output is correct |
70 |
Correct |
542 ms |
78260 KB |
Output is correct |
71 |
Correct |
568 ms |
75076 KB |
Output is correct |
72 |
Correct |
557 ms |
78044 KB |
Output is correct |
73 |
Correct |
581 ms |
85572 KB |
Output is correct |
74 |
Correct |
612 ms |
86040 KB |
Output is correct |
75 |
Correct |
577 ms |
87272 KB |
Output is correct |
76 |
Correct |
579 ms |
87968 KB |
Output is correct |
77 |
Correct |
1 ms |
204 KB |
Output is correct |