#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);
cin.tie(0);
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 |
25 ms |
28672 KB |
Output is correct |
2 |
Correct |
25 ms |
28664 KB |
Output is correct |
3 |
Correct |
24 ms |
28544 KB |
Output is correct |
4 |
Correct |
26 ms |
28544 KB |
Output is correct |
5 |
Correct |
25 ms |
28544 KB |
Output is correct |
6 |
Correct |
27 ms |
28636 KB |
Output is correct |
7 |
Correct |
25 ms |
28644 KB |
Output is correct |
8 |
Correct |
30 ms |
28664 KB |
Output is correct |
9 |
Correct |
26 ms |
28544 KB |
Output is correct |
10 |
Correct |
26 ms |
28672 KB |
Output is correct |
11 |
Correct |
28 ms |
28672 KB |
Output is correct |
12 |
Correct |
28 ms |
28672 KB |
Output is correct |
13 |
Correct |
26 ms |
28672 KB |
Output is correct |
14 |
Correct |
27 ms |
28704 KB |
Output is correct |
15 |
Correct |
28 ms |
28672 KB |
Output is correct |
16 |
Correct |
30 ms |
28672 KB |
Output is correct |
17 |
Correct |
26 ms |
28572 KB |
Output is correct |
18 |
Correct |
28 ms |
28664 KB |
Output is correct |
19 |
Correct |
27 ms |
28664 KB |
Output is correct |
20 |
Correct |
28 ms |
28672 KB |
Output is correct |
21 |
Correct |
26 ms |
28664 KB |
Output is correct |
22 |
Correct |
28 ms |
28628 KB |
Output is correct |
23 |
Correct |
28 ms |
28664 KB |
Output is correct |
24 |
Correct |
26 ms |
28672 KB |
Output is correct |
25 |
Correct |
27 ms |
28664 KB |
Output is correct |
26 |
Correct |
27 ms |
28672 KB |
Output is correct |
27 |
Correct |
29 ms |
28668 KB |
Output is correct |
28 |
Correct |
26 ms |
28664 KB |
Output is correct |
29 |
Correct |
30 ms |
28664 KB |
Output is correct |
30 |
Correct |
26 ms |
28644 KB |
Output is correct |
31 |
Correct |
29 ms |
28664 KB |
Output is correct |
32 |
Correct |
28 ms |
28684 KB |
Output is correct |
33 |
Correct |
28 ms |
28756 KB |
Output is correct |
34 |
Correct |
27 ms |
28672 KB |
Output is correct |
35 |
Correct |
25 ms |
28672 KB |
Output is correct |
36 |
Correct |
25 ms |
28672 KB |
Output is correct |
37 |
Correct |
27 ms |
28664 KB |
Output is correct |
38 |
Correct |
26 ms |
28572 KB |
Output is correct |
39 |
Correct |
29 ms |
28672 KB |
Output is correct |
40 |
Correct |
30 ms |
28672 KB |
Output is correct |
41 |
Correct |
29 ms |
28516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
707 ms |
43040 KB |
Output is correct |
2 |
Correct |
754 ms |
42888 KB |
Output is correct |
3 |
Correct |
646 ms |
43000 KB |
Output is correct |
4 |
Correct |
633 ms |
42864 KB |
Output is correct |
5 |
Correct |
834 ms |
43028 KB |
Output is correct |
6 |
Correct |
760 ms |
43100 KB |
Output is correct |
7 |
Correct |
849 ms |
43384 KB |
Output is correct |
8 |
Correct |
733 ms |
43324 KB |
Output is correct |
9 |
Correct |
859 ms |
43000 KB |
Output is correct |
10 |
Correct |
782 ms |
43256 KB |
Output is correct |
11 |
Correct |
825 ms |
43096 KB |
Output is correct |
12 |
Correct |
741 ms |
43216 KB |
Output is correct |
13 |
Correct |
734 ms |
43432 KB |
Output is correct |
14 |
Correct |
31 ms |
28544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
28672 KB |
Output is correct |
2 |
Correct |
25 ms |
28664 KB |
Output is correct |
3 |
Correct |
24 ms |
28544 KB |
Output is correct |
4 |
Correct |
26 ms |
28544 KB |
Output is correct |
5 |
Correct |
25 ms |
28544 KB |
Output is correct |
6 |
Correct |
27 ms |
28636 KB |
Output is correct |
7 |
Correct |
25 ms |
28644 KB |
Output is correct |
8 |
Correct |
30 ms |
28664 KB |
Output is correct |
9 |
Correct |
26 ms |
28544 KB |
Output is correct |
10 |
Correct |
26 ms |
28672 KB |
Output is correct |
11 |
Correct |
28 ms |
28672 KB |
Output is correct |
12 |
Correct |
28 ms |
28672 KB |
Output is correct |
13 |
Correct |
26 ms |
28672 KB |
Output is correct |
14 |
Correct |
27 ms |
28704 KB |
Output is correct |
15 |
Correct |
28 ms |
28672 KB |
Output is correct |
16 |
Correct |
30 ms |
28672 KB |
Output is correct |
17 |
Correct |
26 ms |
28572 KB |
Output is correct |
18 |
Correct |
28 ms |
28664 KB |
Output is correct |
19 |
Correct |
27 ms |
28664 KB |
Output is correct |
20 |
Correct |
28 ms |
28672 KB |
Output is correct |
21 |
Correct |
26 ms |
28664 KB |
Output is correct |
22 |
Correct |
28 ms |
28628 KB |
Output is correct |
23 |
Correct |
28 ms |
28664 KB |
Output is correct |
24 |
Correct |
26 ms |
28672 KB |
Output is correct |
25 |
Correct |
27 ms |
28664 KB |
Output is correct |
26 |
Correct |
27 ms |
28672 KB |
Output is correct |
27 |
Correct |
29 ms |
28668 KB |
Output is correct |
28 |
Correct |
26 ms |
28664 KB |
Output is correct |
29 |
Correct |
30 ms |
28664 KB |
Output is correct |
30 |
Correct |
26 ms |
28644 KB |
Output is correct |
31 |
Correct |
29 ms |
28664 KB |
Output is correct |
32 |
Correct |
28 ms |
28684 KB |
Output is correct |
33 |
Correct |
28 ms |
28756 KB |
Output is correct |
34 |
Correct |
27 ms |
28672 KB |
Output is correct |
35 |
Correct |
25 ms |
28672 KB |
Output is correct |
36 |
Correct |
25 ms |
28672 KB |
Output is correct |
37 |
Correct |
27 ms |
28664 KB |
Output is correct |
38 |
Correct |
26 ms |
28572 KB |
Output is correct |
39 |
Correct |
29 ms |
28672 KB |
Output is correct |
40 |
Correct |
30 ms |
28672 KB |
Output is correct |
41 |
Correct |
29 ms |
28516 KB |
Output is correct |
42 |
Correct |
707 ms |
43040 KB |
Output is correct |
43 |
Correct |
754 ms |
42888 KB |
Output is correct |
44 |
Correct |
646 ms |
43000 KB |
Output is correct |
45 |
Correct |
633 ms |
42864 KB |
Output is correct |
46 |
Correct |
834 ms |
43028 KB |
Output is correct |
47 |
Correct |
760 ms |
43100 KB |
Output is correct |
48 |
Correct |
849 ms |
43384 KB |
Output is correct |
49 |
Correct |
733 ms |
43324 KB |
Output is correct |
50 |
Correct |
859 ms |
43000 KB |
Output is correct |
51 |
Correct |
782 ms |
43256 KB |
Output is correct |
52 |
Correct |
825 ms |
43096 KB |
Output is correct |
53 |
Correct |
741 ms |
43216 KB |
Output is correct |
54 |
Correct |
734 ms |
43432 KB |
Output is correct |
55 |
Correct |
31 ms |
28544 KB |
Output is correct |
56 |
Correct |
724 ms |
44512 KB |
Output is correct |
57 |
Correct |
702 ms |
44552 KB |
Output is correct |
58 |
Correct |
765 ms |
44644 KB |
Output is correct |
59 |
Correct |
761 ms |
44700 KB |
Output is correct |
60 |
Correct |
855 ms |
44612 KB |
Output is correct |
61 |
Correct |
793 ms |
44652 KB |
Output is correct |
62 |
Correct |
772 ms |
44708 KB |
Output is correct |
63 |
Correct |
828 ms |
44664 KB |
Output is correct |
64 |
Correct |
756 ms |
44648 KB |
Output is correct |
65 |
Correct |
786 ms |
44564 KB |
Output is correct |
66 |
Correct |
769 ms |
44664 KB |
Output is correct |
67 |
Correct |
851 ms |
44920 KB |
Output is correct |
68 |
Correct |
784 ms |
44492 KB |
Output is correct |
69 |
Correct |
780 ms |
44780 KB |
Output is correct |
70 |
Correct |
725 ms |
44528 KB |
Output is correct |
71 |
Correct |
701 ms |
44372 KB |
Output is correct |
72 |
Correct |
743 ms |
44508 KB |
Output is correct |
73 |
Correct |
723 ms |
44792 KB |
Output is correct |
74 |
Correct |
795 ms |
44792 KB |
Output is correct |
75 |
Correct |
744 ms |
44836 KB |
Output is correct |
76 |
Correct |
824 ms |
44860 KB |
Output is correct |
77 |
Correct |
30 ms |
28544 KB |
Output is correct |