Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 865230e

Browse files
Create 3624-number-of-integers-with-popcount-depth-equal-to-k-ii.js
1 parent a448004 commit 865230e

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number[][]} queries
4+
* @return {number[]}
5+
*/
6+
var popcountDepth = function(nums, queries) {
7+
let n = nums.length;
8+
let st = new SegTree(n);
9+
let id = 0;
10+
for (let x of nums) {
11+
let pd = calcPd(x);
12+
if (pd <= 5) {
13+
st.update(id, pd);
14+
}
15+
id++;
16+
}
17+
let res = [];
18+
for (let v of queries) {
19+
if (v[0] === 1) {
20+
res.push(st.calc(v[1], v[2] + 1, v[3]));
21+
} else {
22+
let pd = calcPd(v[2]);
23+
if (pd <= 5) {
24+
st.update(v[1], pd);
25+
}
26+
}
27+
}
28+
return res
29+
};
30+
class Node {
31+
constructor(n) {
32+
this.dep_mask = new Array(n).fill(0);
33+
this._n = n;
34+
}
35+
36+
setPd(x) {
37+
this.dep_mask = x;
38+
}
39+
40+
incPd(pd) {
41+
this.dep_mask[pd]++;
42+
}
43+
44+
getPd() {
45+
return this.dep_mask;
46+
}
47+
48+
getPdAt(id) {
49+
return this.dep_mask[id];
50+
}
51+
52+
clearPd() {
53+
for (let i = 0; i < this._n; ++i) {
54+
this.dep_mask[i] = 0;
55+
}
56+
}
57+
}
58+
59+
class SegTree {
60+
constructor(n) {
61+
this.sz = 1;
62+
while (this.sz < n) {
63+
this.sz *= 2;
64+
}
65+
this.T = new Array(2 * this.sz).fill(null).map(() => new Node(6));
66+
}
67+
68+
mergePd(a, b) {
69+
let c = new Array(6).fill(0);
70+
for (let i = 0; i < 6; ++i) {
71+
c[i] = a.dep_mask[i] + b.dep_mask[i];
72+
}
73+
return c;
74+
}
75+
76+
update(id, pd) {
77+
this.updateRec(0, 0, this.sz, id, pd);
78+
}
79+
80+
calc(l, r, k) {
81+
return this.calcRec(0, 0, this.sz, l, r, k);
82+
}
83+
84+
updateRec(x, l, r, pos, pd) {
85+
if ((r - l) === 1) {
86+
this.T[x].clearPd();
87+
this.T[x].incPd(pd);
88+
return;
89+
}
90+
let m = Math.floor((l + r) / 2);
91+
if (pos < m) {
92+
this.updateRec(2 * x + 1, l, m, pos, pd);
93+
} else {
94+
this.updateRec(2 * x + 2, m, r, pos, pd);
95+
}
96+
this.T[x].setPd(this.mergePd(this.T[2 * x + 1], this.T[2 * x + 2]));
97+
}
98+
99+
calcRec(x, l, r, ql, qr, req_pd) {
100+
if (ql >= r || qr <= l) {
101+
return 0;
102+
}
103+
if (l >= ql && r <= qr) {
104+
return this.T[x].getPdAt(req_pd);
105+
}
106+
let m = Math.floor((l + r) / 2);
107+
let le = this.calcRec(2 * x + 1, l, m, ql, qr, req_pd);
108+
let ri = this.calcRec(2 * x + 2, m, r, ql, qr, req_pd);
109+
return le + ri;
110+
}
111+
}
112+
113+
function calcPd(x) {
114+
let dep = 0;
115+
while (x > 1) {
116+
x = x.toString(2).split('').reduce((count, bit) => count + Number(bit), 0);
117+
dep++;
118+
}
119+
return dep;
120+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /