#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
const int T = 1'000'000'000;
struct node
{
ll pL = 0, pR = 2 * T, ansR = 0, w = 0, posL = 0, sz = 0;
bool non = 1;
ll pos(ll k)
{
if (k <= pL)
return posL;
else if (k >= pR)
return posL + (pR - pL);
return posL + (k - pL);
}
ll weight(ll k)
{
assert(ansR <= pR);
if (k >= ansR)
return k - ansR + w;
return w;
}
void increase(int t)
{
posL -= t, pL -= t, pR -= t, ansR -= t;
}
};
node merge(node n1, node n2)
{
node res;
if (n1.non)
return n2;
if (n2.non)
return n1;
n2.increase(n1.sz);
if (n1.posL + n1.pR - n1.pL < n2.pL)
res = node{n1.ansR, n1.ansR, n1.ansR};
else if (n2.pR < n1.posL)
res = node{n1.ansR, n1.ansR, n1.ansR};
else
{
if (n1.posL <= n2.pL)
res.pL = n1.pL + n2.pL - n1.posL;
else
res.pL = n1.pL;
if (n1.posL + n1.pR - n1.pL >= n2.pR)
res.pR = n1.pL + n2.pR - n1.posL;
else
res.pR = n1.pR;
}
if (n2.ansR <= n1.posL + (n1.pR - n1.pL))
res.ansR = min(n1.ansR, max(0LL, n2.ansR - n1.posL) + n1.pL);
else
res.ansR = n1.ansR;
res.w = n1.weight(res.ansR) + n2.weight(n1.pos(res.ansR));
res.posL = n2.pos(n1.pos(res.pL));
res.sz = n1.sz + n2.sz;
res.non = 0;
return res;
}
int N, Q, pre[300005];
struct segTree
{
vector<node> seg;
// i: [L, R)
void modify(int pos, node n, int i = 1, int L = 0, int R = N)
{
if (R - L == 1)
seg[i] = n;
else
{
int mid = (L + R) / 2;
if (pos < mid)
modify(pos, n, i << 1, L, mid);
else
modify(pos, n, i << 1 | 1, mid, R);
seg[i] = merge(seg[i << 1], seg[i << 1 | 1]);
}
}
node query(int qL, int qR, int i = 1, int L = 0, int R = N)
{
if (qL <= L && R <= qR)
return seg[i];
else if (qR <= L || R <= qL)
return node{};
else
{
int mid = (L + R) / 2;
return merge(query(qL, qR, i << 1, L, mid),
query(qL, qR, i << 1 | 1, mid, R));
}
}
} Rseg, Lseg;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> Q;
Rseg.seg.resize(1200005);
Lseg.seg.resize(1200005);
for (int i = 1; i < N; i++)
{
int L, R;
cin >> L >> R;
Rseg.modify(i, node{L, R - 1, R - 1, 0, L, 1, 0});
Lseg.modify(N - i, node{L, R - 1, R - 1, 0, L, 1, 0});
}
for (int i = 1; i <= Q; i++)
{
int T, A, B, C, D;
cin >> T;
if (T == 1)
{
cin >> A >> B >> C;
Rseg.modify(A, node{B, C - 1, C - 1, 0, B, 1, 0});
Lseg.modify(N - A, node{B, C - 1, C - 1, 0, B, 1, 0});
}
else
{
cin >> A >> B >> C >> D;
if (A < C)
{
// Rseg.query(A, C).debug();
// merge(node{B, B, B, 0, B, 0}, Rseg.query(A, C)).debug();
// merge(merge(node{B, B, B, 0, B, 0}, Rseg.query(A, C)), node{D, D, D, 0, D, 0}).debug();
cout << merge(merge(node{B, B, B, 0, B, 0, 0}, Rseg.query(A, C)), node{D, D, D, 0, D, 0, 0}).w << '\n';
}
else if (C < A)
cout << merge(merge(node{B, B, B, 0, B, 0, 0}, Lseg.query(N - A + 1, N - C + 1)), node{D, D, D, 0, D, 0, 0}).w << '\n';
else
cout << max(0LL, B - D) << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
131788 KB |
Output is correct |
2 |
Correct |
56 ms |
131816 KB |
Output is correct |
3 |
Correct |
51 ms |
131748 KB |
Output is correct |
4 |
Correct |
54 ms |
131804 KB |
Output is correct |
5 |
Correct |
54 ms |
131788 KB |
Output is correct |
6 |
Correct |
55 ms |
131716 KB |
Output is correct |
7 |
Correct |
56 ms |
131732 KB |
Output is correct |
8 |
Correct |
55 ms |
131848 KB |
Output is correct |
9 |
Correct |
53 ms |
131732 KB |
Output is correct |
10 |
Correct |
54 ms |
131852 KB |
Output is correct |
11 |
Correct |
59 ms |
131824 KB |
Output is correct |
12 |
Correct |
59 ms |
131884 KB |
Output is correct |
13 |
Correct |
64 ms |
131800 KB |
Output is correct |
14 |
Correct |
56 ms |
131796 KB |
Output is correct |
15 |
Correct |
56 ms |
131776 KB |
Output is correct |
16 |
Correct |
56 ms |
131772 KB |
Output is correct |
17 |
Correct |
57 ms |
131872 KB |
Output is correct |
18 |
Correct |
64 ms |
131880 KB |
Output is correct |
19 |
Correct |
59 ms |
131780 KB |
Output is correct |
20 |
Correct |
62 ms |
131792 KB |
Output is correct |
21 |
Correct |
56 ms |
131836 KB |
Output is correct |
22 |
Correct |
55 ms |
131812 KB |
Output is correct |
23 |
Correct |
60 ms |
131756 KB |
Output is correct |
24 |
Correct |
55 ms |
131916 KB |
Output is correct |
25 |
Correct |
57 ms |
131840 KB |
Output is correct |
26 |
Correct |
52 ms |
131792 KB |
Output is correct |
27 |
Correct |
54 ms |
131788 KB |
Output is correct |
28 |
Correct |
57 ms |
131764 KB |
Output is correct |
29 |
Correct |
53 ms |
131832 KB |
Output is correct |
30 |
Correct |
57 ms |
131824 KB |
Output is correct |
31 |
Correct |
72 ms |
131820 KB |
Output is correct |
32 |
Correct |
62 ms |
131820 KB |
Output is correct |
33 |
Correct |
61 ms |
131784 KB |
Output is correct |
34 |
Correct |
54 ms |
131816 KB |
Output is correct |
35 |
Correct |
54 ms |
131848 KB |
Output is correct |
36 |
Correct |
61 ms |
131816 KB |
Output is correct |
37 |
Correct |
69 ms |
131780 KB |
Output is correct |
38 |
Correct |
60 ms |
131876 KB |
Output is correct |
39 |
Correct |
59 ms |
131844 KB |
Output is correct |
40 |
Correct |
58 ms |
131792 KB |
Output is correct |
41 |
Correct |
53 ms |
131708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1126 ms |
139556 KB |
Output is correct |
2 |
Correct |
939 ms |
138340 KB |
Output is correct |
3 |
Correct |
928 ms |
138344 KB |
Output is correct |
4 |
Correct |
896 ms |
138344 KB |
Output is correct |
5 |
Correct |
1031 ms |
138256 KB |
Output is correct |
6 |
Correct |
947 ms |
138180 KB |
Output is correct |
7 |
Correct |
967 ms |
138228 KB |
Output is correct |
8 |
Correct |
979 ms |
138240 KB |
Output is correct |
9 |
Correct |
932 ms |
138184 KB |
Output is correct |
10 |
Correct |
971 ms |
138216 KB |
Output is correct |
11 |
Correct |
973 ms |
137900 KB |
Output is correct |
12 |
Correct |
988 ms |
137756 KB |
Output is correct |
13 |
Correct |
956 ms |
137504 KB |
Output is correct |
14 |
Correct |
62 ms |
131792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
131788 KB |
Output is correct |
2 |
Correct |
56 ms |
131816 KB |
Output is correct |
3 |
Correct |
51 ms |
131748 KB |
Output is correct |
4 |
Correct |
54 ms |
131804 KB |
Output is correct |
5 |
Correct |
54 ms |
131788 KB |
Output is correct |
6 |
Correct |
55 ms |
131716 KB |
Output is correct |
7 |
Correct |
56 ms |
131732 KB |
Output is correct |
8 |
Correct |
55 ms |
131848 KB |
Output is correct |
9 |
Correct |
53 ms |
131732 KB |
Output is correct |
10 |
Correct |
54 ms |
131852 KB |
Output is correct |
11 |
Correct |
59 ms |
131824 KB |
Output is correct |
12 |
Correct |
59 ms |
131884 KB |
Output is correct |
13 |
Correct |
64 ms |
131800 KB |
Output is correct |
14 |
Correct |
56 ms |
131796 KB |
Output is correct |
15 |
Correct |
56 ms |
131776 KB |
Output is correct |
16 |
Correct |
56 ms |
131772 KB |
Output is correct |
17 |
Correct |
57 ms |
131872 KB |
Output is correct |
18 |
Correct |
64 ms |
131880 KB |
Output is correct |
19 |
Correct |
59 ms |
131780 KB |
Output is correct |
20 |
Correct |
62 ms |
131792 KB |
Output is correct |
21 |
Correct |
56 ms |
131836 KB |
Output is correct |
22 |
Correct |
55 ms |
131812 KB |
Output is correct |
23 |
Correct |
60 ms |
131756 KB |
Output is correct |
24 |
Correct |
55 ms |
131916 KB |
Output is correct |
25 |
Correct |
57 ms |
131840 KB |
Output is correct |
26 |
Correct |
52 ms |
131792 KB |
Output is correct |
27 |
Correct |
54 ms |
131788 KB |
Output is correct |
28 |
Correct |
57 ms |
131764 KB |
Output is correct |
29 |
Correct |
53 ms |
131832 KB |
Output is correct |
30 |
Correct |
57 ms |
131824 KB |
Output is correct |
31 |
Correct |
72 ms |
131820 KB |
Output is correct |
32 |
Correct |
62 ms |
131820 KB |
Output is correct |
33 |
Correct |
61 ms |
131784 KB |
Output is correct |
34 |
Correct |
54 ms |
131816 KB |
Output is correct |
35 |
Correct |
54 ms |
131848 KB |
Output is correct |
36 |
Correct |
61 ms |
131816 KB |
Output is correct |
37 |
Correct |
69 ms |
131780 KB |
Output is correct |
38 |
Correct |
60 ms |
131876 KB |
Output is correct |
39 |
Correct |
59 ms |
131844 KB |
Output is correct |
40 |
Correct |
58 ms |
131792 KB |
Output is correct |
41 |
Correct |
53 ms |
131708 KB |
Output is correct |
42 |
Correct |
1126 ms |
139556 KB |
Output is correct |
43 |
Correct |
939 ms |
138340 KB |
Output is correct |
44 |
Correct |
928 ms |
138344 KB |
Output is correct |
45 |
Correct |
896 ms |
138344 KB |
Output is correct |
46 |
Correct |
1031 ms |
138256 KB |
Output is correct |
47 |
Correct |
947 ms |
138180 KB |
Output is correct |
48 |
Correct |
967 ms |
138228 KB |
Output is correct |
49 |
Correct |
979 ms |
138240 KB |
Output is correct |
50 |
Correct |
932 ms |
138184 KB |
Output is correct |
51 |
Correct |
971 ms |
138216 KB |
Output is correct |
52 |
Correct |
973 ms |
137900 KB |
Output is correct |
53 |
Correct |
988 ms |
137756 KB |
Output is correct |
54 |
Correct |
956 ms |
137504 KB |
Output is correct |
55 |
Correct |
62 ms |
131792 KB |
Output is correct |
56 |
Correct |
1093 ms |
141644 KB |
Output is correct |
57 |
Correct |
1034 ms |
141928 KB |
Output is correct |
58 |
Correct |
1128 ms |
141504 KB |
Output is correct |
59 |
Correct |
1091 ms |
141504 KB |
Output is correct |
60 |
Correct |
1043 ms |
141504 KB |
Output is correct |
61 |
Correct |
1134 ms |
141556 KB |
Output is correct |
62 |
Correct |
1129 ms |
141084 KB |
Output is correct |
63 |
Correct |
1155 ms |
141108 KB |
Output is correct |
64 |
Correct |
1086 ms |
140772 KB |
Output is correct |
65 |
Correct |
1062 ms |
140748 KB |
Output is correct |
66 |
Correct |
1093 ms |
140864 KB |
Output is correct |
67 |
Correct |
1163 ms |
140568 KB |
Output is correct |
68 |
Correct |
1059 ms |
140924 KB |
Output is correct |
69 |
Correct |
1095 ms |
140768 KB |
Output is correct |
70 |
Correct |
1113 ms |
140444 KB |
Output is correct |
71 |
Correct |
1031 ms |
140440 KB |
Output is correct |
72 |
Correct |
1126 ms |
140204 KB |
Output is correct |
73 |
Correct |
1115 ms |
140148 KB |
Output is correct |
74 |
Correct |
1184 ms |
140080 KB |
Output is correct |
75 |
Correct |
1174 ms |
139864 KB |
Output is correct |
76 |
Correct |
1179 ms |
139724 KB |
Output is correct |
77 |
Correct |
65 ms |
131804 KB |
Output is correct |