Skip to content

Commit b30da1d

Browse files
authored
Merge branch 'opencodeiiita:main' into main
2 parents b0c4d57 + 8834de5 commit b30da1d

14 files changed

Lines changed: 1012 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[PROBLEM LINK](https://codeforces.com/contest/2169/problem/D1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[PROBLEM LINK](https://www.codechef.com/problems/PERRANGES?tab=statement)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
DAY 8
3+
Q2 Enemy Is Weak
4+
5+
*/
6+
/*
7+
8+
9+
For each position j (as middle element), count how many elements on its left are greater than a[j] and how many on its right are smaller than a[j]. Multiply these two counts - this gives triplets with j as middle. Sum this for all positions.
10+
Use Fenwick Tree (BIT) to efficiently count elements in ranges. Since values go up to 10^9, compress them to 1..n first. Two passes: left-to-right for greater left counts, right-to-left for smaller right counts.
11+
12+
*/
13+
#include <bits/stdc++.h>
14+
using namespace std;
15+
16+
int ft[1000005];
17+
int n;
18+
19+
void upd(int idx, int val){
20+
for(; idx<=n; idx+=idx&-idx)
21+
ft[idx]+=val;
22+
}
23+
24+
int qry(int idx){
25+
int ans=0;
26+
for(; idx>0; idx-=idx&-idx)
27+
ans+=ft[idx];
28+
return ans;
29+
}
30+
31+
int main(){
32+
cin>>n;
33+
vector<int> a(n);
34+
for(int i=0;i<n;i++) cin>>a[i];
35+
36+
// compress coordinates
37+
vector<int> sorted_vals=a;
38+
sort(sorted_vals.begin(),sorted_vals.end());
39+
map<int,int> mp;
40+
for(int i=0;i<n;i++){
41+
mp[sorted_vals[i]]=i+1;
42+
}
43+
44+
vector<int> compressed(n);
45+
for(int i=0;i<n;i++){
46+
compressed[i]=mp[a[i]];
47+
}
48+
49+
vector<long long> left_cnt(n), right_cnt(n);
50+
51+
// left pass
52+
memset(ft,0,sizeof(ft));
53+
for(int i=0;i<n;i++){
54+
int pos=compressed[i];
55+
left_cnt[i]=qry(n)-qry(pos);
56+
upd(pos,1);
57+
}
58+
59+
// right pass
60+
memset(ft,0,sizeof(ft));
61+
for(int i=n-1;i>=0;i--){
62+
int pos=compressed[i];
63+
right_cnt[i]=qry(pos-1);
64+
upd(pos,1);
65+
}
66+
67+
long long ans=0;
68+
for(int i=0;i<n;i++){
69+
ans+=left_cnt[i]*right_cnt[i];
70+
}
71+
72+
cout<<ans<<"\n";
73+
74+
return 0;
75+
}
76+
77+
//Tc =O(nlogn) per test SC = O(n)
78+
/*
79+
My submission : https://codeforces.com/contest/61/submission/356391525
80+
*/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Submission link - https://cses.fi/problemset/result/15829712/
3+
4+
TC - o((N + Q) log(N+Q) )
5+
SC - O(N + Q)
6+
7+
Approach (Simple & Short):
8+
9+
Compress salary values since they can be as large as 10^9.
10+
Use a Fenwick Tree to store and manage salary frequencies.
11+
On updates, reduce the count of the old salary and increase the new one.
12+
For range queries, get the answer using prefix sums from the Fenwick Tree.
13+
14+
*/
15+
16+
17+
#include <bits/stdc++.h>
18+
using namespace std;
19+
const int N = 600005;
20+
int n, q;
21+
int cur_p[200005];
22+
int t[N];
23+
struct Query {
24+
int type, a, b;
25+
} qs[200005];
26+
27+
void add(int i, int v) {
28+
for (; i < N; i += i & -i) t[i] += v;
29+
}
30+
31+
int sum(int i) {
32+
int s = 0;
33+
for (; i > 0; i -= i & -i) s += t[i];
34+
return s;
35+
}
36+
37+
int main() {
38+
ios::sync_with_stdio(0); cin.tie(0);
39+
cin >> n >> q;
40+
vector<int> coords;
41+
for (int i = 1; i <= n; i++) {
42+
cin >> cur_p[i];
43+
coords.push_back(cur_p[i]);
44+
}
45+
for (int i = 0; i < q; i++) {
46+
char op; cin >> op;
47+
cin >> qs[i].a >> qs[i].b;
48+
if (op == '!') {
49+
qs[i].type = 1;
50+
coords.push_back(qs[i].b);
51+
} else {
52+
qs[i].type = 2;
53+
coords.push_back(qs[i].a);
54+
coords.push_back(qs[i].b);
55+
}
56+
}
57+
sort(coords.begin(), coords.end());
58+
coords.erase(unique(coords.begin(), coords.end()), coords.end());
59+
60+
auto get_idx = [&](int x) {
61+
return lower_bound(coords.begin(), coords.end(), x) - coords.begin() + 1;
62+
};
63+
64+
for (int i = 1; i <= n; i++) add(get_idx(cur_p[i]), 1);
65+
66+
for (int i = 0; i < q; i++) {
67+
if (qs[i].type == 1) {
68+
add(get_idx(cur_p[qs[i].a]), -1);
69+
cur_p[qs[i].a] = qs[i].b;
70+
add(get_idx(cur_p[qs[i].a]), 1);
71+
} else {
72+
cout << sum(get_idx(qs[i].b)) - sum(get_idx(qs[i].a) - 1) << "\n";
73+
}
74+
}
75+
return 0;
76+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
3+
Submission link: https://codeforces.com/contest/61/submission/356395307
4+
5+
TC- o(n log n)
6+
SC - o(n)
7+
8+
Approach:
9+
Take each element as one by one and treat that element as middle element,count number of elements greater than that element on left side of that element and count
10+
number of elements smaller than that element on right side of that element.
11+
12+
Use a Fenwick Tree (Binary Indexed Tree) to efficiently count elements in ranges, Since array values can be as large as 10^9, first compress the values to ranks 1..n
13+
14+
Do the left to right pass in Fenwick tree to count number of elements greater than current element on left side.
15+
16+
Reset Fenwick tree and do right to left pass to count number of elements smaller than current element on right side.
17+
18+
Finally, for each element multiply the two counts and sum them up to get the final answer.
19+
20+
*/
21+
22+
#include <bits/stdc++.h>
23+
using namespace std;
24+
25+
typedef long long ll;
26+
const int N = 1000005;
27+
int n;
28+
int a[N], t[N];
29+
int l_big[N], r_small[N];
30+
31+
void add(int i, int v) {
32+
for (; i <= n; i += i & -i) t[i] += v;
33+
}
34+
35+
int sum(int i) {
36+
int s = 0;
37+
for (; i > 0; i -= i & -i) s += t[i];
38+
return s;
39+
}
40+
41+
int main() {
42+
ios::sync_with_stdio(0); cin.tie(0);
43+
cin >> n;
44+
vector<int> v;
45+
for (int i = 0; i < n; i++) {
46+
cin >> a[i];
47+
v.push_back(a[i]);
48+
}
49+
sort(v.begin(), v.end());
50+
51+
for (int i = 0; i < n; i++) {
52+
int rk = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;
53+
l_big[i] = i - sum(rk);
54+
add(rk, 1);
55+
}
56+
57+
memset(t, 0, sizeof(t));
58+
59+
for (int i = n - 1; i >= 0; i--) {
60+
int rk = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;
61+
r_small[i] = sum(rk - 1);
62+
add(rk, 1);
63+
}
64+
65+
ll total = 0;
66+
for (int i = 0; i < n; i++) {
67+
total += (ll)l_big[i] * r_small[i];
68+
}
69+
cout << total << endl;
70+
71+
return 0;
72+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
Submission Link:
3+
https://cses.fi/paste/df31068806cfc4bef186a9/
4+
*/
5+
6+
/*
7+
Problem: Salary Queries
8+
Link: https://cses.fi/problemset/task/1144/
9+
Author: Krishna200608
10+
11+
Short Problem Statement:
12+
Maintain an array of salaries. Process queries to update a salary or count employees
13+
with salaries in a range [a, b]. Values up to 10^9 require coordinate compression.
14+
15+
Approach:
16+
Coordinate Compression: Collect all initial salaries and update values.
17+
Sort and remove duplicates to map large values to indices.
18+
Use a Fenwick Tree (BIT) over compressed indices.
19+
Point updates for salary changes.
20+
Range queries using prefix sums on BIT.
21+
22+
Time : O(Q log(N+Q))
23+
Space Complexity: O(N+Q)
24+
*/
25+
26+
#include <bits/stdc++.h>
27+
using namespace std;
28+
29+
#define MOD 1000000007
30+
const int M = 1e9 + 7;
31+
const double PI = acos(-1.0);
32+
#define INF 1e18
33+
#define pb push_back
34+
#define ppb pop_back
35+
#define ll long long
36+
#define no cout << "NO" << endl
37+
#define yes cout << "YES" << endl
38+
#define ff first
39+
#define ss second
40+
#define inn(x) int x; cin >> x
41+
#define ill(x) ll x; cin >> x
42+
#define all(x) x.begin(), x.end()
43+
#define in(a) for (int i = 0; i < (int)a.size(); i++) cin >> a[i]
44+
#define out(a) for (int i = 0; i < (int)a.size(); i++) cout << a[i] << " "
45+
typedef vector<int> vi;
46+
typedef vector<ll> vll;
47+
#define ceil_div(n, x) (((n) % (x) == 0) ? ((n) / (x)) : ((n) / (x) + 1))
48+
#define debug(x) cout << "x -> " << x << endl
49+
#define outt(x) cout << x << endl
50+
#define endl "\n"
51+
52+
const int MAX_M = 600005;
53+
int bit[MAX_M];
54+
int max_val;
55+
56+
void update(int idx, int val) {
57+
for (; idx <= max_val; idx += idx & -idx)
58+
bit[idx] += val;
59+
}
60+
61+
int query(int idx) {
62+
int sum = 0;
63+
for (; idx > 0; idx -= idx & -idx)
64+
sum += bit[idx];
65+
return sum;
66+
}
67+
68+
void solve() {
69+
int n, q;
70+
if (!(cin >> n >> q)) return;
71+
72+
vi p(n);
73+
vi vals;
74+
75+
for (int i = 0; i < n; i++) {
76+
cin >> p[i];
77+
vals.pb(p[i]);
78+
}
79+
80+
struct Qry {
81+
char type;
82+
int k, x;
83+
};
84+
85+
vector<Qry> qs(q);
86+
87+
for (int i = 0; i < q; i++) {
88+
cin >> qs[i].type >> qs[i].k >> qs[i].x;
89+
if (qs[i].type == '!') vals.pb(qs[i].x);
90+
}
91+
92+
sort(all(vals));
93+
vals.erase(unique(all(vals)), vals.end());
94+
max_val = vals.size();
95+
96+
auto get_idx = [&](int v) {
97+
return lower_bound(all(vals), v) - vals.begin() + 1;
98+
};
99+
100+
for (int x : p) {
101+
update(get_idx(x), 1);
102+
}
103+
104+
for (int i = 0; i < q; i++) {
105+
if (qs[i].type == '!') {
106+
int k = qs[i].k - 1;
107+
int x = qs[i].x;
108+
update(get_idx(p[k]), -1);
109+
p[k] = x;
110+
update(get_idx(x), 1);
111+
} else {
112+
int a = qs[i].k;
113+
int b = qs[i].x;
114+
115+
int r = upper_bound(all(vals), b) - vals.begin();
116+
int l = lower_bound(all(vals), a) - vals.begin();
117+
118+
outt(query(r) - query(l));
119+
}
120+
}
121+
}
122+
123+
signed main() {
124+
ios_base::sync_with_stdio(false);
125+
cin.tie(nullptr);
126+
cout.tie(nullptr);
127+
128+
auto begin = chrono::high_resolution_clock::now();
129+
130+
int t = 1;
131+
while (t--) {
132+
solve();
133+
}
134+
135+
auto end = chrono::high_resolution_clock::now();
136+
auto elapsed = chrono::duration_cast<chrono::nanoseconds>(end - begin);
137+
cerr << "Time measured: " << elapsed.count() * 1e-6 << "ms";
138+
139+
return 0;
140+
}

0 commit comments

Comments
 (0)