#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();
aint.build(1, 1, n-1);
int i;
for(i=1; i<=q; ++i)
{
if(tip[i] == 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);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
28544 KB |
Output is correct |
2 |
Correct |
28 ms |
28672 KB |
Output is correct |
3 |
Correct |
26 ms |
28536 KB |
Output is correct |
4 |
Correct |
26 ms |
28672 KB |
Output is correct |
5 |
Correct |
28 ms |
28632 KB |
Output is correct |
6 |
Correct |
27 ms |
28672 KB |
Output is correct |
7 |
Correct |
25 ms |
28672 KB |
Output is correct |
8 |
Correct |
25 ms |
28672 KB |
Output is correct |
9 |
Correct |
26 ms |
28664 KB |
Output is correct |
10 |
Correct |
27 ms |
28644 KB |
Output is correct |
11 |
Correct |
28 ms |
28672 KB |
Output is correct |
12 |
Correct |
27 ms |
28720 KB |
Output is correct |
13 |
Correct |
118 ms |
28636 KB |
Output is correct |
14 |
Correct |
29 ms |
28672 KB |
Output is correct |
15 |
Correct |
25 ms |
28672 KB |
Output is correct |
16 |
Correct |
25 ms |
28664 KB |
Output is correct |
17 |
Correct |
29 ms |
28664 KB |
Output is correct |
18 |
Correct |
25 ms |
28664 KB |
Output is correct |
19 |
Correct |
28 ms |
28664 KB |
Output is correct |
20 |
Correct |
29 ms |
28664 KB |
Output is correct |
21 |
Correct |
28 ms |
28664 KB |
Output is correct |
22 |
Correct |
28 ms |
28636 KB |
Output is correct |
23 |
Correct |
26 ms |
28664 KB |
Output is correct |
24 |
Correct |
26 ms |
28632 KB |
Output is correct |
25 |
Correct |
25 ms |
28664 KB |
Output is correct |
26 |
Correct |
26 ms |
28672 KB |
Output is correct |
27 |
Correct |
27 ms |
28672 KB |
Output is correct |
28 |
Correct |
27 ms |
28664 KB |
Output is correct |
29 |
Correct |
26 ms |
28672 KB |
Output is correct |
30 |
Correct |
28 ms |
28792 KB |
Output is correct |
31 |
Correct |
27 ms |
28672 KB |
Output is correct |
32 |
Correct |
28 ms |
28664 KB |
Output is correct |
33 |
Correct |
28 ms |
28664 KB |
Output is correct |
34 |
Correct |
30 ms |
28672 KB |
Output is correct |
35 |
Correct |
28 ms |
28672 KB |
Output is correct |
36 |
Correct |
29 ms |
28664 KB |
Output is correct |
37 |
Correct |
29 ms |
28672 KB |
Output is correct |
38 |
Correct |
34 ms |
28672 KB |
Output is correct |
39 |
Correct |
27 ms |
28664 KB |
Output is correct |
40 |
Correct |
28 ms |
28672 KB |
Output is correct |
41 |
Runtime error |
388 ms |
525312 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
786 ms |
47044 KB |
Output is correct |
2 |
Correct |
793 ms |
58232 KB |
Output is correct |
3 |
Correct |
685 ms |
58336 KB |
Output is correct |
4 |
Correct |
661 ms |
58060 KB |
Output is correct |
5 |
Correct |
721 ms |
58744 KB |
Output is correct |
6 |
Correct |
709 ms |
58488 KB |
Output is correct |
7 |
Correct |
857 ms |
58692 KB |
Output is correct |
8 |
Correct |
770 ms |
59420 KB |
Output is correct |
9 |
Correct |
804 ms |
58392 KB |
Output is correct |
10 |
Correct |
795 ms |
58744 KB |
Output is correct |
11 |
Correct |
729 ms |
58572 KB |
Output is correct |
12 |
Correct |
763 ms |
59180 KB |
Output is correct |
13 |
Correct |
831 ms |
59300 KB |
Output is correct |
14 |
Runtime error |
398 ms |
525312 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
28544 KB |
Output is correct |
2 |
Correct |
28 ms |
28672 KB |
Output is correct |
3 |
Correct |
26 ms |
28536 KB |
Output is correct |
4 |
Correct |
26 ms |
28672 KB |
Output is correct |
5 |
Correct |
28 ms |
28632 KB |
Output is correct |
6 |
Correct |
27 ms |
28672 KB |
Output is correct |
7 |
Correct |
25 ms |
28672 KB |
Output is correct |
8 |
Correct |
25 ms |
28672 KB |
Output is correct |
9 |
Correct |
26 ms |
28664 KB |
Output is correct |
10 |
Correct |
27 ms |
28644 KB |
Output is correct |
11 |
Correct |
28 ms |
28672 KB |
Output is correct |
12 |
Correct |
27 ms |
28720 KB |
Output is correct |
13 |
Correct |
118 ms |
28636 KB |
Output is correct |
14 |
Correct |
29 ms |
28672 KB |
Output is correct |
15 |
Correct |
25 ms |
28672 KB |
Output is correct |
16 |
Correct |
25 ms |
28664 KB |
Output is correct |
17 |
Correct |
29 ms |
28664 KB |
Output is correct |
18 |
Correct |
25 ms |
28664 KB |
Output is correct |
19 |
Correct |
28 ms |
28664 KB |
Output is correct |
20 |
Correct |
29 ms |
28664 KB |
Output is correct |
21 |
Correct |
28 ms |
28664 KB |
Output is correct |
22 |
Correct |
28 ms |
28636 KB |
Output is correct |
23 |
Correct |
26 ms |
28664 KB |
Output is correct |
24 |
Correct |
26 ms |
28632 KB |
Output is correct |
25 |
Correct |
25 ms |
28664 KB |
Output is correct |
26 |
Correct |
26 ms |
28672 KB |
Output is correct |
27 |
Correct |
27 ms |
28672 KB |
Output is correct |
28 |
Correct |
27 ms |
28664 KB |
Output is correct |
29 |
Correct |
26 ms |
28672 KB |
Output is correct |
30 |
Correct |
28 ms |
28792 KB |
Output is correct |
31 |
Correct |
27 ms |
28672 KB |
Output is correct |
32 |
Correct |
28 ms |
28664 KB |
Output is correct |
33 |
Correct |
28 ms |
28664 KB |
Output is correct |
34 |
Correct |
30 ms |
28672 KB |
Output is correct |
35 |
Correct |
28 ms |
28672 KB |
Output is correct |
36 |
Correct |
29 ms |
28664 KB |
Output is correct |
37 |
Correct |
29 ms |
28672 KB |
Output is correct |
38 |
Correct |
34 ms |
28672 KB |
Output is correct |
39 |
Correct |
27 ms |
28664 KB |
Output is correct |
40 |
Correct |
28 ms |
28672 KB |
Output is correct |
41 |
Runtime error |
388 ms |
525312 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |