Meesho Questions
Meesho Questions
- Best Subsequence
- Array And Queries
- Node Queries
- Energy Nodes
- Shopping Mall
- Budget Planning
- Divisor Queries
- Max Profit
- Ben and His Work
- Two Groups
- Chain Square
- Maximum Profit
- TreeNode
- Special Sequence
- Task
- Functions Description
- Special Nodes
- XOR On ARRAY
1- Divisor Queries ➖
#include <bits/stdc++.h>
using namespace std;
struct SegmentTree {
ll N;
vll tree;
ll neutral = 0;
ll combine(ll a, ll b) {
return a + b;
}
SegmentTree(vll v) {
N = v.size() - 1;
tree.assign(4 * N + 1, 0);
build(1, 1, N, v);
}
void sieve() {
spf[1] = 1;
for (int i = 2; i < MAXN; i++)
spf[i] = i;
for (int i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (int i = 3; i * i < MAXN; i++) {
if (spf[i] == i) {
for (int j = i * i; j < MAXN; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
sieve();
int n, q;
cin >> n >> q;
vll arr(n + 1);
for (int i = 1; i <= n; i++)
cin >> arr[i];
SegmentTree sgt(arr);
set<int> s;
for (int i = 1; i <= n; i++)
s.insert(i);
while (q--) {
int type;
cin >> type;
int a, b;
cin >> a >> b;
if (type == 3) {
sgt.update(a, b, 1, 1, n);
int temp = a;
auto it = s.lower_bound(temp);
if (it != s.end() && s.size() != 0 && *it == a) {
s.erase(temp);
s.insert(a);
} else {
s.insert(a);
}
arr[a] = b;
} else if (type == 2) {
cout << sgt.query(a, b, 1, 1, n) << endl;
} else {
while (a <= b) {
int temp = a;
auto it = s.lower_bound(temp);
if (it == s.end())
break;
if (*it > b)
break;
int c = *it + 1;
sgt.update(a, arr[a] / spf[arr[a]], 1, 1, n);
if (spf[a] == 1) {
s.erase(temp);
}
arr[a] = spf[a];
a = c;
}
}
}
return 0;
}
2 - NODE QUERIES ➖
def solve(edges, values, queries):
import sys
sys.setrecursionlimit(10**6)
N = len(values)
vals = [0] + values
g = [[] for _ in range(N + 1)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
tin = [0] * (N + 1)
tout = [0] * (N + 1)
timer = 0
def dfs(u, p):
nonlocal timer
timer += 1
tin[u] = timer
for v in g[u]:
if v == p:
continue
dfs(v, u)
tout[u] = timer
dfs(1, -1)
diff = [0] * (N + 2)
for typ, x, y in queries:
if typ == 1:
diff[tin[x]] += y
diff[tout[x] + 1] -= y
elif typ == 2:
diff[1] += y
diff[N + 1] -= y
diff[tin[x]] -= y
diff[tout[x] + 1] += y
for i in range(1, N + 2):
diff[i] += diff[i - 1]
res = [vals[i] + diff[tin[i]] for i in range(1, N + 1)]
return res
if _name_ == '_main_':
N = int(input())
edges = []
for _ in range(N - 1):
edges.append(list(map(int, input().split())))
values = list(map(int, input().split()))
Q = int(input())
queries = []
for _ in range(Q):
queries.append(list(map(int, input().split())))
out = solve(edges, values, queries)
print(' '.join(map(str, out)))
3- Maximum Profit ➖
def max_profit(N, S, Q):
max_profit = 0
for i in range(L):
xor_value = (xor_value << 1) | (int(S[i]) ^ int(Q[i]))
profit = (L // 2) * xor_value
max_profit = max(max_profit, profit)
T = int(input().strip())
for _ in range(T):
N = int(input().strip())
S = input().strip()
Q = input().strip()
print(max_profit(N, S, Q))
6- FENWICK TREE ➖
#include <bits/stdc++.h>
using namespace std;
class FenwickTree {
public:
vector<int> bit;
int size;
FenwickTree(int n) {
size = n;
bit.assign(n + 1, 0);
}
out_time[node] = timer;
}
void solve() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int q;
cin >> q;
vector<tuple<int, int, int>> queries(q);
for (int i = 0; i < q; i++) {
int t, x, y;
cin >> t >> x >> y;
queries[i] = {t, x, y};
}
// Output results
for (int i = 1; i <= n; i++) {
cout << result[i] << " ";
}
cout << "\n";
}
int main() {
solve();
return 0;
}
#Fenwick Tree
7- ENERGY NODES CODES ➖
#include <bits/stdc++.h>
queue<int> q;
q.push(1);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : g[u]) {
if (distOne[v] > distOne[u] + 1) {
distOne[v] = distOne[u] + 1;
q.push(v);
}
}
}
while (!boosterQueue.empty()) {
int u = boosterQueue.front();
boosterQueue.pop();
for (int v : g[u]) {
if (distB[v] > distB[u] + 1) {
distB[v] = distB[u] + 1;
own[v] = own[u];
boosterQueue.push(v);
}
}
}
bool ok = false;
while (!qq.empty()) {
int u = qq.front();
qq.pop();
if (isBN && u == bN) {
ok = true;
break;
}
for (auto &ed : bg[u]) {
if (ed.second <= mid && !vis[ed.first]) {
vis[ed.first] = true;
if (distNBooster[u] <= mid) {
ok = true;
break;
}
qq.push(ed.first);
}
}
}
if (ok) {
ans = mid;
R = mid - 1;
} else {
L = mid + 1;
}
}
return ans;
}
8- TWO GROUPS ➖
from collections import deque
def bfs(start):
queue = deque([start])
color[start] = 1 # Assign first color
while queue:
node = queue.popleft()
for neighbor in graph[node]:
if color[neighbor] == 0: # Not visited
color[neighbor] = -color[node] # Assign opposite group
if color[neighbor] == 1:
group1 += 1
else:
group2 += 1
queue.append(neighbor)
elif color[neighbor] == color[node]:
return 0 # Not bipartite
return max_S
print(solve(N, M, enemy))
# TWO GROUPS
9- XOR ON ARRAY ➖
#include <bits/stdc++.h>
using namespace std;
int minimize_difference(int N, vector<int>& A, int K, vector<int>&
operand) {
vector<vector<int>> possible_values(N);
sort(all_values.begin(), all_values.end());
if (count[index] == 0) {
++distinct_count;
}
++count[index];
while (distinct_count == N) {
min_range = min(min_range, value - all_values[left].first);
int left_index = all_values[left].second;
--count[left_index];
if (count[left_index] == 0) {
--distinct_count;
}
++left;
}
}
return min_range;
}
arr=sorted(set(arr))
basis=[]
num=min(num, num^b)
if num:
basis.append(num)
basis.sort(reverse=True)
return len(basis)
pass
N = int(input())
print (out_)