#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Nmax = 3e5 + 5, inf = 2e9;
int L[Nmax], R[Nmax], tip[Nmax];
int t1[Nmax], t2[Nmax], node1[Nmax], node2[Nmax], id[Nmax], newL[Nmax], newR[Nmax];
int n, q;
ll ans[Nmax];
namespace prepare
{
void Swap()
{
int i;
for(i=1; i<=(n-1)/2; ++i) swap(L[n-i], L[i]), swap(R[n-i], R[i]);
for(i=1; i<=q; ++i)
if(tip[i] == 1) id[i] = n - id[i];
else node1[i] = n+1-node1[i], node2[i] = n+1-node2[i];
}
void Do()
{
int i;
for(i=1; i<n; ++i) L[i] -= i, R[i] -= i + 1;
for(i=1; i<=q; ++i)
if(tip[i] == 1) newL[i] -= id[i], newR[i] -= id[i] + 1;
else if(node1[i] <= node2[i]) t1[i] -= node1[i], t2[i] -= node2[i];
}
void Undo()
{
int i;
for(i=1; i<n; ++i) L[i] += i, R[i] += i + 1;
for(i=1; i<=q; ++i)
if(tip[i] == 1) newL[i] += id[i], newR[i] += id[i] + 1;
else if(node1[i] <= node2[i]) t1[i] += node1[i], t2[i] += node2[i];
}
}
struct tam
{
int l, r, to;
ll cost;
tam() { l = -2e9; r = 2e9; cost = 0; to = inf; }
tam(int L, int R)
{
l = L; r = R; cost = 0; to = inf;
}
tam(int L, int R, ll Cost, int To)
{
l = L, r = R; cost = Cost; to = To;
}
};
int par(int x, int l, int r)
{
return (x > r ? x - r : 0);
}
int modf(int x, int l, int r)
{
if(x > r) return r;
if(x < l) return l;
return x;
}
tam comb(tam a, tam b)
{
if(a.to != inf)
{
if(b.to != inf)
return tam(a.l, a.r, a.cost + b.cost + par(a.to, b.l, b.r), b.to);
else
return tam(a.l, a.r, a.cost + b.cost + par(a.to, b.l, b.r), modf(a.to, b.l, b.r));
}
else
{
if(a.l > b.r)
{
a.to = a.r = a.l;
return comb(a, b);
}
else if(b.l > a.r)
{
a.to = a.l = a.r;
return comb(a, b);
}
b.l = max(a.l, b.l);
b.r = min(a.r, b.r);
assert(b.l <= b.r);
return b;
}
}
#define mid ((st+dr)>>1)
#define left_son ((node<<1))
#define right_son ((node<<1)|1)
class Segtree
{
tam a[Nmax<<2];
public:
void build(int node, int st, int dr)
{
if(st == dr)
{
a[node] = tam(L[st], R[st]);
return;
}
build(left_son, st, mid);
build(right_son, mid+1, dr);
a[node] = comb(a[left_son], a[right_son]);
}
void update(int node, int st, int dr, int pos, int L, int R)
{
if(st == dr)
{
a[node] = tam(L, R);
return;
}
if(pos <= mid) update(left_son, st, mid, pos, L, R);
else update(right_son, mid+1, dr, pos, L, R);
a[node] = comb(a[left_son], a[right_son]);
}
tam query(int node, int st, int dr, int Left, int Right)
{
if(dr < Left || Right < st) return tam();
if(Left <= st && dr <= Right) return a[node];
return comb(query(left_son, st, mid, Left, Right), query(right_son, mid+1, dr, Left, Right));
}
} aint;
void solve()
{
prepare :: Do();
if(n>1) aint.build(1, 1, n-1);
int i;
for(i=1; i<=q; ++i)
{
if(tip[i] == 1)
{
if(n>1) aint.update(1, 1, n-1, id[i], newL[i], newR[i]);
continue;
}
if(node1[i] > node2[i]) continue;
tam res = aint.query(1, 1, n-1, node1[i], node2[i] - 1);
tam start(t1[i], t1[i]);
tam finish(t2[i], t2[i]);
res = comb(comb(start, res), finish);
ans[i] = res.cost;
}
prepare :: Undo();
}
int main()
{
// freopen("input", "r", stdin);
// freopen("output", "w", stdout);
cin.sync_with_stdio(false);
// cerr << sizeof(tam) * Nmax * 4 << '\n';
int i;
cin >> n >> q;
for(i=1; i<n; ++i) cin >> L[i] >> R[i];
for(i=1; i<=q; ++i)
{
cin >> tip[i];
if(tip[i] == 1) cin >> id[i] >> newL[i] >> newR[i];
else cin >> node1[i] >> t1[i] >> node2[i] >> t2[i];
}
solve();
prepare :: Swap();
solve();
for(i=1; i<=q; ++i)
if(tip[i] == 2) cout << ans[i] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
28672 KB |
Output is correct |
2 |
Correct |
25 ms |
28664 KB |
Output is correct |
3 |
Correct |
25 ms |
28672 KB |
Output is correct |
4 |
Correct |
27 ms |
28540 KB |
Output is correct |
5 |
Correct |
24 ms |
28544 KB |
Output is correct |
6 |
Correct |
25 ms |
28544 KB |
Output is correct |
7 |
Correct |
25 ms |
28536 KB |
Output is correct |
8 |
Correct |
76 ms |
28664 KB |
Output is correct |
9 |
Correct |
25 ms |
28544 KB |
Output is correct |
10 |
Correct |
26 ms |
28536 KB |
Output is correct |
11 |
Correct |
26 ms |
28612 KB |
Output is correct |
12 |
Correct |
27 ms |
28664 KB |
Output is correct |
13 |
Correct |
26 ms |
28664 KB |
Output is correct |
14 |
Correct |
26 ms |
28664 KB |
Output is correct |
15 |
Correct |
26 ms |
28672 KB |
Output is correct |
16 |
Correct |
27 ms |
28664 KB |
Output is correct |
17 |
Correct |
29 ms |
28664 KB |
Output is correct |
18 |
Correct |
27 ms |
28664 KB |
Output is correct |
19 |
Correct |
27 ms |
28664 KB |
Output is correct |
20 |
Correct |
26 ms |
28672 KB |
Output is correct |
21 |
Correct |
28 ms |
28672 KB |
Output is correct |
22 |
Correct |
27 ms |
28664 KB |
Output is correct |
23 |
Correct |
27 ms |
28664 KB |
Output is correct |
24 |
Correct |
25 ms |
28672 KB |
Output is correct |
25 |
Correct |
29 ms |
28664 KB |
Output is correct |
26 |
Correct |
29 ms |
28664 KB |
Output is correct |
27 |
Correct |
33 ms |
28672 KB |
Output is correct |
28 |
Correct |
26 ms |
28672 KB |
Output is correct |
29 |
Correct |
29 ms |
28672 KB |
Output is correct |
30 |
Correct |
33 ms |
28664 KB |
Output is correct |
31 |
Correct |
35 ms |
28716 KB |
Output is correct |
32 |
Correct |
29 ms |
28580 KB |
Output is correct |
33 |
Correct |
31 ms |
28724 KB |
Output is correct |
34 |
Correct |
28 ms |
28632 KB |
Output is correct |
35 |
Correct |
26 ms |
28664 KB |
Output is correct |
36 |
Correct |
27 ms |
28672 KB |
Output is correct |
37 |
Correct |
27 ms |
28664 KB |
Output is correct |
38 |
Correct |
28 ms |
28672 KB |
Output is correct |
39 |
Correct |
34 ms |
28720 KB |
Output is correct |
40 |
Correct |
30 ms |
28672 KB |
Output is correct |
41 |
Correct |
29 ms |
28536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
775 ms |
43036 KB |
Output is correct |
2 |
Correct |
715 ms |
43000 KB |
Output is correct |
3 |
Correct |
765 ms |
42956 KB |
Output is correct |
4 |
Correct |
808 ms |
42892 KB |
Output is correct |
5 |
Correct |
855 ms |
43128 KB |
Output is correct |
6 |
Correct |
794 ms |
42980 KB |
Output is correct |
7 |
Correct |
791 ms |
43228 KB |
Output is correct |
8 |
Correct |
724 ms |
43384 KB |
Output is correct |
9 |
Correct |
765 ms |
42904 KB |
Output is correct |
10 |
Correct |
721 ms |
43320 KB |
Output is correct |
11 |
Correct |
794 ms |
43128 KB |
Output is correct |
12 |
Correct |
817 ms |
43384 KB |
Output is correct |
13 |
Correct |
764 ms |
43256 KB |
Output is correct |
14 |
Correct |
26 ms |
28544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
28672 KB |
Output is correct |
2 |
Correct |
25 ms |
28664 KB |
Output is correct |
3 |
Correct |
25 ms |
28672 KB |
Output is correct |
4 |
Correct |
27 ms |
28540 KB |
Output is correct |
5 |
Correct |
24 ms |
28544 KB |
Output is correct |
6 |
Correct |
25 ms |
28544 KB |
Output is correct |
7 |
Correct |
25 ms |
28536 KB |
Output is correct |
8 |
Correct |
76 ms |
28664 KB |
Output is correct |
9 |
Correct |
25 ms |
28544 KB |
Output is correct |
10 |
Correct |
26 ms |
28536 KB |
Output is correct |
11 |
Correct |
26 ms |
28612 KB |
Output is correct |
12 |
Correct |
27 ms |
28664 KB |
Output is correct |
13 |
Correct |
26 ms |
28664 KB |
Output is correct |
14 |
Correct |
26 ms |
28664 KB |
Output is correct |
15 |
Correct |
26 ms |
28672 KB |
Output is correct |
16 |
Correct |
27 ms |
28664 KB |
Output is correct |
17 |
Correct |
29 ms |
28664 KB |
Output is correct |
18 |
Correct |
27 ms |
28664 KB |
Output is correct |
19 |
Correct |
27 ms |
28664 KB |
Output is correct |
20 |
Correct |
26 ms |
28672 KB |
Output is correct |
21 |
Correct |
28 ms |
28672 KB |
Output is correct |
22 |
Correct |
27 ms |
28664 KB |
Output is correct |
23 |
Correct |
27 ms |
28664 KB |
Output is correct |
24 |
Correct |
25 ms |
28672 KB |
Output is correct |
25 |
Correct |
29 ms |
28664 KB |
Output is correct |
26 |
Correct |
29 ms |
28664 KB |
Output is correct |
27 |
Correct |
33 ms |
28672 KB |
Output is correct |
28 |
Correct |
26 ms |
28672 KB |
Output is correct |
29 |
Correct |
29 ms |
28672 KB |
Output is correct |
30 |
Correct |
33 ms |
28664 KB |
Output is correct |
31 |
Correct |
35 ms |
28716 KB |
Output is correct |
32 |
Correct |
29 ms |
28580 KB |
Output is correct |
33 |
Correct |
31 ms |
28724 KB |
Output is correct |
34 |
Correct |
28 ms |
28632 KB |
Output is correct |
35 |
Correct |
26 ms |
28664 KB |
Output is correct |
36 |
Correct |
27 ms |
28672 KB |
Output is correct |
37 |
Correct |
27 ms |
28664 KB |
Output is correct |
38 |
Correct |
28 ms |
28672 KB |
Output is correct |
39 |
Correct |
34 ms |
28720 KB |
Output is correct |
40 |
Correct |
30 ms |
28672 KB |
Output is correct |
41 |
Correct |
29 ms |
28536 KB |
Output is correct |
42 |
Correct |
775 ms |
43036 KB |
Output is correct |
43 |
Correct |
715 ms |
43000 KB |
Output is correct |
44 |
Correct |
765 ms |
42956 KB |
Output is correct |
45 |
Correct |
808 ms |
42892 KB |
Output is correct |
46 |
Correct |
855 ms |
43128 KB |
Output is correct |
47 |
Correct |
794 ms |
42980 KB |
Output is correct |
48 |
Correct |
791 ms |
43228 KB |
Output is correct |
49 |
Correct |
724 ms |
43384 KB |
Output is correct |
50 |
Correct |
765 ms |
42904 KB |
Output is correct |
51 |
Correct |
721 ms |
43320 KB |
Output is correct |
52 |
Correct |
794 ms |
43128 KB |
Output is correct |
53 |
Correct |
817 ms |
43384 KB |
Output is correct |
54 |
Correct |
764 ms |
43256 KB |
Output is correct |
55 |
Correct |
26 ms |
28544 KB |
Output is correct |
56 |
Correct |
791 ms |
59004 KB |
Output is correct |
57 |
Correct |
739 ms |
58616 KB |
Output is correct |
58 |
Correct |
757 ms |
59404 KB |
Output is correct |
59 |
Correct |
790 ms |
59512 KB |
Output is correct |
60 |
Correct |
772 ms |
58872 KB |
Output is correct |
61 |
Correct |
936 ms |
59740 KB |
Output is correct |
62 |
Correct |
810 ms |
59472 KB |
Output is correct |
63 |
Correct |
807 ms |
59484 KB |
Output is correct |
64 |
Correct |
736 ms |
59576 KB |
Output is correct |
65 |
Correct |
856 ms |
59256 KB |
Output is correct |
66 |
Correct |
809 ms |
59256 KB |
Output is correct |
67 |
Correct |
731 ms |
59416 KB |
Output is correct |
68 |
Correct |
714 ms |
58524 KB |
Output is correct |
69 |
Correct |
715 ms |
59760 KB |
Output is correct |
70 |
Correct |
697 ms |
58744 KB |
Output is correct |
71 |
Correct |
748 ms |
58076 KB |
Output is correct |
72 |
Correct |
830 ms |
58392 KB |
Output is correct |
73 |
Correct |
742 ms |
59064 KB |
Output is correct |
74 |
Correct |
725 ms |
59256 KB |
Output is correct |
75 |
Correct |
824 ms |
59256 KB |
Output is correct |
76 |
Correct |
816 ms |
59896 KB |
Output is correct |
77 |
Correct |
26 ms |
28544 KB |
Output is correct |