Skip to content

Commit 65b14fe

Browse files
committed
Split files into folders
1 parent 8243eda commit 65b14fe

25 files changed

+392
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

data_structures/mo_hilbert.cpp

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long ll;
6+
7+
const int maxn = 100010;
8+
const int sqt = 330;
9+
10+
inline int64_t ord(int x, int y, int pow, int rotate) {
11+
if (pow == 0) {
12+
return 0;
13+
}
14+
int hpow = 1 << (pow-1);
15+
int seg = (x < hpow) ? (
16+
(y < hpow) ? 0 : 3
17+
) : (
18+
(y < hpow) ? 1 : 2
19+
);
20+
seg = (seg + rotate) & 3;
21+
const int rotateDelta[4] = {3, 0, 0, 1};
22+
int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
23+
int nrot = (rotate + rotateDelta[seg]) & 3;
24+
int64_t subSquareSize = int64_t(1) << (2*pow - 2);
25+
int64_t ans = seg * subSquareSize;
26+
int64_t add = ord(nx, ny, pow-1, nrot);
27+
ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
28+
return ans;
29+
}
30+
31+
struct query
32+
{
33+
int l, r, id;
34+
ll w;
35+
36+
bool operator<(const query &rhs) const {
37+
return w < rhs.w;
38+
}
39+
};
40+
41+
int lim = 0;
42+
43+
struct bit
44+
{
45+
int b[maxn];
46+
47+
inline void add(int x, int d)
48+
{
49+
for (int i = x; i <= lim; i += i&-i) b[i] += d;
50+
}
51+
52+
inline int get(int x)
53+
{
54+
int ans = 0;
55+
for (int i = x; i > 0; i -= i&-i) ans += b[i];
56+
return ans;
57+
}
58+
};
59+
60+
bit re;
61+
query qr[maxn];
62+
int v[maxn], v2[maxn];
63+
64+
ll tot = 0;
65+
ll sum = 0;
66+
67+
inline void addl(int x)
68+
{
69+
tot++;
70+
re.add(v[x], 1);
71+
sum += re.get(v[x]-1);
72+
}
73+
74+
inline void addr(int x)
75+
{
76+
tot++;
77+
re.add(v[x], 1);
78+
sum += tot - re.get(v[x]);
79+
}
80+
81+
inline void reml(int x)
82+
{
83+
tot--;
84+
re.add(v[x], -1);
85+
sum -= re.get(v[x]-1);
86+
}
87+
88+
inline void remr(int x)
89+
{
90+
tot--;
91+
re.add(v[x], -1);
92+
sum -= tot - re.get(v[x]);
93+
}
94+
95+
ll res[maxn];
96+
97+
98+
99+
inline int readChar();
100+
template <class T = int> inline T readInt();
101+
template <class T> inline void writeInt( T x, char end = 0 );
102+
inline void writeChar( int x );
103+
inline void writeWord( const char *s );
104+
105+
/** Read */
106+
107+
static const int buf_size = 4096;
108+
109+
inline int getChar() {
110+
static char buf[buf_size];
111+
static int len = 0, pos = 0;
112+
if (pos == len)
113+
pos = 0, len = fread(buf, 1, buf_size, stdin);
114+
if (pos == len)
115+
return -1;
116+
return buf[pos++];
117+
}
118+
119+
inline int readChar() {
120+
int c = getChar();
121+
while (c <= 32)
122+
c = getChar();
123+
return c;
124+
}
125+
126+
template <class T>
127+
inline T readInt() {
128+
char c = readChar();
129+
T x = 0;
130+
while ('0' <= c && c <= '9')
131+
x = x * 10 + c - '0', c = getChar();
132+
return x;
133+
}
134+
135+
/** Write */
136+
137+
static int write_pos = 0;
138+
static char write_buf[buf_size];
139+
140+
inline void writeChar( int x ) {
141+
if (write_pos == buf_size)
142+
fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
143+
write_buf[write_pos++] = x;
144+
}
145+
146+
template <class T>
147+
inline void writeInt( T x, char end ) {
148+
if (x < 0)
149+
writeChar('-'), x = -x;
150+
151+
char s[24];
152+
int n = 0;
153+
while (x || !n)
154+
s[n++] = '0' + x % 10, x /= 10;
155+
while (n--)
156+
writeChar(s[n]);
157+
if (end)
158+
writeChar(end);
159+
}
160+
161+
inline void writeWord( const char *s ) {
162+
while (*s)
163+
writeChar(*s++);
164+
}
165+
166+
struct Flusher {
167+
~Flusher() {
168+
if (write_pos)
169+
fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
170+
}
171+
} flusher;
172+
173+
174+
175+
int main()
176+
{
177+
int n;
178+
n = readInt();
179+
180+
ll nn = 0;
181+
while ((1<<nn) < n) nn++;
182+
// cerr << nn << "\n";
183+
184+
for (int i = 1; i <= n; i++) v[i] = readInt(), v2[i] = v[i];
185+
186+
sort(v2+1, v2+1+n);
187+
lim = unique(v2+1, v2+1+n)-v2;
188+
189+
for (int i = 1; i <= n; i++)
190+
v[i] = lower_bound(v2+1, v2+lim, v[i])-v2;
191+
192+
int q;
193+
q = readInt();
194+
195+
for (int i = 0; i < q; i++) {
196+
qr[i].l = readInt();
197+
qr[i].r = readInt();
198+
199+
qr[i].w = ord(qr[i].l-1, qr[i].r-1, nn, 0);
200+
// qr[i].w = qr[i].l/sqt;
201+
qr[i].id = i;
202+
}
203+
204+
sort(qr, qr+q);
205+
206+
// ll dist = qr[0].l + qr[0].r;
207+
// for (int i = 1; i < q; i++)
208+
// dist += abs(qr[i].l-qr[i-1].l)+abs(qr[i].r-qr[i-1].r);
209+
210+
// cerr << dist << "\n";
211+
212+
int cl = 1, cr = 0;
213+
214+
for (int i = 0; i < q; i++) {
215+
216+
int l = qr[i].l, r = qr[i].r, id = qr[i].id;
217+
218+
while (r > cr) addr(++cr);
219+
while (r < cr) remr(cr--);
220+
while (l < cl) addl(--cl);
221+
while (l > cl) reml(cl++);
222+
223+
res[id] = sum;
224+
}
225+
226+
for (int i = 0; i < q; i++) writeInt(res[i],'\n');
227+
228+
return 0;
229+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

dynamic_programming/minqueue_sum.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <bits/stdc++.h>
2+
3+
#define ff first
4+
#define ss second
5+
6+
using namespace std;
7+
8+
typedef pair<int, int> ii;
9+
10+
struct minqueue
11+
{
12+
deque<ii> dq;
13+
14+
int l, r, sum;
15+
16+
void clear()
17+
{
18+
l = r = sum = 0;
19+
dq.clear();
20+
}
21+
22+
void push(int x)
23+
{
24+
while (!dq.empty() and dq.back().ff > x-sum) dq.pop_back();
25+
dq.push_back({x-sum,r});
26+
r++;
27+
}
28+
29+
void pop()
30+
{
31+
if (!dq.empty() and dq.front().ss == l) dq.pop_front();
32+
l++;
33+
}
34+
35+
void add(int x) {sum += x;}
36+
37+
int getmin() {return dq[0].ff+sum;}
38+
39+
int size() {return r-l;}
40+
};
41+
42+
const int maxn = 1000;
43+
const int maxm = 100000;
44+
const int inf = 0x3f3f3f;
45+
46+
minqueue q[maxm];
47+
48+
int d[maxm];
49+
int w[maxn], e[maxn], c[maxn];
50+
51+
int main()
52+
{
53+
int n, m;
54+
cin >> n >> m;
55+
56+
for (int i = 1; i <= n; i++) cin >> w[i] >> e[i] >> c[i];
57+
58+
for (int i = 1; i <= m; i++) d[i] = inf;
59+
60+
for (int i = 1; i <= n; i++) {
61+
62+
for (int j = 0; j < w[i]; j++) q[j].clear();
63+
64+
for (int j = 0; j <= m; j++) {
65+
66+
minqueue& mq = q[j%w[i]];
67+
68+
if (mq.size() > e[i]) mq.pop();
69+
70+
mq.add(c[i]);
71+
mq.push(d[j]);
72+
73+
d[j] = mq.getmin();
74+
}
75+
}
76+
77+
for (int i = 0; i <= m; i++) cout << d[i] << " ";
78+
cout << "\n";
79+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

string/suffix_array.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <bits/stdc++.h>
2+
3+
#define REP(i, n) for(int i = 0; i < int(n); ++i)
4+
#define REP2(i, ini, fim) for (int i = int(ini); i <= int(fim); ++i)
5+
6+
using namespace std;
7+
8+
const int maxn = 101010;
9+
10+
namespace sarray
11+
{
12+
string s;
13+
int n, gap;
14+
int sa[maxn], pos[maxn], tmp[maxn], lcp[maxn];
15+
16+
bool suf_comp(int i, int j)
17+
{
18+
if (pos[i] != pos[j]) return pos[i] < pos[j];
19+
20+
i += gap, j += gap;
21+
22+
return (i < n and j < n)? pos[i] < pos[j] : i > j;
23+
}
24+
25+
void build()
26+
{
27+
n = s.size();
28+
29+
REP(i, n) sa[i] = i, pos[i] = s[i];
30+
31+
for (gap=1;; gap <<= 1) {
32+
33+
sort(sa, sa+n, suf_comp);
34+
35+
REP(i, n-1) tmp[i+1] = tmp[i] + suf_comp(sa[i], sa[i+1]);
36+
REP(i, n) pos[sa[i]] = tmp[i];
37+
38+
if (tmp[n-1] == n-1) break;
39+
}
40+
}
41+
42+
void buildLCP()
43+
{
44+
for (int i = 0, k = 0; i < n; ++i) if (pos[i] != n - 1)
45+
{
46+
for (int j = sa[pos[i] + 1]; s[i + k] == s[j + k]; ++k);
47+
lcp[pos[i]] = k;
48+
if (k)--k;
49+
}
50+
}
51+
52+
int ds()
53+
{
54+
int result = n - sa[0];
55+
56+
for (int i = 1; i < n; i++)
57+
result += (n - sa[i]) - lcp[i - 1];
58+
59+
result++;
60+
return result;
61+
}
62+
} // namespace sarray
63+
64+
int main()
65+
{
66+
ios_base::sync_with_stdio(false);
67+
cin.tie(0);
68+
69+
// int t;
70+
// cin >> t;
71+
72+
// while (t--) {
73+
74+
cin >> sarray::s;
75+
76+
sarray::build();
77+
sarray::buildLCP();
78+
REP(i, sarray::n) cout << sarray::lcp[i] << " ";
79+
cout << "\n";
80+
// sarray::buildLCP();
81+
82+
// cout << sarray::ds() << "\n";
83+
// }
84+
}

0 commit comments

Comments
 (0)