1- #include < bits/stdc++.h >
1+ #include < iostream >
22using namespace std ;
3- const int MAX_N = 3e5 + 6 ;
43
5- struct Node {
6- Node* child[2 ];
7- Node () {
8- this -> child[0 ] = this -> child[1 ] = NULL ;
9- }
10- };
11- void insert (Node* root, int x) {
12- Node* cur = root;
13- for (int bit = 21 ; bit >= 0 ; bit--) {
14- int p = (x & (1 << bit)) ? 1 : 0 ;
15- if (!cur -> child[p]) {
16- cur -> child[p] = new Node ();
4+ void fizzBuzz (int n) {
5+ for (int i = 1 ; i <= n; ++i) {
6+ if (i % 3 == 0 && i % 5 == 0 ) {
7+ cout << " FizzBuzz" << endl;
8+ } else if (i % 3 == 0 ) {
9+ cout << " Fizz" << endl;
10+ } else if (i % 5 == 0 ) {
11+ cout << " Buzz" << endl;
12+ } else {
13+ cout << i << endl;
14+ }
1715 }
18- cur = cur -> child[p];
19- }
20- }
21- int xor_max_query (Node* root, int x) {
22- int ans = 0 ;
23- Node* cur = root;
24- for (int bit = 21 ; bit >= 0 ; bit--) {
25- int p = (x & (1 << bit)) ? 1 : 0 ;
26- if (cur -> child[1 ^p]) {
27- ans += (1 << bit);
28- cur = cur -> child[1 ^p];
29- } else if (cur -> child[p]) {
30- cur = cur -> child[p];
31- } else {
32- break ;
33- }
34- }
35- return ans;
3616}
3717
38- // vector<Node*> tree;
39- Node* tree[4 * MAX_N];
40- void merge (Node* &root, Node* root1, Node* root2) {
41- if (!root1 && !root2)
42- return ;
43- root = new Node ();
44- if (!root1) {
45- root = root2;
46- return ;
47- }
48- if (!root2) {
49- root = root1;
50- return ;
51- }
52- merge (root -> child[0 ], root1 -> child[0 ], root2 -> child[0 ]);
53- merge (root -> child[1 ], root1 -> child[1 ], root2 -> child[1 ]);
54- }
55- void build (int id, int l, int r, vector<int >& a) {
56- if (l == r) {
57- tree[id] = new Node ();
58- insert (tree[id], a[l]);
59- return ;
60- }
61- int mid = (l + r) / 2 ;
62- build (2 * id, l, mid, a);
63- build (2 * id + 1 , mid + 1 , r, a);
64- merge (tree[id], tree[2 * id], tree[2 * id + 1 ]);
65- }
66- int query (int id, int l, int r, int lq, int rq, int x) {
67- if (lq > r || l > rq)
68- return 0 ;
69- if (lq <= l && r <= rq)
70- return xor_max_query (tree[id], x);
71- int mid = (l + r) / 2 ;
72- int on_left = query (2 * id, l, mid, lq, rq, x);
73- int on_right = query (2 * id + 1 , mid + 1 , r, lq, rq, x);
74- return max (on_left, on_right);
75- }
7618int main () {
77- ios::sync_with_stdio (false );
78- cin.tie (0 );
79- int n;
80- cin >> n;
81- vector<int > a (n);
82- for (int i = 0 ; i < n; i++)
83- cin >> a[i];
84- build (1 , 0 , n - 1 , a);
85- int Q;
86- cin >> Q;
87- for (int q = 0 ; q < Q; q++) {
88- int l, r, x;
89- cin >> l >> r >> x;
90- l -= 1 ;
91- r -= 1 ;
92- cout << query (1 , 0 , n - 1 , l, r, x) << ' \n ' ;
93- }
94- return 0 ;
95- }
19+ int n;
20+ // cout << "Enter the upper limit: ";
21+ cin >> n;
22+ fizzBuzz (n);
23+ return 0 ;
24+ }
0 commit comments