Skip to content

Commit ae92042

Browse files
committed
Solved problem 911G from codeforces
1 parent 5bedb7f commit ae92042

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Idea:
3+
- Segment Tree.
4+
- In the beginning each element `x` equal to its value,
5+
and after each query the element with value `x` will
6+
change to be equal to `y`.
7+
- To maintain the previous effect we can use any data
8+
structure like Segment Tree, each node in the segment
9+
tree contains the mapping between the real values and
10+
the current values.
11+
- We should use lazy propagation to pass the time limit.
12+
*/
13+
14+
#include <bits/stdc++.h>
15+
16+
using namespace std;
17+
18+
int const N = 2e5 + 1, M = 1e2 + 1;
19+
int n, q, s, e, x, y, tar, a[N], seg[4 * N][M], lazy[4 * N];
20+
21+
void build(int at, int l, int r) {
22+
for(int i = 1; i < M; ++i)
23+
seg[at][i] = i;
24+
25+
if(l == r)
26+
return;
27+
28+
int mid = (l + r) >> 1;
29+
build(at << 1, l, mid);
30+
build(at << 1 | 1, mid + 1, r);
31+
}
32+
33+
void pro(int at, int l, int r) {
34+
if(l != r) {
35+
lazy[at << 1] = lazy[at << 1 | 1] = 1;
36+
37+
for(int i = 1; i < M; ++i)
38+
seg[at << 1][i] = seg[at][seg[at << 1][i]],
39+
seg[at << 1 | 1][i] = seg[at][seg[at << 1 | 1][i]];
40+
41+
lazy[at] = 0;
42+
for(int i = 1; i < M; ++i)
43+
seg[at][i] = i;
44+
}
45+
}
46+
47+
void update(int at, int l, int r) {
48+
if(lazy[at] != 0)
49+
pro(at, l, r);
50+
51+
if(l > e || r < s)
52+
return;
53+
54+
if(l >= s && r <= e) {
55+
lazy[at] = 1;
56+
for(int i = 1; i < M; ++i)
57+
if(seg[at][i] == x)
58+
seg[at][i] = y;
59+
pro(at, l, r);
60+
return;
61+
}
62+
63+
int mid = (l + r) >> 1;
64+
update(at << 1, l, mid);
65+
update(at << 1 | 1, mid + 1, r);
66+
}
67+
68+
int get(int at, int l, int r) {
69+
if(lazy[at] != 0)
70+
pro(at, l, r);
71+
72+
if(l > e || r < s)
73+
return 0;
74+
75+
if(l >= s && r <= e)
76+
return seg[at][tar];
77+
78+
int mid = (l + r) >> 1;
79+
return get(at << 1, l, mid) + get(at << 1 | 1, mid + 1, r);
80+
}
81+
82+
int main() {
83+
scanf("%d", &n);
84+
for(int i = 1; i <= n; ++i)
85+
scanf("%d", a + i);
86+
87+
build(1, 1, n);
88+
89+
scanf("%d", &q);
90+
for(int i = 0; i < q; ++i) {
91+
scanf("%d %d %d %d", &s, &e, &x, &y);
92+
update(1, 1, n);
93+
}
94+
95+
for(int i = 1; i <= n; ++i) {
96+
s = e = i;
97+
tar = a[i];
98+
printf("%s%d", i == 1 ? "" : " ", get(1, 1, n));
99+
}
100+
puts("");
101+
102+
return 0;
103+
}

CodeForces/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@
421421
- [908A. New Year and Counting Cards](http://codeforces.com/contest/908/problem/A)
422422
- [908B. New Year and Buggy Bot](http://codeforces.com/contest/908/problem/B)
423423
- [909D. Colorful Points](http://codeforces.com/problemset/problem/909/D)
424+
- [911G. Mass Change Queries](https://codeforces.com/contest/911/problem/G)
424425
- [913A - Modular Exponentiation](http://codeforces.com/problemset/problem/913/A)
425426
- [913B. Christmas Spruce](http://codeforces.com/problemset/problem/913/B)
426427
- [915A. Garden](http://codeforces.com/problemset/problem/915/A)

0 commit comments

Comments
 (0)