#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
struct zigzag
{
bool isline;
int height; // if line
ll addtoall;
int a, b; // if not line
vector<pair<pair<int, int>, pair<bool, int> > > add; // first pair range of elements, 2nd bool as to whether to add current loc, then add amount
};
zigzag merge(zigzag a, zigzag &b)
{
if (a.isline)
{
for (auto &f : b.add)
{
if (f.first.first <= a.height && a.height <= f.first.second)
{
a.addtoall += f.second.second;
if (f.second.first) a.addtoall += a.height;
}
}
if (b.isline)
{
a.addtoall += b.addtoall;
a.height = b.height;
}
else
{
if (b.a > a.height) a.height = b.a;
else if (b.b < a.height) a.height = b.b;
}
return a;
}
else
{
if (b.isline)
{
a.isline = 1;
for (auto &f : b.add)
{
if (f.first.first <= a.a && a.a <= f.first.second)
{
ll am = f.second.second;
if (f.second.first) am += a.a;
a.add.push_back({ { -1.1e9, a.a }, { 0, am }});
}
int bot = max(f.first.first, a.a+1);
int top = min(f.first.second, a.b-1);
if (bot <= top)
{
a.add.push_back({ { bot, top }, f.second });
}
if (f.first.first <= a.b && a.b <= f.first.second)
{
ll am = f.second.second;
if (f.second.first) am += a.b;
a.add.push_back({ { a.b, 1.1e9 }, { 0, am }});
}
}
a.addtoall += b.addtoall;
a.height = b.height;
return a;
}
else if (b.a >= a.b) // second one is completely above
{
// just make a into a line and return
a.isline = 1;
a.height = b.a;
return a;
}
else if (b.b <= a.a) // second one is completely below
{
// make into a line, moving all groups down
a.isline = 1;
a.height = b.b;
a.add.clear();
a.add.push_back({ { a.a+1, 1.1e9 }, { 1, -a.height }});
a.add.push_back({ { -1.1e9, a.a }, { 0, a.a-a.height }});
return a;
}
else // overlap
{
a.add.clear();
a.b = min(a.b, b.b);
a.a = max(a.a, b.a);
assert(a.a <= a.b);
a.add.push_back({ { a.b, 1.1e9 }, { 1, -a.b } });
if (a.a == a.b)
{
a.isline = 1;
a.height = a.a;
}
return a;
}
}
}
zigzag merge2(zigzag a, zigzag b)
{
if (a.isline)
{
for (auto f : b.add)
{
if (f.first.first <= a.height && a.height <= f.first.second)
{
a.addtoall += f.second.second;
if (f.second.first) a.addtoall += a.height;
}
}
if (b.isline)
{
a.addtoall += b.addtoall;
a.height = b.height;
}
else
{
if (b.a > a.height) a.height = b.a;
else if (b.b < a.height) a.height = b.b;
}
return a;
}
else
{
if (b.isline)
{
a.isline = 1;
for (auto f : b.add)
{
if (f.first.first <= a.a && a.a <= f.first.second)
{
ll am = f.second.second;
if (f.second.first) am += a.a;
a.add.push_back({ { -1.1e9, a.a }, { 0, am }});
}
int bot = max(f.first.first, a.a+1);
int top = min(f.first.second, a.b-1);
if (bot <= top)
{
a.add.push_back({ { bot, top }, f.second });
}
if (f.first.first <= a.b && a.b <= f.first.second)
{
ll am = f.second.second;
if (f.second.first) am += a.b;
a.add.push_back({ { a.b, 1.1e9 }, { 0, am }});
}
}
a.addtoall += b.addtoall;
a.height = b.height;
return a;
}
else if (b.a >= a.b) // second one is completely above
{
// just make a into a line and return
a.isline = 1;
a.height = b.a;
return a;
}
else if (b.b <= a.a) // second one is completely below
{
// make into a line, moving all groups down
a.isline = 1;
a.height = b.b;
a.add.clear();
a.add.push_back({ { a.a+1, 1.1e9 }, { 1, -a.height }});
a.add.push_back({ { -1.1e9, a.a }, { 0, a.a-a.height }});
return a;
}
else // overlap
{
a.add.clear();
a.b = min(a.b, b.b);
a.a = max(a.a, b.a);
assert(a.a <= a.b);
a.add.push_back({ { a.b, 1.1e9 }, { 1, -a.b } });
if (a.a == a.b)
{
a.isline = 1;
a.height = a.a;
}
return a;
}
}
}
zigzag rangetree[300010*4];
void update(int node, pair<int, int> val, int curr = 1, int cstart = 0, int cend = 300010)
{
if (cstart == cend)
{
rangetree[curr].a = val.first, rangetree[curr].b = val.second;
rangetree[curr].add = { { { val.second, 1.1e9 }, { 1, -val.second } } };
if (rangetree[curr].a == rangetree[curr].b)
{
rangetree[curr].isline = 1;
rangetree[curr].height = rangetree[curr].a;
}
else rangetree[curr].isline = 0;
return;
}
int mid = (cstart+cend)/2;
if (node <= mid) update(node, val, 2*curr, cstart, mid);
else update(node, val, 2*curr+1, mid+1, cend);
rangetree[curr] = merge(rangetree[2*curr], rangetree[2*curr+1]);
};
zigzag query(int s, int e, int curr = 1, int cstart = 0, int cend = 300010)
{
if (s <= cstart && cend <= e) return rangetree[curr];
int mid = (cstart+cend)/2;
if (e <= mid) return query(s, e, 2*curr, cstart, mid);
else if (s > mid) return query(s, e, 2*curr+1, mid+1, cend);
else return merge2(query(s, e, 2*curr, cstart, mid), query(s, e, 2*curr+1, mid+1, cend));
}
int A[300010], B[300010], C[300010], D[300010], T[300010], L[300010], R[300010], l[300010], r[300010];
ll ANS[300010];
int n, q;
void dothing(bool reverse)
{
for (int i = 1; i < n; i++)
{
l[i] = L[i], r[i] = R[i];
if (reverse) l[i] = L[n-i], r[i] = R[n-i];
l[i]-=i;
r[i]-=i+1;
D("%lld %lld\n", l[i], r[i]);
update(i, { l[i], r[i] });
assert(l[i] <= r[i]);
}
for (int i = 0; i < q; i++)
{
ll t, a, b, c, d;
t = T[i];
if (t == 1)
{
ll a = A[i], x = B[i], y = C[i];
if (reverse) a = n-a;
l[a] = x-a, r[a] = y-a-1;
assert(l[a] <= r[a]);
update(a, { l[a], r[a] });
continue;
}
a = A[i], b = B[i], c = C[i], d = D[i];
if (a > c && !reverse) continue;
if (reverse)
{
if (a <= c) continue;
a = n-a+1, c = n-c+1;
}
b-=a, d-=c;
if (a == c)
{
ANS[i] = max(0ll, b-d);
continue;
}
zigzag q = query(a, c-1);
ll ans = q.addtoall;
for (auto f : q.add)
{
if (f.first.first <= b && b <= f.first.second)
{
ans += f.second.second;
if (f.second.first) ans += b;
}
}
int endpoint = q.height;
if (!q.isline)
{
if (b < q.a) endpoint = q.a;
else if (b > q.b) endpoint = q.b;
else endpoint = b;
}
ans += max(0ll, endpoint-d);
ANS[i] = ans;
}
}
int main()
{
scanf("%d%d", &n, &q);
for (int i = 1; i < n; i++) scanf("%d%d", &L[i], &R[i]);
for (int i = 0; i < q; i++)
{
scanf("%d", &T[i]);
if (T[i] == 1) scanf("%d%d%d", &A[i], &B[i], &C[i]);
else scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
}
dothing(0);
dothing(1);
for (int i = 0; i < q; i++)
{
if (T[i] == 2) printf("%lld\n", ANS[i]);
}
}
Compilation message
timeleap.cpp: In function 'int main()':
timeleap.cpp:283:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &q);
~~~~~^~~~~~~~~~~~~~~~
timeleap.cpp:284:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i < n; i++) scanf("%d%d", &L[i], &R[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:287:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &T[i]);
~~~~~^~~~~~~~~~~~~
timeleap.cpp:288:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
if (T[i] == 1) scanf("%d%d%d", &A[i], &B[i], &C[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:289:13: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
else scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
56824 KB |
Output is correct |
2 |
Correct |
51 ms |
56696 KB |
Output is correct |
3 |
Correct |
48 ms |
56700 KB |
Output is correct |
4 |
Correct |
49 ms |
56696 KB |
Output is correct |
5 |
Correct |
51 ms |
56824 KB |
Output is correct |
6 |
Correct |
51 ms |
56704 KB |
Output is correct |
7 |
Correct |
50 ms |
56696 KB |
Output is correct |
8 |
Correct |
51 ms |
56824 KB |
Output is correct |
9 |
Correct |
50 ms |
56696 KB |
Output is correct |
10 |
Correct |
52 ms |
56696 KB |
Output is correct |
11 |
Correct |
54 ms |
56824 KB |
Output is correct |
12 |
Correct |
54 ms |
56824 KB |
Output is correct |
13 |
Correct |
60 ms |
56952 KB |
Output is correct |
14 |
Correct |
63 ms |
56824 KB |
Output is correct |
15 |
Correct |
54 ms |
56824 KB |
Output is correct |
16 |
Correct |
55 ms |
56824 KB |
Output is correct |
17 |
Correct |
54 ms |
56952 KB |
Output is correct |
18 |
Correct |
55 ms |
56796 KB |
Output is correct |
19 |
Correct |
90 ms |
56904 KB |
Output is correct |
20 |
Correct |
55 ms |
56832 KB |
Output is correct |
21 |
Correct |
56 ms |
56796 KB |
Output is correct |
22 |
Correct |
53 ms |
56796 KB |
Output is correct |
23 |
Correct |
52 ms |
56828 KB |
Output is correct |
24 |
Correct |
52 ms |
56824 KB |
Output is correct |
25 |
Correct |
54 ms |
56824 KB |
Output is correct |
26 |
Correct |
54 ms |
56824 KB |
Output is correct |
27 |
Correct |
52 ms |
56824 KB |
Output is correct |
28 |
Correct |
69 ms |
56904 KB |
Output is correct |
29 |
Correct |
58 ms |
56824 KB |
Output is correct |
30 |
Correct |
53 ms |
56824 KB |
Output is correct |
31 |
Correct |
52 ms |
56952 KB |
Output is correct |
32 |
Correct |
52 ms |
56824 KB |
Output is correct |
33 |
Correct |
52 ms |
56824 KB |
Output is correct |
34 |
Correct |
52 ms |
56952 KB |
Output is correct |
35 |
Correct |
52 ms |
56824 KB |
Output is correct |
36 |
Correct |
55 ms |
56824 KB |
Output is correct |
37 |
Correct |
56 ms |
56888 KB |
Output is correct |
38 |
Correct |
53 ms |
56824 KB |
Output is correct |
39 |
Correct |
56 ms |
56952 KB |
Output is correct |
40 |
Correct |
52 ms |
56824 KB |
Output is correct |
41 |
Correct |
47 ms |
56696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1785 ms |
93060 KB |
Output is correct |
2 |
Correct |
1749 ms |
91512 KB |
Output is correct |
3 |
Correct |
1821 ms |
91740 KB |
Output is correct |
4 |
Correct |
1816 ms |
91020 KB |
Output is correct |
5 |
Correct |
1821 ms |
92852 KB |
Output is correct |
6 |
Correct |
1791 ms |
92400 KB |
Output is correct |
7 |
Correct |
1763 ms |
93324 KB |
Output is correct |
8 |
Correct |
1829 ms |
94584 KB |
Output is correct |
9 |
Correct |
1736 ms |
91368 KB |
Output is correct |
10 |
Correct |
1865 ms |
93056 KB |
Output is correct |
11 |
Correct |
1821 ms |
92844 KB |
Output is correct |
12 |
Correct |
1918 ms |
94696 KB |
Output is correct |
13 |
Correct |
1897 ms |
95056 KB |
Output is correct |
14 |
Correct |
49 ms |
56668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
56824 KB |
Output is correct |
2 |
Correct |
51 ms |
56696 KB |
Output is correct |
3 |
Correct |
48 ms |
56700 KB |
Output is correct |
4 |
Correct |
49 ms |
56696 KB |
Output is correct |
5 |
Correct |
51 ms |
56824 KB |
Output is correct |
6 |
Correct |
51 ms |
56704 KB |
Output is correct |
7 |
Correct |
50 ms |
56696 KB |
Output is correct |
8 |
Correct |
51 ms |
56824 KB |
Output is correct |
9 |
Correct |
50 ms |
56696 KB |
Output is correct |
10 |
Correct |
52 ms |
56696 KB |
Output is correct |
11 |
Correct |
54 ms |
56824 KB |
Output is correct |
12 |
Correct |
54 ms |
56824 KB |
Output is correct |
13 |
Correct |
60 ms |
56952 KB |
Output is correct |
14 |
Correct |
63 ms |
56824 KB |
Output is correct |
15 |
Correct |
54 ms |
56824 KB |
Output is correct |
16 |
Correct |
55 ms |
56824 KB |
Output is correct |
17 |
Correct |
54 ms |
56952 KB |
Output is correct |
18 |
Correct |
55 ms |
56796 KB |
Output is correct |
19 |
Correct |
90 ms |
56904 KB |
Output is correct |
20 |
Correct |
55 ms |
56832 KB |
Output is correct |
21 |
Correct |
56 ms |
56796 KB |
Output is correct |
22 |
Correct |
53 ms |
56796 KB |
Output is correct |
23 |
Correct |
52 ms |
56828 KB |
Output is correct |
24 |
Correct |
52 ms |
56824 KB |
Output is correct |
25 |
Correct |
54 ms |
56824 KB |
Output is correct |
26 |
Correct |
54 ms |
56824 KB |
Output is correct |
27 |
Correct |
52 ms |
56824 KB |
Output is correct |
28 |
Correct |
69 ms |
56904 KB |
Output is correct |
29 |
Correct |
58 ms |
56824 KB |
Output is correct |
30 |
Correct |
53 ms |
56824 KB |
Output is correct |
31 |
Correct |
52 ms |
56952 KB |
Output is correct |
32 |
Correct |
52 ms |
56824 KB |
Output is correct |
33 |
Correct |
52 ms |
56824 KB |
Output is correct |
34 |
Correct |
52 ms |
56952 KB |
Output is correct |
35 |
Correct |
52 ms |
56824 KB |
Output is correct |
36 |
Correct |
55 ms |
56824 KB |
Output is correct |
37 |
Correct |
56 ms |
56888 KB |
Output is correct |
38 |
Correct |
53 ms |
56824 KB |
Output is correct |
39 |
Correct |
56 ms |
56952 KB |
Output is correct |
40 |
Correct |
52 ms |
56824 KB |
Output is correct |
41 |
Correct |
47 ms |
56696 KB |
Output is correct |
42 |
Correct |
1785 ms |
93060 KB |
Output is correct |
43 |
Correct |
1749 ms |
91512 KB |
Output is correct |
44 |
Correct |
1821 ms |
91740 KB |
Output is correct |
45 |
Correct |
1816 ms |
91020 KB |
Output is correct |
46 |
Correct |
1821 ms |
92852 KB |
Output is correct |
47 |
Correct |
1791 ms |
92400 KB |
Output is correct |
48 |
Correct |
1763 ms |
93324 KB |
Output is correct |
49 |
Correct |
1829 ms |
94584 KB |
Output is correct |
50 |
Correct |
1736 ms |
91368 KB |
Output is correct |
51 |
Correct |
1865 ms |
93056 KB |
Output is correct |
52 |
Correct |
1821 ms |
92844 KB |
Output is correct |
53 |
Correct |
1918 ms |
94696 KB |
Output is correct |
54 |
Correct |
1897 ms |
95056 KB |
Output is correct |
55 |
Correct |
49 ms |
56668 KB |
Output is correct |
56 |
Correct |
2737 ms |
90728 KB |
Output is correct |
57 |
Correct |
2642 ms |
88992 KB |
Output is correct |
58 |
Correct |
2787 ms |
91292 KB |
Output is correct |
59 |
Correct |
2936 ms |
91564 KB |
Output is correct |
60 |
Correct |
2803 ms |
89556 KB |
Output is correct |
61 |
Correct |
2886 ms |
92232 KB |
Output is correct |
62 |
Correct |
2778 ms |
92132 KB |
Output is correct |
63 |
Correct |
2765 ms |
92160 KB |
Output is correct |
64 |
Correct |
2943 ms |
92328 KB |
Output is correct |
65 |
Correct |
2717 ms |
90920 KB |
Output is correct |
66 |
Correct |
2596 ms |
90956 KB |
Output is correct |
67 |
Correct |
2746 ms |
92052 KB |
Output is correct |
68 |
Correct |
2619 ms |
90176 KB |
Output is correct |
69 |
Correct |
2819 ms |
93128 KB |
Output is correct |
70 |
Correct |
2741 ms |
90012 KB |
Output is correct |
71 |
Correct |
2842 ms |
89260 KB |
Output is correct |
72 |
Correct |
2897 ms |
90244 KB |
Output is correct |
73 |
Correct |
2937 ms |
93648 KB |
Output is correct |
74 |
Correct |
2848 ms |
93816 KB |
Output is correct |
75 |
Correct |
2956 ms |
107768 KB |
Output is correct |
76 |
Correct |
2829 ms |
108320 KB |
Output is correct |
77 |
Correct |
48 ms |
56668 KB |
Output is correct |