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 cfc10f3

Browse files
Added problems
1 parent 4b9a4a8 commit cfc10f3

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

‎range-queries/range-xor-queries.cpp‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int lli;
6+
typedef vector<lli> vi;
7+
8+
class FenwickTree
9+
{
10+
11+
vi tree;
12+
lli nodes;
13+
14+
public:
15+
FenwickTree(lli _nodes)
16+
{
17+
nodes = _nodes;
18+
tree.resize(nodes + 1, 0);
19+
}
20+
21+
void add(lli pos, lli val)
22+
{
23+
lli idx = pos;
24+
while (idx <= nodes)
25+
{
26+
tree[idx] ^= val;
27+
idx += idx & (-idx);
28+
}
29+
}
30+
31+
lli prefixSum(lli pos)
32+
{
33+
lli sum = 0;
34+
lli idx = pos;
35+
while (idx)
36+
{
37+
sum ^= tree[idx];
38+
idx -= idx & (-idx);
39+
}
40+
return sum;
41+
}
42+
43+
lli rangeSum(lli a, lli b)
44+
{
45+
return prefixSum(b) ^ prefixSum(a - 1);
46+
}
47+
};
48+
49+
void task()
50+
{
51+
lli lenNumbers, lenQueries;
52+
cin >> lenNumbers >> lenQueries;
53+
FenwickTree ft(lenNumbers);
54+
lli number;
55+
for (lli i = 1; i <= lenNumbers; i++)
56+
{
57+
cin >> number;
58+
ft.add(i, number);
59+
}
60+
vi res;
61+
lli a, b;
62+
for (lli i = 0; i < lenQueries; i++)
63+
{
64+
cin >> a >> b;
65+
res.emplace_back(ft.rangeSum(a, b));
66+
}
67+
for (auto r : res)
68+
{
69+
cout << r << '\n';
70+
}
71+
}
72+
73+
int main()
74+
{
75+
76+
ios::sync_with_stdio(false);
77+
cin.tie(NULL);
78+
79+
task();
80+
81+
return 0;
82+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int lli;
6+
typedef vector<lli> vi;
7+
8+
vi calculateLPS(string pattern)
9+
{
10+
11+
lli n = pattern.length();
12+
13+
vi lps(n, 0);
14+
15+
lli j = 0;
16+
17+
for (lli i = 1; i < n; i++)
18+
{
19+
20+
while (j > 0 && pattern[i] != pattern[j])
21+
{
22+
j = lps[j - 1];
23+
}
24+
25+
if (pattern[i] == pattern[j])
26+
{
27+
j++;
28+
}
29+
30+
lps[i] = j;
31+
}
32+
33+
return lps;
34+
}
35+
36+
lli strStr(string str, string pattern, vi lps)
37+
{
38+
39+
lli i = 0;
40+
lli j = 0;
41+
42+
lli n = str.length();
43+
44+
lli count = 0;
45+
46+
while (i < n)
47+
{
48+
49+
if (str[i] == pattern[j])
50+
{
51+
i++;
52+
j++;
53+
54+
if (j == pattern.length())
55+
{
56+
count++;
57+
i = i - j + 1;
58+
j = 0;
59+
}
60+
}
61+
else if (j == 0)
62+
{
63+
i++;
64+
}
65+
else
66+
{
67+
j = lps[j - 1];
68+
}
69+
}
70+
71+
return count;
72+
}
73+
74+
void task()
75+
{
76+
77+
string str;
78+
string pattern;
79+
80+
cin >> str;
81+
cin >> pattern;
82+
83+
auto lps = calculateLPS(pattern);
84+
85+
auto count = strStr(str, pattern, lps);
86+
cout << count << '\n';
87+
}
88+
89+
int main()
90+
{
91+
92+
ios::sync_with_stdio(false);
93+
cin.tie(NULL);
94+
95+
task();
96+
97+
return 0;
98+
}

0 commit comments

Comments
(0)

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