problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int nmax = 5e5 + 3;
int n, q, usu[nmax], sol, v[nmax];
struct Query {
int l, r, idx;
inline pair<int, int> toPair() const { return make_pair(l / sqrt(n), r); }
} t[nmax];
int ap[nmax];
inline bool operator<(const Query &a, const Query &b) {
return a.toPair() < b.toPair();
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> q;
for (int i = 1; i <= n; ++i)
cin >> v[i];
for (int i = 1; i <= q; ++i) {
cin >> t[i].l >> t[i].r;
t[i].idx = i;
}
sort(t + 1, t + q + 1);
int st = 0;
int dr = 0;
for (int i = 1; i <= q; ++i) {
// cout << t[i].l << ' ' << t[i].r << '\n';
if (t[i].l > dr || t[i].r < st) {
for (int j = st; j <= dr; ++j)
ap[v[j]] = 0;
st = t[i].l;
dr = st;
sol = 1;
++ap[v[st]];
}
while (dr < t[i].r) {
ap[v[++dr]]++;
if (ap[v[dr]] == 1)
++sol;
}
while (st > t[i].l) {
ap[v[--st]]++;
if (ap[v[st]] == 1)
++sol;
}
while (st < t[i].l) {
ap[v[st++]]--;
if (ap[v[st - 1]] == 0)
--sol;
}
while (dr > t[i].r) {
ap[v[dr--]]--;
if (ap[v[dr + 1]] == 0)
--sol;
}
usu[t[i].idx] = sol;
}
for (int i = 1; i <= q; ++i)
cout << usu[i] << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int nmax = 5e5 + 3;
int n, q, usu[nmax], sol, v[nmax];
struct Query {
int l, r, idx;
inline pair<int, int> toPair() const {
int ceva;
if ((int)(l / sqrt(n)) % 2)
ceva = -r;
else
ceva = r;
return make_pair(l / sqrt(n), ceva);
}
} t[nmax];
int ap[nmax];
inline bool operator<(const Query &a, const Query &b) {
return a.toPair() < b.toPair();
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> q;
for (int i = 1; i <= n; ++i)
cin >> v[i];
for (int i = 1; i <= q; ++i) {
cin >> t[i].l >> t[i].r;
t[i].idx = i;
}
sort(t + 1, t + q + 1);
int st = 0;
int dr = 0;
for (int i = 1; i <= q; ++i) {
// cout << t[i].l << ' ' << t[i].r << '\n';
if (t[i].l > dr || t[i].r < st) {
for (int j = st; j <= dr; ++j)
ap[v[j]] = 0;
st = t[i].l;
dr = st;
sol = 1;
++ap[v[st]];
}
while (dr < t[i].r) {
ap[v[++dr]]++;
if (ap[v[dr]] == 1)
++sol;
}
while (st > t[i].l) {
ap[v[--st]]++;
if (ap[v[st]] == 1)
++sol;
}
while (st < t[i].l) {
ap[v[st++]]--;
if (ap[v[st - 1]] == 0)
--sol;
}
while (dr > t[i].r) {
ap[v[dr--]]--;
if (ap[v[dr + 1]] == 0)
--sol;
}
usu[t[i].idx] = sol;
}
for (int i = 1; i <= q; ++i)
cout << usu[i] << '\n';
return 0;
}
| replace | 9 | 10 | 9 | 18 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define BLOCK 700
#define mxn 5000001
using namespace std;
struct query {
int l;
int r;
int i;
};
query Q[mxn];
int ar[mxn], ans[mxn];
int fre[mxn];
int cnt = 0;
bool comp(query a, query b) {
if (a.l / BLOCK != b.l / BLOCK)
return a.l / BLOCK < b.l / BLOCK;
return a.r < b.r;
}
void add(int pos) {
fre[ar[pos]]++;
if (fre[ar[pos]] == 1)
cnt++;
}
void remove(int pos) {
fre[ar[pos]]--;
if (fre[ar[pos]] == 0)
cnt--;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> ar[i];
for (int i = 0; i < q; i++) {
cin >> Q[i].l >> Q[i].r;
Q[i].i = i, Q[i].l--, Q[i].r--;
}
sort(Q, Q + q, comp);
int ML = 0, MR = -1;
for (int i = 0; i < q; i++) {
int L = Q[i].l;
int R = Q[i].r;
while (ML > L)
ML--, add(ML);
while (MR < R)
MR++, add(MR);
while (ML < L)
remove(ML), ML++;
while (MR > R)
remove(MR), MR--;
ans[Q[i].i] = cnt;
}
for (int i = 0; i < q; i++)
cout << ans[i] << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define BLOCK 750
#define mxn 5000001
using namespace std;
struct query {
int l;
int r;
int i;
};
query Q[mxn];
int ar[mxn], ans[mxn];
int fre[mxn];
int cnt = 0;
bool comp(query a, query b) {
if (a.l / BLOCK != b.l / BLOCK)
return a.l / BLOCK < b.l / BLOCK;
return a.r < b.r;
}
void add(int pos) {
fre[ar[pos]]++;
if (fre[ar[pos]] == 1)
cnt++;
}
void remove(int pos) {
fre[ar[pos]]--;
if (fre[ar[pos]] == 0)
cnt--;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> ar[i];
for (int i = 0; i < q; i++) {
cin >> Q[i].l >> Q[i].r;
Q[i].i = i, Q[i].l--, Q[i].r--;
}
sort(Q, Q + q, comp);
int ML = 0, MR = -1;
for (int i = 0; i < q; i++) {
int L = Q[i].l;
int R = Q[i].r;
while (ML > L)
ML--, add(ML);
while (MR < R)
MR++, add(MR);
while (ML < L)
remove(ML), ML++;
while (MR > R)
remove(MR), MR--;
ans[Q[i].i] = cnt;
}
for (int i = 0; i < q; i++)
cout << ans[i] << '\n';
}
| replace | 3 | 4 | 3 | 4 | TLE | |
p02599 | C++ | Time Limit Exceeded | #ifndef _TEMPLATE_ROOT
#define _TEMPLATE_ROOT
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define repl(i, a, b) for (ll i = a; i < (b); ++i)
#define repd(i, a, b) for (int i = b; i >= (a); --i)
#define repdl(i, a, b) for (ll i = b; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
if (v1 > v2) {
v1 = v2;
return true;
}
return false;
}
template <typename H> bool ihmax(H &v1, const H v2) {
if (v1 < v2) {
v1 = v2;
return true;
}
return false;
}
#endif
#ifndef _TEMPLATE_IO
#define _TEMPLATE_IO
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
cin >> head;
read(tail...);
}
template <typename H> void write(H head) { cout << head << '\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
cout << head << " ";
write(tail...);
}
template <typename... T> void die(T... tok) {
write(tok...);
exit(0);
}
#endif
#ifndef _TEMPLATE_OPT
#define _TEMPLATE_OPT
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#endif
using namespace std;
int a[500005];
struct Query {
int l, r, id, ans;
};
// freqcnt
int freq[500005];
int distinct = 0;
void freqadd(int x) {
// write("FA", x, freq[x], "->", freq[x]+1);
if (!freq[x])
distinct++;
freq[x]++;
}
void freqrem(int x) {
// write("FR", x, freq[x], "->", freq[x]-1);
freq[x]--;
if (!freq[x])
distinct--;
}
Query query[500005];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n, q;
read(n, q);
rep(i, 1, n + 1) read(a[i]);
rep(i, 0, q) read(query[i].l, query[i].r), query[i].id = i;
const int BS = 500;
sort(query, query + q, [](const Query &a, const Query &b) {
if (a.l / BS != b.l / BS)
return a.l / BS < b.l / BS;
else
return a.r < b.r;
});
// inclusive [cl, cr]
int cl = 1, cr = 0;
rep(i, 0, q) {
int nl = query[i].l, nr = query[i].r;
while (cl > nl)
freqadd(a[--cl]);
while (cr < nr)
freqadd(a[++cr]);
while (cl < nl)
freqrem(a[cl++]);
while (cr > nr)
freqrem(a[cr--]);
query[i].ans = distinct;
}
sort(query, query + q,
[](const Query &a, const Query &b) { return a.id < b.id; });
rep(i, 0, q) write(query[i].ans);
}
| #ifndef _TEMPLATE_ROOT
#define _TEMPLATE_ROOT
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define repl(i, a, b) for (ll i = a; i < (b); ++i)
#define repd(i, a, b) for (int i = b; i >= (a); --i)
#define repdl(i, a, b) for (ll i = b; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
if (v1 > v2) {
v1 = v2;
return true;
}
return false;
}
template <typename H> bool ihmax(H &v1, const H v2) {
if (v1 < v2) {
v1 = v2;
return true;
}
return false;
}
#endif
#ifndef _TEMPLATE_IO
#define _TEMPLATE_IO
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
cin >> head;
read(tail...);
}
template <typename H> void write(H head) { cout << head << '\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
cout << head << " ";
write(tail...);
}
template <typename... T> void die(T... tok) {
write(tok...);
exit(0);
}
#endif
#ifndef _TEMPLATE_OPT
#define _TEMPLATE_OPT
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#endif
using namespace std;
int a[500005];
struct Query {
int l, r, id, ans;
};
// freqcnt
int freq[500005];
int distinct = 0;
void freqadd(int x) {
// write("FA", x, freq[x], "->", freq[x]+1);
if (!freq[x])
distinct++;
freq[x]++;
}
void freqrem(int x) {
// write("FR", x, freq[x], "->", freq[x]-1);
freq[x]--;
if (!freq[x])
distinct--;
}
Query query[500005];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n, q;
read(n, q);
rep(i, 1, n + 1) read(a[i]);
rep(i, 0, q) read(query[i].l, query[i].r), query[i].id = i;
const int BS = 1500;
sort(query, query + q, [](const Query &a, const Query &b) {
if (a.l / BS != b.l / BS)
return a.l / BS < b.l / BS;
else
return a.r < b.r;
});
// inclusive [cl, cr]
int cl = 1, cr = 0;
rep(i, 0, q) {
int nl = query[i].l, nr = query[i].r;
while (cl > nl)
freqadd(a[--cl]);
while (cr < nr)
freqadd(a[++cr]);
while (cl < nl)
freqrem(a[cl++]);
while (cr > nr)
freqrem(a[cr--]);
query[i].ans = distinct;
}
sort(query, query + q,
[](const Query &a, const Query &b) { return a.id < b.id; });
rep(i, 0, q) write(query[i].ans);
}
| replace | 86 | 87 | 86 | 87 | TLE | |
p02599 | C++ | Runtime Error | #pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define fax(i, a) for (int i = 0; i < (a); i++)
#define f0x(i, a, b) for (int i = (a); i < (b); i++)
#define faxd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define f0xd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define memeset memset
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize
const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 200005;
const ld PI = 4 * atan((ld)1);
ll tree[4 * MX];
ll in[MX];
void build(int v, int l, int r) {
if (l == r) {
tree[v] = 0;
} else {
int m = (l + r) / 2;
build(2 * v + 1, l, m);
build(2 * v + 2, m + 1, r);
tree[v] = tree[2 * v + 1] + tree[2 * v + 2];
}
}
void update(int v, int l, int r, int i, int x) {
if (i > r || i < l) {
return;
} else if (l == r) {
tree[v] += x;
} else {
int m = (l + r) / 2;
update(2 * v + 1, l, m, i, x);
update(2 * v + 2, m + 1, r, i, x);
tree[v] = tree[2 * v + 1] + tree[2 * v + 2];
}
}
ll query(int v, int l, int r, int ql, int qr) {
if (l > qr || r < ql) {
return 0;
} else if (l >= ql && r <= qr) {
return tree[v];
} else {
int m = (l + r) / 2;
ll a = query(2 * v + 1, l, m, ql, qr);
ll b = query(2 * v + 2, m + 1, r, ql, qr);
return a + b;
}
}
template <class T> void ckmin(T &a, T b) { a = min(a, b); }
template <class T> void ckmax(T &a, T b) { a = max(a, b); }
map<int, int> compress;
int cnt = 0;
set<int> vals;
vector<pi> queries[MX];
int pre[MX], ret[MX];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
fax(i, n) {
cin >> in[i];
vals.insert(in[i]);
}
trav(curr, vals) {
compress.insert({curr, cnt});
cnt++;
}
fax(i, q) {
int a, b;
cin >> a >> b;
a--;
b--;
queries[b].pb({a, i});
}
build(0, 0, n - 1);
memeset(pre, -1, sizeof(pre));
fax(i, n) {
int comp = compress[in[i]];
// cout << i << " " << pre[comp] << "\n";
if (pre[comp] == -1) {
update(0, 0, n - 1, i, 1);
pre[comp] = i;
} else {
update(0, 0, n - 1, pre[comp], -1);
update(0, 0, n - 1, i, 1);
pre[comp] = i;
}
for (pi val : queries[i]) {
// cout << val << " ";
ret[val.s] = query(0, 0, n - 1, val.f, i);
}
// cout << endl;
}
fax(i, q) { cout << ret[i] << "\n"; }
} | #pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define fax(i, a) for (int i = 0; i < (a); i++)
#define f0x(i, a, b) for (int i = (a); i < (b); i++)
#define faxd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define f0xd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define memeset memset
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize
const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 500005;
const ld PI = 4 * atan((ld)1);
ll tree[4 * MX];
ll in[MX];
void build(int v, int l, int r) {
if (l == r) {
tree[v] = 0;
} else {
int m = (l + r) / 2;
build(2 * v + 1, l, m);
build(2 * v + 2, m + 1, r);
tree[v] = tree[2 * v + 1] + tree[2 * v + 2];
}
}
void update(int v, int l, int r, int i, int x) {
if (i > r || i < l) {
return;
} else if (l == r) {
tree[v] += x;
} else {
int m = (l + r) / 2;
update(2 * v + 1, l, m, i, x);
update(2 * v + 2, m + 1, r, i, x);
tree[v] = tree[2 * v + 1] + tree[2 * v + 2];
}
}
ll query(int v, int l, int r, int ql, int qr) {
if (l > qr || r < ql) {
return 0;
} else if (l >= ql && r <= qr) {
return tree[v];
} else {
int m = (l + r) / 2;
ll a = query(2 * v + 1, l, m, ql, qr);
ll b = query(2 * v + 2, m + 1, r, ql, qr);
return a + b;
}
}
template <class T> void ckmin(T &a, T b) { a = min(a, b); }
template <class T> void ckmax(T &a, T b) { a = max(a, b); }
map<int, int> compress;
int cnt = 0;
set<int> vals;
vector<pi> queries[MX];
int pre[MX], ret[MX];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
fax(i, n) {
cin >> in[i];
vals.insert(in[i]);
}
trav(curr, vals) {
compress.insert({curr, cnt});
cnt++;
}
fax(i, q) {
int a, b;
cin >> a >> b;
a--;
b--;
queries[b].pb({a, i});
}
build(0, 0, n - 1);
memeset(pre, -1, sizeof(pre));
fax(i, n) {
int comp = compress[in[i]];
// cout << i << " " << pre[comp] << "\n";
if (pre[comp] == -1) {
update(0, 0, n - 1, i, 1);
pre[comp] = i;
} else {
update(0, 0, n - 1, pre[comp], -1);
update(0, 0, n - 1, i, 1);
pre[comp] = i;
}
for (pi val : queries[i]) {
// cout << val << " ";
ret[val.s] = query(0, 0, n - 1, val.f, i);
}
// cout << endl;
}
fax(i, q) { cout << ret[i] << "\n"; }
} | replace | 43 | 44 | 43 | 44 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 10000;
int n = 0, q = 0;
struct Query {
int id, l, r;
bool operator<(Query a) const { return r < a.r; }
} query[maxn];
int v[maxn], sum[maxn], ans[maxn];
int lowbit(int x) { return x & (-x); }
void change(int i, int a) {
while (i <= n) {
sum[i] += a;
i += lowbit(i);
}
}
int getsum(int i) {
int sum_t = 0;
while (i > 0) {
sum_t += sum[i];
i -= lowbit(i);
}
return sum_t;
}
int main() {
scanf("%d %d", &n, &q);
memset(v, 0, sizeof(v));
memset(sum, 0, sizeof(sum));
memset(ans, 0, sizeof(ans));
int t = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &v[i]);
}
// scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d%d", &query[i].l, &query[i].r);
query[i].id = i;
}
sort(query + 1, query + q + 1);
int pre = 1;
map<int, int> mymap;
for (int i = 1; i <= q; i++) {
for (int j = pre; j <= query[i].r; j++) {
// 已存在
if (mymap[v[j]]) {
change(mymap[v[j]], -1);
}
mymap[v[j]] = j;
change(j, 1);
}
pre = query[i].r + 1;
ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 5e5 + 5;
int n = 0, q = 0;
struct Query {
int id, l, r;
bool operator<(Query a) const { return r < a.r; }
} query[maxn];
int v[maxn], sum[maxn], ans[maxn];
int lowbit(int x) { return x & (-x); }
void change(int i, int a) {
while (i <= n) {
sum[i] += a;
i += lowbit(i);
}
}
int getsum(int i) {
int sum_t = 0;
while (i > 0) {
sum_t += sum[i];
i -= lowbit(i);
}
return sum_t;
}
int main() {
scanf("%d %d", &n, &q);
memset(v, 0, sizeof(v));
memset(sum, 0, sizeof(sum));
memset(ans, 0, sizeof(ans));
int t = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &v[i]);
}
// scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d%d", &query[i].l, &query[i].r);
query[i].id = i;
}
sort(query + 1, query + q + 1);
int pre = 1;
map<int, int> mymap;
for (int i = 1; i <= q; i++) {
for (int j = pre; j <= query[i].r; j++) {
// 已存在
if (mymap[v[j]]) {
change(mymap[v[j]], -1);
}
mymap[v[j]] = j;
change(j, 1);
}
pre = query[i].r + 1;
ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 10000;
int n = 0, q = 0;
struct Query {
int id, l, r;
bool operator<(Query a) const { return r < a.r; }
} query[maxn];
int v[maxn], sum[maxn], ans[maxn];
int lowbit(int x) { return x & (-x); }
void change(int i, int a) {
while (i <= n) {
sum[i] += a;
i += lowbit(i);
}
}
int getsum(int i) {
int sum_t = 0;
while (i > 0) {
sum_t += sum[i];
i -= lowbit(i);
}
return sum_t;
}
int main() {
while (~scanf("%d", &n)) {
scanf("%d", &q);
memset(v, 0, sizeof(v));
memset(sum, 0, sizeof(sum));
memset(ans, 0, sizeof(ans));
int t = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &v[i]);
}
for (int i = 1; i <= q; i++) {
scanf("%d%d", &query[i].l, &query[i].r);
query[i].id = i;
}
sort(query + 1, query + q + 1);
int pre = 1;
map<int, int> mymap;
for (int i = 1; i <= q; i++) {
for (int j = pre; j <= query[i].r; j++) {
// 已存在
if (mymap[v[j]]) {
change(mymap[v[j]], -1);
}
mymap[v[j]] = j;
change(j, 1);
}
pre = query[i].r + 1;
ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 5e5 + 5;
int n = 0, q = 0;
struct Query {
int id, l, r;
bool operator<(Query a) const { return r < a.r; }
} query[maxn];
int v[maxn], sum[maxn], ans[maxn];
int lowbit(int x) { return x & (-x); }
void change(int i, int a) {
while (i <= n) {
sum[i] += a;
i += lowbit(i);
}
}
int getsum(int i) {
int sum_t = 0;
while (i > 0) {
sum_t += sum[i];
i -= lowbit(i);
}
return sum_t;
}
int main() {
while (~scanf("%d", &n)) {
scanf("%d", &q);
memset(v, 0, sizeof(v));
memset(sum, 0, sizeof(sum));
memset(ans, 0, sizeof(ans));
int t = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &v[i]);
}
for (int i = 1; i <= q; i++) {
scanf("%d%d", &query[i].l, &query[i].r);
query[i].id = i;
}
sort(query + 1, query + q + 1);
int pre = 1;
map<int, int> mymap;
for (int i = 1; i <= q; i++) {
for (int j = pre; j <= query[i].r; j++) {
// 已存在
if (mymap[v[j]]) {
change(mymap[v[j]], -1);
}
mymap[v[j]] = j;
change(j, 1);
}
pre = query[i].r + 1;
ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
}
return 0;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int N = 500005;
// [1, n]
int bit[N], n, q;
int sum(int i) {
int s = 0;
while (i > 0) {
s += bit[i];
i -= i & -i;
}
return s;
}
int add(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & -i;
}
}
int main() {
cin >> n >> q;
vector<int> c(n);
rep(i, n) cin >> c[i];
rep(i, n) c[i]--;
vector<pair<P, int>> query(q);
rep(i, q) {
int l, r;
cin >> l >> r;
query[i] = make_pair(make_pair(r, l), i);
}
sort(query.begin(), query.end());
vector<int> gb(n, -1);
rep(i, n + 1) bit[i] = 0;
vector<int> ans(q);
int qi = 0;
rep(i, n) {
if (gb[c[i]] != -1)
add(gb[c[i]], -1);
gb[c[i]] = i + 1;
add(i + 1, 1);
while (query[qi].first.first == i + 1) {
int ai = query[qi].second;
int l = query[qi].first.second;
int r = query[qi].first.first;
ans[ai] = sum(r) - sum(l - 1);
qi++;
}
}
rep(i, q) cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int N = 500005;
// [1, n]
int bit[N], n, q;
int sum(int i) {
int s = 0;
while (i > 0) {
s += bit[i];
i -= i & -i;
}
return s;
}
void add(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & -i;
}
}
int main() {
cin >> n >> q;
vector<int> c(n);
rep(i, n) cin >> c[i];
rep(i, n) c[i]--;
vector<pair<P, int>> query(q);
rep(i, q) {
int l, r;
cin >> l >> r;
query[i] = make_pair(make_pair(r, l), i);
}
sort(query.begin(), query.end());
vector<int> gb(n, -1);
rep(i, n + 1) bit[i] = 0;
vector<int> ans(q);
int qi = 0;
rep(i, n) {
if (gb[c[i]] != -1)
add(gb[c[i]], -1);
gb[c[i]] = i + 1;
add(i + 1, 1);
while (query[qi].first.first == i + 1) {
int ai = query[qi].second;
int l = query[qi].first.second;
int r = query[qi].first.first;
ans[ai] = sum(r) - sum(l - 1);
qi++;
}
}
rep(i, q) cout << ans[i] << endl;
return 0;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e7, maxk = 1e7;
int root[maxn], L[maxn], R[maxn], sum[maxn];
int rt = 1, sz = 1;
int lpos[maxk];
int copy(int v, int &u) {
L[sz] = L[v];
R[sz] = R[v];
sum[sz] = sum[v];
return u = sz++;
}
void make_root() {
copy(root[rt - 1], root[rt]);
rt++;
}
void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
sum[v] += x;
if (r - l == 1)
return;
int m = (l + r) / 2;
if (pos < m)
add(pos, x, copy(L[v], L[v]), l, m);
else
add(pos, x, copy(R[v], R[v]), m, r);
}
int get(int a, int b, int v, int l = 0, int r = maxn) {
if (a <= l && r <= b)
return sum[v];
if (r <= a || b <= l)
return 0;
int m = (l + r) / 2;
return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
int t;
cin >> t;
make_root();
add(lpos[t], -1);
lpos[t] = i;
add(lpos[t], 1);
}
int l, r;
while (q--) {
cin >> l >> r;
cout << get(l, r + 1, root[r]) << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int maxn = 15e5, maxk = 1e7;
int root[maxn], L[16 * maxn], R[16 * maxn], sum[16 * maxn];
int rt = 1, sz = 1;
int lpos[maxk];
int copy(int v, int &u) {
L[sz] = L[v];
R[sz] = R[v];
sum[sz] = sum[v];
return u = sz++;
}
void make_root() {
copy(root[rt - 1], root[rt]);
rt++;
}
void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
sum[v] += x;
if (r - l == 1)
return;
int m = (l + r) / 2;
if (pos < m)
add(pos, x, copy(L[v], L[v]), l, m);
else
add(pos, x, copy(R[v], R[v]), m, r);
}
int get(int a, int b, int v, int l = 0, int r = maxn) {
if (a <= l && r <= b)
return sum[v];
if (r <= a || b <= l)
return 0;
int m = (l + r) / 2;
return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
int t;
cin >> t;
make_root();
add(lpos[t], -1);
lpos[t] = i;
add(lpos[t], 1);
}
int l, r;
while (q--) {
cin >> l >> r;
cout << get(l, r + 1, root[r]) << "\n";
}
return 0;
}
| replace | 4 | 6 | 4 | 6 | -11 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repA(i, a, b) for (int i = a; i <= b; i++)
#define repD(i, b, a) for (int i = b; i >= a; i--)
#define fill(a, val) memset(a, val, sizeof(a))
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int block;
const int maxN = 1e5 + 5;
int vis[maxN];
bool cmp(pair<int, pii> &a, pair<int, pii> &b) {
if (a.s.f / block == b.s.f / block)
return a.s.s < b.s.s;
return a.s.f / block < b.s.f / block;
}
int main() {
int n, q;
cin >> n >> q;
int a[n];
rep(i, 0, n) cin >> a[i];
block = sqrt(n);
pair<int, pii> p[q];
rep(i, 0, q) {
int l, r;
cin >> l >> r;
l--;
r--;
p[i].f = i;
p[i].s.f = l;
p[i].s.s = r;
}
sort(p, p + q, cmp);
int answer = 0;
int curl = 0, curr = 0;
fill(vis, 0);
int res[q];
rep(i, 0, q) {
int pos = p[i].f;
int l = p[i].s.f;
int r = p[i].s.s;
while (curl < l) {
vis[a[curl]]--;
if (vis[a[curl]] == 0)
answer--;
curl++;
}
while (curl > l) {
vis[a[curl - 1]]++;
if (vis[a[curl - 1]] == 1)
answer++;
curl--;
}
while (curr <= r) {
vis[a[curr]]++;
if (vis[a[curr]] == 1)
answer++;
curr++;
}
while (curr > r + 1) {
vis[a[curr - 1]]--;
if (vis[a[curr - 1]] == 0)
answer--;
curr--;
}
res[pos] = answer;
}
rep(i, 0, q) cout << res[i] << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repA(i, a, b) for (int i = a; i <= b; i++)
#define repD(i, b, a) for (int i = b; i >= a; i--)
#define fill(a, val) memset(a, val, sizeof(a))
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int block;
const int maxN = 5e5 + 5;
int vis[maxN];
bool cmp(pair<int, pii> &a, pair<int, pii> &b) {
if (a.s.f / block == b.s.f / block)
return a.s.s < b.s.s;
return a.s.f / block < b.s.f / block;
}
int main() {
int n, q;
cin >> n >> q;
int a[n];
rep(i, 0, n) cin >> a[i];
block = sqrt(n);
pair<int, pii> p[q];
rep(i, 0, q) {
int l, r;
cin >> l >> r;
l--;
r--;
p[i].f = i;
p[i].s.f = l;
p[i].s.s = r;
}
sort(p, p + q, cmp);
int answer = 0;
int curl = 0, curr = 0;
fill(vis, 0);
int res[q];
rep(i, 0, q) {
int pos = p[i].f;
int l = p[i].s.f;
int r = p[i].s.s;
while (curl < l) {
vis[a[curl]]--;
if (vis[a[curl]] == 0)
answer--;
curl++;
}
while (curl > l) {
vis[a[curl - 1]]++;
if (vis[a[curl - 1]] == 1)
answer++;
curl--;
}
while (curr <= r) {
vis[a[curr]]++;
if (vis[a[curr]] == 1)
answer++;
curr++;
}
while (curr > r + 1) {
vis[a[curr - 1]]--;
if (vis[a[curr - 1]] == 0)
answer--;
curr--;
}
res[pos] = answer;
}
rep(i, 0, q) cout << res[i] << endl;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02599 | C++ | Runtime Error | #ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, t) for (ll i = (ll)(s); i < (ll)(t); i++)
#define rrep(i, s, t) for (ll i = (ll)(s - 1); (ll)(t) <= i; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> Pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
constexpr ll INF = numeric_limits<ll>::max() / 4;
constexpr ll n_max = 2e5 + 10;
#define int ll
template <typename A, typename B> string to_string(pair<A, B> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *c) { return to_string((string)c); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++)
res += static_cast<char>('0' + v[i]);
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(const int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build(vector<Monoid> &vec) {
for (int i = 0; i < vec.size(); i++)
set(i, vec[i]);
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
void print() { cout << endl; }
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
cout << head;
if (sizeof...(tail) != 0)
cout << " ";
print(forward<Tail>(tail)...);
}
template <class T> void print(vector<T> &vec) {
for (auto &a : vec) {
cout << a;
if (&a != &vec.back())
cout << "\n";
}
cout << endl;
}
template <class T> void print(vector<vector<T>> &df) {
for (auto &vec : df) {
print(vec);
}
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n, q;
cin >> n >> q;
vector<ll> c(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
c[i]--;
}
auto f = [&](ll a, ll b) { return a + b; };
SegmentTree<ll> seg(n, f, 0);
vector<tuple<ll, ll, ll>> que(q);
rep(i, 0, q) {
ll l, r;
cin >> l >> r;
l--;
que[i] = {l, r, i};
}
sort(all(que));
vector<ll> cnt(n_max);
rep(i, 0, n) {
if (!cnt[c[i]])
seg.update(i, 1);
cnt[c[i]] = 1;
}
vector<ll> next(n, n);
fill(all(cnt), n);
rrep(i, n, 0) {
next[i] = cnt[c[i]];
cnt[c[i]] = i;
}
debug(next);
debug(seg.seg);
vector<ll> ans(q);
ll right = 0;
rep(i, 0, n) {
while (right < q && get<0>(que[right]) == i) {
auto &[l, r, id] = que[right];
ans[id] = seg.query(l, r);
right++;
}
if (next[i] < n)
seg.update(next[i], 1);
}
print(ans);
} | #ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, t) for (ll i = (ll)(s); i < (ll)(t); i++)
#define rrep(i, s, t) for (ll i = (ll)(s - 1); (ll)(t) <= i; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> Pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
constexpr ll INF = numeric_limits<ll>::max() / 4;
constexpr ll n_max = 2e5 + 10;
#define int ll
template <typename A, typename B> string to_string(pair<A, B> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *c) { return to_string((string)c); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++)
res += static_cast<char>('0' + v[i]);
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(const int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build(vector<Monoid> &vec) {
for (int i = 0; i < vec.size(); i++)
set(i, vec[i]);
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
void print() { cout << endl; }
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
cout << head;
if (sizeof...(tail) != 0)
cout << " ";
print(forward<Tail>(tail)...);
}
template <class T> void print(vector<T> &vec) {
for (auto &a : vec) {
cout << a;
if (&a != &vec.back())
cout << "\n";
}
cout << endl;
}
template <class T> void print(vector<vector<T>> &df) {
for (auto &vec : df) {
print(vec);
}
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n, q;
cin >> n >> q;
vector<ll> c(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
c[i]--;
}
auto f = [&](ll a, ll b) { return a + b; };
SegmentTree<ll> seg(n, f, 0);
vector<tuple<ll, ll, ll>> que(q);
rep(i, 0, q) {
ll l, r;
cin >> l >> r;
l--;
que[i] = {l, r, i};
}
sort(all(que));
vector<ll> cnt(n);
rep(i, 0, n) {
if (!cnt[c[i]])
seg.update(i, 1);
cnt[c[i]] = 1;
}
vector<ll> next(n, n);
fill(all(cnt), n);
rrep(i, n, 0) {
next[i] = cnt[c[i]];
cnt[c[i]] = i;
}
debug(next);
debug(seg.seg);
vector<ll> ans(q);
ll right = 0;
rep(i, 0, n) {
while (right < q && get<0>(que[right]) == i) {
auto &[l, r, id] = que[right];
ans[id] = seg.query(l, r);
right++;
}
if (next[i] < n)
seg.update(next[i], 1);
}
print(ans);
} | replace | 212 | 213 | 212 | 213 | 0 | |
p02599 | C++ | Time Limit Exceeded | // HEADER FILES AND NAMESPACES
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;
// DEFINE STATEMENTS
#define num1 1000000007
#define num2 998244353
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define REPd(i, a, n) for (ll i = a; i >= n; i--)
#define pb push_back
#define pob pop_back
#define f first
#define s second
#define fix(f, n) std::fixed << std::setprecision(n) << f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.000000001)
typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<int> vii;
// DEBUG FUNCTIONS
#ifndef ONLINE_JUDGE
template <typename T> void __p(T a) { cout << a; }
template <typename T, typename F> void __p(pair<T, F> a) {
cout << "{";
__p(a.first);
cout << ",";
__p(a.second);
cout << "}";
}
template <typename T> void __p(std::vector<T> a) {
cout << "{";
for (auto it = a.begin(); it < a.end(); it++)
__p(*it), cout << ",}"[it + 1 == a.end()];
}
template <typename T> void __p(std::set<T> a) {
cout << "{";
for (auto it = a.begin(); it != a.end();) {
__p(*it);
cout << ",}"[++it == a.end()];
}
}
template <typename T> void __p(std::multiset<T> a) {
cout << "{";
for (auto it = a.begin(); it != a.end();) {
__p(*it);
cout << ",}"[++it == a.end()];
}
}
template <typename T, typename F> void __p(std::map<T, F> a) {
cout << "{\n";
for (auto it = a.begin(); it != a.end(); ++it) {
__p(it->first);
cout << ": ";
__p(it->second);
cout << "\n";
}
cout << "}\n";
}
template <typename T, typename... Arg> void __p(T a1, Arg... a) {
__p(a1);
__p(a...);
}
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cout << name << " : ";
__p(arg1);
cout << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
int bracket = 0, i = 0;
for (;; i++)
if (names[i] == ',' && bracket == 0)
break;
else if (names[i] == '(')
bracket++;
else if (names[i] == ')')
bracket--;
const char *comma = names + i;
cout.write(names, comma - names) << " : ";
__p(arg1);
cout << " | ";
__f(comma + 1, args...);
}
#define trace(...) \
cout << "Line:" << __LINE__ << " ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#endif
// DEBUG FUNCTIONS END
// CUSTOM HASH TO SPEED UP UNORDERED MAP AND TO AVOID FORCED CLASHES
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
mt19937_64 rng(chrono::steady_clock::now()
.time_since_epoch()
.count()); // FOR RANDOM NUMBER GENERATION
ll mod_exp(ll a, ll b, ll c) {
ll res = 1;
a = a % c;
while (b > 0) {
if (b % 2 == 1)
res = (res * a) % c;
b /= 2;
a = (a * a) % c;
}
return res;
}
ll mymod(ll a, ll b) { return ((a % b) + b) % b; }
ll gcdExtended(ll, ll, ll *, ll *);
ll modInverse(ll a, ll m) {
ll x, y;
ll g = gcdExtended(a, m, &x, &y);
g++; // this line was added just to remove compiler warning
ll res = (x % m + m) % m;
return res;
}
ll gcdExtended(ll a, ll b, ll *x, ll *y) {
if (a == 0) {
*x = 0, *y = 1;
return b;
}
ll x1, y1;
ll gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
struct Query {
ll l, r, index;
};
const int xx = 300;
bool cmp(Query a, Query b) {
if (a.l / xx < b.l / xx) {
return true;
} else if (a.l / xx == b.l / xx && a.r < b.r) {
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, q;
cin >> n >> q;
vll freq(n + 1, 0);
ll init = 0;
ll A[n];
REP(i, 0, n) {
cin >> A[i];
freq[A[i]]++;
if (freq[A[i]] == 1)
init++;
}
Query Q[q];
REP(i, 0, q) {
cin >> Q[i].l >> Q[i].r;
Q[i].l--;
Q[i].r--;
Q[i].index = i;
}
vll ans(q, -1);
sort(Q, Q + q, cmp);
ll left = 0, right = n - 1;
ll cur_ans = init;
REP(i, 0, q) {
if (Q[i].l < left) {
// cerr << "1" << endl;
REP(j, Q[i].l, left) {
freq[A[j]]++;
if (freq[A[j]] == 1)
cur_ans++;
}
} else if (Q[i].l > left) {
// cerr << "2" << endl;
REP(j, left, Q[i].l) {
freq[A[j]]--;
if (freq[A[j]] == 0)
cur_ans--;
}
}
if (Q[i].r > right) {
// cerr << "3" << endl;
REP(j, right + 1, Q[i].r + 1) {
freq[A[j]]++;
if (freq[A[j]] == 1)
cur_ans++;
}
} else if (Q[i].r < right) {
// cerr << "4" << endl;
REP(j, Q[i].r + 1, right + 1) {
freq[A[j]]--;
if (freq[A[j]] == 0)
cur_ans--;
}
}
// trace("here");
ans[Q[i].index] = cur_ans;
left = Q[i].l;
right = Q[i].r;
}
REP(i, 0, q) { cout << ans[i] << "\n"; }
return 0;
}
| // HEADER FILES AND NAMESPACES
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;
// DEFINE STATEMENTS
#define num1 1000000007
#define num2 998244353
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define REPd(i, a, n) for (ll i = a; i >= n; i--)
#define pb push_back
#define pob pop_back
#define f first
#define s second
#define fix(f, n) std::fixed << std::setprecision(n) << f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.000000001)
typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<int> vii;
// DEBUG FUNCTIONS
#ifndef ONLINE_JUDGE
template <typename T> void __p(T a) { cout << a; }
template <typename T, typename F> void __p(pair<T, F> a) {
cout << "{";
__p(a.first);
cout << ",";
__p(a.second);
cout << "}";
}
template <typename T> void __p(std::vector<T> a) {
cout << "{";
for (auto it = a.begin(); it < a.end(); it++)
__p(*it), cout << ",}"[it + 1 == a.end()];
}
template <typename T> void __p(std::set<T> a) {
cout << "{";
for (auto it = a.begin(); it != a.end();) {
__p(*it);
cout << ",}"[++it == a.end()];
}
}
template <typename T> void __p(std::multiset<T> a) {
cout << "{";
for (auto it = a.begin(); it != a.end();) {
__p(*it);
cout << ",}"[++it == a.end()];
}
}
template <typename T, typename F> void __p(std::map<T, F> a) {
cout << "{\n";
for (auto it = a.begin(); it != a.end(); ++it) {
__p(it->first);
cout << ": ";
__p(it->second);
cout << "\n";
}
cout << "}\n";
}
template <typename T, typename... Arg> void __p(T a1, Arg... a) {
__p(a1);
__p(a...);
}
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cout << name << " : ";
__p(arg1);
cout << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
int bracket = 0, i = 0;
for (;; i++)
if (names[i] == ',' && bracket == 0)
break;
else if (names[i] == '(')
bracket++;
else if (names[i] == ')')
bracket--;
const char *comma = names + i;
cout.write(names, comma - names) << " : ";
__p(arg1);
cout << " | ";
__f(comma + 1, args...);
}
#define trace(...) \
cout << "Line:" << __LINE__ << " ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#endif
// DEBUG FUNCTIONS END
// CUSTOM HASH TO SPEED UP UNORDERED MAP AND TO AVOID FORCED CLASHES
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
mt19937_64 rng(chrono::steady_clock::now()
.time_since_epoch()
.count()); // FOR RANDOM NUMBER GENERATION
ll mod_exp(ll a, ll b, ll c) {
ll res = 1;
a = a % c;
while (b > 0) {
if (b % 2 == 1)
res = (res * a) % c;
b /= 2;
a = (a * a) % c;
}
return res;
}
ll mymod(ll a, ll b) { return ((a % b) + b) % b; }
ll gcdExtended(ll, ll, ll *, ll *);
ll modInverse(ll a, ll m) {
ll x, y;
ll g = gcdExtended(a, m, &x, &y);
g++; // this line was added just to remove compiler warning
ll res = (x % m + m) % m;
return res;
}
ll gcdExtended(ll a, ll b, ll *x, ll *y) {
if (a == 0) {
*x = 0, *y = 1;
return b;
}
ll x1, y1;
ll gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
struct Query {
ll l, r, index;
};
const int xx = 750;
bool cmp(Query a, Query b) {
if (a.l / xx < b.l / xx) {
return true;
} else if (a.l / xx == b.l / xx && a.r < b.r) {
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, q;
cin >> n >> q;
vll freq(n + 1, 0);
ll init = 0;
ll A[n];
REP(i, 0, n) {
cin >> A[i];
freq[A[i]]++;
if (freq[A[i]] == 1)
init++;
}
Query Q[q];
REP(i, 0, q) {
cin >> Q[i].l >> Q[i].r;
Q[i].l--;
Q[i].r--;
Q[i].index = i;
}
vll ans(q, -1);
sort(Q, Q + q, cmp);
ll left = 0, right = n - 1;
ll cur_ans = init;
REP(i, 0, q) {
if (Q[i].l < left) {
// cerr << "1" << endl;
REP(j, Q[i].l, left) {
freq[A[j]]++;
if (freq[A[j]] == 1)
cur_ans++;
}
} else if (Q[i].l > left) {
// cerr << "2" << endl;
REP(j, left, Q[i].l) {
freq[A[j]]--;
if (freq[A[j]] == 0)
cur_ans--;
}
}
if (Q[i].r > right) {
// cerr << "3" << endl;
REP(j, right + 1, Q[i].r + 1) {
freq[A[j]]++;
if (freq[A[j]] == 1)
cur_ans++;
}
} else if (Q[i].r < right) {
// cerr << "4" << endl;
REP(j, Q[i].r + 1, right + 1) {
freq[A[j]]--;
if (freq[A[j]] == 0)
cur_ans--;
}
}
// trace("here");
ans[Q[i].index] = cur_ans;
left = Q[i].l;
right = Q[i].r;
}
REP(i, 0, q) { cout << ans[i] << "\n"; }
return 0;
}
| replace | 166 | 167 | 166 | 167 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(obj) (obj).begin(), (obj).end()
#define SPEED \
cin.tie(0); \
ios::sync_with_stdio(false);
template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;
constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;
template <class T> vector<T> multivector(size_t N, T init) {
return vector<T>(N, init);
}
template <class... T> auto multivector(size_t N, T... t) {
return vector<decltype(multivector(t...))>(N, multivector(t...));
}
template <class T> void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
exit(0);
}
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
o << "{";
for (auto &x : obj)
o << " {" << x.first << " : " << x.second << "}"
<< ",";
o << " }";
return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
template <class T> ostream &operator<<(ostream &o, const multiset<T> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
o << "{";
for (int i = 0; i < (int)obj.size(); ++i)
o << (i > 0 ? ", " : "") << obj[i];
o << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
o << "{" << obj.first << ", " << obj.second << "}";
return o;
}
void print(void) { cout << endl; }
template <class Head> void print(Head &&head) {
cout << head;
print();
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
cout << head << " ";
print(forward<Tail>(tail)...);
}
template <class T> void chmax(T &a, const T b) { a = max(a, b); }
template <class T> void chmin(T &a, const T b) { a = min(a, b); }
std::vector<std::string> split(const std::string &str, const char delemiter) {
std::vector<std::string> res;
std::stringstream ss(str);
std::string buffer;
while (std::getline(ss, buffer, delemiter))
res.push_back(buffer);
return res;
}
int msb(int x) { return x ? 31 - __builtin_clz(x) : -1; }
void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; }
template <class T> class Mo {
unordered_map<long long, int> mp;
long long N;
int bucket;
vector<pair<int, int>> range;
vector<int> idx;
public:
Mo(ll N, const vector<pair<int, int>> &range)
: N(N), range(range), idx(range.size()), bucket(700) {
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int a, int b) {
auto al = range[a].first / bucket;
auto ar = range[a].second;
auto bl = range[b].first / bucket;
auto br = range[b].second;
return (al != bl) ? (al < bl) : ((al % 2) ? (ar > br) : (ar < br));
});
}
// 参照でvectorを渡したりすると良い
void solve(const vector<int> &A, vector<int> &cnt, int &sum) {
int l = 0, r = 0;
for (int &i : idx) {
auto &xl = range[i].first;
auto &xr = range[i].second;
// 左端を広げる
while (xl < l) {
l--;
extend(l, A, cnt, sum);
}
// 右端を広げる
while (r < xr) {
r++;
extend(r, A, cnt, sum);
}
// 左端を狭める
while (l < xl) {
shrink(l, A, cnt, sum);
l++;
}
// 右端を狭める
while (xr < r) {
shrink(r, A, cnt, sum);
r--;
}
mp[xl * N + xr] = sum;
// print("?",range[i]);
// print(cnt);
// print(sum);
// print(A);
}
}
T &operator[](pair<int, int> p) { return mp[p.first * N + p.second]; }
private:
// 伸びるとき
void extend(int k, const vector<int> &A, vector<int> &cnt, int &sum) {
cnt[A[k]]++;
if (cnt[A[k]] == 1)
sum++;
}
// 縮むとき
void shrink(int k, const vector<int> &A, vector<int> &cnt, int &sum) {
cnt[A[k]]--;
if (cnt[A[k]] == 0)
sum--;
}
// クエリ結果
T get(set<int> &sum) { return sum; }
};
int main() {
int N, Q;
scanf("%d %d", &N, &Q);
vector<int> A(N);
for (int i = 0; i < N; ++i)
scanf("%d", &A[i]);
// クエリ区間を列挙
vector<pair<int, int>> range(Q);
for (int i = 0; i < Q; ++i) {
int l, r;
cin >> l >> r;
l--, r--;
range[i] = {l, r};
}
// Mo O(N*sqrt(N)*(logN))
Mo<int> mo(N, range);
vector<int> cnt(N + 1, 0);
int sum = 1;
cnt[A[0]]++;
mo.solve(A, cnt, sum);
for (int i = 0; i < Q; ++i) {
printf("%d\n", mo[range[i]]);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(obj) (obj).begin(), (obj).end()
#define SPEED \
cin.tie(0); \
ios::sync_with_stdio(false);
template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;
constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;
template <class T> vector<T> multivector(size_t N, T init) {
return vector<T>(N, init);
}
template <class... T> auto multivector(size_t N, T... t) {
return vector<decltype(multivector(t...))>(N, multivector(t...));
}
template <class T> void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
exit(0);
}
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
o << "{";
for (auto &x : obj)
o << " {" << x.first << " : " << x.second << "}"
<< ",";
o << " }";
return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
template <class T> ostream &operator<<(ostream &o, const multiset<T> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
o << "{";
for (int i = 0; i < (int)obj.size(); ++i)
o << (i > 0 ? ", " : "") << obj[i];
o << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
o << "{" << obj.first << ", " << obj.second << "}";
return o;
}
void print(void) { cout << endl; }
template <class Head> void print(Head &&head) {
cout << head;
print();
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
cout << head << " ";
print(forward<Tail>(tail)...);
}
template <class T> void chmax(T &a, const T b) { a = max(a, b); }
template <class T> void chmin(T &a, const T b) { a = min(a, b); }
std::vector<std::string> split(const std::string &str, const char delemiter) {
std::vector<std::string> res;
std::stringstream ss(str);
std::string buffer;
while (std::getline(ss, buffer, delemiter))
res.push_back(buffer);
return res;
}
int msb(int x) { return x ? 31 - __builtin_clz(x) : -1; }
void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; }
template <class T> class Mo {
unordered_map<long long, int> mp;
long long N;
int bucket;
vector<pair<int, int>> range;
vector<int> idx;
public:
Mo(ll N, const vector<pair<int, int>> &range)
: N(N), range(range), idx(range.size()), bucket(sqrt(N)) {
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int a, int b) {
auto al = range[a].first / bucket;
auto ar = range[a].second;
auto bl = range[b].first / bucket;
auto br = range[b].second;
return (al != bl) ? (al < bl) : ((al % 2) ? (ar > br) : (ar < br));
});
}
// 参照でvectorを渡したりすると良い
void solve(const vector<int> &A, vector<int> &cnt, int &sum) {
int l = 0, r = 0;
for (int &i : idx) {
auto &xl = range[i].first;
auto &xr = range[i].second;
// 左端を広げる
while (xl < l) {
l--;
extend(l, A, cnt, sum);
}
// 右端を広げる
while (r < xr) {
r++;
extend(r, A, cnt, sum);
}
// 左端を狭める
while (l < xl) {
shrink(l, A, cnt, sum);
l++;
}
// 右端を狭める
while (xr < r) {
shrink(r, A, cnt, sum);
r--;
}
mp[xl * N + xr] = sum;
// print("?",range[i]);
// print(cnt);
// print(sum);
// print(A);
}
}
T &operator[](pair<int, int> p) { return mp[p.first * N + p.second]; }
private:
// 伸びるとき
void extend(int k, const vector<int> &A, vector<int> &cnt, int &sum) {
cnt[A[k]]++;
if (cnt[A[k]] == 1)
sum++;
}
// 縮むとき
void shrink(int k, const vector<int> &A, vector<int> &cnt, int &sum) {
cnt[A[k]]--;
if (cnt[A[k]] == 0)
sum--;
}
// クエリ結果
T get(set<int> &sum) { return sum; }
};
int main() {
int N, Q;
scanf("%d %d", &N, &Q);
vector<int> A(N);
for (int i = 0; i < N; ++i)
scanf("%d", &A[i]);
// クエリ区間を列挙
vector<pair<int, int>> range(Q);
for (int i = 0; i < Q; ++i) {
int l, r;
cin >> l >> r;
l--, r--;
range[i] = {l, r};
}
// Mo O(N*sqrt(N)*(logN))
Mo<int> mo(N, range);
vector<int> cnt(N + 1, 0);
int sum = 1;
cnt[A[0]]++;
mo.solve(A, cnt, sum);
for (int i = 0; i < Q; ++i) {
printf("%d\n", mo[range[i]]);
}
return 0;
}
| replace | 98 | 99 | 98 | 99 | TLE | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
// #define N (998244353)
#define INF 1e16
typedef long long ll;
typedef pair<int, int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
template <typename T> struct BIT {
private:
vector<T> array;
int length;
public:
BIT(int _n = 0) : array(_n + 1), length(_n) {}
T sum(int i) {
T s = 0;
while (i > 0) {
s += array[i];
i -= i & (-i);
}
return s;
}
T sum(int i, int j) {
T ret1 = sum(i - 1);
T ret2 = sum(j);
return ret2 - ret1;
}
void add(int i, T x) {
while (i <= length) {
array[i] += x;
i += i & (-i);
}
}
};
int main(void) {
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
vector<int> pi(n + 1, -1);
vector<vector<int>> ps(n + 1);
for (int i = 0; i < n; i++) {
int l = pi[a[i]];
if (l != -1)
ps[l].push_back(i + 1);
pi[a[i]] = i + 1;
}
vector<vector<P>> qs(n + 1);
for (int qi = 0; qi < q; qi++) {
int l, r;
cin >> l >> r;
qs[l].emplace_back(r, qi);
}
BIT<int> d(n + 1);
vector<int> ans(n);
for (int x = n; x >= 1; x--) {
for (int y : ps[x]) {
d.add(y, 1);
}
for (P query : qs[x]) {
int r = query.first, i = query.second;
ans[i] = r - x + 1 - d.sum(r);
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << endl;
}
} | #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
// #define N (998244353)
#define INF 1e16
typedef long long ll;
typedef pair<int, int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
template <typename T> struct BIT {
private:
vector<T> array;
int length;
public:
BIT(int _n = 0) : array(_n + 1), length(_n) {}
T sum(int i) {
T s = 0;
while (i > 0) {
s += array[i];
i -= i & (-i);
}
return s;
}
T sum(int i, int j) {
T ret1 = sum(i - 1);
T ret2 = sum(j);
return ret2 - ret1;
}
void add(int i, T x) {
while (i <= length) {
array[i] += x;
i += i & (-i);
}
}
};
int main(void) {
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
vector<int> pi(n + 1, -1);
vector<vector<int>> ps(n + 1);
for (int i = 0; i < n; i++) {
int l = pi[a[i]];
if (l != -1)
ps[l].push_back(i + 1);
pi[a[i]] = i + 1;
}
vector<vector<P>> qs(n + 1);
for (int qi = 0; qi < q; qi++) {
int l, r;
cin >> l >> r;
qs[l].emplace_back(r, qi);
}
BIT<int> d(n + 1);
vector<int> ans(q);
for (int x = n; x >= 1; x--) {
for (int y : ps[x]) {
d.add(y, 1);
}
for (P query : qs[x]) {
int r = query.first, i = query.second;
ans[i] = r - x + 1 - d.sum(r);
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << endl;
}
} | replace | 72 | 73 | 72 | 73 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
// [a -> b-1]
#define reps(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
// [0 -> a-1]
#define rep(i, a) reps(i, 0, (a))
// [a-1 -> b]
#define rreps(i, a, b) for (int i = (int)((a)-1); i >= (int)(b); i--)
// [a-1 -> 0]
#define rrep(i, a) rreps(i, a, 0)
#define all(v) (v).begin(), (v).end()
// next_permutation(all(v))
#define PERM(v) next_permutation(all(v))
/*sort(all(v));
* (v).erase(unique(all(v)), v.end())*/
#define UNIQUE(v) \
sort(all(v)); \
(v).erase(unique(all(v)), v.end())
// b is [a, c)
#define RANGE(a, b, c) ((a) <= (b) && (b) < (c))
// c is [a, e) && d is [b, f)
#define RANGE2D(a, b, c, d, e, f) (RANGE((a), (c), (e)) && RANGE((b), (d), (f)))
#define chmin(a, b) a = min(a, (b))
#define chmax(a, b) a = max(a, (b))
#define fcout cout << fixed << setprecision(15)
#define YES(f) cout << ((f) ? YES_STR : NO_STR) << endl;
#ifdef LOCAL_DEBUG_OPTION
namespace debug_print_n {
struct DebugPrint {
template <class T, class U = remove_const_t<remove_reference_t<T>>>
static enable_if_t<is_integral<U>::value && !is_same<U, bool>::value &&
!is_same<U, char>::value>
DUMP_SUB_FUNCTION(T x) {
cerr << x;
}
template <class T, class U = remove_const_t<remove_reference_t<T>>>
static enable_if_t<is_same<U, bool>::value> DUMP_SUB_FUNCTION(T x) {
if (x)
cerr << "True";
else
cerr << "False";
}
template <class T, class U = remove_const_t<remove_reference_t<T>>>
static enable_if_t<is_same<U, char>::value> DUMP_SUB_FUNCTION(T x) {
cerr << '\'' << x << '\'';
}
template <class T, class U = remove_const_t<remove_reference_t<T>>>
static enable_if_t<is_same<U, string>::value> DUMP_SUB_FUNCTION(T x) {
cerr << '\"' << x << '\"';
}
template <class T>
static enable_if_t<is_same<T, const char *>::value> DUMP_SUB_FUNCTION(T x) {
cerr << string(x);
}
template <class T> static enable_if_t<true> DUMP_SUB_FUNCTION(vector<T> x) {
cerr << '{';
for (size_t i = 0; i < x.size(); i++) {
if (i)
cerr << ", ";
DUMP_SUB_FUNCTION(x[i]);
}
cerr << '}';
}
template <class T, size_t N>
static enable_if_t<true> DUMP_SUB_FUNCTION(array<T, N> x) {
cerr << '{';
for (size_t i = 0; i < x.size(); i++) {
if (i)
cerr << ", ";
DUMP_SUB_FUNCTION(x[i]);
}
cerr << '}';
}
template <class T> static enable_if_t<true> DUMP_SUB_FUNCTION(set<T> x) {
cerr << '[';
size_t i = 0;
for (auto itr = x.begin(); itr != x.end(); itr++, i++) {
if (i)
cerr << ", ";
DUMP_SUB_FUNCTION(*itr);
}
cerr << ']';
}
template <class T, class U>
static enable_if_t<true> DUMP_SUB_FUNCTION(map<T, U> x) {
cerr << '[';
size_t i = 0;
for (auto itr = x.begin(); itr != x.end(); itr++, i++) {
if (i)
cerr << ", ";
DUMP_SUB_FUNCTION(itr->first);
cerr << " : ";
DUMP_SUB_FUNCTION(itr->second);
}
cerr << ']';
}
template <class T, class U = remove_const_t<remove_reference_t<T>>>
static enable_if_t<is_floating_point<U>::value> DUMP_SUB_FUNCTION(T x) {
cerr << fixed << setprecision(5) << x;
}
template <class T, class U>
static enable_if_t<true> DUMP_SUB_FUNCTION(pair<T, U> p) {
cerr << '(';
DUMP_SUB_FUNCTION(p.first);
cerr << ", ";
DUMP_SUB_FUNCTION(p.second);
cerr << ')';
}
template <int num = 0, class... Args>
static enable_if_t<num == sizeof...(Args)>
DUMP_SUB_FUNCTION(tuple<Args...> t) {
if (num)
cerr << ')';
}
template <int num = 0, class... Args>
static enable_if_t <
num<sizeof...(Args)> DUMP_SUB_FUNCTION(tuple<Args...> t) {
if (num == 0)
cerr << '(';
DUMP_SUB_FUNCTION(get<num>(t));
if (num + 1 < sizeof...(Args))
cerr << ", ";
DUMP_SUB_FUNCTION<num + 1>(t);
}
template <long unsigned N>
static enable_if_t<true> DUMP_SUB_FUNCTION(bitset<N> x) {
cerr << x;
}
};
void dump() { cerr << endl; }
template <class Head, class... Tail> void dump(Head &&head, Tail &&...tail) {
DebugPrint::DUMP_SUB_FUNCTION(head);
if (sizeof...(Tail) != 0) {
cerr << ' ';
}
dump(forward<Tail>(tail)...);
}
} // namespace debug_print_n
using debug_print_n::dump;
#else
template <class... Args> void dump(Args &&...args) {}
#endif
namespace bit {
using u32 = uint_fast32_t;
using u64 = unsigned long long;
template <class T> constexpr T set(T bin, u32 pos) {
return bin | (1ull << pos);
}
template <class T> constexpr T unset(T bin, u32 pos) {
return bin & ~(1ull << pos);
}
template <class T> constexpr T get(T bin, u32 pos) {
return bin & (1ull << pos);
}
template <class T> constexpr bool check(T bin, u32 pos) {
return get(bin, pos);
}
constexpr u32 popcount(u64 bin) { return __builtin_popcountll(bin); }
} // namespace bit
/**
* @title BIT (Binary Indexed Tree)
* @brief 0-indexed 半開区間で扱う。フェニック木 (Fenwick Tree) とも呼ばれる。
*/
template <class T> struct BIT {
int n;
vector<T> bit;
// @brief 長さ N で初期化 O(N)$
BIT(int n_) : n(n_), bit(n) {}
// @brief vector で初期化 O(N)$
BIT(const vector<T> &v) : n(v.size()), bit(v) {
for (int i = 0; i < n - 1; i++)
if ((i | (i + 1)) < n)
bit[i | (i + 1)] += bit[i];
}
// @brief i 番目の要素に v を足す O(\log N)$
// 0-indexed
void add(int i, T v) {
for (; i < n; i |= i + 1)
bit[i] += v;
}
// @brief [0, i) の区間の和 の計算 O(\log N)$
// 0-indexed 半開区間
T get(int i) {
T res = 0;
for (i--; i >= 0; i = (i & (i + 1)) - 1)
res += bit[i];
return res;
}
// @brief [i, j) の区間の和 の計算 O(\log N)$
// 0-indexed 半開区間
T get(int i, int j) { return get(j) - get(i); }
};
signed main() {
int N, Q;
cin >> N >> Q;
vector<tuple<int, int, int>> LR;
vector<unsigned int> c(N);
rep(i, N) cin >> c[i], c[i]--;
rep(i, Q) {
int l, r;
cin >> l >> r;
l--;
LR.emplace_back(l, r, i);
}
sort(all(LR), [](auto &a, auto &b) { return get<1>(a) < get<1>(b); });
vector<int> LA(N, -1);
vector<int> res(N);
BIT<int> B(N);
int r = 0;
for (auto [L, R, I] : LR) {
while (r < R) {
if (LA[c[r]] >= 0)
B.add(LA[c[r]], -1);
LA[c[r]] = r;
B.add(r, 1);
r++;
}
res[I] = B.get(L, R);
}
rep(i, Q) { cout << res[i] << endl; }
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
// [a -> b-1]
#define reps(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
// [0 -> a-1]
#define rep(i, a) reps(i, 0, (a))
// [a-1 -> b]
#define rreps(i, a, b) for (int i = (int)((a)-1); i >= (int)(b); i--)
// [a-1 -> 0]
#define rrep(i, a) rreps(i, a, 0)
#define all(v) (v).begin(), (v).end()
// next_permutation(all(v))
#define PERM(v) next_permutation(all(v))
/*sort(all(v));
* (v).erase(unique(all(v)), v.end())*/
#define UNIQUE(v) \
sort(all(v)); \
(v).erase(unique(all(v)), v.end())
// b is [a, c)
#define RANGE(a, b, c) ((a) <= (b) && (b) < (c))
// c is [a, e) && d is [b, f)
#define RANGE2D(a, b, c, d, e, f) (RANGE((a), (c), (e)) && RANGE((b), (d), (f)))
#define chmin(a, b) a = min(a, (b))
#define chmax(a, b) a = max(a, (b))
#define fcout cout << fixed << setprecision(15)
#define YES(f) cout << ((f) ? YES_STR : NO_STR) << endl;
#ifdef LOCAL_DEBUG_OPTION
namespace debug_print_n {
struct DebugPrint {
template <class T, class U = remove_const_t<remove_reference_t<T>>>
static enable_if_t<is_integral<U>::value && !is_same<U, bool>::value &&
!is_same<U, char>::value>
DUMP_SUB_FUNCTION(T x) {
cerr << x;
}
template <class T, class U = remove_const_t<remove_reference_t<T>>>
static enable_if_t<is_same<U, bool>::value> DUMP_SUB_FUNCTION(T x) {
if (x)
cerr << "True";
else
cerr << "False";
}
template <class T, class U = remove_const_t<remove_reference_t<T>>>
static enable_if_t<is_same<U, char>::value> DUMP_SUB_FUNCTION(T x) {
cerr << '\'' << x << '\'';
}
template <class T, class U = remove_const_t<remove_reference_t<T>>>
static enable_if_t<is_same<U, string>::value> DUMP_SUB_FUNCTION(T x) {
cerr << '\"' << x << '\"';
}
template <class T>
static enable_if_t<is_same<T, const char *>::value> DUMP_SUB_FUNCTION(T x) {
cerr << string(x);
}
template <class T> static enable_if_t<true> DUMP_SUB_FUNCTION(vector<T> x) {
cerr << '{';
for (size_t i = 0; i < x.size(); i++) {
if (i)
cerr << ", ";
DUMP_SUB_FUNCTION(x[i]);
}
cerr << '}';
}
template <class T, size_t N>
static enable_if_t<true> DUMP_SUB_FUNCTION(array<T, N> x) {
cerr << '{';
for (size_t i = 0; i < x.size(); i++) {
if (i)
cerr << ", ";
DUMP_SUB_FUNCTION(x[i]);
}
cerr << '}';
}
template <class T> static enable_if_t<true> DUMP_SUB_FUNCTION(set<T> x) {
cerr << '[';
size_t i = 0;
for (auto itr = x.begin(); itr != x.end(); itr++, i++) {
if (i)
cerr << ", ";
DUMP_SUB_FUNCTION(*itr);
}
cerr << ']';
}
template <class T, class U>
static enable_if_t<true> DUMP_SUB_FUNCTION(map<T, U> x) {
cerr << '[';
size_t i = 0;
for (auto itr = x.begin(); itr != x.end(); itr++, i++) {
if (i)
cerr << ", ";
DUMP_SUB_FUNCTION(itr->first);
cerr << " : ";
DUMP_SUB_FUNCTION(itr->second);
}
cerr << ']';
}
template <class T, class U = remove_const_t<remove_reference_t<T>>>
static enable_if_t<is_floating_point<U>::value> DUMP_SUB_FUNCTION(T x) {
cerr << fixed << setprecision(5) << x;
}
template <class T, class U>
static enable_if_t<true> DUMP_SUB_FUNCTION(pair<T, U> p) {
cerr << '(';
DUMP_SUB_FUNCTION(p.first);
cerr << ", ";
DUMP_SUB_FUNCTION(p.second);
cerr << ')';
}
template <int num = 0, class... Args>
static enable_if_t<num == sizeof...(Args)>
DUMP_SUB_FUNCTION(tuple<Args...> t) {
if (num)
cerr << ')';
}
template <int num = 0, class... Args>
static enable_if_t <
num<sizeof...(Args)> DUMP_SUB_FUNCTION(tuple<Args...> t) {
if (num == 0)
cerr << '(';
DUMP_SUB_FUNCTION(get<num>(t));
if (num + 1 < sizeof...(Args))
cerr << ", ";
DUMP_SUB_FUNCTION<num + 1>(t);
}
template <long unsigned N>
static enable_if_t<true> DUMP_SUB_FUNCTION(bitset<N> x) {
cerr << x;
}
};
void dump() { cerr << endl; }
template <class Head, class... Tail> void dump(Head &&head, Tail &&...tail) {
DebugPrint::DUMP_SUB_FUNCTION(head);
if (sizeof...(Tail) != 0) {
cerr << ' ';
}
dump(forward<Tail>(tail)...);
}
} // namespace debug_print_n
using debug_print_n::dump;
#else
template <class... Args> void dump(Args &&...args) {}
#endif
namespace bit {
using u32 = uint_fast32_t;
using u64 = unsigned long long;
template <class T> constexpr T set(T bin, u32 pos) {
return bin | (1ull << pos);
}
template <class T> constexpr T unset(T bin, u32 pos) {
return bin & ~(1ull << pos);
}
template <class T> constexpr T get(T bin, u32 pos) {
return bin & (1ull << pos);
}
template <class T> constexpr bool check(T bin, u32 pos) {
return get(bin, pos);
}
constexpr u32 popcount(u64 bin) { return __builtin_popcountll(bin); }
} // namespace bit
/**
* @title BIT (Binary Indexed Tree)
* @brief 0-indexed 半開区間で扱う。フェニック木 (Fenwick Tree) とも呼ばれる。
*/
template <class T> struct BIT {
int n;
vector<T> bit;
// @brief 長さ N で初期化 O(N)$
BIT(int n_) : n(n_), bit(n) {}
// @brief vector で初期化 O(N)$
BIT(const vector<T> &v) : n(v.size()), bit(v) {
for (int i = 0; i < n - 1; i++)
if ((i | (i + 1)) < n)
bit[i | (i + 1)] += bit[i];
}
// @brief i 番目の要素に v を足す O(\log N)$
// 0-indexed
void add(int i, T v) {
for (; i < n; i |= i + 1)
bit[i] += v;
}
// @brief [0, i) の区間の和 の計算 O(\log N)$
// 0-indexed 半開区間
T get(int i) {
T res = 0;
for (i--; i >= 0; i = (i & (i + 1)) - 1)
res += bit[i];
return res;
}
// @brief [i, j) の区間の和 の計算 O(\log N)$
// 0-indexed 半開区間
T get(int i, int j) { return get(j) - get(i); }
};
signed main() {
int N, Q;
cin >> N >> Q;
vector<tuple<int, int, int>> LR;
vector<unsigned int> c(N);
rep(i, N) cin >> c[i], c[i]--;
rep(i, Q) {
int l, r;
cin >> l >> r;
l--;
LR.emplace_back(l, r, i);
}
sort(all(LR), [](auto &a, auto &b) { return get<1>(a) < get<1>(b); });
vector<int> LA(N, -1);
vector<int> res(Q);
BIT<int> B(N);
int r = 0;
for (auto [L, R, I] : LR) {
while (r < R) {
if (LA[c[r]] >= 0)
B.add(LA[c[r]], -1);
LA[c[r]] = r;
B.add(r, 1);
r++;
}
res[I] = B.get(L, R);
}
rep(i, Q) { cout << res[i] << endl; }
}
| replace | 213 | 214 | 213 | 214 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define ll long long
using namespace std;
int read() {
int k = 0, f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-')
f = -1;
for (; isdigit(c); c = getchar())
k = k * 10 + c - '0';
return k * f;
}
const int N = 200055;
int n, m, tot, a[N], rt[N], lch[N * 30], rch[N * 30], sum[N * 30];
void add(int &k, int pre, int l, int r, int a, int b) {
k = ++tot;
sum[k] = sum[pre] + b;
if (l == r)
return;
int mid = l + r >> 1;
if (a <= mid)
rch[k] = rch[pre], add(lch[k], lch[pre], l, mid, a, b);
else
lch[k] = lch[pre], add(rch[k], rch[pre], mid + 1, r, a, b);
}
int query(int k, int l, int r, int a, int b) {
if (!k)
return 0;
if (a <= l && b >= r)
return sum[k];
int mid = l + r >> 1, res = 0;
if (a <= mid)
res = query(lch[k], l, mid, a, b);
if (b > mid)
res += query(rch[k], mid + 1, r, a, b);
return res;
}
map<int, int> mp;
int main() {
n = read();
m = read();
for (int i = 1; i <= n; i++)
a[i] = read();
for (int i = 1; i <= n; i++) {
rt[i] = rt[i - 1];
if (mp[a[i]])
add(rt[i], rt[i], 1, n, mp[a[i]], -1);
mp[a[i]] = i;
add(rt[i], rt[i], 1, n, mp[a[i]], 1);
}
for (int i = 1; i <= m; i++) {
int l, r;
l = read();
r = read();
printf("%d\n", query(rt[r], 1, n, l, r));
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define ll long long
using namespace std;
int read() {
int k = 0, f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-')
f = -1;
for (; isdigit(c); c = getchar())
k = k * 10 + c - '0';
return k * f;
}
const int N = 1000055;
int n, m, tot, a[N], rt[N], lch[N * 30], rch[N * 30], sum[N * 30];
void add(int &k, int pre, int l, int r, int a, int b) {
k = ++tot;
sum[k] = sum[pre] + b;
if (l == r)
return;
int mid = l + r >> 1;
if (a <= mid)
rch[k] = rch[pre], add(lch[k], lch[pre], l, mid, a, b);
else
lch[k] = lch[pre], add(rch[k], rch[pre], mid + 1, r, a, b);
}
int query(int k, int l, int r, int a, int b) {
if (!k)
return 0;
if (a <= l && b >= r)
return sum[k];
int mid = l + r >> 1, res = 0;
if (a <= mid)
res = query(lch[k], l, mid, a, b);
if (b > mid)
res += query(rch[k], mid + 1, r, a, b);
return res;
}
map<int, int> mp;
int main() {
n = read();
m = read();
for (int i = 1; i <= n; i++)
a[i] = read();
for (int i = 1; i <= n; i++) {
rt[i] = rt[i - 1];
if (mp[a[i]])
add(rt[i], rt[i], 1, n, mp[a[i]], -1);
mp[a[i]] = i;
add(rt[i], rt[i], 1, n, mp[a[i]], 1);
}
for (int i = 1; i <= m; i++) {
int l, r;
l = read();
r = read();
printf("%d\n", query(rt[r], 1, n, l, r));
}
return 0;
} | replace | 17 | 18 | 17 | 18 | 0 | |
p02599 | C++ | Runtime Error | #pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <cstdlib>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
// #define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>,
// rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<pair<ll, ll>> vpll;
#define Boost() \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define forn(i, n) for (ll i = 0; i < n; i++)
#define fo(i, n) for (ll i = 1; i <= n; i++)
#define fore(i, l, r) for (ll i = l; i <= r; i++)
#define rev(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(c) c.begin(), c.end()
#define go(c, it) for (auto it = (c).begin(); it != (c).end(); it++)
#define back(c, it) for (auto it = (c).rbegin(); it != (c).rend(); it++)
#define fi first
#define se second
#define MOD 998244353
#define MAX 1000000007
ll power(ll a, ll b) {
ll res = 1;
a = a % MOD;
while (b > 0) {
if (b & 1) {
res = (res * a) % MOD;
}
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
ll fact[500009];
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll nCrMod(ll n, ll r) {
ll p = fact[n];
ll q = (fact[r] * fact[n - r]) % MOD;
return (p * fermat_inv(q)) % MOD;
}
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
const ld PI = acos(-1.0);
vector<int> g[200009];
struct query {
int l, r, ind;
};
bool cmp(query x, query y) { return x.r <= y.r; }
ll BIT[200009] = {0};
void update(ll x, ll y, ll n) {
for (; x <= n; x += x & -x)
BIT[x] += y;
}
ll sum(ll x, ll n) {
ll s = 0;
for (; x > 0; x -= x & -x)
s += BIT[x];
return s;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.cpp", "r", stdin);
freopen("output.cpp", "w", stdout);
#endif
Boost();
int t;
// cin>>t;
t = 1;
int t1 = t;
while (t--) {
ll n, q;
cin >> n >> q;
ll a[n + 1];
fo(i, n) cin >> a[i];
query qu[q + 1];
fo(i, q) {
cin >> qu[i].l >> qu[i].r;
qu[i].ind = i;
}
sort(qu + 1, qu + q + 1, cmp);
ll lst[500007];
memset(lst, -1, sizeof(lst));
int c = 1;
ll ans[q + 1];
fo(i, n) {
if (lst[a[i]] != -1)
update(lst[a[i]], -1, n);
lst[a[i]] = i;
update(lst[a[i]], 1, n);
while (c <= q && qu[c].r == i) {
ans[qu[c].ind] = sum(qu[c].r, n) - sum(qu[c].l - 1, n);
c++;
}
}
fo(i, q) cout << ans[i] << endl;
}
return 0;
}
| #pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <cstdlib>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
// #define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>,
// rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<pair<ll, ll>> vpll;
#define Boost() \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define forn(i, n) for (ll i = 0; i < n; i++)
#define fo(i, n) for (ll i = 1; i <= n; i++)
#define fore(i, l, r) for (ll i = l; i <= r; i++)
#define rev(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(c) c.begin(), c.end()
#define go(c, it) for (auto it = (c).begin(); it != (c).end(); it++)
#define back(c, it) for (auto it = (c).rbegin(); it != (c).rend(); it++)
#define fi first
#define se second
#define MOD 998244353
#define MAX 1000000007
ll power(ll a, ll b) {
ll res = 1;
a = a % MOD;
while (b > 0) {
if (b & 1) {
res = (res * a) % MOD;
}
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
ll fact[500009];
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll nCrMod(ll n, ll r) {
ll p = fact[n];
ll q = (fact[r] * fact[n - r]) % MOD;
return (p * fermat_inv(q)) % MOD;
}
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
const ld PI = acos(-1.0);
vector<int> g[200009];
struct query {
int l, r, ind;
};
bool cmp(query x, query y) { return x.r <= y.r; }
ll BIT[500009] = {0};
void update(ll x, ll y, ll n) {
for (; x <= n; x += x & -x)
BIT[x] += y;
}
ll sum(ll x, ll n) {
ll s = 0;
for (; x > 0; x -= x & -x)
s += BIT[x];
return s;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.cpp", "r", stdin);
freopen("output.cpp", "w", stdout);
#endif
Boost();
int t;
// cin>>t;
t = 1;
int t1 = t;
while (t--) {
ll n, q;
cin >> n >> q;
ll a[n + 1];
fo(i, n) cin >> a[i];
query qu[q + 1];
fo(i, q) {
cin >> qu[i].l >> qu[i].r;
qu[i].ind = i;
}
sort(qu + 1, qu + q + 1, cmp);
ll lst[500007];
memset(lst, -1, sizeof(lst));
int c = 1;
ll ans[q + 1];
fo(i, n) {
if (lst[a[i]] != -1)
update(lst[a[i]], -1, n);
lst[a[i]] = i;
update(lst[a[i]], 1, n);
while (c <= q && qu[c].r == i) {
ans[qu[c].ind] = sum(qu[c].r, n) - sum(qu[c].l - 1, n);
c++;
}
}
fo(i, q) cout << ans[i] << endl;
}
return 0;
}
| replace | 60 | 61 | 60 | 61 | 0 | |
p02599 | C++ | Time Limit Exceeded | #pragma GCC optimize("O3")
// #pragma GCC target ("avx")
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) \
do { \
} while (0)
#endif
#include <cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define EACH(i, c) \
for (__typeof((c).begin()) i = (c).begin(), i##_end = (c).end(); \
i != i##_end; ++i)
template <class T> inline void amin(T &x, const T &y) {
if (y < x)
x = y;
}
template <class T> inline void amax(T &x, const T &y) {
if (x < y)
x = y;
}
template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
for (bool sp = 0; begin != end; ++begin) {
if (sp)
putchar(' ');
else
sp = true;
printf(fmt, *begin);
}
putchar('\n');
}
int N, Q;
struct Query {
int l, r, i;
bool operator<(const Query &o) const { return r < o.r; }
};
const int MAXN = 500011;
const int MAGIC = 300;
const int SIZE = MAXN / MAGIC;
vector<Query> B[SIZE], P;
int C[MAXN];
int cnt;
int mem[MAXN];
int ans[MAXN];
void MAIN() {
scanf("%d%d", &N, &Q);
REP(i, N) scanf("%d", C + i);
REP(i, Q) {
int l, r;
scanf("%d%d", &l, &r);
l--;
if (l / MAGIC == r / MAGIC) {
P.push_back((Query){l, r, i});
} else {
B[l / MAGIC].push_back((Query){l, r, i});
}
}
REP(k, SIZE) if (B[k].size()) {
int sep = min(N, k * MAGIC + MAGIC);
int R = sep;
sort(B[k].begin(), B[k].end());
cnt = 0;
memset(mem, 0, sizeof mem);
EACH(e, B[k]) {
while (R < N && R < e->r) {
if (mem[C[R]] == 0)
cnt++;
mem[C[R]]++;
R++;
}
for (int i = e->l; i < sep; i++) {
if (mem[C[i]] == 0)
cnt++;
mem[C[i]]++;
}
ans[e->i] = cnt;
for (int i = e->l; i < sep; i++) {
mem[C[i]]--;
if (mem[C[i]] == 0)
cnt--;
}
}
}
cnt = 0;
memset(mem, 0, sizeof mem);
EACH(e, P) {
for (int i = e->l; i < e->r; i++) {
if (mem[C[i]] == 0)
cnt++;
mem[C[i]]++;
}
ans[e->i] = cnt;
for (int i = e->l; i < e->r; i++) {
mem[C[i]]--;
if (mem[C[i]] == 0)
cnt--;
}
}
REP(i, Q) printf("%d\n", ans[i]);
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP(tc, TC) MAIN();
return 0;
}
| #pragma GCC optimize("O3")
// #pragma GCC target ("avx")
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) \
do { \
} while (0)
#endif
#include <cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define EACH(i, c) \
for (__typeof((c).begin()) i = (c).begin(), i##_end = (c).end(); \
i != i##_end; ++i)
template <class T> inline void amin(T &x, const T &y) {
if (y < x)
x = y;
}
template <class T> inline void amax(T &x, const T &y) {
if (x < y)
x = y;
}
template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
for (bool sp = 0; begin != end; ++begin) {
if (sp)
putchar(' ');
else
sp = true;
printf(fmt, *begin);
}
putchar('\n');
}
int N, Q;
struct Query {
int l, r, i;
bool operator<(const Query &o) const { return r < o.r; }
};
const int MAXN = 500011;
const int MAGIC = 700;
const int SIZE = MAXN / MAGIC;
vector<Query> B[SIZE], P;
int C[MAXN];
int cnt;
int mem[MAXN];
int ans[MAXN];
void MAIN() {
scanf("%d%d", &N, &Q);
REP(i, N) scanf("%d", C + i);
REP(i, Q) {
int l, r;
scanf("%d%d", &l, &r);
l--;
if (l / MAGIC == r / MAGIC) {
P.push_back((Query){l, r, i});
} else {
B[l / MAGIC].push_back((Query){l, r, i});
}
}
REP(k, SIZE) if (B[k].size()) {
int sep = min(N, k * MAGIC + MAGIC);
int R = sep;
sort(B[k].begin(), B[k].end());
cnt = 0;
memset(mem, 0, sizeof mem);
EACH(e, B[k]) {
while (R < N && R < e->r) {
if (mem[C[R]] == 0)
cnt++;
mem[C[R]]++;
R++;
}
for (int i = e->l; i < sep; i++) {
if (mem[C[i]] == 0)
cnt++;
mem[C[i]]++;
}
ans[e->i] = cnt;
for (int i = e->l; i < sep; i++) {
mem[C[i]]--;
if (mem[C[i]] == 0)
cnt--;
}
}
}
cnt = 0;
memset(mem, 0, sizeof mem);
EACH(e, P) {
for (int i = e->l; i < e->r; i++) {
if (mem[C[i]] == 0)
cnt++;
mem[C[i]]++;
}
ans[e->i] = cnt;
for (int i = e->l; i < e->r; i++) {
mem[C[i]]--;
if (mem[C[i]] == 0)
cnt--;
}
}
REP(i, Q) printf("%d\n", ans[i]);
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP(tc, TC) MAIN();
return 0;
}
| replace | 57 | 58 | 57 | 58 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int max_n = 5 * (int)1e5 + 5;
#define mp make_pair
#define pii pair<int, int>
/* Zero indexed FT */
class FenwickTree {
vector<int> ft;
public:
FenwickTree(int n) { ft.resize(n + 1); }
int rsq(int b);
int rsq(int a, int b);
void adjust(int ind, int to_add);
};
int FenwickTree ::rsq(int b) {
int sum = 0;
for (; b > 0; b -= (b & -b))
sum += ft[b];
return sum;
}
int FenwickTree ::rsq(int a, int b) {
a++;
b++;
return rsq(b) - (a > 0 ? rsq(a - 1) : 0);
}
void FenwickTree ::adjust(int ind, int to_add) {
ind++;
for (; ind < (int)ft.size(); ind += (ind & -ind))
ft[ind] += to_add;
}
void solve() {
int n, q_len;
cin >> n >> q_len;
vector<int> a(n);
for (auto &ele : a)
cin >> ele;
vector<pair<pii, int>> queries;
for (int ind = 0; ind < q_len; ind++) {
int left, right;
cin >> left >> right;
left--;
right--;
queries.push_back(mp(mp(right, left), ind));
}
vector<int> ans(n);
vector<int> last_occ(max_n, -1);
FenwickTree ft(n);
for (int i = 0; i < n; i++)
ft.adjust(i, 1);
sort(queries.begin(), queries.end());
auto itr = queries.begin();
for (int i = 0; i < n; i++) {
if (last_occ[a[i]] != -1)
ft.adjust(last_occ[a[i]], -1);
while (itr != queries.end() and itr->first.first == i) {
int r = (itr->first).first;
int l = (itr->first).second;
int q_ind = itr->second;
ans[q_ind] = ft.rsq(l, r);
itr++;
}
last_occ[a[i]] = i;
}
for (auto &ele : ans)
cout << ele << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int max_n = 5 * (int)1e5 + 5;
#define mp make_pair
#define pii pair<int, int>
/* Zero indexed FT */
class FenwickTree {
vector<int> ft;
public:
FenwickTree(int n) { ft.resize(n + 1); }
int rsq(int b);
int rsq(int a, int b);
void adjust(int ind, int to_add);
};
int FenwickTree ::rsq(int b) {
int sum = 0;
for (; b > 0; b -= (b & -b))
sum += ft[b];
return sum;
}
int FenwickTree ::rsq(int a, int b) {
a++;
b++;
return rsq(b) - (a > 0 ? rsq(a - 1) : 0);
}
void FenwickTree ::adjust(int ind, int to_add) {
ind++;
for (; ind < (int)ft.size(); ind += (ind & -ind))
ft[ind] += to_add;
}
void solve() {
int n, q_len;
cin >> n >> q_len;
vector<int> a(n);
for (auto &ele : a)
cin >> ele;
vector<pair<pii, int>> queries;
for (int ind = 0; ind < q_len; ind++) {
int left, right;
cin >> left >> right;
left--;
right--;
queries.push_back(mp(mp(right, left), ind));
}
vector<int> ans(q_len);
vector<int> last_occ(max_n, -1);
FenwickTree ft(n);
for (int i = 0; i < n; i++)
ft.adjust(i, 1);
sort(queries.begin(), queries.end());
auto itr = queries.begin();
for (int i = 0; i < n; i++) {
if (last_occ[a[i]] != -1)
ft.adjust(last_occ[a[i]], -1);
while (itr != queries.end() and itr->first.first == i) {
int r = (itr->first).first;
int l = (itr->first).second;
int q_ind = itr->second;
ans[q_ind] = ft.rsq(l, r);
itr++;
}
last_occ[a[i]] = i;
}
for (auto &ele : ans)
cout << ele << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
| replace | 56 | 57 | 56 | 57 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #pragma optimize GCC("O3")
typedef long long ll;
typedef long double ld;
using namespace std;
class query {
public:
int l, r, in;
};
long long int pole[2000079];
void ii(long long int kde, long long int r, int n) {
while (kde <= n) {
pole[kde] += r;
kde += kde & -kde;
}
}
long long int ss(long long int kde) {
long long int sum = 0;
while (kde > 0) {
sum += pole[kde];
kde = kde & (kde - 1);
}
return sum;
}
int main() {
int n, q;
cin >> n >> q;
vector<int> c(n);
for (int i = 0; i < n; i++)
cin >> c[i];
vector<int> ans(n, 0);
vector<vector<query>> v(n);
for (int i = 0; i < q; i++) {
query a;
cin >> a.l >> a.r;
a.l--;
a.r--;
a.in = i;
v[a.r].push_back(a);
}
vector<int> last(n + 1, -1);
int in = 0;
for (int r = 0; r < n; r++) {
ii(r + 1, 1, n + 1);
if (last[c[r]] != -1)
ii(last[c[r]] + 1, -1, n + 1);
last[c[r]] = r;
for (query i : v[r])
ans[i.in] = ss(r + 1) - ss(i.l);
}
for (int i = 0; i < q; i++)
cout << ans[i] << "\n";
return 0;
} | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #pragma optimize GCC("O3")
typedef long long ll;
typedef long double ld;
using namespace std;
class query {
public:
int l, r, in;
};
long long int pole[2000079];
void ii(long long int kde, long long int r, int n) {
while (kde <= n) {
pole[kde] += r;
kde += kde & -kde;
}
}
long long int ss(long long int kde) {
long long int sum = 0;
while (kde > 0) {
sum += pole[kde];
kde = kde & (kde - 1);
}
return sum;
}
int main() {
int n, q;
cin >> n >> q;
vector<int> c(n);
for (int i = 0; i < n; i++)
cin >> c[i];
vector<int> ans(q, 0);
vector<vector<query>> v(n);
for (int i = 0; i < q; i++) {
query a;
cin >> a.l >> a.r;
a.l--;
a.r--;
a.in = i;
v[a.r].push_back(a);
}
vector<int> last(n + 1, -1);
int in = 0;
for (int r = 0; r < n; r++) {
ii(r + 1, 1, n + 1);
if (last[c[r]] != -1)
ii(last[c[r]] + 1, -1, n + 1);
last[c[r]] = r;
for (query i : v[r])
ans[i.in] = ss(r + 1) - ss(i.l);
}
for (int i = 0; i < q; i++)
cout << ans[i] << "\n";
return 0;
} | replace | 41 | 42 | 41 | 42 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 10000;
int n = 0, q = 0;
struct Query {
int id, l, r;
bool operator<(Query a) const { return r < a.r; }
} query[maxn];
int v[maxn], sum[maxn], ans[maxn];
int lowbit(int x) { return x & (-x); }
void change(int i, int a) {
while (i <= n) {
sum[i] += a;
i += lowbit(i);
}
}
int getsum(int i) {
int sum_t = 0;
while (i > 0) {
sum_t += sum[i];
i -= lowbit(i);
}
return sum_t;
}
int main() {
scanf("%d", &n);
memset(v, 0, sizeof(v));
memset(sum, 0, sizeof(sum));
memset(ans, 0, sizeof(ans));
int t = 0;
scanf("%d", &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &v[i]);
}
for (int i = 1; i <= q; i++) {
scanf("%d%d", &query[i].l, &query[i].r);
query[i].id = i;
}
sort(query + 1, query + q + 1);
int pre = 1;
map<int, int> mymap;
for (int i = 1; i <= q; i++) {
for (int j = pre; j <= query[i].r; j++) {
// 已存在
if (mymap[v[j]]) {
change(mymap[v[j]], -1);
}
mymap[v[j]] = j;
change(j, 1);
}
pre = query[i].r + 1;
ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
int ab;
cin >> ab;
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 500010;
int n = 0, q = 0;
struct Query {
int id, l, r;
bool operator<(Query a) const { return r < a.r; }
} query[maxn];
int v[maxn], sum[maxn], ans[maxn];
int lowbit(int x) { return x & (-x); }
void change(int i, int a) {
while (i <= n) {
sum[i] += a;
i += lowbit(i);
}
}
int getsum(int i) {
int sum_t = 0;
while (i > 0) {
sum_t += sum[i];
i -= lowbit(i);
}
return sum_t;
}
int main() {
scanf("%d", &n);
memset(v, 0, sizeof(v));
memset(sum, 0, sizeof(sum));
memset(ans, 0, sizeof(ans));
int t = 0;
scanf("%d", &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &v[i]);
}
for (int i = 1; i <= q; i++) {
scanf("%d%d", &query[i].l, &query[i].r);
query[i].id = i;
}
sort(query + 1, query + q + 1);
int pre = 1;
map<int, int> mymap;
for (int i = 1; i <= q; i++) {
for (int j = pre; j <= query[i].r; j++) {
// 已存在
if (mymap[v[j]]) {
change(mymap[v[j]], -1);
}
mymap[v[j]] = j;
change(j, 1);
}
pre = query[i].r + 1;
ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
int ab;
cin >> ab;
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define mx 300008
#define pii pair<int, int>
#define pi acos(-1.0)
#define pb push_back
#define ss second
#define ff first
typedef long long int ll;
/// adjacent move
int fx[4] = {0, +1, 0, -1};
int fy[4] = {+1, 0, -1, 0};
/// knight
ll prime[mx];
ll a[mx], tree[4 * mx];
ll n, m;
void build(ll node, ll s, ll e) {
if (s == e) {
tree[node] = a[s];
return;
}
ll mid = (s + e) / 2;
ll left = node * 2;
ll right = node * 2 + 1;
build(left, s, mid);
build(right, mid + 1, e);
tree[node] = (tree[left] + tree[right]);
}
void update(ll node, ll s, ll e, ll idx, ll val) {
if (s > idx || e < idx)
return;
if (s == e && s == idx) {
tree[node] = val;
return;
}
ll mid = (s + e) / 2;
ll left = 2 * node;
ll right = left + 1;
update(left, s, mid, idx, val);
update(right, mid + 1, e, idx, val);
tree[node] = tree[left] + tree[right];
}
ll query(ll node, ll s, ll e, ll i, ll j) {
if (i > e || j < s)
return 0;
if (s >= i && e <= j)
return tree[node];
ll mid = (s + e) / 2;
ll left = 2 * node;
ll right = left + 1;
ll x = query(left, s, mid, i, j);
ll y = query(right, mid + 1, e, i, j);
return (x + y);
}
int last[mx];
int tot[mx];
int ans[mx];
vector<pii> v[mx];
int main() {
int t = 1;
// cin>>t;
int c = 0;
while (t--) {
ll n, m;
memset(ans, 0, sizeof(ans));
memset(last, 0, sizeof(last));
memset(v, 0, sizeof(v));
memset(tree, 0, sizeof(tree));
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
v[r].push_back({l, i});
}
for (int i = 1; i <= n; i++) {
update(1, 1, n, i, 1);
if (last[a[i]] != 0)
update(1, 1, n, last[a[i]], 0);
last[a[i]] = i;
for (int j = 0; j < v[i].size(); j++) {
int idx = v[i][j].ss;
int val = v[i][j].ff;
ans[idx] = query(1, 1, n, val, i);
}
}
for (int i = 1; i <= m; i++)
cout << ans[i] << "\n";
}
}
| #include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define mx 500008
#define pii pair<int, int>
#define pi acos(-1.0)
#define pb push_back
#define ss second
#define ff first
typedef long long int ll;
/// adjacent move
int fx[4] = {0, +1, 0, -1};
int fy[4] = {+1, 0, -1, 0};
/// knight
ll prime[mx];
ll a[mx], tree[4 * mx];
ll n, m;
void build(ll node, ll s, ll e) {
if (s == e) {
tree[node] = a[s];
return;
}
ll mid = (s + e) / 2;
ll left = node * 2;
ll right = node * 2 + 1;
build(left, s, mid);
build(right, mid + 1, e);
tree[node] = (tree[left] + tree[right]);
}
void update(ll node, ll s, ll e, ll idx, ll val) {
if (s > idx || e < idx)
return;
if (s == e && s == idx) {
tree[node] = val;
return;
}
ll mid = (s + e) / 2;
ll left = 2 * node;
ll right = left + 1;
update(left, s, mid, idx, val);
update(right, mid + 1, e, idx, val);
tree[node] = tree[left] + tree[right];
}
ll query(ll node, ll s, ll e, ll i, ll j) {
if (i > e || j < s)
return 0;
if (s >= i && e <= j)
return tree[node];
ll mid = (s + e) / 2;
ll left = 2 * node;
ll right = left + 1;
ll x = query(left, s, mid, i, j);
ll y = query(right, mid + 1, e, i, j);
return (x + y);
}
int last[mx];
int tot[mx];
int ans[mx];
vector<pii> v[mx];
int main() {
int t = 1;
// cin>>t;
int c = 0;
while (t--) {
ll n, m;
memset(ans, 0, sizeof(ans));
memset(last, 0, sizeof(last));
memset(v, 0, sizeof(v));
memset(tree, 0, sizeof(tree));
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
v[r].push_back({l, i});
}
for (int i = 1; i <= n; i++) {
update(1, 1, n, i, 1);
if (last[a[i]] != 0)
update(1, 1, n, last[a[i]], 0);
last[a[i]] = i;
for (int j = 0; j < v[i].size(); j++) {
int idx = v[i][j].ss;
int val = v[i][j].ff;
ans[idx] = query(1, 1, n, val, i);
}
}
for (int i = 1; i <= m; i++)
cout << ans[i] << "\n";
}
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02599 | C++ | Runtime Error |
#include <bits/stdc++.h>
using namespace std;
const int Max = 1e5 + 5;
int K, ans[Max], arra[Max], sum, cnt, mp[Max];
struct Query {
int index, l, r;
};
bool cmp(Query a, Query b) {
if (a.l / K != b.l / K) {
return a.l / K < b.l / K;
} else if ((a.l / K) & 1)
return a.r < b.r;
return a.r > b.r;
}
Query query[Max];
void add(int index) {
if (!mp[index]) {
cnt++;
}
mp[index]++;
}
void del(int index) {
mp[index]--;
if (!mp[index]) {
cnt--;
}
}
void MO(int n, int q) {
K = sqrt(n);
sort(query, query + q, cmp);
int st = 0, en = -1;
cnt = 0;
for (int i = 0; i < q; i++) {
int l = query[i].l - 1;
int r = query[i].r - 1;
int idx = query[i].index;
while (st > l) {
st--;
add(arra[st]);
}
while (st < l) {
del(arra[st]);
st++;
}
while (en < r) {
en++;
add(arra[en]);
}
while (en > r) {
del(arra[en]);
en--;
}
ans[idx] = cnt;
}
for (int i = 0; i < q; i++)
printf("%d\n", ans[i]);
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> arra[i];
}
for (int i = 0; i < q; i++) {
cin >> query[i].l >> query[i].r;
query[i].index = i;
}
MO(n, q);
}
|
#include <bits/stdc++.h>
using namespace std;
const int Max = 5e5 + 5;
int K, ans[Max], arra[Max], sum, cnt, mp[Max];
struct Query {
int index, l, r;
};
bool cmp(Query a, Query b) {
if (a.l / K != b.l / K) {
return a.l / K < b.l / K;
} else if ((a.l / K) & 1)
return a.r < b.r;
return a.r > b.r;
}
Query query[Max];
void add(int index) {
if (!mp[index]) {
cnt++;
}
mp[index]++;
}
void del(int index) {
mp[index]--;
if (!mp[index]) {
cnt--;
}
}
void MO(int n, int q) {
K = sqrt(n);
sort(query, query + q, cmp);
int st = 0, en = -1;
cnt = 0;
for (int i = 0; i < q; i++) {
int l = query[i].l - 1;
int r = query[i].r - 1;
int idx = query[i].index;
while (st > l) {
st--;
add(arra[st]);
}
while (st < l) {
del(arra[st]);
st++;
}
while (en < r) {
en++;
add(arra[en]);
}
while (en > r) {
del(arra[en]);
en--;
}
ans[idx] = cnt;
}
for (int i = 0; i < q; i++)
printf("%d\n", ans[i]);
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> arra[i];
}
for (int i = 0; i < q; i++) {
cin >> query[i].l >> query[i].r;
query[i].index = i;
}
MO(n, q);
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
vector<vector<int>> seg;
int query(int l, int r, int u, int a, int b) {
if (l <= a && b <= r)
return lower_bound(seg[u].begin(), seg[u].end(), l) - seg[u].begin();
int res = 0;
if (l < (a + b) / 2)
res += query(l, r, 2 * u + 1, a, (a + b) / 2);
if ((a + b) / 2 < r)
res += query(l, r, 2 * u + 2, (a + b) / 2, b);
return res;
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
vector<int> a(n);
rep(i, n) scanf("%d", &a[i]), a[i]--;
vector<int> pre(1e5, -1);
rep(i, n) {
int x = a[i];
a[i] = pre[x];
pre[x] = i;
}
int N;
for (N = 1; N < n; N <<= 1)
;
seg.resize(2 * N - 1);
rep(i, n) seg[N - 1 + i].emplace_back(a[i]);
for (int i = N - 2; i >= 0; i--) {
merge(seg[2 * i + 1].begin(), seg[2 * i + 1].end(), seg[2 * i + 2].begin(),
seg[2 * i + 2].end(), back_inserter(seg[i]));
}
rep(_, q) {
int l, r;
scanf("%d%d", &l, &r);
l--;
printf("%d\n", query(l, r, 0, 0, N));
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
vector<vector<int>> seg;
int query(int l, int r, int u, int a, int b) {
if (l <= a && b <= r)
return lower_bound(seg[u].begin(), seg[u].end(), l) - seg[u].begin();
int res = 0;
if (l < (a + b) / 2)
res += query(l, r, 2 * u + 1, a, (a + b) / 2);
if ((a + b) / 2 < r)
res += query(l, r, 2 * u + 2, (a + b) / 2, b);
return res;
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
vector<int> a(n);
rep(i, n) scanf("%d", &a[i]), a[i]--;
vector<int> pre(n, -1);
rep(i, n) {
int x = a[i];
a[i] = pre[x];
pre[x] = i;
}
int N;
for (N = 1; N < n; N <<= 1)
;
seg.resize(2 * N - 1);
rep(i, n) seg[N - 1 + i].emplace_back(a[i]);
for (int i = N - 2; i >= 0; i--) {
merge(seg[2 * i + 1].begin(), seg[2 * i + 1].end(), seg[2 * i + 2].begin(),
seg[2 * i + 2].end(), back_inserter(seg[i]));
}
rep(_, q) {
int l, r;
scanf("%d%d", &l, &r);
l--;
printf("%d\n", query(l, r, 0, 0, N));
}
return 0;
}
| replace | 26 | 27 | 26 | 27 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vll vector<long long>
#define pll pair<long long, long long>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1e18
typedef long long ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
o_tree;
const int N = 2e5 + 6;
ll lo[4 * N + 1], hi[4 * N + 1], delta[4 * N + 1], mini[4 * N + 1];
// change update and rmq according to question
void buildSeg(ll node, ll l, ll r) {
lo[node] = l;
hi[node] = r;
if (l == r) {
return;
}
ll mid = l + (r - l) / 2;
buildSeg(2 * node + 1, l, mid);
buildSeg(2 * node + 2, mid + 1, r);
}
void prop(ll i) {
delta[2 * i + 1] += delta[i];
delta[2 * i + 2] += delta[i];
delta[i] = 0;
}
void update(ll i) {
mini[i] =
mini[2 * i + 1] + delta[2 * i + 1] + mini[2 * i + 2] + delta[2 * i + 2];
}
void increment(ll i, ll l, ll r, ll val) {
if (r < lo[i] || l > hi[i]) {
return;
}
if (l <= lo[i] && r >= hi[i]) {
delta[i] += val;
return;
}
prop(i);
increment(2 * i + 1, l, r, val);
increment(2 * i + 2, l, r, val);
update(i);
}
ll rmq(ll i, ll l, ll r) {
if (r < lo[i] || l > hi[i]) {
return 0;
}
if (l <= lo[i] && r >= hi[i]) {
return (mini[i] + delta[i]);
}
prop(i);
ll lefmin = rmq(2 * i + 1, l, r);
ll rigmin = rmq(2 * i + 2, l, r);
update(i);
return lefmin + rigmin;
}
void solve() {
ll n, q;
cin >> n >> q;
ll i, j;
ll a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
array<ll, 3> op[q];
for (i = 0; i < q; i++) {
cin >> op[i][1] >> op[i][0];
op[i][1]--;
op[i][0]--;
op[i][2] = i;
}
sort(op, op + q);
buildSeg(1, 1, n + 5);
ll last[n + 1];
ll ans[q];
clr(ans);
ll qq = 0;
for (i = 0; i <= n; i++) {
last[i] = -1;
}
for (i = 0; i < n; i++) {
if (last[a[i]] != -1) {
increment(1, last[a[i]] + 1, last[a[i]] + 1, -1);
}
last[a[i]] = i;
increment(1, i + 1, i + 1, 1);
while (qq < q && op[qq][0] == (i)) {
ans[op[qq][2]] = rmq(1, op[qq][1] + 1, op[qq][0] + 1);
qq++;
}
}
for (i = 0; i < q; i++) {
cout << ans[i] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
cout << setprecision(20);
ll t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vll vector<long long>
#define pll pair<long long, long long>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1e18
typedef long long ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
o_tree;
const int N = 6e5 + 6;
ll lo[4 * N + 1], hi[4 * N + 1], delta[4 * N + 1], mini[4 * N + 1];
// change update and rmq according to question
void buildSeg(ll node, ll l, ll r) {
lo[node] = l;
hi[node] = r;
if (l == r) {
return;
}
ll mid = l + (r - l) / 2;
buildSeg(2 * node + 1, l, mid);
buildSeg(2 * node + 2, mid + 1, r);
}
void prop(ll i) {
delta[2 * i + 1] += delta[i];
delta[2 * i + 2] += delta[i];
delta[i] = 0;
}
void update(ll i) {
mini[i] =
mini[2 * i + 1] + delta[2 * i + 1] + mini[2 * i + 2] + delta[2 * i + 2];
}
void increment(ll i, ll l, ll r, ll val) {
if (r < lo[i] || l > hi[i]) {
return;
}
if (l <= lo[i] && r >= hi[i]) {
delta[i] += val;
return;
}
prop(i);
increment(2 * i + 1, l, r, val);
increment(2 * i + 2, l, r, val);
update(i);
}
ll rmq(ll i, ll l, ll r) {
if (r < lo[i] || l > hi[i]) {
return 0;
}
if (l <= lo[i] && r >= hi[i]) {
return (mini[i] + delta[i]);
}
prop(i);
ll lefmin = rmq(2 * i + 1, l, r);
ll rigmin = rmq(2 * i + 2, l, r);
update(i);
return lefmin + rigmin;
}
void solve() {
ll n, q;
cin >> n >> q;
ll i, j;
ll a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
array<ll, 3> op[q];
for (i = 0; i < q; i++) {
cin >> op[i][1] >> op[i][0];
op[i][1]--;
op[i][0]--;
op[i][2] = i;
}
sort(op, op + q);
buildSeg(1, 1, n + 5);
ll last[n + 1];
ll ans[q];
clr(ans);
ll qq = 0;
for (i = 0; i <= n; i++) {
last[i] = -1;
}
for (i = 0; i < n; i++) {
if (last[a[i]] != -1) {
increment(1, last[a[i]] + 1, last[a[i]] + 1, -1);
}
last[a[i]] = i;
increment(1, i + 1, i + 1, 1);
while (qq < q && op[qq][0] == (i)) {
ans[op[qq][2]] = rmq(1, op[qq][1] + 1, op[qq][0] + 1);
qq++;
}
}
for (i = 0; i < q; i++) {
cout << ans[i] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
cout << setprecision(20);
ll t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | replace | 24 | 25 | 24 | 25 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TU;
template <typename T> class BIT {
vector<T> array;
int n;
public:
BIT(int num) {
n = num;
array.resize(n + 1);
}
T sum(int i) {
T s = 0;
while (i > 0) {
s += array[i];
i -= i & -i;
}
return s;
}
T sum(int i, int j) { return sum(j) - sum(i - 1); }
void add(int i, T x) {
while (i <= n) {
array[i] += x;
i += i & -i;
}
}
};
int c[500010];
TU que[500010];
int main() {
int n, q;
cin >> n >> q;
BIT<int> bit(n);
for (int i = 1; i <= n; ++i)
cin >> c[i];
rep(i, q) {
int l, r;
cin >> l >> r;
que[i] = TU(r, l, i);
}
sort(que, que + q);
rep(i, q) swap(get<0>(que[i]), get<1>(que[i]));
vector<int> x(n + 1, -1), ans(q);
int now = 0;
rep(i, q) {
for (int j = now + 1; j <= get<1>(que[i]); ++j) {
if (x[c[j]] != -1) {
bit.add(x[c[j]], -1);
}
x[c[j]] = j;
bit.add(x[c[j]], 1);
}
ans[get<2>(que[i])] = bit.sum(get<0>(que[i]), get<1>(que[i]));
}
rep(i, q) cout << ans[i] << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TU;
template <typename T> class BIT {
vector<T> array;
int n;
public:
BIT(int num) {
n = num;
array.resize(n + 1);
}
T sum(int i) {
T s = 0;
while (i > 0) {
s += array[i];
i -= i & -i;
}
return s;
}
T sum(int i, int j) { return sum(j) - sum(i - 1); }
void add(int i, T x) {
while (i <= n) {
array[i] += x;
i += i & -i;
}
}
};
int c[500010];
TU que[500010];
int main() {
int n, q;
cin >> n >> q;
BIT<int> bit(n);
for (int i = 1; i <= n; ++i)
cin >> c[i];
rep(i, q) {
int l, r;
cin >> l >> r;
que[i] = TU(r, l, i);
}
sort(que, que + q);
rep(i, q) swap(get<0>(que[i]), get<1>(que[i]));
vector<int> x(n + 1, -1), ans(q);
int now = 0;
rep(i, q) {
for (int j = now + 1; j <= get<1>(que[i]); ++j) {
if (x[c[j]] != -1) {
bit.add(x[c[j]], -1);
}
x[c[j]] = j;
bit.add(x[c[j]], 1);
}
ans[get<2>(que[i])] = bit.sum(get<0>(que[i]), get<1>(que[i]));
now = get<1>(que[i]);
}
rep(i, q) cout << ans[i] << endl;
} | insert | 60 | 60 | 60 | 61 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define Fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
int block;
int m[2000000];
int a[2000000];
struct query {
int l, r, idx;
};
int value = 0;
bool comper(query a, query b) {
if (a.l / block != b.l / block)
return a.l / block < b.l / block;
return a.r < b.r;
}
void add(int pos) {
int val = a[pos];
m[val]++;
if (m[val] == 1) {
value++;
} else if (!m[val]) {
value--;
}
}
void remove(int pos) {
int val = a[pos];
m[val]--;
if (!m[val]) {
value--;
}
}
int main(void) {
Fast;
int n, q;
cin >> n >> q;
block = sqrt(n);
for (int i = 1; i < n + 1; i++)
cin >> a[i];
query qr[q];
for (int i = 0; i < q; i++)
cin >> qr[i].l >> qr[i].r, qr[i].idx = i;
sort(qr, qr + q, comper);
set<int> s;
int L = 1, R = 1, ml = 0, mr = 0;
int ans[q];
for (int i = 0; i < q; i++) {
int L = qr[i].l;
int R = qr[i].r;
while (mr < R)
mr++, add(mr);
while (ml > L)
ml--, add(ml);
while (ml < L)
remove(ml), ml++;
while (mr > R)
remove(mr), mr--;
ans[qr[i].idx] = value;
}
for (int i = 0; i < q; i++)
cout << ans[i] << "\n";
} | #include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define Fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
int block;
int m[2000000];
int a[2000000];
struct query {
int l, r, idx;
};
int value = 0;
bool comper(query a, query b) {
if (a.l / block != b.l / block)
return a.l < b.l;
return a.l / block % 2 ? a.r < b.r : a.r > b.r;
}
void add(int pos) {
int val = a[pos];
m[val]++;
if (m[val] == 1) {
value++;
} else if (!m[val]) {
value--;
}
}
void remove(int pos) {
int val = a[pos];
m[val]--;
if (!m[val]) {
value--;
}
}
int main(void) {
Fast;
int n, q;
cin >> n >> q;
block = sqrt(n);
for (int i = 1; i < n + 1; i++)
cin >> a[i];
query qr[q];
for (int i = 0; i < q; i++)
cin >> qr[i].l >> qr[i].r, qr[i].idx = i;
sort(qr, qr + q, comper);
set<int> s;
int L = 1, R = 1, ml = 0, mr = 0;
int ans[q];
for (int i = 0; i < q; i++) {
int L = qr[i].l;
int R = qr[i].r;
while (mr < R)
mr++, add(mr);
while (ml > L)
ml--, add(ml);
while (ml < L)
remove(ml), ml++;
while (mr > R)
remove(mr), mr--;
ans[qr[i].idx] = value;
}
for (int i = 0; i < q; i++)
cout << ans[i] << "\n";
} | replace | 17 | 19 | 17 | 19 | TLE | |
p02599 | Python | Time Limit Exceeded | # from sys import stdin
# input = stdin.readline
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
N, Q = map(int, input().split())
c = list(map(int, input().split()))
lr = []
for i in range(Q):
l, r = map(int, input().split())
lr.append([l, r, i])
lr.sort(key=lambda x: x[1])
# print("lr", lr)
rightest = [-1] * (N + 1)
current_q = 0
bit = Bit(N)
ans = [0] * Q
for i in range(N):
if rightest[c[i]] != -1:
bit.add(rightest[c[i]] + 1, -1)
rightest[c[i]] = i
bit.add(i + 1, 1)
# print(bit.tree)
while current_q < Q and lr[current_q][1] == i + 1:
ans[lr[current_q][2]] = bit.sum(lr[current_q][1]) - bit.sum(
lr[current_q][0] - 1
)
current_q += 1
for i in range(Q):
print(ans[i])
| from sys import stdin
input = stdin.readline
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
N, Q = map(int, input().split())
c = list(map(int, input().split()))
lr = []
for i in range(Q):
l, r = map(int, input().split())
lr.append([l, r, i])
lr.sort(key=lambda x: x[1])
# print("lr", lr)
rightest = [-1] * (N + 1)
current_q = 0
bit = Bit(N)
ans = [0] * Q
for i in range(N):
if rightest[c[i]] != -1:
bit.add(rightest[c[i]] + 1, -1)
rightest[c[i]] = i
bit.add(i + 1, 1)
# print(bit.tree)
while current_q < Q and lr[current_q][1] == i + 1:
ans[lr[current_q][2]] = bit.sum(lr[current_q][1]) - bit.sum(
lr[current_q][0] - 1
)
current_q += 1
for i in range(Q):
print(ans[i])
| replace | 0 | 2 | 0 | 3 | TLE | |
p02599 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
#define REP(i, a, n) for (LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP \
{ \
cin.tie(NULL); \
ios::sync_with_stdio(false); \
}
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
for (int i = 0; i < M; ++i)
Fill(dest[i], val);
}
template <class T> T Next() {
T buf;
cin >> buf;
return buf;
}
istream &Ignore(istream &ist) {
string s;
ist >> s;
return ist;
}
bool Inside(int i, int j, int h, int w) {
return i >= 0 && i < h && j >= 0 && j < w;
}
#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BIT.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "FFT.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Geometric2D.h"
#include "Geometric2DFloat.h"
#include "Geometric3D.h"
#include "Geometric3DFloat.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "IntMod.h"
#include "Interpolation.h"
#include "KDTree.h"
#include "LIS.h"
#include "LazySegmentTree.h"
#include "List.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif
#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
LL tmp;
ist >> tmp;
val = tmp;
return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
LL tmp = val;
ost << tmp;
return ost;
}
#endif
struct Query {
int id;
int l;
int r;
};
const int B = 800;
const int MAX = 500000;
int n, q;
int c[MAX];
vector<Query> vs[(MAX - B + 1) / B];
int ans[MAX];
int cnts[MAX];
void sub(vector<Query> &v) {
sort(v.begin(), v.end(), [](auto &&a, auto &&b) { return a.r < b.r; });
int l = 0;
int r = 0;
Fill(cnts, 0);
int cnt = 0;
for (auto &&q : v) {
if (q.r >= r) {
while (r < q.r) {
int x = c[r];
if (!cnts[x])
++cnt;
++cnts[x];
++r;
}
} else {
while (r > q.r) {
--r;
int x = c[r];
--cnts[x];
if (!cnts[x])
--cnt;
}
}
if (q.l >= l) {
while (l < q.l) {
int x = c[l];
--cnts[x];
if (!cnts[x])
--cnt;
++l;
}
} else {
while (l > q.l) {
--l;
int x = c[l];
if (!cnts[x])
++cnt;
++cnts[x];
}
}
ans[q.id] = cnt;
}
}
int main() {
cin >> n >> q;
REP(i, 0, n) {
scanf("%d", c + i);
--c[i];
}
REP(k, 0, q) {
int l, r;
scanf("%d %d", &l, &r);
--l;
vs[l / B].emplace_back(Query{(int)k, l, r});
}
for (auto &&v : vs) {
sub(v);
}
REP(k, 0, q) { printf("%d\n", ans[k]); }
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
#define REP(i, a, n) for (LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP \
{ \
cin.tie(NULL); \
ios::sync_with_stdio(false); \
}
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
for (int i = 0; i < M; ++i)
Fill(dest[i], val);
}
template <class T> T Next() {
T buf;
cin >> buf;
return buf;
}
istream &Ignore(istream &ist) {
string s;
ist >> s;
return ist;
}
bool Inside(int i, int j, int h, int w) {
return i >= 0 && i < h && j >= 0 && j < w;
}
#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BIT.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "FFT.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Geometric2D.h"
#include "Geometric2DFloat.h"
#include "Geometric3D.h"
#include "Geometric3DFloat.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "IntMod.h"
#include "Interpolation.h"
#include "KDTree.h"
#include "LIS.h"
#include "LazySegmentTree.h"
#include "List.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif
#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
LL tmp;
ist >> tmp;
val = tmp;
return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
LL tmp = val;
ost << tmp;
return ost;
}
#endif
struct Query {
int id;
int l;
int r;
};
const int B = 800;
const int MAX = 500000;
int n, q;
int c[MAX];
vector<Query> vs[(MAX + B - 1) / B];
int ans[MAX];
int cnts[MAX];
void sub(vector<Query> &v) {
sort(v.begin(), v.end(), [](auto &&a, auto &&b) { return a.r < b.r; });
int l = 0;
int r = 0;
Fill(cnts, 0);
int cnt = 0;
for (auto &&q : v) {
if (q.r >= r) {
while (r < q.r) {
int x = c[r];
if (!cnts[x])
++cnt;
++cnts[x];
++r;
}
} else {
while (r > q.r) {
--r;
int x = c[r];
--cnts[x];
if (!cnts[x])
--cnt;
}
}
if (q.l >= l) {
while (l < q.l) {
int x = c[l];
--cnts[x];
if (!cnts[x])
--cnt;
++l;
}
} else {
while (l > q.l) {
--l;
int x = c[l];
if (!cnts[x])
++cnt;
++cnts[x];
}
}
ans[q.id] = cnt;
}
}
int main() {
cin >> n >> q;
REP(i, 0, n) {
scanf("%d", c + i);
--c[i];
}
REP(k, 0, q) {
int l, r;
scanf("%d %d", &l, &r);
--l;
vs[l / B].emplace_back(Query{(int)k, l, r});
}
for (auto &&v : vs) {
sub(v);
}
REP(k, 0, q) { printf("%d\n", ans[k]); }
return 0;
}
| replace | 166 | 167 | 166 | 167 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define int long long int
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 500001
#define MAXL 20
#define INF 200001
#define mod 1000000007
int n, q, block;
int m[MAXN];
int v[MAXN];
int ans[MAXN];
vector<pii> queries;
bool cmp(pii x, pii y) {
if (x.fir / block != y.fir / block)
return x.fir / block < y.fir / block;
return x.sec.fir < y.sec.fir;
}
void sqrt_decomposition() {
block = (int)sqrt(n);
sort(queries.begin(), queries.end(), cmp);
int curr_left = 0, curr_right = 0, curr_sum = 0;
for (int i = 0; i < queries.size(); i++) {
int l = queries[i].fir, r = queries[i].sec.fir;
while (curr_left < l) {
m[v[curr_left]]--;
if (m[v[curr_left]] == 0)
curr_sum--;
curr_left++;
}
while (curr_left > l) {
m[v[curr_left - 1]]++;
if (m[v[curr_left - 1]] == 1)
curr_sum++;
curr_left--;
}
while (curr_right <= r) {
m[v[curr_right]]++;
if (m[v[curr_right]] == 1)
curr_sum++;
curr_right++;
}
while (curr_right > r + 1) {
m[v[curr_right - 1]]--;
if (m[v[curr_right - 1]] == 0)
curr_sum--;
curr_right--;
}
ans[queries[i].sec.sec] = curr_sum;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> v[i];
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--, r--;
queries.pb(mp(l, mp(r, i)));
}
sqrt_decomposition();
for (int i = 0; i < q; i++)
cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 500001
#define MAXL 20
#define INF 200001
#define mod 1000000007
int n, q, block;
int m[MAXN];
int v[MAXN];
int ans[MAXN];
vector<pii> queries;
bool cmp(pii x, pii y) {
if (x.fir / block != y.fir / block)
return x.fir / block < y.fir / block;
return x.sec.fir < y.sec.fir;
}
void sqrt_decomposition() {
block = (int)sqrt(n);
sort(queries.begin(), queries.end(), cmp);
int curr_left = 0, curr_right = 0, curr_sum = 0;
for (int i = 0; i < queries.size(); i++) {
int l = queries[i].fir, r = queries[i].sec.fir;
while (curr_left < l) {
m[v[curr_left]]--;
if (m[v[curr_left]] == 0)
curr_sum--;
curr_left++;
}
while (curr_left > l) {
m[v[curr_left - 1]]++;
if (m[v[curr_left - 1]] == 1)
curr_sum++;
curr_left--;
}
while (curr_right <= r) {
m[v[curr_right]]++;
if (m[v[curr_right]] == 1)
curr_sum++;
curr_right++;
}
while (curr_right > r + 1) {
m[v[curr_right - 1]]--;
if (m[v[curr_right - 1]] == 0)
curr_sum--;
curr_right--;
}
ans[queries[i].sec.sec] = curr_sum;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> v[i];
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--, r--;
queries.pb(mp(l, mp(r, i)));
}
sqrt_decomposition();
for (int i = 0; i < q; i++)
cout << ans[i] << endl;
return 0;
}
| delete | 6 | 7 | 6 | 6 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define lli long long
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
#define rep0(i, j) for (int i = 0; i < j; i++)
#define rep1(i, j) for (int i = 1; i <= j; i++)
#define rep0d(i, j) for (int i = j - 1; i >= 0; i--)
#define MOD 1000000007
#define MAX (lli)1e12
typedef pair<lli, lli> p;
class Graph {
int V;
vector<list<p>> adj;
const int INF = (int)1e6;
public:
Graph(lli V) : adj(V) { this->V = V; }
void addEdge(int u, int v, int w) { adj[u].push_back(make_pair(v, w)); }
lli edgeNum() {
lli sum = 0;
rep0(i, V) sum += adj[i].size();
return sum;
}
bool route(lli from, lli to) { // vec[i]==j: j->iっていうもとに戻れる道がある
vector<lli> vec(V, -1);
dfs(1, from, vec);
return vec[to] >= 0;
}
void dfs(lli ed, lli num, vector<lli> &vec) {
for (auto e : adj[num]) {
if (vec[e.first] != -1)
continue;
vec[e.first] = num;
dfs(ed + 1, e.first, vec);
}
}
vector<int> bfs(int n) {
queue<int> q;
vector<int> vec(V, -1);
q.push(n);
vec[n] = 0;
while (!q.empty()) {
int p = q.front();
q.pop();
for (auto e : adj[p]) {
if (vec[e.first] == -1) {
q.push(e.first);
vec[e.first] = e.second + vec[p];
}
}
}
return vec;
}
vector<int> shortestPath(int src) {
priority_queue<p, vector<p>, greater<p>> pq;
vector<int> dist(V, INF);
pq.push(make_pair(0, src));
dist[src] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
list<p>::iterator i;
for (auto i = adj[u].begin(); i != adj[u].end(); ++i) {
int v = i->first;
int weight = i->second;
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}
}
for (int i = 0; i < V; i++)
if (dist[i] == INF)
dist[i] = -1;
return dist;
}
};
struct UF {
vector<int> par, size;
UF(int N) : par(N), size(N, 1) { rep0(i, N) par[i] = i; }
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] = ry;
size[rx] += size[ry];
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int getSize(int x) { return size[x]; }
};
struct BIT {
vector<lli> tab; // bit table
lli count;
BIT(int n) : tab(n + 1, 0) { count = n + 1; }
void add(int p, lli num) {
for (++p; p < count; p += p & -p)
tab[p] += num;
}
lli sum(int p) { // from 0 to p
lli ans = 0;
for (++p; p > 0; p -= p & -p)
ans += tab[p];
return ans;
}
void debug() {
rep0(i, count) cout << tab[i] << ' ';
cout << endl;
}
};
// pairに関するもの
lli sum(p c) { return (c.first + c.second); }
p operator-(const p &l, const p &r) {
return {l.first - r.first, l.second - r.second};
}
bool cmpf(const pair<lli, lli> left, const pair<lli, lli> right) {
return left.first < right.first;
}
bool cmps(const pair<lli, lli> left, const pair<lli, lli> right) {
return left.second < right.second;
}
lli gcd(lli a, lli b) {
if (a < b)
return gcd(b, a);
lli temp;
while ((temp = a % b)) {
a = b;
b = temp;
}
return b;
}
struct nCr {
vector<lli> f, rf;
lli count = 0;
nCr(lli num) : f(num), rf(num) {
count = num;
f[0] = 1;
rep1(i, num) f[i] = (f[i - 1] * i) % MOD;
rep0(i, num + 1) rf[i] = inv(f[i]);
}
lli inv(lli x) {
lli res = 1;
lli k = MOD - 2;
lli y = x;
while (k) {
if (k & 1)
res = (res * y) % MOD;
y = (y * y) % MOD;
k /= 2;
}
return res;
}
lli C(lli n, lli k) {
if (k > n || k < 0)
return 0;
lli a = f[n];
lli b = rf[n - k];
lli c = rf[k];
lli bc = (b * c) % MOD;
return (a * bc) % MOD;
}
};
int main() {
lli n, q;
cin >> n >> q;
vector<lli> orig(n), lastappend(500001, -1);
rep0(i, n) cin >> orig[i];
BIT b(500001);
vector<p> vec(n);
map<p, lli> mp;
map<lli, set<lli>> pq;
rep0(i, q) {
cin >> vec[i].first >> vec[i].second;
pq[vec[i].second].insert(vec[i].first);
}
lli right = 0;
for (auto e : pq) {
while (e.first > right) {
if (lastappend[orig[right]] != -1) {
b.add(lastappend[orig[right]], -1);
}
lastappend[orig[right]] = right + 1;
b.add(right + 1, 1);
// cout << b.sum(right) << '\n';
right++;
}
for (auto f : e.second) {
mp[make_pair(f, e.first)] = b.sum(e.first) - b.sum(f - 1);
}
}
// b.debug();
rep0(i, q) { cout << mp[vec[i]] << '\n'; }
}
| #include <bits/stdc++.h>
using namespace std;
#define lli long long
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
#define rep0(i, j) for (int i = 0; i < j; i++)
#define rep1(i, j) for (int i = 1; i <= j; i++)
#define rep0d(i, j) for (int i = j - 1; i >= 0; i--)
#define MOD 1000000007
#define MAX (lli)1e12
typedef pair<lli, lli> p;
class Graph {
int V;
vector<list<p>> adj;
const int INF = (int)1e6;
public:
Graph(lli V) : adj(V) { this->V = V; }
void addEdge(int u, int v, int w) { adj[u].push_back(make_pair(v, w)); }
lli edgeNum() {
lli sum = 0;
rep0(i, V) sum += adj[i].size();
return sum;
}
bool route(lli from, lli to) { // vec[i]==j: j->iっていうもとに戻れる道がある
vector<lli> vec(V, -1);
dfs(1, from, vec);
return vec[to] >= 0;
}
void dfs(lli ed, lli num, vector<lli> &vec) {
for (auto e : adj[num]) {
if (vec[e.first] != -1)
continue;
vec[e.first] = num;
dfs(ed + 1, e.first, vec);
}
}
vector<int> bfs(int n) {
queue<int> q;
vector<int> vec(V, -1);
q.push(n);
vec[n] = 0;
while (!q.empty()) {
int p = q.front();
q.pop();
for (auto e : adj[p]) {
if (vec[e.first] == -1) {
q.push(e.first);
vec[e.first] = e.second + vec[p];
}
}
}
return vec;
}
vector<int> shortestPath(int src) {
priority_queue<p, vector<p>, greater<p>> pq;
vector<int> dist(V, INF);
pq.push(make_pair(0, src));
dist[src] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
list<p>::iterator i;
for (auto i = adj[u].begin(); i != adj[u].end(); ++i) {
int v = i->first;
int weight = i->second;
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}
}
for (int i = 0; i < V; i++)
if (dist[i] == INF)
dist[i] = -1;
return dist;
}
};
struct UF {
vector<int> par, size;
UF(int N) : par(N), size(N, 1) { rep0(i, N) par[i] = i; }
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] = ry;
size[rx] += size[ry];
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int getSize(int x) { return size[x]; }
};
struct BIT {
vector<lli> tab; // bit table
lli count;
BIT(int n) : tab(n + 1, 0) { count = n + 1; }
void add(int p, lli num) {
for (++p; p < count; p += p & -p)
tab[p] += num;
}
lli sum(int p) { // from 0 to p
lli ans = 0;
for (++p; p > 0; p -= p & -p)
ans += tab[p];
return ans;
}
void debug() {
rep0(i, count) cout << tab[i] << ' ';
cout << endl;
}
};
// pairに関するもの
lli sum(p c) { return (c.first + c.second); }
p operator-(const p &l, const p &r) {
return {l.first - r.first, l.second - r.second};
}
bool cmpf(const pair<lli, lli> left, const pair<lli, lli> right) {
return left.first < right.first;
}
bool cmps(const pair<lli, lli> left, const pair<lli, lli> right) {
return left.second < right.second;
}
lli gcd(lli a, lli b) {
if (a < b)
return gcd(b, a);
lli temp;
while ((temp = a % b)) {
a = b;
b = temp;
}
return b;
}
struct nCr {
vector<lli> f, rf;
lli count = 0;
nCr(lli num) : f(num), rf(num) {
count = num;
f[0] = 1;
rep1(i, num) f[i] = (f[i - 1] * i) % MOD;
rep0(i, num + 1) rf[i] = inv(f[i]);
}
lli inv(lli x) {
lli res = 1;
lli k = MOD - 2;
lli y = x;
while (k) {
if (k & 1)
res = (res * y) % MOD;
y = (y * y) % MOD;
k /= 2;
}
return res;
}
lli C(lli n, lli k) {
if (k > n || k < 0)
return 0;
lli a = f[n];
lli b = rf[n - k];
lli c = rf[k];
lli bc = (b * c) % MOD;
return (a * bc) % MOD;
}
};
int main() {
lli n, q;
cin >> n >> q;
vector<lli> orig(n), lastappend(500001, -1);
rep0(i, n) cin >> orig[i];
BIT b(500001);
vector<p> vec(q);
map<p, lli> mp;
map<lli, set<lli>> pq;
rep0(i, q) {
cin >> vec[i].first >> vec[i].second;
pq[vec[i].second].insert(vec[i].first);
}
lli right = 0;
for (auto e : pq) {
while (e.first > right) {
if (lastappend[orig[right]] != -1) {
b.add(lastappend[orig[right]], -1);
}
lastappend[orig[right]] = right + 1;
b.add(right + 1, 1);
// cout << b.sum(right) << '\n';
right++;
}
for (auto f : e.second) {
mp[make_pair(f, e.first)] = b.sum(e.first) - b.sum(f - 1);
}
}
// b.debug();
rep0(i, q) { cout << mp[vec[i]] << '\n'; }
}
| replace | 207 | 208 | 207 | 208 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define N 300001
typedef struct Query {
int l, r, idx;
} query;
int block;
bool cmp(query a, query b) {
if (a.l / block != b.l / block)
return a.l / block < b.l / block;
return a.r < b.r;
}
int ans[N], c[N], cnt[N];
query q[N];
int main() {
fastIO;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> c[i];
block = (int)sqrt(n);
for (int i = 0; i < m; i++)
cin >> q[i].l >> q[i].r, q[i].idx = i;
sort(q, q + m, cmp);
int l = 0, r = -1, temp = 0;
for (int i = 0; i < m; i++) {
int ql = q[i].l - 1, qr = q[i].r - 1;
while (r < qr)
temp += !cnt[c[++r]]++;
while (r > qr)
temp -= !--cnt[c[r--]];
while (l < ql)
temp -= !--cnt[c[l++]];
while (l > ql)
temp += !cnt[c[--l]]++;
ans[q[i].idx] = temp;
}
for (int i = 0; i < m; i++)
cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define N 500001
typedef struct Query {
int l, r, idx;
} query;
int block;
bool cmp(query a, query b) {
if (a.l / block != b.l / block)
return a.l / block < b.l / block;
return a.r < b.r;
}
int ans[N], c[N], cnt[N];
query q[N];
int main() {
fastIO;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> c[i];
block = (int)sqrt(n);
for (int i = 0; i < m; i++)
cin >> q[i].l >> q[i].r, q[i].idx = i;
sort(q, q + m, cmp);
int l = 0, r = -1, temp = 0;
for (int i = 0; i < m; i++) {
int ql = q[i].l - 1, qr = q[i].r - 1;
while (r < qr)
temp += !cnt[c[++r]]++;
while (r > qr)
temp -= !--cnt[c[r--]];
while (l < ql)
temp -= !--cnt[c[l++]];
while (l > ql)
temp += !cnt[c[--l]]++;
ans[q[i].idx] = temp;
}
for (int i = 0; i < m; i++)
cout << ans[i] << endl;
return 0;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p02599 | C++ | Runtime Error | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define lfs cout << fixed << setprecision(10)
#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i, n, m) for (ll i = (n); i < (ll)(m); i++)
#define rrep(i, n, m) for (ll i = (ll)(m)-1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9 + 7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
ll median(ll a, ll b, ll c) {
return a + b + c - max({a, b, c}) - min({a, b, c});
}
void ans1(bool x) {
if (x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void ans2(bool x) {
if (x)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void ans3(bool x) {
if (x)
cout << "Yay!" << endl;
else
cout << ":(" << endl;
}
template <typename T1, typename T2> void ans(bool x, T1 y, T2 z) {
if (x)
cout << y << endl;
else
cout << z << endl;
}
template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
cout << v[i][0];
for (ll j = 1; j < w; j++)
cout spa v[i][j];
cout << endl;
}
};
void debug(vector<string> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
for (ll j = 0; j < w; j++)
cout << v[i][j];
cout << endl;
}
};
template <typename T> void debug(vector<T> &v, ll n) {
if (n != 0)
cout << v[0];
for (ll i = 1; i < n; i++)
cout spa v[i];
cout << endl;
};
template <typename T> vector<vector<T>> vec(ll x, ll y, T w) {
vector<vector<T>> v(x, vector<T>(y, w));
return v;
}
ll gcd(ll x, ll y) {
ll r;
while (y != 0 && (r = x % y) != 0) {
x = y;
y = r;
}
return y == 0 ? x : y;
}
vector<ll> dx = {1, -1, 0, 0, 1, 1, -1, -1};
vector<ll> dy = {0, 0, 1, -1, 1, -1, 1, -1};
template <typename T> vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << p.first << " " << p.second;
}
// mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> struct BIT {
ll n;
ll k = 1;
vector<T> data;
BIT() = default;
BIT(ll size) : n(size) {
data.assign(n, 0);
while (k * 2 <= n)
k *= 2;
}
void add(ll a, T w) {
for (ll i = a + 1; i <= n; i += i & -i)
data[i - 1] += w;
}
T sum(ll a) {
if (a < 0)
return 0;
T ret = 0;
for (ll i = a + 1; i > 0; i -= i & -i)
ret += data[i - 1];
return ret;
}
T sum(ll a, ll b) { return a > b ? 0 : sum(b) - sum(a - 1); }
T operator[](ll pos) { return sum(pos, pos); }
ll lower_bound(ll x) {
ll ret = 0;
for (ll i = k; i > 0; i /= 2) {
if (ret + i <= n && data[ret + i - 1] < x) {
x -= data[ret + i - 1];
ret += i;
}
}
return ret;
}
void print() {
for (ll i = 0; i < n; i++) {
if (i != 0)
cout << " ";
cout << (*this)[i];
}
cout << endl;
}
};
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
ll res = 0, buf = 0;
bool judge = true;
ll n, q;
cin >> n >> q;
vector<ll> c(n), l(q), r(q);
rep(i, 0, n) cin >> c[i], c[i]--;
;
vector<vector<ll>> g(n);
rep(i, 0, q) {
cin >> l[i] >> r[i];
l[i]--;
r[i]--;
g[r[i]].PB(i);
}
BIT<ll> bit(n);
vector<ll> ret(q);
vector<ll> pre(100005, -1);
rep(i, 0, n) {
bit.add(i, 1);
if (pre[c[i]] != -1)
bit.add(pre[c[i]], -1);
pre[c[i]] = i;
for (auto z : g[i])
ret[z] = bit.sum(l[z], r[z]);
}
rep(i, 0, q) cout << ret[i] << endl;
return 0;
} | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define lfs cout << fixed << setprecision(10)
#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i, n, m) for (ll i = (n); i < (ll)(m); i++)
#define rrep(i, n, m) for (ll i = (ll)(m)-1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9 + 7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
ll median(ll a, ll b, ll c) {
return a + b + c - max({a, b, c}) - min({a, b, c});
}
void ans1(bool x) {
if (x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void ans2(bool x) {
if (x)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void ans3(bool x) {
if (x)
cout << "Yay!" << endl;
else
cout << ":(" << endl;
}
template <typename T1, typename T2> void ans(bool x, T1 y, T2 z) {
if (x)
cout << y << endl;
else
cout << z << endl;
}
template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
cout << v[i][0];
for (ll j = 1; j < w; j++)
cout spa v[i][j];
cout << endl;
}
};
void debug(vector<string> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
for (ll j = 0; j < w; j++)
cout << v[i][j];
cout << endl;
}
};
template <typename T> void debug(vector<T> &v, ll n) {
if (n != 0)
cout << v[0];
for (ll i = 1; i < n; i++)
cout spa v[i];
cout << endl;
};
template <typename T> vector<vector<T>> vec(ll x, ll y, T w) {
vector<vector<T>> v(x, vector<T>(y, w));
return v;
}
ll gcd(ll x, ll y) {
ll r;
while (y != 0 && (r = x % y) != 0) {
x = y;
y = r;
}
return y == 0 ? x : y;
}
vector<ll> dx = {1, -1, 0, 0, 1, 1, -1, -1};
vector<ll> dy = {0, 0, 1, -1, 1, -1, 1, -1};
template <typename T> vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << p.first << " " << p.second;
}
// mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> struct BIT {
ll n;
ll k = 1;
vector<T> data;
BIT() = default;
BIT(ll size) : n(size) {
data.assign(n, 0);
while (k * 2 <= n)
k *= 2;
}
void add(ll a, T w) {
for (ll i = a + 1; i <= n; i += i & -i)
data[i - 1] += w;
}
T sum(ll a) {
if (a < 0)
return 0;
T ret = 0;
for (ll i = a + 1; i > 0; i -= i & -i)
ret += data[i - 1];
return ret;
}
T sum(ll a, ll b) { return a > b ? 0 : sum(b) - sum(a - 1); }
T operator[](ll pos) { return sum(pos, pos); }
ll lower_bound(ll x) {
ll ret = 0;
for (ll i = k; i > 0; i /= 2) {
if (ret + i <= n && data[ret + i - 1] < x) {
x -= data[ret + i - 1];
ret += i;
}
}
return ret;
}
void print() {
for (ll i = 0; i < n; i++) {
if (i != 0)
cout << " ";
cout << (*this)[i];
}
cout << endl;
}
};
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
ll res = 0, buf = 0;
bool judge = true;
ll n, q;
cin >> n >> q;
vector<ll> c(n), l(q), r(q);
rep(i, 0, n) cin >> c[i], c[i]--;
;
vector<vector<ll>> g(n);
rep(i, 0, q) {
cin >> l[i] >> r[i];
l[i]--;
r[i]--;
g[r[i]].PB(i);
}
BIT<ll> bit(n);
vector<ll> ret(q);
vector<ll> pre(500005, -1);
rep(i, 0, n) {
bit.add(i, 1);
if (pre[c[i]] != -1)
bit.add(pre[c[i]], -1);
pre[c[i]] = i;
for (auto z : g[i])
ret[z] = bit.sum(l[z], r[z]);
}
rep(i, 0, q) cout << ret[i] << endl;
return 0;
} | replace | 174 | 175 | 174 | 175 | 0 | |
p02599 | C++ | Time Limit Exceeded | #define taskname "test"
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define fi first
#define se second
typedef long long lli;
typedef pair<int, int> pii;
const int maxn = 5e5 + 5;
const int K = 710;
int n, Q;
int a[maxn];
pair<pii, int> query[maxn];
int cnt[maxn], ans[maxn];
template <typename T> void Read(T &x) {
bool Neg = false;
char c;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
if (c == '-')
Neg = !Neg;
x = c - '0';
for (c = getchar(); c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
if (Neg && x > 0)
x = -x;
}
template <typename T> void WriteU(T x) {
if (x > 9)
WriteU(x / 10);
putchar(x % 10 + '0');
}
void read_input() {
// cin >> n >> Q;
Read(n);
Read(Q);
for (int i = 1; i <= n; ++i)
// cin >> a[i];
Read(a[i]);
for (int i = 1; i <= Q; ++i) {
int l, r;
// cin >> l >> r;
Read(l);
Read(r);
query[i] = make_pair(pii(l, r), i);
}
}
bool cmp(const pair<pii, int> &x, const pair<pii, int> &y) {
if (x.fi.fi / K == y.fi.fi / K)
return x.fi.se < y.fi.se;
return x.fi.fi < y.fi.fi;
}
void update(int l, int r, int &cur) {
static int curl = 1, curr = 0;
while (curr < r) {
++curr;
++cnt[a[curr]];
if (cnt[a[curr]] == 1)
++cur;
}
while (curr > r) {
--cnt[a[curr]];
if (cnt[a[curr]] == 0)
--cur;
--curr;
}
while (curl < l) {
--cnt[a[curl]];
if (cnt[a[curl]] == 0)
--cur;
++curl;
}
while (curl > l) {
--curl;
++cnt[a[curl]];
if (cnt[a[curl]] == 1)
++cur;
}
}
void solve() {
sort(query + 1, query + Q + 1, cmp);
fill(begin(cnt), end(cnt), 0);
int cur = 0;
for (int i = 1; i <= Q; ++i) {
int l = query[i].fi.fi, r = query[i].fi.se;
int id = query[i].se;
update(l, r, cur);
ans[id] = cur;
}
for (int i = 1; i <= Q; ++i) {
WriteU(ans[i]);
putchar('\n');
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
read_input();
solve();
}
| #define taskname "test"
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define fi first
#define se second
typedef long long lli;
typedef pair<int, int> pii;
const int maxn = 5e5 + 5;
const int K = 710;
int n, Q;
int a[maxn];
pair<pii, int> query[maxn];
int cnt[maxn], ans[maxn];
template <typename T> void Read(T &x) {
bool Neg = false;
char c;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
if (c == '-')
Neg = !Neg;
x = c - '0';
for (c = getchar(); c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
if (Neg && x > 0)
x = -x;
}
template <typename T> void WriteU(T x) {
if (x > 9)
WriteU(x / 10);
putchar(x % 10 + '0');
}
void read_input() {
// cin >> n >> Q;
Read(n);
Read(Q);
for (int i = 1; i <= n; ++i)
// cin >> a[i];
Read(a[i]);
for (int i = 1; i <= Q; ++i) {
int l, r;
// cin >> l >> r;
Read(l);
Read(r);
query[i] = make_pair(pii(l, r), i);
}
}
bool cmp(const pair<pii, int> &x, const pair<pii, int> &y) {
if (x.fi.fi / K == y.fi.fi / K)
return x.fi.se < y.fi.se;
return x.fi.fi < y.fi.fi;
}
inline void update(const int &l, const int &r, int &cur) {
static int curl = 1, curr = 0;
while (curr < r) {
++curr;
++cnt[a[curr]];
if (cnt[a[curr]] == 1)
++cur;
}
while (curr > r) {
--cnt[a[curr]];
if (cnt[a[curr]] == 0)
--cur;
--curr;
}
while (curl < l) {
--cnt[a[curl]];
if (cnt[a[curl]] == 0)
--cur;
++curl;
}
while (curl > l) {
--curl;
++cnt[a[curl]];
if (cnt[a[curl]] == 1)
++cur;
}
}
void solve() {
sort(query + 1, query + Q + 1, cmp);
fill(begin(cnt), end(cnt), 0);
int cur = 0;
for (int i = 1; i <= Q; ++i) {
int l = query[i].fi.fi, r = query[i].fi.se;
int id = query[i].se;
update(l, r, cur);
ans[id] = cur;
}
for (int i = 1; i <= Q; ++i) {
WriteU(ans[i]);
putchar('\n');
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
read_input();
solve();
}
| replace | 63 | 64 | 63 | 64 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 200200;
#include <iostream>
#include <map>
using namespace std;
#define N 500500
int t[N * 20];
int L[N * 20];
int R[N * 20];
int f[N];
#define next jklsjgklsjg
#define prev kfjgls
int prev[N];
int next[N];
int a[N];
int next_free = 1;
int n = N;
void build(int v, int tl, int tr) {
if (tl == tr) {
t[v] = prev[tl] == 0;
} else {
int tm = (tl + tr) >> 1;
L[v] = ++next_free;
R[v] = ++next_free;
build(L[v], tl, tm);
build(R[v], tm + 1, tr);
t[v] = t[L[v]] + t[R[v]];
}
}
void update(int v, int orig_v, int pos, int tl, int tr) {
if (tl == tr) {
t[v] = 1;
} else {
int tm = (tl + tr) >> 1;
if (pos <= tm) {
L[v] = ++next_free;
R[v] = R[orig_v];
update(L[v], L[orig_v], pos, tl, tm);
} else {
L[v] = L[orig_v];
R[v] = ++next_free;
update(R[v], R[orig_v], pos, tm + 1, tr);
}
t[v] = t[L[v]] + t[R[v]];
}
}
int getsum(int l, int r, int v, int tl, int tr) {
if (l > tr || r < tl)
return 0;
if (l <= tl && tr <= r)
return t[v];
int tm = (tl + tr) >> 1;
return getsum(l, r, L[v], tl, tm) + getsum(l, r, R[v], tm + 1, tr);
}
int distinct(int l, int r) { return getsum(l, r, f[l], 1, n); }
void solve() {
cin >> n;
int q;
cin >> q;
map<int, int> last;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (last[a[i]] == 0) {
last[a[i]] = i;
} else {
prev[i] = last[a[i]];
next[prev[i]] = i;
last[a[i]] = i;
}
}
build(0, 1, n);
for (int i = 2; i <= n; i++) {
if (next[i - 1] != 0) {
f[i] = ++next_free;
update(f[i], f[i - 1], next[i - 1], 1, n);
} else
f[i] = f[i - 1];
}
int l, r;
while (q--) {
cin >> l >> r;
cout << distinct(l, r) << endl;
}
}
int main() {
#ifdef QWERTY
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case #" << i << ':' << endl;
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 200200;
#include <iostream>
#include <map>
using namespace std;
#define N 500500
int t[N * 25];
int L[N * 25];
int R[N * 25];
int f[N];
#define next jklsjgklsjg
#define prev kfjgls
int prev[N];
int next[N];
int a[N];
int next_free = 1;
int n = N;
void build(int v, int tl, int tr) {
if (tl == tr) {
t[v] = prev[tl] == 0;
} else {
int tm = (tl + tr) >> 1;
L[v] = ++next_free;
R[v] = ++next_free;
build(L[v], tl, tm);
build(R[v], tm + 1, tr);
t[v] = t[L[v]] + t[R[v]];
}
}
void update(int v, int orig_v, int pos, int tl, int tr) {
if (tl == tr) {
t[v] = 1;
} else {
int tm = (tl + tr) >> 1;
if (pos <= tm) {
L[v] = ++next_free;
R[v] = R[orig_v];
update(L[v], L[orig_v], pos, tl, tm);
} else {
L[v] = L[orig_v];
R[v] = ++next_free;
update(R[v], R[orig_v], pos, tm + 1, tr);
}
t[v] = t[L[v]] + t[R[v]];
}
}
int getsum(int l, int r, int v, int tl, int tr) {
if (l > tr || r < tl)
return 0;
if (l <= tl && tr <= r)
return t[v];
int tm = (tl + tr) >> 1;
return getsum(l, r, L[v], tl, tm) + getsum(l, r, R[v], tm + 1, tr);
}
int distinct(int l, int r) { return getsum(l, r, f[l], 1, n); }
void solve() {
cin >> n;
int q;
cin >> q;
map<int, int> last;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (last[a[i]] == 0) {
last[a[i]] = i;
} else {
prev[i] = last[a[i]];
next[prev[i]] = i;
last[a[i]] = i;
}
}
build(0, 1, n);
for (int i = 2; i <= n; i++) {
if (next[i - 1] != 0) {
f[i] = ++next_free;
update(f[i], f[i - 1], next[i - 1], 1, n);
} else
f[i] = f[i - 1];
}
int l, r;
while (q--) {
cin >> l >> r;
cout << distinct(l, r) << endl;
}
}
int main() {
#ifdef QWERTY
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case #" << i << ':' << endl;
solve();
}
return 0;
} | replace | 10 | 13 | 10 | 13 | -11 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long int
#define PI acos(-1)
#define read freopen("in.txt", "r", stdin)
#define uu first
#define vv second
#define write freopen("out.txt", "w", stdout)
#define pii pair<int, int>
#define INF 1e9
#define EPS 1e-8
#define MAX 100005
#define MOD 1000000007
#define fastRead \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
using namespace std;
int t, tst, n, m;
int tree[4 * MAX], ara[MAX], now[MAX], val[MAX], ans[MAX];
vector<int> edg[MAX];
struct info {
int uu, vv, id;
};
vector<info> range;
bool comp(info in1, info in2) {
if (in1.uu == in2.uu) {
if (in1.vv == in2.vv) {
return in1.id < in2.id;
}
return in1.vv < in2.vv;
}
return in1.uu < in2.uu;
}
void build(int node, int l, int r) {
if (l == r) {
tree[node] = val[l];
return;
}
int mid = (l + r) >> 1;
build(node << 1, l, mid);
build(node << 1 | 1, mid + 1, r);
tree[node] = tree[node << 1] + tree[node << 1 | 1];
}
void update(int node, int l, int r, int key, int val) {
if (l > r || l > key || r < key)
return;
if (l == r) {
tree[node] = val;
return;
}
int mid = (l + r) >> 1;
update(node << 1, l, mid, key, val);
update(node << 1 | 1, mid + 1, r, key, val);
tree[node] = tree[node << 1] + tree[node << 1 | 1];
}
int query(int node, int l, int r, int frm, int to) {
if (l > r || l > to || r < frm)
return 0;
if (l >= frm && r <= to) {
return tree[node];
}
int mid = (l + r) >> 1;
int q1 = query(node << 1, l, mid, frm, to);
int q2 = query(node << 1 | 1, mid + 1, r, frm, to);
return q1 + q2;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &ara[i]);
edg[ara[i]].push_back(i);
}
for (int i = 0; i < m; i++) {
int frm, to;
scanf("%d %d", &frm, &to);
frm--;
to--;
info tmp;
tmp.uu = frm;
tmp.vv = to;
tmp.id = i;
range.push_back(tmp);
}
sort(range.begin(), range.end(), comp);
for (int i = 0; i < n; i++) {
if (edg[ara[i]][now[ara[i]]] == i) {
val[i] = 1;
}
}
build(1, 0, n - 1);
int prev = 0;
for (int i = 0; i < range.size(); i++) {
int frm = range[i].uu;
int to = range[i].vv;
for (int j = prev; j < frm; j++) {
if (val[j] == 1) {
update(1, 0, n - 1, j, 0);
val[j] = 0;
now[ara[j]]++;
if (now[ara[j]] < edg[ara[j]].size()) {
int id = edg[ara[j]][now[ara[j]]];
val[id] = 1;
update(1, 0, n - 1, id, 1);
}
}
}
prev = frm;
ans[range[i].id] = query(1, 0, n - 1, frm, to);
}
for (int i = 0; i < m; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
| #include <bits/stdc++.h>
#define MAX 500005
using namespace std;
int t, tst, n, m;
int tree[4 * MAX], ara[MAX], now[MAX], val[MAX], ans[MAX];
vector<int> edg[MAX];
struct info {
int uu, vv, id;
};
vector<info> range;
bool comp(info in1, info in2) {
if (in1.uu == in2.uu) {
if (in1.vv == in2.vv) {
return in1.id < in2.id;
}
return in1.vv < in2.vv;
}
return in1.uu < in2.uu;
}
void build(int node, int l, int r) {
if (l == r) {
tree[node] = val[l];
return;
}
int mid = (l + r) >> 1;
build(node << 1, l, mid);
build(node << 1 | 1, mid + 1, r);
tree[node] = tree[node << 1] + tree[node << 1 | 1];
}
void update(int node, int l, int r, int key, int val) {
if (l > r || l > key || r < key)
return;
if (l == r) {
tree[node] = val;
return;
}
int mid = (l + r) >> 1;
update(node << 1, l, mid, key, val);
update(node << 1 | 1, mid + 1, r, key, val);
tree[node] = tree[node << 1] + tree[node << 1 | 1];
}
int query(int node, int l, int r, int frm, int to) {
if (l > r || l > to || r < frm)
return 0;
if (l >= frm && r <= to) {
return tree[node];
}
int mid = (l + r) >> 1;
int q1 = query(node << 1, l, mid, frm, to);
int q2 = query(node << 1 | 1, mid + 1, r, frm, to);
return q1 + q2;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &ara[i]);
edg[ara[i]].push_back(i);
}
for (int i = 0; i < m; i++) {
int frm, to;
scanf("%d %d", &frm, &to);
frm--;
to--;
info tmp;
tmp.uu = frm;
tmp.vv = to;
tmp.id = i;
range.push_back(tmp);
}
sort(range.begin(), range.end(), comp);
for (int i = 0; i < n; i++) {
if (edg[ara[i]][now[ara[i]]] == i) {
val[i] = 1;
}
}
build(1, 0, n - 1);
int prev = 0;
for (int i = 0; i < range.size(); i++) {
int frm = range[i].uu;
int to = range[i].vv;
for (int j = prev; j < frm; j++) {
if (val[j] == 1) {
update(1, 0, n - 1, j, 0);
val[j] = 0;
now[ara[j]]++;
if (now[ara[j]] < edg[ara[j]].size()) {
int id = edg[ara[j]][now[ara[j]]];
val[id] = 1;
update(1, 0, n - 1, id, 1);
}
}
}
prev = frm;
ans[range[i].id] = query(1, 0, n - 1, frm, to);
}
for (int i = 0; i < m; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
| replace | 1 | 16 | 1 | 2 | 0 | |
p02599 | C++ | Time Limit Exceeded | // author: hitch_hiker42;
#include <bits/stdc++.h>
using namespace std;
// solution:
#define int int64_t
#define block(i) (i / B)
constexpr int N = 500001, A = 500'001, Q = 500'001;
int f[A], a[N], ans[Q], B = 707, answer;
struct query {
int lo, hi, idx;
query() {}
query(int lo, int hi, int idx) : lo(lo), hi(hi), idx(idx) {}
bool operator<(const query &q) {
return block(lo) < block(q.lo) || (block(lo) == block(q.lo) and hi < q.hi);
}
} q[Q];
void hike() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < m; ++i) {
cin >> q[i].lo >> q[i].hi;
--q[i].lo;
--q[i].hi;
q[i].idx = i;
}
sort(q, q + m);
auto insert = [&](int i) {
f[a[i]]++;
if (f[a[i]] == 1)
++answer;
};
auto remove = [&](int i) {
f[a[i]]--;
if (f[a[i]] == 0)
--answer;
};
int l = 0, r = -1;
for (int i = 0; i < m; ++i) {
int lo = q[i].lo, hi = q[i].hi;
while (lo < l)
insert(--l);
while (hi > r)
insert(++r);
while (lo > l)
remove(l++);
while (hi < r)
remove(r--);
ans[q[i].idx] = answer;
}
for (int i = 0; i < m; ++i)
cout << ans[i] << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1; // cin >> t;
while (t--)
hike();
return 0;
} // farewell, until we meet again..
| // author: hitch_hiker42;
#include <bits/stdc++.h>
using namespace std;
// solution:
#define block(i) (i / B)
constexpr int N = 500001, A = 500'001, Q = 500'001;
int f[A], a[N], ans[Q], B = 707, answer;
struct query {
int lo, hi, idx;
query() {}
query(int lo, int hi, int idx) : lo(lo), hi(hi), idx(idx) {}
bool operator<(const query &q) {
return block(lo) < block(q.lo) || (block(lo) == block(q.lo) and hi < q.hi);
}
} q[Q];
void hike() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < m; ++i) {
cin >> q[i].lo >> q[i].hi;
--q[i].lo;
--q[i].hi;
q[i].idx = i;
}
sort(q, q + m);
auto insert = [&](int i) {
f[a[i]]++;
if (f[a[i]] == 1)
++answer;
};
auto remove = [&](int i) {
f[a[i]]--;
if (f[a[i]] == 0)
--answer;
};
int l = 0, r = -1;
for (int i = 0; i < m; ++i) {
int lo = q[i].lo, hi = q[i].hi;
while (lo < l)
insert(--l);
while (hi > r)
insert(++r);
while (lo > l)
remove(l++);
while (hi < r)
remove(r--);
ans[q[i].idx] = answer;
}
for (int i = 0; i < m; ++i)
cout << ans[i] << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1; // cin >> t;
while (t--)
hike();
return 0;
} // farewell, until we meet again.. | delete | 5 | 6 | 5 | 5 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
inline int popcount(const int x) { return __builtin_popcount(x); }
template <class T> void chmin(T &a, const T &b) noexcept {
if (b < a)
a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
if (a < b)
a = b;
}
template <class T> void drop(const T &x) {
std::cout << x << endl;
exit(0);
}
void debug_out() { std::cout << "\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
std::cout << x << " ";
debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
struct InitIO {
InitIO() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
}
} init_io;
template <typename T> struct BIT {
int n;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {}
void add(int i, T x = 1) {
assert(i < n);
for (i++; i <= n; i += i & -i)
d[i] += x; // LSB:i&(-i)
}
T sum(int i) {
assert(i < n);
T x = 0;
for (i++; i; i -= i & -i)
x += d[i];
return x;
}
T sum(int l, int r) { //[l,r)
assert(l < n and r <= n);
return sum(r - 1) - sum(l - 1);
}
T operator[](const int &i) const {
assert(i < n);
return sum(i, i + 1);
}
};
int main() {
int n, q;
cin >> n >> q;
vector<int> c(n);
rep(i, n) {
cin >> c[i];
c[i]--;
}
using P = pair<pair<int, int>, int>;
vector<P> query(q);
rep(i, q) {
int l, r;
cin >> l >> r;
l--;
r--;
query[i].second = i;
query[i].first.first = r;
query[i].first.second = l;
}
sort(all(query));
BIT<int> bit(n);
vector<int> last(n, -1);
auto update = [&](int i) {
if (last[c[i]] != -1)
bit.add(last[c[i]], -1);
last[c[i]] = i;
bit.add(i);
};
int r = query[0].first.first;
vector<int> ans(q);
rep(i, r + 1) update(i);
ans[query[0].second] =
bit.sum(query[0].first.second, query[0].first.first + 1);
rep2(i, 1, q) {
int nr = query[i].first.first;
rep2(i, r + 1, nr + 1) update(i);
ans[query[i].second] =
bit.sum(query[i].first.second, query[i].first.first + 1);
}
rep(i, q) cout << ans[i] << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
inline int popcount(const int x) { return __builtin_popcount(x); }
template <class T> void chmin(T &a, const T &b) noexcept {
if (b < a)
a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
if (a < b)
a = b;
}
template <class T> void drop(const T &x) {
std::cout << x << endl;
exit(0);
}
void debug_out() { std::cout << "\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
std::cout << x << " ";
debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
struct InitIO {
InitIO() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
}
} init_io;
template <typename T> struct BIT {
int n;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {}
void add(int i, T x = 1) {
assert(i < n);
for (i++; i <= n; i += i & -i)
d[i] += x; // LSB:i&(-i)
}
T sum(int i) {
assert(i < n);
T x = 0;
for (i++; i; i -= i & -i)
x += d[i];
return x;
}
T sum(int l, int r) { //[l,r)
assert(l < n and r <= n);
return sum(r - 1) - sum(l - 1);
}
T operator[](const int &i) const {
assert(i < n);
return sum(i, i + 1);
}
};
int main() {
int n, q;
cin >> n >> q;
vector<int> c(n);
rep(i, n) {
cin >> c[i];
c[i]--;
}
using P = pair<pair<int, int>, int>;
vector<P> query(q);
rep(i, q) {
int l, r;
cin >> l >> r;
l--;
r--;
query[i].second = i;
query[i].first.first = r;
query[i].first.second = l;
}
sort(all(query));
BIT<int> bit(n);
vector<int> last(n, -1);
auto update = [&](int i) {
if (last[c[i]] != -1)
bit.add(last[c[i]], -1);
last[c[i]] = i;
bit.add(i);
};
int r = query[0].first.first;
vector<int> ans(q);
rep(i, r + 1) update(i);
ans[query[0].second] =
bit.sum(query[0].first.second, query[0].first.first + 1);
rep2(i, 1, q) {
int nr = query[i].first.first;
rep2(i, r + 1, nr + 1) update(i);
ans[query[i].second] =
bit.sum(query[i].first.second, query[i].first.first + 1);
r = nr;
}
rep(i, q) cout << ans[i] << "\n";
return 0;
} | insert | 106 | 106 | 106 | 107 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
#define ll long long
struct node {
int l, r;
int id;
} ask[1000005];
ll q[1000005];
ll Ans[1000005];
int block;
bool cmp(node a, node b) {
if (a.l / block == b.l / block) {
return a.r < b.r;
}
return a.l / block < b.l / block;
}
#define il inline
inline ll read() {
char c = getchar();
ll x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0', c = getchar();
}
return x * f;
}
inline void out(ll x) {
if (x > 9)
out(x / 10);
putchar(x % 10 + '0');
}
ll ans = 0;
il void add(int pos) {
if (cnt[q[pos]] == 0)
ans++;
cnt[q[pos]]++;
}
il void del(int pos) {
cnt[q[pos]]--;
if (cnt[q[pos]] == 0)
ans--;
}
int main() {
int n, t;
int m;
n = read(), m = read();
for (int i = 1; i <= n; ++i) {
q[i] = read();
}
block = 500;
for (int i = 1; i <= m; ++i) {
ask[i].l = read();
ask[i].r = read();
// cin>>ask[i].l>>ask[i].r;
ask[i].id = i;
}
sort(ask + 1, ask + 1 + m, cmp);
int l = 1;
int r = 0;
for (int i = 1; i <= m; ++i) {
int L = ask[i].l;
int R = ask[i].r;
int id = ask[i].id;
while (l < L)
del(l++);
while (l > L)
add(--l);
while (r < R)
add(++r);
while (r > R)
del(r--);
Ans[id] = ans;
}
for (int i = 1; i <= m; ++i) {
out(Ans[i]);
puts("");
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
#define ll long long
struct node {
int l, r;
int id;
} ask[1000005];
ll q[1000005];
ll Ans[1000005];
int block;
bool cmp(node a, node b) {
if (a.l / block == b.l / block) {
return a.r < b.r;
}
return a.l / block < b.l / block;
}
#define il inline
inline ll read() {
char c = getchar();
ll x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0', c = getchar();
}
return x * f;
}
inline void out(ll x) {
if (x > 9)
out(x / 10);
putchar(x % 10 + '0');
}
ll ans = 0;
il void add(int pos) {
if (cnt[q[pos]] == 0)
ans++;
cnt[q[pos]]++;
}
il void del(int pos) {
cnt[q[pos]]--;
if (cnt[q[pos]] == 0)
ans--;
}
int main() {
int n, t;
int m;
n = read(), m = read();
for (int i = 1; i <= n; ++i) {
q[i] = read();
}
block = sqrt(n);
for (int i = 1; i <= m; ++i) {
ask[i].l = read();
ask[i].r = read();
// cin>>ask[i].l>>ask[i].r;
ask[i].id = i;
}
sort(ask + 1, ask + 1 + m, cmp);
int l = 1;
int r = 0;
for (int i = 1; i <= m; ++i) {
int L = ask[i].l;
int R = ask[i].r;
int id = ask[i].id;
while (l < L)
del(l++);
while (l > L)
add(--l);
while (r < R)
add(++r);
while (r > R)
del(r--);
Ans[id] = ans;
}
for (int i = 1; i <= m; ++i) {
out(Ans[i]);
puts("");
}
return 0;
} | replace | 54 | 55 | 54 | 55 | TLE | |
p02599 | C++ | Runtime Error | // RAKSHIT KADAM
// -- Declare array globally...
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree< int , null_type , less<int> , rb_tree_tag ,
// tree_order_statistics_node_update> find_by_order returns iterator to element
// present at that index... order_of_key returns no. of elements having value
// stricktly less than that given in the order_of_key();
#warning !!!check the size of arrayss!!!
// NUMERIC DEFINITIONS :
#define INF 100000000000000005
#define inf 1000000003
#define MOD 1000000007
#define newMOD 998244353
#define MAX 200006
#define pi 3.1415926535897932384626433832795
#define ld long doubles
#define ll long long
#define int long long
#define P(gg) \
for (auto rax : gg) { \
cout << rax << " "; \
} \
cout << endl;
#define Need_for_speed(activated) \
ios_base ::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL)
#define satisfy \
ll t; \
cin >> t; \
while (t--)
#define inp(n) \
int n; \
cin >> n
#define mp make_pair
#define pb push_back
#define endl "\n"
#define x first
#define y second
#define fore(i, a, b) for (ll i = a; i < b; i++)
#define forci(i, n) for (ll i = 0; i < n; i++)
#define vi vector<ll>
#define Endl endl
#define lb lower_bound
#define ub upper_bound
#define pii pair<ll, ll>
#define input \
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define time \
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
using namespace std;
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int power(int x, unsigned int y, unsigned int m) {
if (y == 0)
return 1;
int p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(int a, int m) {
{ return power(a, m - 2, m); }
}
const int maxn = 500003;
ll tree[2 * MAX];
ll n;
void build() {
for (ll i = n - 1; i > 0; i--) {
tree[i] = tree[i << 1] + tree[i << 1 | 1];
}
}
void update(ll val, ll pos) {
for (tree[pos += n] = val; pos > 1; pos >>= 1) {
tree[pos >> 1] = tree[pos] + tree[pos ^ 1];
// MODIFY THE SIGN AS PER NEED;
}
}
ll query(ll l, ll r) {
// for querying [1,2]use 0,2
ll res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
res += tree[l++];
if (r & 1)
res += tree[--r];
}
return res;
}
vector<pii> v[maxn];
map<ll, ll> m;
ll ans[maxn];
void solve() {
cin >> n;
ll q;
cin >> q;
ll a[n + 1];
for (ll i = 1; i <= n; i++)
cin >> a[i];
ll aa, bb;
for (ll i = 1; i <= q; i++) {
cin >> aa >> bb;
v[bb].pb(mp(aa, i));
}
for (ll i = 1; i <= n; i++) {
update(1, i - 1);
if (m.count(a[i])) {
update(0, m[a[i]] - 1);
}
m[a[i]] = i;
for (auto j : v[i]) {
ans[j.y] = query(j.x - 1, i);
}
}
for (ll i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
}
signed main() {
Need_for_speed(activated);
// satisfy
// {
// solve();
// }
//
solve();
}
| // RAKSHIT KADAM
// -- Declare array globally...
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree< int , null_type , less<int> , rb_tree_tag ,
// tree_order_statistics_node_update> find_by_order returns iterator to element
// present at that index... order_of_key returns no. of elements having value
// stricktly less than that given in the order_of_key();
#warning !!!check the size of arrayss!!!
// NUMERIC DEFINITIONS :
#define INF 100000000000000005
#define inf 1000000003
#define MOD 1000000007
#define newMOD 998244353
#define MAX 200006
#define pi 3.1415926535897932384626433832795
#define ld long doubles
#define ll long long
#define int long long
#define P(gg) \
for (auto rax : gg) { \
cout << rax << " "; \
} \
cout << endl;
#define Need_for_speed(activated) \
ios_base ::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL)
#define satisfy \
ll t; \
cin >> t; \
while (t--)
#define inp(n) \
int n; \
cin >> n
#define mp make_pair
#define pb push_back
#define endl "\n"
#define x first
#define y second
#define fore(i, a, b) for (ll i = a; i < b; i++)
#define forci(i, n) for (ll i = 0; i < n; i++)
#define vi vector<ll>
#define Endl endl
#define lb lower_bound
#define ub upper_bound
#define pii pair<ll, ll>
#define input \
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define time \
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
using namespace std;
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int power(int x, unsigned int y, unsigned int m) {
if (y == 0)
return 1;
int p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(int a, int m) {
{ return power(a, m - 2, m); }
}
const int maxn = 500003;
ll tree[2 * maxn];
ll n;
void build() {
for (ll i = n - 1; i > 0; i--) {
tree[i] = tree[i << 1] + tree[i << 1 | 1];
}
}
void update(ll val, ll pos) {
for (tree[pos += n] = val; pos > 1; pos >>= 1) {
tree[pos >> 1] = tree[pos] + tree[pos ^ 1];
// MODIFY THE SIGN AS PER NEED;
}
}
ll query(ll l, ll r) {
// for querying [1,2]use 0,2
ll res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
res += tree[l++];
if (r & 1)
res += tree[--r];
}
return res;
}
vector<pii> v[maxn];
map<ll, ll> m;
ll ans[maxn];
void solve() {
cin >> n;
ll q;
cin >> q;
ll a[n + 1];
for (ll i = 1; i <= n; i++)
cin >> a[i];
ll aa, bb;
for (ll i = 1; i <= q; i++) {
cin >> aa >> bb;
v[bb].pb(mp(aa, i));
}
for (ll i = 1; i <= n; i++) {
update(1, i - 1);
if (m.count(a[i])) {
update(0, m[a[i]] - 1);
}
m[a[i]] = i;
for (auto j : v[i]) {
ans[j.y] = query(j.x - 1, i);
}
}
for (ll i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
}
signed main() {
Need_for_speed(activated);
// satisfy
// {
// solve();
// }
//
solve();
}
| replace | 74 | 75 | 74 | 75 | 0 | |
p02599 | C++ | Time Limit Exceeded | // Program to compute no. of different elements
// of ranges for different range queries
#include <bits/stdc++.h>
using namespace std;
// Used in frequency array (maximum value of an
// array element).
const int MAX = 1000000;
// Variable to represent block size. This is made
// global so compare() of sort can use it.
int block;
// Structure to represent a query range and to store
// index and result of a particular query range
struct Query {
int L, R, index, result;
};
// Function used to sort all queries so that all queries
// of same block are arranged together and within a block,
// queries are sorted in increasing order of R values.
bool compare(Query x, Query y) {
// Different blocks, sort by block.
if (x.L / block != y.L / block)
return x.L / block < y.L / block;
// Same block, sort by R value
return x.R < y.R;
}
// Function used to sort all queries in order of their
// index value so that results of queries can be printed
// in same order as of input
bool compare1(Query x, Query y) { return x.index < y.index; }
// calculate distinct elements of all query ranges.
// m is number of queries n is size of array a[].
void queryResults(int a[], int n, Query q[], int m) {
// Find block size
block = (int)sqrt(n);
// Sort all queries so that queries of same
// blocks are arranged together.
sort(q, q + m, compare);
// Initialize current L, current R and current
// different elements
int currL = 0, currR = 0;
int curr_Diff_elements = 0;
// Initialize frequency array with 0
int freq[MAX] = {0};
// Traverse through all queries
for (int i = 0; i < m; i++) {
// L and R values of current range
int L = q[i].L, R = q[i].R;
// Remove extra elements of previous range.
// For example if previous range is [0, 3]
// and current range is [2, 5], then a[0]
// and a[1] are subtracted
while (currL < L) {
// element a[currL] is removed
freq[a[currL]]--;
if (freq[a[currL]] == 0)
curr_Diff_elements--;
currL++;
}
// Add Elements of current Range
// Note:- during addition of the left
// side elements we have to add currL-1
// because currL is already in range
while (currL > L) {
freq[a[currL - 1]]++;
// include a element if it occurs first time
if (freq[a[currL - 1]] == 1)
curr_Diff_elements++;
currL--;
}
while (currR <= R) {
freq[a[currR]]++;
// include a element if it occurs first time
if (freq[a[currR]] == 1)
curr_Diff_elements++;
currR++;
}
// Remove elements of previous range. For example
// when previous range is [0, 10] and current range
// is [3, 8], then a[9] and a[10] are subtracted
// Note:- Basically for a previous query L to R
// currL is L and currR is R+1. So during removal
// of currR remove currR-1 because currR was
// never included
while (currR > R + 1) {
// element a[currL] is removed
freq[a[currR - 1]]--;
// if occurrence of a number is reduced
// to zero remove it from list of
// different elements
if (freq[a[currR - 1]] == 0)
curr_Diff_elements--;
currR--;
}
q[i].result = curr_Diff_elements;
}
}
// print the result of all range queries in
// initial order of queries
void printResults(Query q[], int m) {
sort(q, q + m, compare1);
for (int i = 0; i < m; i++) {
cout << q[i].result << endl;
}
}
// Driver program
int main() {
int n, m;
cin >> n >> m;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
Query q[m];
for (int i = 0; i < m; ++i) {
cin >> q[i].L >> q[i].R;
q[i].L--;
q[i].R--;
q[i].index = i;
q[i].result = 0;
}
queryResults(a, n, q, m);
printResults(q, m);
return 0;
}
| // Program to compute no. of different elements
// of ranges for different range queries
#include <bits/stdc++.h>
using namespace std;
// Used in frequency array (maximum value of an
// array element).
const int MAX = 1000000;
// Variable to represent block size. This is made
// global so compare() of sort can use it.
int block;
// Structure to represent a query range and to store
// index and result of a particular query range
struct Query {
int L, R, index, result;
};
// Function used to sort all queries so that all queries
// of same block are arranged together and within a block,
// queries are sorted in increasing order of R values.
bool compare(Query x, Query y) {
// Different blocks, sort by block.
if (x.L / block != y.L / block)
return x.L / block < y.L / block;
// Same block, sort by R value
return x.R < y.R;
}
// Function used to sort all queries in order of their
// index value so that results of queries can be printed
// in same order as of input
bool compare1(Query x, Query y) { return x.index < y.index; }
// calculate distinct elements of all query ranges.
// m is number of queries n is size of array a[].
void queryResults(int a[], int n, Query q[], int m) {
// Find block size
block = 800;
// Sort all queries so that queries of same
// blocks are arranged together.
sort(q, q + m, compare);
// Initialize current L, current R and current
// different elements
int currL = 0, currR = 0;
int curr_Diff_elements = 0;
// Initialize frequency array with 0
int freq[MAX] = {0};
// Traverse through all queries
for (int i = 0; i < m; i++) {
// L and R values of current range
int L = q[i].L, R = q[i].R;
// Remove extra elements of previous range.
// For example if previous range is [0, 3]
// and current range is [2, 5], then a[0]
// and a[1] are subtracted
while (currL < L) {
// element a[currL] is removed
freq[a[currL]]--;
if (freq[a[currL]] == 0)
curr_Diff_elements--;
currL++;
}
// Add Elements of current Range
// Note:- during addition of the left
// side elements we have to add currL-1
// because currL is already in range
while (currL > L) {
freq[a[currL - 1]]++;
// include a element if it occurs first time
if (freq[a[currL - 1]] == 1)
curr_Diff_elements++;
currL--;
}
while (currR <= R) {
freq[a[currR]]++;
// include a element if it occurs first time
if (freq[a[currR]] == 1)
curr_Diff_elements++;
currR++;
}
// Remove elements of previous range. For example
// when previous range is [0, 10] and current range
// is [3, 8], then a[9] and a[10] are subtracted
// Note:- Basically for a previous query L to R
// currL is L and currR is R+1. So during removal
// of currR remove currR-1 because currR was
// never included
while (currR > R + 1) {
// element a[currL] is removed
freq[a[currR - 1]]--;
// if occurrence of a number is reduced
// to zero remove it from list of
// different elements
if (freq[a[currR - 1]] == 0)
curr_Diff_elements--;
currR--;
}
q[i].result = curr_Diff_elements;
}
}
// print the result of all range queries in
// initial order of queries
void printResults(Query q[], int m) {
sort(q, q + m, compare1);
for (int i = 0; i < m; i++) {
cout << q[i].result << endl;
}
}
// Driver program
int main() {
int n, m;
cin >> n >> m;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
Query q[m];
for (int i = 0; i < m; ++i) {
cin >> q[i].L >> q[i].R;
q[i].L--;
q[i].R--;
q[i].index = i;
q[i].result = 0;
}
queryResults(a, n, q, m);
printResults(q, m);
return 0;
}
| replace | 40 | 41 | 40 | 41 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
#define N 511111
#define A 1111111
#define block 200
struct node {
int L, R, i;
} q[N];
int v[N], cnt[A], ans[N], answer = 0;
bool _compare(node x, node y) {
if (x.L / block != y.L / block)
return x.L < y.L;
return x.R < y.R;
}
void _add(int pos) {
cnt[v[pos]]++;
if (cnt[v[pos]] == 1)
++answer;
}
void _remove(int pos) {
cnt[v[pos]]--;
if (cnt[v[pos]] == 0)
--answer;
}
int main() {
int n, Q;
scanf("%d%d", &n, &Q);
for (int i = 0; i < n; i++) {
scanf("%d", &v[i]);
}
for (int i = 0; i < Q; i++) {
int l, r;
scanf("%d%d", &l, &r);
--l, --r;
q[i].L = l;
q[i].R = r;
q[i].i = i;
}
sort(q, q + Q, _compare);
int curr_L = 0, curr_R = 0;
for (int i = 0; i < Q; i++) {
int L = q[i].L, R = q[i].R;
while (curr_L < L) {
_remove(curr_L);
curr_L++;
}
while (curr_L > L) {
_add(curr_L - 1);
curr_L--;
}
while (curr_R <= R) {
_add(curr_R);
curr_R++;
}
while (curr_R > R + 1) {
_remove(curr_R - 1);
curr_R--;
}
ans[q[i].i] = answer;
}
for (int i = 0; i < Q; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
| #include <algorithm>
#include <iostream>
using namespace std;
#define N 511111
#define A 1111111
#define block 800
struct node {
int L, R, i;
} q[N];
int v[N], cnt[A], ans[N], answer = 0;
bool _compare(node x, node y) {
if (x.L / block != y.L / block)
return x.L < y.L;
return x.R < y.R;
}
void _add(int pos) {
cnt[v[pos]]++;
if (cnt[v[pos]] == 1)
++answer;
}
void _remove(int pos) {
cnt[v[pos]]--;
if (cnt[v[pos]] == 0)
--answer;
}
int main() {
int n, Q;
scanf("%d%d", &n, &Q);
for (int i = 0; i < n; i++) {
scanf("%d", &v[i]);
}
for (int i = 0; i < Q; i++) {
int l, r;
scanf("%d%d", &l, &r);
--l, --r;
q[i].L = l;
q[i].R = r;
q[i].i = i;
}
sort(q, q + Q, _compare);
int curr_L = 0, curr_R = 0;
for (int i = 0; i < Q; i++) {
int L = q[i].L, R = q[i].R;
while (curr_L < L) {
_remove(curr_L);
curr_L++;
}
while (curr_L > L) {
_add(curr_L - 1);
curr_L--;
}
while (curr_R <= R) {
_add(curr_R);
curr_R++;
}
while (curr_R > R + 1) {
_remove(curr_R - 1);
curr_R--;
}
ans[q[i].i] = answer;
}
for (int i = 0; i < Q; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
| replace | 7 | 8 | 7 | 8 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;
template <class T> bool ChangeMax(T &a, const T &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> bool ChangeMin(T &a, const T &b) {
if (a <= b)
return false;
a = b;
return true;
}
template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
fill((U *)a, (U *)(a + N), v);
}
template <class T, class U> istream &operator>>(istream &is, P<T, U> &p) {
is >> p.first >> p.second;
return is;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v) {
is >> e;
}
return is;
}
int main() {
int n, q;
cin >> n >> q;
vector<int> line(n);
cin >> line;
vector<int> from_list(n);
{
map<int, int> mp;
for (int i = 0; i < n; ++i) {
if (mp.count(line[i]) == 0) {
from_list[i] = -1;
} else {
from_list[i] = mp[line[i]];
}
mp[line[i]] = i;
}
}
vector<P<int, int>> queries(q);
cin >> queries;
for (auto &e : queries) {
e.first--;
e.second--;
}
static vector<P<int, int>> r_base[500000];
for (int i = 0; i < q; ++i)
r_base[queries[i].second].emplace_back(queries[i].first, i);
vector<int> vec;
vector<int> ans(q);
for (int i = 0; i < n; ++i) {
// debug(i);
auto it = upper_bound(vec.begin(), vec.end(), from_list[i]);
if (it == vec.end() || *it != from_list[i])
vec.insert(it, from_list[i]);
for (auto e : r_base[i]) {
ans[e.second] =
i - e.first + 1 -
(vec.end() - lower_bound(vec.begin(), vec.end(), e.first));
// debug(e.first);
// debug(e.second);
// debug(distance(st.lower_bound(e.first),st.end()));
}
}
for (auto e : ans)
cout << e << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;
template <class T> bool ChangeMax(T &a, const T &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> bool ChangeMin(T &a, const T &b) {
if (a <= b)
return false;
a = b;
return true;
}
template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
fill((U *)a, (U *)(a + N), v);
}
template <class T, class U> istream &operator>>(istream &is, P<T, U> &p) {
is >> p.first >> p.second;
return is;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v) {
is >> e;
}
return is;
}
int main() {
int n, q;
cin >> n >> q;
vector<int> line(n);
cin >> line;
vector<int> from_list(n);
{
map<int, int> mp;
for (int i = 0; i < n; ++i) {
if (mp.count(line[i]) == 0) {
from_list[i] = -1;
} else {
from_list[i] = mp[line[i]];
}
mp[line[i]] = i;
}
}
vector<P<int, int>> queries(q);
cin >> queries;
for (auto &e : queries) {
e.first--;
e.second--;
}
static vector<P<int, int>> r_base[500000];
for (int i = 0; i < q; ++i)
r_base[queries[i].second].emplace_back(queries[i].first, i);
vector<int> vec;
vector<int> ans(q);
for (int i = 0; i < n; ++i) {
// debug(i);
auto it = lower_bound(vec.begin(), vec.end(), from_list[i]);
if (it == vec.end() || *it != from_list[i])
vec.insert(it, from_list[i]);
for (auto e : r_base[i]) {
ans[e.second] =
i - e.first + 1 -
(vec.end() - lower_bound(vec.begin(), vec.end(), e.first));
// debug(e.first);
// debug(e.second);
// debug(distance(st.lower_bound(e.first),st.end()));
}
}
for (auto e : ans)
cout << e << endl;
}
| replace | 75 | 76 | 75 | 76 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
mt19937 rnd;
const int maxn = 1e6, maxk = 1e6 + 1;
int root[maxn], L[16 * maxn], R[16 * maxn], sum[16 * maxn];
int rt = 1, sz = 1;
int lpos[maxk];
int copy(int v, int &u) {
L[sz] = L[v];
R[sz] = R[v];
sum[sz] = sum[v];
return u = sz++;
}
void make_root() {
copy(root[rt - 1], root[rt]);
rt++;
}
void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
sum[v] += x;
if (r - l == 1)
return;
int m = (l + r) / 2;
if (pos < m)
add(pos, x, copy(L[v], L[v]), l, m);
else
add(pos, x, copy(R[v], R[v]), m, r);
}
int get(int a, int b, int v, int l = 0, int r = maxn) {
if (a <= l && r <= b)
return sum[v];
if (r <= a || b <= l)
return 0;
int m = (l + r) / 2;
return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
int t;
cin >> t;
make_root();
add(lpos[t], -1);
lpos[t] = i;
add(lpos[t], 1);
}
while (q--) {
int l, r;
cin >> l >> r;
cout << get(l, r + 1, root[r]) << "\n";
}
#ifdef LOCAL
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
mt19937 rnd;
const int maxn = 5e6, maxk = 5e6 + 1;
int root[maxn], L[16 * maxn], R[16 * maxn], sum[16 * maxn];
int rt = 1, sz = 1;
int lpos[maxk];
int copy(int v, int &u) {
L[sz] = L[v];
R[sz] = R[v];
sum[sz] = sum[v];
return u = sz++;
}
void make_root() {
copy(root[rt - 1], root[rt]);
rt++;
}
void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
sum[v] += x;
if (r - l == 1)
return;
int m = (l + r) / 2;
if (pos < m)
add(pos, x, copy(L[v], L[v]), l, m);
else
add(pos, x, copy(R[v], R[v]), m, r);
}
int get(int a, int b, int v, int l = 0, int r = maxn) {
if (a <= l && r <= b)
return sum[v];
if (r <= a || b <= l)
return 0;
int m = (l + r) / 2;
return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
int t;
cin >> t;
make_root();
add(lpos[t], -1);
lpos[t] = i;
add(lpos[t], 1);
}
while (q--) {
int l, r;
cin >> l >> r;
cout << get(l, r + 1, root[r]) << "\n";
}
#ifdef LOCAL
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
| replace | 10 | 11 | 10 | 11 | -11 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
// #define int long long
#define endl '\n'
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
const int S = 448, maxn = 2e5 + 5;
struct box {
int l, r, i;
bool operator<(const box other) {
if (l / S != other.l / S)
return l / S < other.l / S;
return r < other.r;
}
};
vector<int> a(maxn), ans(maxn), cnt(maxn);
vector<box> q(maxn);
vector<int> mp;
int n, m, tot = 0;
void insert(int x) {
if (!cnt[x])
tot++;
cnt[x]++;
}
void remove(int x) {
cnt[x]--;
if (!cnt[x]) {
tot--;
}
}
int32_t main() {
IOS cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mp.push_back(a[i]);
}
for (int i = 1; i <= m; i++) {
cin >> q[i].l >> q[i].r;
q[i].i = i;
}
sort(q.begin() + 1, q.begin() + m + 1);
sort(mp.begin(), mp.end());
mp.erase(unique(mp.begin(), mp.end()), mp.end());
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(mp.begin(), mp.end(), a[i]) - mp.begin() + 1;
}
int l = 1, r = 1;
for (int i = 1; i <= m; i++) {
while (r <= q[i].r) {
insert(a[r++]);
}
while (l < q[i].l) {
remove(a[l++]);
}
while (l > q[i].l) {
insert(a[--l]);
}
while (r > q[i].r + 1) {
remove(a[--r]);
}
ans[q[i].i] = tot;
}
for (int i = 1; i <= m; i++) {
cout << ans[i] << endl;
}
}
| #include <bits/stdc++.h>
// #define int long long
#define endl '\n'
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
const int S = 1000, maxn = 5e5 + 5;
struct box {
int l, r, i;
bool operator<(const box other) {
if (l / S != other.l / S)
return l / S < other.l / S;
return r < other.r;
}
};
vector<int> a(maxn), ans(maxn), cnt(maxn);
vector<box> q(maxn);
vector<int> mp;
int n, m, tot = 0;
void insert(int x) {
if (!cnt[x])
tot++;
cnt[x]++;
}
void remove(int x) {
cnt[x]--;
if (!cnt[x]) {
tot--;
}
}
int32_t main() {
IOS cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mp.push_back(a[i]);
}
for (int i = 1; i <= m; i++) {
cin >> q[i].l >> q[i].r;
q[i].i = i;
}
sort(q.begin() + 1, q.begin() + m + 1);
sort(mp.begin(), mp.end());
mp.erase(unique(mp.begin(), mp.end()), mp.end());
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(mp.begin(), mp.end(), a[i]) - mp.begin() + 1;
}
int l = 1, r = 1;
for (int i = 1; i <= m; i++) {
while (r <= q[i].r) {
insert(a[r++]);
}
while (l < q[i].l) {
remove(a[l++]);
}
while (l > q[i].l) {
insert(a[--l]);
}
while (r > q[i].r + 1) {
remove(a[--r]);
}
ans[q[i].i] = tot;
}
for (int i = 1; i <= m; i++) {
cout << ans[i] << endl;
}
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#ifdef _DEBUG
#define DUMP(x) std::cerr << (#x) << " = " << (x) << "\n"
#else
#define DUMP(x)
#endif
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define EREP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define RREP(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) REP(i, 0, n)
#define erep(i, n) EREP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)
#define ALL(r) (r).begin(), (r).end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
rep(i, v.size()) os << v[i] << (i == (int)v.size() - 1 ? "" : ", ");
os << "}";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return (os << "(" << p.first << ", " << p.second << ")");
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
bool first = true;
os << "{";
for (const auto &e : m) {
if (!first)
os << ", ";
os << "{" << e.first << ": " << e.second << "}";
first = false;
}
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &s) {
os << "{";
bool first = true;
for (const auto &e : s) {
if (!first)
os << ", ";
os << e;
first = false;
}
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &s) {
os << "{";
bool first = true;
for (const auto &e : s) {
if (!first)
os << ", ";
os << e;
first = false;
}
os << "}";
return os;
}
template <typename T> T dup(T x, T y) { return (x + y - 1) / y; };
template <typename A, size_t N, typename T>
inline void arrayFill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
struct in {
const size_t n = 0;
in() = default;
in(size_t n) : n(n){};
template <typename T> operator T() {
T ret;
cin >> ret;
return ret;
}
template <typename T> operator vector<T>() {
assert(n != 0);
vector<T> ret(n);
for (T &x : ret) {
T tmp = in();
x = tmp;
}
return ret;
}
template <typename T, typename U> operator pair<T, U>() {
pair<T, U> ret;
ret.first = in();
ret.second = in();
return ret;
}
};
namespace fiore::impl {
template <typename T> inline void out_impl(const T &x, char end_char) {
std::cout << x << end_char;
}
template <typename T> inline void out_impl(const vector<T> &x, char end_char) {
bool first = true;
for (const auto &e : x) {
if (!first)
std::cout << ' ';
std::cout << e;
first = false;
}
std::cout << end_char;
}
} // namespace fiore::impl
template <typename T> inline void out(const T &x) {
fiore::impl::out_impl(x, '\n');
};
template <typename T, typename U, typename... Args>
inline void out(const T &x, const U &y, const Args &...args) {
fiore::impl::out_impl(x, ' ');
out(y, args...);
}
using ll = int64_t;
using vint = vector<int32_t>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pint = pair<int32_t, int32_t>;
using vpint = vector<pint>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using setint = set<int32_t>;
using qint = queue<int32_t>;
using qpint = queue<pint>;
constexpr std::int32_t INF = 1001001001;
constexpr std::int64_t LINF = 1001001001001001001;
template <typename T> class SegmentTree {
using F = function<T(T, T)>;
int n;
const F f;
const T ti;
vector<T> dat;
public:
SegmentTree(const int n_, const F f, const T ti,
const vector<T> &v = vector<T>())
: f(f), ti(ti) {
if (n_ == (int)v.size())
build(n_, v);
else
init(n_);
}
// update k_th element to x
void set_val(int k, const T x) {
dat[k += n] = x;
while (k >>= 1)
dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
}
// execute a query against [a, b)
T query(const int a, const int b) {
if (a >= b)
return ti;
T vl = ti, vr = ti;
for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
vl = f(vl, dat[l++]);
if (r & 1)
vr = f(dat[--r], vr);
}
return f(vl, vr);
}
// get k_th element
T operator[](const int k) const { return dat[k + n]; }
private:
void init(const int n_) {
n = 1;
while (n < n_)
n <<= 1;
dat.assign(n << 1, ti);
}
void build(const int n_, const vector<T> &v) {
init(n_);
for (int i = 0; i < n_; i++)
dat[n + i] = v[i];
for (int i = n - 1; i; i--)
dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
}
};
void Main() {
int n = in(), q = in();
vint c = in(n);
vvint pos(n);
rep(i, n) { pos[c[i]].pb(i); }
vector<tuple<int, int, int>> v;
v.reserve(q);
rep(i, n) {
rep(j, pos[i].size()) {
if (j == pos[i].size() - 1) {
v.eb(n + 1, -1, pos[i][j]);
} else
v.eb(pos[i][j + 1], -1, pos[i][j]);
}
}
rep(i, q) {
int l = in(), r = in();
--l, --r;
v.eb(r, i, l);
}
sort(ALL(v));
SegmentTree<int> seg(n + 2, plus<int>(), 0);
vint ans(q);
DUMP(v.size());
for (auto [r, type, l] : v) {
DUMP(l);
DUMP(r);
DUMP(type);
if (type == -1) {
seg.set_val(l, seg[l] + 1);
} else {
ans[type] = (r + 1 - l) - seg.query(l, r + 1);
}
}
for (auto e : ans)
out(e);
}
signed main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
} | #include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#ifdef _DEBUG
#define DUMP(x) std::cerr << (#x) << " = " << (x) << "\n"
#else
#define DUMP(x)
#endif
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define EREP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define RREP(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) REP(i, 0, n)
#define erep(i, n) EREP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)
#define ALL(r) (r).begin(), (r).end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
rep(i, v.size()) os << v[i] << (i == (int)v.size() - 1 ? "" : ", ");
os << "}";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return (os << "(" << p.first << ", " << p.second << ")");
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
bool first = true;
os << "{";
for (const auto &e : m) {
if (!first)
os << ", ";
os << "{" << e.first << ": " << e.second << "}";
first = false;
}
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &s) {
os << "{";
bool first = true;
for (const auto &e : s) {
if (!first)
os << ", ";
os << e;
first = false;
}
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &s) {
os << "{";
bool first = true;
for (const auto &e : s) {
if (!first)
os << ", ";
os << e;
first = false;
}
os << "}";
return os;
}
template <typename T> T dup(T x, T y) { return (x + y - 1) / y; };
template <typename A, size_t N, typename T>
inline void arrayFill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
struct in {
const size_t n = 0;
in() = default;
in(size_t n) : n(n){};
template <typename T> operator T() {
T ret;
cin >> ret;
return ret;
}
template <typename T> operator vector<T>() {
assert(n != 0);
vector<T> ret(n);
for (T &x : ret) {
T tmp = in();
x = tmp;
}
return ret;
}
template <typename T, typename U> operator pair<T, U>() {
pair<T, U> ret;
ret.first = in();
ret.second = in();
return ret;
}
};
namespace fiore::impl {
template <typename T> inline void out_impl(const T &x, char end_char) {
std::cout << x << end_char;
}
template <typename T> inline void out_impl(const vector<T> &x, char end_char) {
bool first = true;
for (const auto &e : x) {
if (!first)
std::cout << ' ';
std::cout << e;
first = false;
}
std::cout << end_char;
}
} // namespace fiore::impl
template <typename T> inline void out(const T &x) {
fiore::impl::out_impl(x, '\n');
};
template <typename T, typename U, typename... Args>
inline void out(const T &x, const U &y, const Args &...args) {
fiore::impl::out_impl(x, ' ');
out(y, args...);
}
using ll = int64_t;
using vint = vector<int32_t>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pint = pair<int32_t, int32_t>;
using vpint = vector<pint>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using setint = set<int32_t>;
using qint = queue<int32_t>;
using qpint = queue<pint>;
constexpr std::int32_t INF = 1001001001;
constexpr std::int64_t LINF = 1001001001001001001;
template <typename T> class SegmentTree {
using F = function<T(T, T)>;
int n;
const F f;
const T ti;
vector<T> dat;
public:
SegmentTree(const int n_, const F f, const T ti,
const vector<T> &v = vector<T>())
: f(f), ti(ti) {
if (n_ == (int)v.size())
build(n_, v);
else
init(n_);
}
// update k_th element to x
void set_val(int k, const T x) {
dat[k += n] = x;
while (k >>= 1)
dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
}
// execute a query against [a, b)
T query(const int a, const int b) {
if (a >= b)
return ti;
T vl = ti, vr = ti;
for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
vl = f(vl, dat[l++]);
if (r & 1)
vr = f(dat[--r], vr);
}
return f(vl, vr);
}
// get k_th element
T operator[](const int k) const { return dat[k + n]; }
private:
void init(const int n_) {
n = 1;
while (n < n_)
n <<= 1;
dat.assign(n << 1, ti);
}
void build(const int n_, const vector<T> &v) {
init(n_);
for (int i = 0; i < n_; i++)
dat[n + i] = v[i];
for (int i = n - 1; i; i--)
dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
}
};
void Main() {
int n = in(), q = in();
vint c = in(n);
vvint pos(n);
rep(i, n) { pos[c[i] - 1].pb(i); }
vector<tuple<int, int, int>> v;
v.reserve(q);
rep(i, n) {
rep(j, pos[i].size()) {
if (j == pos[i].size() - 1) {
v.eb(n + 1, -1, pos[i][j]);
} else
v.eb(pos[i][j + 1], -1, pos[i][j]);
}
}
rep(i, q) {
int l = in(), r = in();
--l, --r;
v.eb(r, i, l);
}
sort(ALL(v));
SegmentTree<int> seg(n + 2, plus<int>(), 0);
vint ans(q);
DUMP(v.size());
for (auto [r, type, l] : v) {
DUMP(l);
DUMP(r);
DUMP(type);
if (type == -1) {
seg.set_val(l, seg[l] + 1);
} else {
ans[type] = (r + 1 - l) - seg.query(l, r + 1);
}
}
for (auto e : ans)
out(e);
}
signed main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
} | replace | 236 | 237 | 236 | 237 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll LINF = 1001002003004005006LL;
const int INF = 1001001001;
const int mod = 1000000007;
template <class Abel> struct BIT {
const Abel UNITY_SUM = 0; // to be set
vector<Abel> dat;
/* [1, n] */
BIT(int n) : dat(n + 1, UNITY_SUM) {}
void init(int n) { dat.assign(n + 1, UNITY_SUM); }
/* a is 1-indexed */
inline void add(int a, Abel x) {
for (int i = a; i < (int)dat.size(); i += i & -i)
dat[i] = dat[i] + x;
}
/* [1, a], a is 1-indexed */
inline Abel sum(int a) {
Abel res = UNITY_SUM;
for (int i = a; i > 0; i -= i & -i)
res = res + dat[i];
return res;
}
/* [a, b), a and b are 1-indexed */
inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); }
/* debug */
void print() {
for (int i = 1; i < (int)dat.size(); ++i)
cout << sum(i, i + 1) << ",";
cout << endl;
}
};
int main() {
int n, q;
cin >> n >> q;
vector<int> c(n);
rep(i, n) cin >> c[i];
vector<int> ids(q);
vector<P> p;
rep(i, q) {
int l, r;
cin >> l >> r;
--l, --r;
ids[i] = i;
p.emplace_back(l, r);
}
sort(ids.begin(), ids.end(),
[&](int x, int y) { return p[x].second < p[y].second; });
// sort(p.begin(), p.end(), [](P x, P y){
// return x.second < y.second;
// });
BIT<int> bit(n);
map<int, int> last;
int now = 0;
vector<int> ans(n);
rep(i, q) {
P x = p[ids[i]];
while (now <= x.second) {
int ci = c[now];
auto itr = last.find(ci);
if (itr != last.end()) {
int pre = last[ci];
last[ci] = now;
bit.add(pre + 1, -1);
bit.add(now + 1, 1);
} else {
last[ci] = now;
bit.add(now + 1, 1);
}
++now;
}
int cnt = bit.sum(x.first + 1, x.second + 2);
ans[ids[i]] = cnt;
}
rep(i, q) cout << ans[i] << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll LINF = 1001002003004005006LL;
const int INF = 1001001001;
const int mod = 1000000007;
template <class Abel> struct BIT {
const Abel UNITY_SUM = 0; // to be set
vector<Abel> dat;
/* [1, n] */
BIT(int n) : dat(n + 1, UNITY_SUM) {}
void init(int n) { dat.assign(n + 1, UNITY_SUM); }
/* a is 1-indexed */
inline void add(int a, Abel x) {
for (int i = a; i < (int)dat.size(); i += i & -i)
dat[i] = dat[i] + x;
}
/* [1, a], a is 1-indexed */
inline Abel sum(int a) {
Abel res = UNITY_SUM;
for (int i = a; i > 0; i -= i & -i)
res = res + dat[i];
return res;
}
/* [a, b), a and b are 1-indexed */
inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); }
/* debug */
void print() {
for (int i = 1; i < (int)dat.size(); ++i)
cout << sum(i, i + 1) << ",";
cout << endl;
}
};
int main() {
int n, q;
cin >> n >> q;
vector<int> c(n);
rep(i, n) cin >> c[i];
vector<int> ids(q);
vector<P> p;
rep(i, q) {
int l, r;
cin >> l >> r;
--l, --r;
ids[i] = i;
p.emplace_back(l, r);
}
sort(ids.begin(), ids.end(),
[&](int x, int y) { return p[x].second < p[y].second; });
// sort(p.begin(), p.end(), [](P x, P y){
// return x.second < y.second;
// });
BIT<int> bit(n);
map<int, int> last;
int now = 0;
vector<int> ans(q);
rep(i, q) {
P x = p[ids[i]];
while (now <= x.second) {
int ci = c[now];
auto itr = last.find(ci);
if (itr != last.end()) {
int pre = last[ci];
last[ci] = now;
bit.add(pre + 1, -1);
bit.add(now + 1, 1);
} else {
last[ci] = now;
bit.add(now + 1, 1);
}
++now;
}
int cnt = bit.sum(x.first + 1, x.second + 2);
ans[ids[i]] = cnt;
}
rep(i, q) cout << ans[i] << endl;
return 0;
} | replace | 77 | 78 | 77 | 78 | 0 | |
p02599 | C++ | Runtime Error | /*
Alfonso2 Peterssen (mukel)
8 - 1 - 2008
SPOJ "DQUERY"
Online algorithm
Preprocessing: O(n lg n)
Query: O(lg n)
Memory: O(n lg n)
*/
#include <cstdio>
#include <map>
using std::map;
const int MAXN = 40000, MAXLGN = 16;
int N, Q;
map<int, int> pos;
int tree[MAXN];
int cant;
struct node {
int val, L, R, size;
} buff[2 * MAXN * MAXLGN];
int build(int lo, int hi) {
if (lo > hi)
return 0;
int idx = ++cant;
int mid = (lo + hi) / 2;
buff[idx] = (node){mid, build(lo, mid - 1), build(mid + 1, hi), 0};
return idx;
}
int update(int x, int val, int amount) {
if (x == 0)
return 0;
int idx = ++cant;
int L = buff[x].L;
int R = buff[x].R;
if (val < buff[x].val)
L = update(L, val, amount);
if (val > buff[x].val)
R = update(R, val, amount);
buff[idx] = (node){buff[x].val, L, R, buff[x].size + amount};
return idx;
}
int query(int x, int val) {
if (val < buff[x].val)
return query(buff[x].L, val) + buff[x].size - buff[buff[x].L].size;
if (val > buff[x].val)
return query(buff[x].R, val);
return buff[x].size - buff[buff[x].L].size;
}
int main() {
scanf("%d %d", &N, &Q);
tree[0] = build(1, N);
for (int i = 1; i <= N; i++) {
int x, posx;
scanf("%d", &x);
posx = pos[x];
if (posx != 0)
tree[i] = update(update(tree[i - 1], posx, -1), i, +1);
else
tree[i] = update(tree[i - 1], i, +1);
pos[x] = i;
}
// scanf( "%d", &Q );
while (Q--) {
int lo, hi;
scanf("%d %d", &lo, &hi);
printf("%d\n", query(tree[hi], lo));
}
return 0;
} | /*
Alfonso2 Peterssen (mukel)
8 - 1 - 2008
SPOJ "DQUERY"
Online algorithm
Preprocessing: O(n lg n)
Query: O(lg n)
Memory: O(n lg n)
*/
#include <cstdio>
#include <map>
using std::map;
const int MAXN = 600000, MAXLGN = 16;
int N, Q;
map<int, int> pos;
int tree[MAXN];
int cant;
struct node {
int val, L, R, size;
} buff[2 * MAXN * MAXLGN];
int build(int lo, int hi) {
if (lo > hi)
return 0;
int idx = ++cant;
int mid = (lo + hi) / 2;
buff[idx] = (node){mid, build(lo, mid - 1), build(mid + 1, hi), 0};
return idx;
}
int update(int x, int val, int amount) {
if (x == 0)
return 0;
int idx = ++cant;
int L = buff[x].L;
int R = buff[x].R;
if (val < buff[x].val)
L = update(L, val, amount);
if (val > buff[x].val)
R = update(R, val, amount);
buff[idx] = (node){buff[x].val, L, R, buff[x].size + amount};
return idx;
}
int query(int x, int val) {
if (val < buff[x].val)
return query(buff[x].L, val) + buff[x].size - buff[buff[x].L].size;
if (val > buff[x].val)
return query(buff[x].R, val);
return buff[x].size - buff[buff[x].L].size;
}
int main() {
scanf("%d %d", &N, &Q);
tree[0] = build(1, N);
for (int i = 1; i <= N; i++) {
int x, posx;
scanf("%d", &x);
posx = pos[x];
if (posx != 0)
tree[i] = update(update(tree[i - 1], posx, -1), i, +1);
else
tree[i] = update(tree[i - 1], i, +1);
pos[x] = i;
}
// scanf( "%d", &Q );
while (Q--) {
int lo, hi;
scanf("%d %d", &lo, &hi);
printf("%d\n", query(tree[hi], lo));
}
return 0;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
// #define double long double
// #define int long long
// #define int unsigned long long
#define ll long long
#define ld long double
#define pi pair<int, int>
#define pl pair<ll, ll>
#define pd pair<ld, ld>
#define str string
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define vd vector<ld>
#define vs vector<str>
#define vpi vector<pi>
#define vpl vector<pl>
#define vpd vector<pd>
#define si set<int>
#define FF(i, j, n) for (signed i = j; i <= n; i++)
#define DD(i, j, n) for (signed i = j; i >= n; i--)
#define F(i, j, n) for (signed i = j; i < n; i++)
#define D(i, j, n) for (signed i = j; i > n; i--)
#define mem(a, b) memset(a, b, sizeof(a))
#define ndl cout << endl
#define ull unsigned long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
#define sz(x) (signed)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define X first
#define Y second
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define SHOWVECTOR(v) \
{ \
std::cerr << #v << "\t: "; \
for (const auto &xxx : v) { \
std::cerr << xxx << " "; \
} \
std::cerr << "\n"; \
}
#define SHOWVECTOR2(v) \
{ \
std::cerr << #v << "\t:\n"; \
for (const auto &xxx : v) { \
for (const auto &yyy : xxx) { \
std::cerr << yyy << " "; \
} \
std::cerr << "\n"; \
} \
}
#define SHOWQUEUE(a) \
{ \
auto tmp(a); \
std::cerr << #a << "\t: "; \
while (!tmp.empty()) { \
std::cerr << tmp.front() << " "; \
tmp.pop(); \
} \
std::cerr << "\n"; \
}
#define SHOWSTACK(a) \
{ \
auto tmp(a); \
std::cerr << #a << "\t: "; \
while (!tmp.empty()) { \
std::cerr << tmp.top() << " "; \
tmp.pop(); \
} \
std::cerr << "\n"; \
}
// math
const int mod = 1e9 + 7;
ll powmod(ll a, ll b) {
ll res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int pct(int x) { return __builtin_popcount(x); }
int bit(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
int cdiv(int a, int b) {
return a / b + !(a < 0 || a % b == 0);
} // division of a by b rounded up, assumes b > 0
void DBG() { cerr << "]" << endl; }
template <class H, class... T> void DBG(H h, T... t) {
cerr << to_string(h);
if (sizeof...(t))
cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#define HERE cout << "=====GOTHERE=====" << endl
#else
#define dbg(...) 42
#define HERE 42
#endif
/* ============== END OF HEADER ============== */
const int N = 500005;
int n, _, q, k;
int m;
int c[N];
int maxn;
int sum = 0;
int ans[N];
int cnt[N];
struct query {
int l, r, id;
bool operator<(const query &x) const {
if (l / maxn != x.l / maxn)
return l < x.l;
// 奇偶排序,为了pointer防止过度震荡
// 奇数index r从小到大,偶数从大到小
return r < x.r;
}
} a[N];
inline void add(int i) {
if (cnt[i] == 0) {
sum += 1;
}
cnt[i]++;
}
inline void del(int i) {
if (cnt[i] == 1) {
sum -= 1;
}
cnt[i]--;
}
void solve() {
cin >> n >> m;
FF(i, 1, n) { cin >> c[i]; }
maxn = sqrt(n);
F(i, 0, m) {
cin >> a[i].l >> a[i].r;
a[i].id = i;
}
sort(a, a + m);
for (int i = 0, l = 1, r = 0; i < m; i++) {
// 这四个while顺序不能乱
// while (l > a[i].l) add(c[--l]);
// while (r < a[i].r) add(c[++r]);
// while (l < a[i].l) del(c[l++]);
// while (r > a[i].r) del(c[r--]);
while (r > a[i].r)
del(c[r--]);
while (r < a[i].r)
add(c[++r]);
while (l > a[i].l)
add(c[--l]);
while (l < a[i].l)
del(c[l++]);
ans[a[i].id] = sum;
}
for (int i = 0; i < m; i++) {
cout << ans[i] << endl;
}
}
signed main() {
// clock_t t1 = clock();
IOS solve();
// cout << "THE ALGORITHM TOOK: " << (clock() - t1) * 1.0 / CLOCKS_PER_SEC *
// 1000 << "ms"<< endl;
return 0;
} | #include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
// #define double long double
// #define int long long
// #define int unsigned long long
#define ll long long
#define ld long double
#define pi pair<int, int>
#define pl pair<ll, ll>
#define pd pair<ld, ld>
#define str string
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define vd vector<ld>
#define vs vector<str>
#define vpi vector<pi>
#define vpl vector<pl>
#define vpd vector<pd>
#define si set<int>
#define FF(i, j, n) for (signed i = j; i <= n; i++)
#define DD(i, j, n) for (signed i = j; i >= n; i--)
#define F(i, j, n) for (signed i = j; i < n; i++)
#define D(i, j, n) for (signed i = j; i > n; i--)
#define mem(a, b) memset(a, b, sizeof(a))
#define ndl cout << endl
#define ull unsigned long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
#define sz(x) (signed)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define X first
#define Y second
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define SHOWVECTOR(v) \
{ \
std::cerr << #v << "\t: "; \
for (const auto &xxx : v) { \
std::cerr << xxx << " "; \
} \
std::cerr << "\n"; \
}
#define SHOWVECTOR2(v) \
{ \
std::cerr << #v << "\t:\n"; \
for (const auto &xxx : v) { \
for (const auto &yyy : xxx) { \
std::cerr << yyy << " "; \
} \
std::cerr << "\n"; \
} \
}
#define SHOWQUEUE(a) \
{ \
auto tmp(a); \
std::cerr << #a << "\t: "; \
while (!tmp.empty()) { \
std::cerr << tmp.front() << " "; \
tmp.pop(); \
} \
std::cerr << "\n"; \
}
#define SHOWSTACK(a) \
{ \
auto tmp(a); \
std::cerr << #a << "\t: "; \
while (!tmp.empty()) { \
std::cerr << tmp.top() << " "; \
tmp.pop(); \
} \
std::cerr << "\n"; \
}
// math
const int mod = 1e9 + 7;
ll powmod(ll a, ll b) {
ll res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int pct(int x) { return __builtin_popcount(x); }
int bit(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
int cdiv(int a, int b) {
return a / b + !(a < 0 || a % b == 0);
} // division of a by b rounded up, assumes b > 0
void DBG() { cerr << "]" << endl; }
template <class H, class... T> void DBG(H h, T... t) {
cerr << to_string(h);
if (sizeof...(t))
cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#define HERE cout << "=====GOTHERE=====" << endl
#else
#define dbg(...) 42
#define HERE 42
#endif
/* ============== END OF HEADER ============== */
const int N = 500005;
int n, _, q, k;
int m;
int c[N];
int maxn;
int sum = 0;
int ans[N];
int cnt[N];
struct query {
int l, r, id;
bool operator<(const query &x) const {
if (l / maxn != x.l / maxn)
return l < x.l;
// 奇偶排序,为了pointer防止过度震荡
// 奇数index r从小到大,偶数从大到小
return (l / maxn) & 1 ? r < x.r : r > x.r;
}
} a[N];
inline void add(int i) {
if (cnt[i] == 0) {
sum += 1;
}
cnt[i]++;
}
inline void del(int i) {
if (cnt[i] == 1) {
sum -= 1;
}
cnt[i]--;
}
void solve() {
cin >> n >> m;
FF(i, 1, n) { cin >> c[i]; }
maxn = sqrt(n);
F(i, 0, m) {
cin >> a[i].l >> a[i].r;
a[i].id = i;
}
sort(a, a + m);
for (int i = 0, l = 1, r = 0; i < m; i++) {
// 这四个while顺序不能乱
// while (l > a[i].l) add(c[--l]);
// while (r < a[i].r) add(c[++r]);
// while (l < a[i].l) del(c[l++]);
// while (r > a[i].r) del(c[r--]);
while (r > a[i].r)
del(c[r--]);
while (r < a[i].r)
add(c[++r]);
while (l > a[i].l)
add(c[--l]);
while (l < a[i].l)
del(c[l++]);
ans[a[i].id] = sum;
}
for (int i = 0; i < m; i++) {
cout << ans[i] << endl;
}
}
signed main() {
// clock_t t1 = clock();
IOS solve();
// cout << "THE ALGORITHM TOOK: " << (clock() - t1) * 1.0 / CLOCKS_PER_SEC *
// 1000 << "ms"<< endl;
return 0;
} | replace | 164 | 165 | 164 | 165 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
for (int i = 0; i < sz(v); i++) {
os << v[i];
if (i < sz(v) - 1)
os << " ";
}
return os;
}
template <typename T> struct SegmentTree {
int n;
vector<T> node;
T calc(T a, T b) {
return a + b;
// return min(a, b);
// return max(a, b);
}
T e = 0;
SegmentTree(vector<T> v) {
int sz = v.size();
n = 1;
while (n < sz) {
n *= 2;
}
node.resize(2 * n - 1, e);
for (int i = 0; i < sz; i++) {
node[i + n - 1] = v[i];
}
for (int i = n - 2; i >= 0; i--) {
node[i] = calc(node[2 * i + 1], node[2 * i + 2]);
}
}
SegmentTree(int sz) {
n = 1;
while (n < sz) {
n *= 2;
}
node.resize(2 * n - 1, e);
}
void update(int x, T val) {
x = n + x - 1;
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = calc(node[2 * x + 1], node[2 * x + 2]);
}
}
void add(int x, T val) {
x = n + x - 1;
node[x] += val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = calc(node[2 * x + 1], node[2 * x + 2]);
}
}
T query(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = n;
if (r <= a || b <= l)
return e;
if (a <= l && r <= b)
return node[k];
T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
return calc(vl, vr);
}
T at(int x) { return node[n + x - 1]; }
};
int main() {
int n, q;
cin >> n >> q;
vector<int> c(n);
rep(i, n) cin >> c[i], c[i]--;
using P = pair<int, int>;
map<P, int> ans;
vector<P> query(q);
vector<vector<int>> ls(n);
rep(i, q) {
int l, r;
cin >> l >> r;
l--, r--;
query[i] = P(l, r);
ls[r].push_back(l);
}
vector<int> good(n, -1);
SegmentTree<int> segt(n);
rep(i, n) {
int now = c[i];
if (good[now] != -1) {
segt.update(good[now], 0);
}
good[now] = i;
segt.update(i, 1);
int r = i;
for (auto l : ls[r]) {
ans[P(l, r)] = segt.query(l, r + 1);
}
}
rep(i, q) cout << ans[query[i]] << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
for (int i = 0; i < sz(v); i++) {
os << v[i];
if (i < sz(v) - 1)
os << " ";
}
return os;
}
template <typename T> struct SegmentTree {
int n;
vector<T> node;
T calc(T a, T b) {
return a + b;
// return min(a, b);
// return max(a, b);
}
T e = 0;
SegmentTree(vector<T> v) {
int sz = v.size();
n = 1;
while (n < sz) {
n *= 2;
}
node.resize(2 * n - 1, e);
for (int i = 0; i < sz; i++) {
node[i + n - 1] = v[i];
}
for (int i = n - 2; i >= 0; i--) {
node[i] = calc(node[2 * i + 1], node[2 * i + 2]);
}
}
SegmentTree(int sz) {
n = 1;
while (n < sz) {
n *= 2;
}
node.resize(2 * n - 1, e);
}
void update(int x, T val) {
x = n + x - 1;
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = calc(node[2 * x + 1], node[2 * x + 2]);
}
}
void add(int x, T val) {
x = n + x - 1;
node[x] += val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = calc(node[2 * x + 1], node[2 * x + 2]);
}
}
T query(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = n;
if (r <= a || b <= l)
return e;
if (a <= l && r <= b)
return node[k];
T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
return calc(vl, vr);
}
T at(int x) { return node[n + x - 1]; }
};
int main() {
int n, q;
cin >> n >> q;
vector<int> c(n);
rep(i, n) cin >> c[i], c[i]--;
using P = pair<int, int>;
map<P, int> ans;
vector<P> query(q);
vector<vector<int>> ls(n);
rep(i, q) {
int l, r;
cin >> l >> r;
l--, r--;
query[i] = P(l, r);
ls[r].push_back(l);
}
vector<int> good(n, -1);
SegmentTree<int> segt(n);
rep(i, n) {
int now = c[i];
if (good[now] != -1) {
segt.update(good[now], 0);
}
good[now] = i;
segt.update(i, 1);
int r = i;
for (auto l : ls[r]) {
ans[P(l, r)] = segt.query(l, r + 1);
}
}
rep(i, q) cout << ans[query[i]] << '\n';
} | replace | 129 | 130 | 129 | 130 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, const T &b) {
if (a < b)
a = b;
}
const int B = 500;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
vector<int> C(N);
rep(i, N) cin >> C[i];
vector<int> L(Q), R(Q);
rep(i, Q) cin >> L[i] >> R[i];
rep(i, Q) L[i]--;
vector<int> ord(Q);
rep(i, Q) ord[i] = i;
sort(ord.begin(), ord.end(), [&](int a, int b) {
if ((L[a] / B) != (L[b] / B))
return L[a] < L[b];
else
return R[a] < R[b];
});
vector<int> exi(N, 0);
int pos = 0;
int lef = 0, rig = 0;
vector<int> cnt(N + 1, 0);
int sum = 0;
auto change = [&](int idx) {
if (exi[idx]) {
exi[idx] = 0;
cnt[C[idx]]--;
if (cnt[C[idx]] == 0)
sum--;
} else {
exi[idx] = 1;
cnt[C[idx]]++;
if (cnt[C[idx]] == 1)
sum++;
}
};
auto go = [&]() {
if (pos == ord.size())
exit(0);
int id = ord[pos];
while (lef < L[id])
change(lef++);
while (rig < R[id])
change(rig++);
while (lef > L[id])
change(--lef);
while (rig > R[id])
change(--rig);
return ord[pos++];
};
vector<int> ans(Q, 0);
for (int i = 0; i < Q; i++) {
int res = go();
ans[res] = sum;
}
rep(i, Q) cout << ans[i] << "\n";
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, const T &b) {
if (a < b)
a = b;
}
const int B = 1500;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
vector<int> C(N);
rep(i, N) cin >> C[i];
vector<int> L(Q), R(Q);
rep(i, Q) cin >> L[i] >> R[i];
rep(i, Q) L[i]--;
vector<int> ord(Q);
rep(i, Q) ord[i] = i;
sort(ord.begin(), ord.end(), [&](int a, int b) {
if ((L[a] / B) != (L[b] / B))
return L[a] < L[b];
else
return R[a] < R[b];
});
vector<int> exi(N, 0);
int pos = 0;
int lef = 0, rig = 0;
vector<int> cnt(N + 1, 0);
int sum = 0;
auto change = [&](int idx) {
if (exi[idx]) {
exi[idx] = 0;
cnt[C[idx]]--;
if (cnt[C[idx]] == 0)
sum--;
} else {
exi[idx] = 1;
cnt[C[idx]]++;
if (cnt[C[idx]] == 1)
sum++;
}
};
auto go = [&]() {
if (pos == ord.size())
exit(0);
int id = ord[pos];
while (lef < L[id])
change(lef++);
while (rig < R[id])
change(rig++);
while (lef > L[id])
change(--lef);
while (rig > R[id])
change(--rig);
return ord[pos++];
};
vector<int> ans(Q, 0);
for (int i = 0; i < Q; i++) {
int res = go();
ans[res] = sum;
}
rep(i, Q) cout << ans[i] << "\n";
return 0;
}
| replace | 26 | 27 | 26 | 27 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int n, q, x, y, arr[MAXN], ans[MAXN], t[MAXN], last[MAXN];
struct Q {
int x, y, i;
};
void update(int i, int v) {
for (; i <= n; i += i & -i)
t[i] += v;
}
int query(int r) {
int sum = 0;
for (; r > 0; r -= r & -r)
sum += t[r];
return sum;
}
bool cmp(Q q1, Q q2) { return q1.y < q2.y; }
int main() {
memset(last, -1, sizeof last);
scanf("%d %d", &n, &q);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
Q qs[q];
for (int i = 0; i < q; i++) {
scanf("%d %d", &x, &y);
x--, y--;
qs[i] = {x, y, i};
}
sort(qs, qs + q, cmp);
int qn = 0;
for (int i = 0; i < n; i++) {
if (last[arr[i]] != -1)
update(last[arr[i]] + 1, -1);
last[arr[i]] = i;
update(i + 1, 1);
while (qn < q && qs[qn].y == i) {
ans[qs[qn].i] = query(qs[qn].y + 1) - query(qs[qn].x);
qn++;
}
}
for (int i = 0; i < q; i++)
printf("%d\n", ans[i]);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int MAXN = 5 * 1e5 + 5;
int n, q, x, y, arr[MAXN], ans[MAXN], t[MAXN], last[MAXN];
struct Q {
int x, y, i;
};
void update(int i, int v) {
for (; i <= n; i += i & -i)
t[i] += v;
}
int query(int r) {
int sum = 0;
for (; r > 0; r -= r & -r)
sum += t[r];
return sum;
}
bool cmp(Q q1, Q q2) { return q1.y < q2.y; }
int main() {
memset(last, -1, sizeof last);
scanf("%d %d", &n, &q);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
Q qs[q];
for (int i = 0; i < q; i++) {
scanf("%d %d", &x, &y);
x--, y--;
qs[i] = {x, y, i};
}
sort(qs, qs + q, cmp);
int qn = 0;
for (int i = 0; i < n; i++) {
if (last[arr[i]] != -1)
update(last[arr[i]] + 1, -1);
last[arr[i]] = i;
update(i + 1, 1);
while (qn < q && qs[qn].y == i) {
ans[qs[qn].i] = query(qs[qn].y + 1) - query(qs[qn].x);
qn++;
}
}
for (int i = 0; i < q; i++)
printf("%d\n", ans[i]);
return 0;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
#define pause cout << " press ansy key to continue...", cin >> chh
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)
#define lowbit(x) ((x) & (-x))
#define repit(i, c) \
for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repe(i, u) for (int i = head[u]; i != -1; i = nxt[i])
#define repd(i, n) for (int i = (n - 1); i >= 0; i--)
#define FOR(i, n, m) for (int i = (n); i <= (m); i++)
#define FORD(i, n, m) for (int i = (n); i >= (m); i--)
#define pb push_back
#define X first
#define Y second
#define ins insert
#define rb rbegin
#define be begin
#define er erase
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define SZ(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
#define sqr(r) ((LL)(r) * (r))
#define dis(x1, y1, x2, y2) \
(((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)))
#define FASTIO \
ios::sync_with_stdio(false); \
cin.tie(0)
#define sc(x) cout << #x " = " << x << endl, system("pause")
#define sc2(x, y) \
cout << #x " = " << x << " " << #y " = " << y << endl, system("pause")
#define sc3(x, y, z) \
cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z \
<< endl, \
system("pause")
#define sc4(x, y, z, w) \
cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z << " " \
<< #w " = " << w << endl, \
system("pause")
#define in(n) scanf("%d", &n)
#define in2(n, m) scanf("%d %d", &n, &m)
#define in3(x, y, z) scanf("%d %d %d", &x, &y, &z)
using namespace std;
int chh;
typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<int, pii> pi3;
typedef vector<pair<int, int>> vpii;
typedef long long LL;
const int N = 500005;
int n, m, e;
int a[N], c[N], pre[N];
int T[N], L[N * 20], R[N * 20], val[N * 20];
struct SegTree {
int e;
void init() {
e = 0;
L[0] = 0, R[0] = 0, val[0] = 0;
}
/**
* 单点更新
*/
void update(int &cur, int k, int s, int t, int x, int w) {
if (x < s || x > t)
return;
cur = ++e;
L[e] = L[k], R[e] = R[k], val[e] = val[k] + w;
if (s == t)
return;
int mid = (s + t) >> 1;
update(L[e], L[k], s, mid, x, w);
update(R[e], R[k], mid + 1, t, x, w);
}
/**
* 区间查询
*/
int query(int S, int E, int s, int t, int l, int r) {
if (t < l || s > r)
return 0;
if (s >= l && t <= r)
return val[E] - val[S];
int mid = (s + t) >> 1;
return query(L[S], L[E], s, mid, l, r) +
query(R[S], R[E], mid + 1, t, l, r);
}
} seg;
int main() {
int l, r, tmp;
while (~in2(n, m)) {
e = 0;
FOR(i, 1, n) in(a[i]), c[e++] = a[i];
sort(c, c + e);
e = unique(c, c + e) - c;
FOR(i, 1, n) a[i] = lower_bound(c, c + e, a[i]) - c + 1;
seg.init();
memset(pre, -1, sizeof(pre));
FOR(i, 1, n) {
if (pre[a[i]] == -1) {
seg.update(T[i], T[i - 1], 1, n, i, 1);
} else {
seg.update(tmp, T[i - 1], 1, n, pre[a[i]], -1);
seg.update(T[i], tmp, 1, n, i, 1);
}
pre[a[i]] = i;
}
while (m--) {
in2(l, r);
printf("%d\n", seg.query(T[l - 1], T[r], 1, n, l, r));
}
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
#define pause cout << " press ansy key to continue...", cin >> chh
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)
#define lowbit(x) ((x) & (-x))
#define repit(i, c) \
for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repe(i, u) for (int i = head[u]; i != -1; i = nxt[i])
#define repd(i, n) for (int i = (n - 1); i >= 0; i--)
#define FOR(i, n, m) for (int i = (n); i <= (m); i++)
#define FORD(i, n, m) for (int i = (n); i >= (m); i--)
#define pb push_back
#define X first
#define Y second
#define ins insert
#define rb rbegin
#define be begin
#define er erase
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define SZ(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
#define sqr(r) ((LL)(r) * (r))
#define dis(x1, y1, x2, y2) \
(((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)))
#define FASTIO \
ios::sync_with_stdio(false); \
cin.tie(0)
#define sc(x) cout << #x " = " << x << endl, system("pause")
#define sc2(x, y) \
cout << #x " = " << x << " " << #y " = " << y << endl, system("pause")
#define sc3(x, y, z) \
cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z \
<< endl, \
system("pause")
#define sc4(x, y, z, w) \
cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z << " " \
<< #w " = " << w << endl, \
system("pause")
#define in(n) scanf("%d", &n)
#define in2(n, m) scanf("%d %d", &n, &m)
#define in3(x, y, z) scanf("%d %d %d", &x, &y, &z)
using namespace std;
int chh;
typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<int, pii> pi3;
typedef vector<pair<int, int>> vpii;
typedef long long LL;
const int N = 500005;
int n, m, e;
int a[N], c[N], pre[N];
int T[N], L[N * 40], R[N * 40], val[N * 40];
struct SegTree {
int e;
void init() {
e = 0;
L[0] = 0, R[0] = 0, val[0] = 0;
}
/**
* 单点更新
*/
void update(int &cur, int k, int s, int t, int x, int w) {
if (x < s || x > t)
return;
cur = ++e;
L[e] = L[k], R[e] = R[k], val[e] = val[k] + w;
if (s == t)
return;
int mid = (s + t) >> 1;
update(L[e], L[k], s, mid, x, w);
update(R[e], R[k], mid + 1, t, x, w);
}
/**
* 区间查询
*/
int query(int S, int E, int s, int t, int l, int r) {
if (t < l || s > r)
return 0;
if (s >= l && t <= r)
return val[E] - val[S];
int mid = (s + t) >> 1;
return query(L[S], L[E], s, mid, l, r) +
query(R[S], R[E], mid + 1, t, l, r);
}
} seg;
int main() {
int l, r, tmp;
while (~in2(n, m)) {
e = 0;
FOR(i, 1, n) in(a[i]), c[e++] = a[i];
sort(c, c + e);
e = unique(c, c + e) - c;
FOR(i, 1, n) a[i] = lower_bound(c, c + e, a[i]) - c + 1;
seg.init();
memset(pre, -1, sizeof(pre));
FOR(i, 1, n) {
if (pre[a[i]] == -1) {
seg.update(T[i], T[i - 1], 1, n, i, 1);
} else {
seg.update(tmp, T[i - 1], 1, n, pre[a[i]], -1);
seg.update(T[i], tmp, 1, n, i, 1);
}
pre[a[i]] = i;
}
while (m--) {
in2(l, r);
printf("%d\n", seg.query(T[l - 1], T[r], 1, n, l, r));
}
}
return 0;
} | replace | 74 | 75 | 74 | 75 | -11 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#define lli long long int
#define mod 1000000007
#define pb push_back
#define mk make_pair
#define fastio \
ios_base::sync_with_stdio(false); \
cout.tie(NULL);
using namespace std;
vector<lli> segtree[800001];
lli val[200001], b[200001];
map<lli, lli> m;
void make_segTree(lli index, lli start, lli end) {
if (start == end) {
segtree[index].pb(b[start]);
return;
}
lli mid = (start + end) >> 1;
make_segTree(index << 1, start, mid);
make_segTree(index << 1 | 1, mid + 1, end);
merge(segtree[index << 1].begin(), segtree[index << 1].end(),
segtree[index << 1 | 1].begin(), segtree[index << 1 | 1].end(),
back_inserter(segtree[index]));
}
lli query(lli index, lli start, lli end, lli l, lli r, lli x) {
if (l == start && r == end) {
lli last_index =
upper_bound(segtree[index].begin(), segtree[index].end(), x) -
segtree[index].begin();
return segtree[index].size() - last_index;
}
lli mid = (start + end) >> 1;
if (r <= mid)
return query(index << 1, start, mid, l, r, x);
else if (l >= mid + 1)
return query(index << 1 | 1, mid + 1, end, l, r, x);
else
return query(index << 1, start, mid, l, mid, x) +
query(index << 1 | 1, mid + 1, end, mid + 1, r, x);
}
int main() {
lli n, q, l, r, i;
scanf("%lld %lld", &n, &q);
for (i = 0; i < n; i++)
scanf("%lld", &val[i]);
for (i = n - 1; i >= 0; i--) {
if (m.find(val[i]) == m.end())
b[i] = mod;
else
b[i] = m[val[i]];
m[val[i]] = i;
}
make_segTree(1, 0, n - 1);
while (q--) {
scanf("%lld %lld", &l, &r);
printf("%lld\n", query(1, 0, n - 1, l - 1, r - 1, r - 1));
}
return 0;
}
| #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#define lli long long int
#define mod 1000000007
#define pb push_back
#define mk make_pair
#define fastio \
ios_base::sync_with_stdio(false); \
cout.tie(NULL);
using namespace std;
vector<lli> segtree[2000001];
lli val[500001], b[500001];
map<lli, lli> m;
void make_segTree(lli index, lli start, lli end) {
if (start == end) {
segtree[index].pb(b[start]);
return;
}
lli mid = (start + end) >> 1;
make_segTree(index << 1, start, mid);
make_segTree(index << 1 | 1, mid + 1, end);
merge(segtree[index << 1].begin(), segtree[index << 1].end(),
segtree[index << 1 | 1].begin(), segtree[index << 1 | 1].end(),
back_inserter(segtree[index]));
}
lli query(lli index, lli start, lli end, lli l, lli r, lli x) {
if (l == start && r == end) {
lli last_index =
upper_bound(segtree[index].begin(), segtree[index].end(), x) -
segtree[index].begin();
return segtree[index].size() - last_index;
}
lli mid = (start + end) >> 1;
if (r <= mid)
return query(index << 1, start, mid, l, r, x);
else if (l >= mid + 1)
return query(index << 1 | 1, mid + 1, end, l, r, x);
else
return query(index << 1, start, mid, l, mid, x) +
query(index << 1 | 1, mid + 1, end, mid + 1, r, x);
}
int main() {
lli n, q, l, r, i;
scanf("%lld %lld", &n, &q);
for (i = 0; i < n; i++)
scanf("%lld", &val[i]);
for (i = n - 1; i >= 0; i--) {
if (m.find(val[i]) == m.end())
b[i] = mod;
else
b[i] = m[val[i]];
m[val[i]] = i;
}
make_segTree(1, 0, n - 1);
while (q--) {
scanf("%lld %lld", &l, &r);
printf("%lld\n", query(1, 0, n - 1, l - 1, r - 1, r - 1));
}
return 0;
}
| replace | 12 | 14 | 12 | 14 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var) \
do { \
cout << #var << " : "; \
view(var); \
} while (0)
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
for (const auto &e : v) {
cout << e << " ";
}
cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;
struct Mo {
vector<int> left, right, order;
vector<bool> v;
int width;
int nl, nr, ptr;
Mo(int n) : width((int)sqrt(n)), nl(0), nr(0), ptr(0), v(n, false) {}
// [l,r)
void insert(int l, int r) {
left.push_back(l);
right.push_back(r);
}
void build() {
order.resize(left.size());
iota(begin(order), end(order), 0);
sort(begin(order), end(order), [&](int a, int b) {
if (left[a] / width != left[b] / width)
return left[a] < left[b];
else
return right[a] < right[b];
});
}
int process() {
if (ptr == order.size())
return -1;
const auto id = order[ptr];
while (nl > left[id])
distribute(--nl);
while (nr < right[id])
distribute(nr++);
while (nl < left[id])
distribute(nl++);
while (nr > right[id])
distribute(--nr);
return order[ptr++];
}
inline void distribute(int idx) {
v[idx].flip();
if (v[idx])
add(idx);
else
del(idx);
}
void add(int idx);
void del(int idx);
};
int n, q;
int a[500000];
int ans[500000];
int cnt[500001], sum;
void Mo::add(int idx) {
if (cnt[a[idx]]++ == 0)
sum++;
}
void Mo::del(int idx) {
if (--cnt[a[idx]] == 0)
sum--;
}
int main() {
scanf("%d %d", &n, &q);
rep(i, n) scanf("%d", &a[i]);
Mo mo(n);
rep(i, q) {
int x, y;
scanf("%d %d", &x, &y);
mo.insert(--x, y);
}
mo.build();
rep(i, q) {
int idx = mo.process();
ans[idx] = sum;
}
rep(i, q) printf("%d\n", ans[i]);
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var) \
do { \
cout << #var << " : "; \
view(var); \
} while (0)
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
for (const auto &e : v) {
cout << e << " ";
}
cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;
struct Mo {
vector<int> left, right, order;
vector<bool> v;
int width;
int nl, nr, ptr;
Mo(int n) : width((int)sqrt(n)), nl(0), nr(0), ptr(0), v(n, false) {}
// [l,r)
void insert(int l, int r) {
left.push_back(l);
right.push_back(r);
}
void build() {
order.resize(left.size());
iota(begin(order), end(order), 0);
sort(begin(order), end(order), [&](int a, int b) {
if (left[a] / width != left[b] / width)
return left[a] < left[b];
return bool((right[a] < right[b]) ^ (left[a] / width % 2));
});
}
int process() {
if (ptr == order.size())
return -1;
const auto id = order[ptr];
while (nl > left[id])
distribute(--nl);
while (nr < right[id])
distribute(nr++);
while (nl < left[id])
distribute(nl++);
while (nr > right[id])
distribute(--nr);
return order[ptr++];
}
inline void distribute(int idx) {
v[idx].flip();
if (v[idx])
add(idx);
else
del(idx);
}
void add(int idx);
void del(int idx);
};
int n, q;
int a[500000];
int ans[500000];
int cnt[500001], sum;
void Mo::add(int idx) {
if (cnt[a[idx]]++ == 0)
sum++;
}
void Mo::del(int idx) {
if (--cnt[a[idx]] == 0)
sum--;
}
int main() {
scanf("%d %d", &n, &q);
rep(i, n) scanf("%d", &a[i]);
Mo mo(n);
rep(i, q) {
int x, y;
scanf("%d %d", &x, &y);
mo.insert(--x, y);
}
mo.build();
rep(i, q) {
int idx = mo.process();
ans[idx] = sum;
}
rep(i, q) printf("%d\n", ans[i]);
} | replace | 67 | 69 | 67 | 68 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
// typedef vector<vector<ll>> Graph;
const ll mod = 1e9 + 7;
// const ll mod = 998244353;
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
os << "(" << v.first << ", " << v.second << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
for (int i = 0; i < (int)v.size(); i++) {
if (i > 0) {
os << " ";
}
os << v[i];
}
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
for (int i = 0; i < (int)v.size(); i++) {
if (i > 0) {
os << endl;
}
os << v[i];
}
return os;
}
template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
cerr << v[i][0];
for (ll j = 1; j < w; j++)
cerr spa v[i][j];
cerr << endl;
}
};
template <typename T> void debug(vector<T> &v, ll n) {
if (n != 0)
cerr << v[0];
for (ll i = 1; i < n; i++)
cerr spa v[i];
cerr << endl;
};
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
// n: 要素数,F: モノイド間の演算,M1: 演算の単位元
// F は無名関数で渡す
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
// k番目の要素にxを代入
void set(int k, const Monoid &x) { seg[k + sz] = x; }
// 構築
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
// k番目の値をxに変更
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
// 区間[a, b)に関して演算して結果を返す
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
// k番目の値を返す(set[k]でアクセス)
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C>
//[a, x)がcheckを満たす最初の要素位置xを返す
// ↑嘘かも.[a, x]が正しい
// 存在しない場合は-1を返すことに注意
// Cには判定関数を入れる
int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
//[x, b)がcheckを満たす最後の要素位置xを返す
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, Q;
cin >> N >> Q;
vector<ll> c(N);
REP(i, N) cin >> c[i];
REP(i, N) c[i]--;
vector<ll> l(Q), r(Q);
REP(i, Q) cin >> l[i] >> r[i];
REP(i, Q) l[i]--, r[i]--;
vl res(N, -1);
vvl pos(N);
REP(i, Q) { pos[r[i]].push_back(i); }
SegmentTree<ll> seg(
N + 1, [](ll a, ll b) { return a + b; }, 0);
vector<ll> memo(N, -1);
REP(i, N) {
if (memo[c[i]] != -1) {
seg.update(memo[c[i]], 0);
}
memo[c[i]] = i;
seg.update(i, 1);
for (auto idx : pos[i]) {
ll tmp = seg.query(l[idx], r[idx] + 1);
res[idx] = tmp;
}
}
REP(i, Q) cout << res[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
// typedef vector<vector<ll>> Graph;
const ll mod = 1e9 + 7;
// const ll mod = 998244353;
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
os << "(" << v.first << ", " << v.second << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
for (int i = 0; i < (int)v.size(); i++) {
if (i > 0) {
os << " ";
}
os << v[i];
}
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
for (int i = 0; i < (int)v.size(); i++) {
if (i > 0) {
os << endl;
}
os << v[i];
}
return os;
}
template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
cerr << v[i][0];
for (ll j = 1; j < w; j++)
cerr spa v[i][j];
cerr << endl;
}
};
template <typename T> void debug(vector<T> &v, ll n) {
if (n != 0)
cerr << v[0];
for (ll i = 1; i < n; i++)
cerr spa v[i];
cerr << endl;
};
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
// n: 要素数,F: モノイド間の演算,M1: 演算の単位元
// F は無名関数で渡す
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
// k番目の要素にxを代入
void set(int k, const Monoid &x) { seg[k + sz] = x; }
// 構築
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
// k番目の値をxに変更
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
// 区間[a, b)に関して演算して結果を返す
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
// k番目の値を返す(set[k]でアクセス)
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C>
//[a, x)がcheckを満たす最初の要素位置xを返す
// ↑嘘かも.[a, x]が正しい
// 存在しない場合は-1を返すことに注意
// Cには判定関数を入れる
int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
//[x, b)がcheckを満たす最後の要素位置xを返す
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, Q;
cin >> N >> Q;
vector<ll> c(N);
REP(i, N) cin >> c[i];
REP(i, N) c[i]--;
vector<ll> l(Q), r(Q);
REP(i, Q) cin >> l[i] >> r[i];
REP(i, Q) l[i]--, r[i]--;
vl res(Q, -1);
vvl pos(N);
REP(i, Q) { pos[r[i]].push_back(i); }
SegmentTree<ll> seg(
N + 1, [](ll a, ll b) { return a + b; }, 0);
vector<ll> memo(N, -1);
REP(i, N) {
if (memo[c[i]] != -1) {
seg.update(memo[c[i]], 0);
}
memo[c[i]] = i;
seg.update(i, 1);
for (auto idx : pos[i]) {
ll tmp = seg.query(l[idx], r[idx] + 1);
res[idx] = tmp;
}
}
REP(i, Q) cout << res[i] << endl;
return 0;
} | replace | 198 | 199 | 198 | 199 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a, n, t) \
for (int ii = 0; ii < n; ii++) \
scanf("%" #t "", &(a)[ii])
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rreps(i, a, n) for (int i = n - 1; i >= a; i--)
#define validl(i, a, b) ((i >= a) && (i <= b)) ? 1 : 0
#define validmat(x, y, a, b, c, d) (validl(x, a, b) && validl(y, a, b)) ? 1 : 0
#define validmn(x, y, m, n) validmat(x, y, 0, m, 0, n)
#define valid(i, n) validl(i, 0, n)
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define mk make_pair
#define pb push_back
#define pa(a, n, t) \
for (int i = 0; i < n; i++) \
printf("%" #t "", a[i])
#define p2 pair<int, int>
#define p3 pair<p2, int>
#define fi first
#define se second
#define sd(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pl(a) printf("%lld", a)
#define pd(a) printf("%d", a)
#define sf(a) scanf("%lf", &a)
#define pf(a) printf("%lf", a)
#define nline printf("\n")
#define ss(a) scanf("%s", a)
#define ps(a) printf("%s", a)
#define sc(a) scanf("%c", &a)
#define pc(a) printf("%c", &a)
#define mf(m, f) m.find(f) != m.end()
#define pp3(m) cout << m.fi.fi << " " << m.fi.se << " " << m.se << " "
#define pp2(m) cout << m.fi << " " << m.se << " "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
rep(j,adj[i].size()){
if(p!=adj[i][j])
dfs(adj[i][j],i);
}
}*/
int rootn;
bool compare(p3 a, p3 b) {
if ((a.fi.fi / rootn) != (b.fi.fi / rootn))
return (a.fi.fi / rootn) < (b.fi.fi / rootn);
return a.fi.se < b.fi.se;
}
int brr[500001] = {0};
int dif = 0;
void add(int x) {
if (brr[x] == 0)
dif++;
brr[x]++;
}
void rem(int x) {
if (brr[x] == 1)
dif--;
brr[x]--;
}
int arr[500001];
int main() {
int n;
string s;
cin >> n;
rootn = static_cast<int>(sqrt(n));
rootn = min(rootn, 500);
// cout<<rootn<<endl;
int k;
cin >> k;
sa(arr, n, d);
int m = 0;
v(p3) vv;
rep(i, k) {
int a, b;
cin >> a >> b;
a--, b--;
vv.pb(mk(mk(a, b), i));
}
sort(vv.begin(), vv.end(), compare);
int ll = 0, rr = -1;
v(int) ans(k);
rep(i, k) {
// cout<<vv[i].fi.fi<<" "<<vv[i].fi.se<<endl;
int l = vv[i].fi.fi;
int r = vv[i].fi.se;
while (rr < r) {
rr++;
add(arr[rr]);
}
while (r < rr) {
rem(arr[rr]);
rr--;
}
while (ll < l) {
rem(arr[ll]);
ll++;
}
while (l < ll) {
ll--;
add(arr[ll]);
}
ans[vv[i].se] = dif;
}
rep(i, k) cout << ans[i] << "\n";
// cout<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a, n, t) \
for (int ii = 0; ii < n; ii++) \
scanf("%" #t "", &(a)[ii])
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rreps(i, a, n) for (int i = n - 1; i >= a; i--)
#define validl(i, a, b) ((i >= a) && (i <= b)) ? 1 : 0
#define validmat(x, y, a, b, c, d) (validl(x, a, b) && validl(y, a, b)) ? 1 : 0
#define validmn(x, y, m, n) validmat(x, y, 0, m, 0, n)
#define valid(i, n) validl(i, 0, n)
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define mk make_pair
#define pb push_back
#define pa(a, n, t) \
for (int i = 0; i < n; i++) \
printf("%" #t "", a[i])
#define p2 pair<int, int>
#define p3 pair<p2, int>
#define fi first
#define se second
#define sd(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pl(a) printf("%lld", a)
#define pd(a) printf("%d", a)
#define sf(a) scanf("%lf", &a)
#define pf(a) printf("%lf", a)
#define nline printf("\n")
#define ss(a) scanf("%s", a)
#define ps(a) printf("%s", a)
#define sc(a) scanf("%c", &a)
#define pc(a) printf("%c", &a)
#define mf(m, f) m.find(f) != m.end()
#define pp3(m) cout << m.fi.fi << " " << m.fi.se << " " << m.se << " "
#define pp2(m) cout << m.fi << " " << m.se << " "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
rep(j,adj[i].size()){
if(p!=adj[i][j])
dfs(adj[i][j],i);
}
}*/
int rootn;
bool compare(p3 a, p3 b) {
if ((a.fi.fi / rootn) != (b.fi.fi / rootn))
return (a.fi.fi / rootn) < (b.fi.fi / rootn);
return a.fi.se < b.fi.se;
}
int brr[500001] = {0};
int dif = 0;
void add(int x) {
if (brr[x] == 0)
dif++;
brr[x]++;
}
void rem(int x) {
if (brr[x] == 1)
dif--;
brr[x]--;
}
int arr[500001];
int main() {
int n;
string s;
cin >> n;
rootn = static_cast<int>(sqrt(n));
rootn = max(rootn, 1000);
// cout<<rootn<<endl;
int k;
cin >> k;
sa(arr, n, d);
int m = 0;
v(p3) vv;
rep(i, k) {
int a, b;
cin >> a >> b;
a--, b--;
vv.pb(mk(mk(a, b), i));
}
sort(vv.begin(), vv.end(), compare);
int ll = 0, rr = -1;
v(int) ans(k);
rep(i, k) {
// cout<<vv[i].fi.fi<<" "<<vv[i].fi.se<<endl;
int l = vv[i].fi.fi;
int r = vv[i].fi.se;
while (rr < r) {
rr++;
add(arr[rr]);
}
while (r < rr) {
rem(arr[rr]);
rr--;
}
while (ll < l) {
rem(arr[ll]);
ll++;
}
while (l < ll) {
ll--;
add(arr[ll]);
}
ans[vv[i].se] = dif;
}
rep(i, k) cout << ans[i] << "\n";
// cout<<endl;
}
| replace | 73 | 74 | 73 | 74 | TLE | |
p02599 | C++ | Time Limit Exceeded | /*** 𝕒𝕜𝕤𝕙𝕒𝕪𝕜𝕦𝕞𝕒𝕣𝟡𝟡 ***/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using id_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS() \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 5e5 + 1;
typedef long long ll;
typedef long double ld;
ll blocksize = 10000, n, m, ans = 0;
ll a[N];
ll cont[N];
void remove(ll ind) {
if (cont[a[ind]] == 1)
ans--;
cont[a[ind]]--;
}
void add(ll ind) {
cont[a[ind]]++;
if (cont[a[ind]] == 1)
ans++;
}
ll getanswer() { return ans; }
struct Query {
ll l, r, ind;
bool operator<(Query other) const {
return make_pair(l / blocksize, r) <
make_pair(other.l / blocksize, other.r);
}
};
vector<ll> mosalgo(vector<Query> queries) {
vector<ll> answer(queries.size());
sort(all(queries));
ll curl = 0, curr = -1;
for (Query q : queries) {
while (curl > q.l) {
curl--;
add(curl);
}
while (curr < q.r) {
curr++;
add(curr);
}
while (curl < q.l) {
remove(curl);
curl++;
}
while (curr > q.r) {
remove(curr);
curr--;
}
answer[q.ind] = getanswer();
}
return answer;
}
void test_case() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<Query> v;
for (int i = 0; i < m; i++) {
ll x, y;
cin >> x >> y;
x--;
y--;
v.pu({x, y, i});
}
vector<ll> res = mosalgo(v);
for (int i = 0; i < m; i++)
cout << res[i] << '\n';
}
int main() {
IOS();
int tc = 1;
// cin>>tc;
while (tc--)
test_case();
return 0;
} | /*** 𝕒𝕜𝕤𝕙𝕒𝕪𝕜𝕦𝕞𝕒𝕣𝟡𝟡 ***/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using id_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS() \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 5e5 + 1;
typedef long long ll;
typedef long double ld;
ll blocksize = 2000, n, m, ans = 0;
ll a[N];
ll cont[N];
void remove(ll ind) {
if (cont[a[ind]] == 1)
ans--;
cont[a[ind]]--;
}
void add(ll ind) {
cont[a[ind]]++;
if (cont[a[ind]] == 1)
ans++;
}
ll getanswer() { return ans; }
struct Query {
ll l, r, ind;
bool operator<(Query other) const {
return make_pair(l / blocksize, r) <
make_pair(other.l / blocksize, other.r);
}
};
vector<ll> mosalgo(vector<Query> queries) {
vector<ll> answer(queries.size());
sort(all(queries));
ll curl = 0, curr = -1;
for (Query q : queries) {
while (curl > q.l) {
curl--;
add(curl);
}
while (curr < q.r) {
curr++;
add(curr);
}
while (curl < q.l) {
remove(curl);
curl++;
}
while (curr > q.r) {
remove(curr);
curr--;
}
answer[q.ind] = getanswer();
}
return answer;
}
void test_case() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<Query> v;
for (int i = 0; i < m; i++) {
ll x, y;
cin >> x >> y;
x--;
y--;
v.pu({x, y, i});
}
vector<ll> res = mosalgo(v);
for (int i = 0; i < m; i++)
cout << res[i] << '\n';
}
int main() {
IOS();
int tc = 1;
// cin>>tc;
while (tc--)
test_case();
return 0;
} | replace | 35 | 36 | 35 | 36 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
typedef struct omaru {
ll l, r, id;
} polka;
#define FENWICK_NMAX 500010
struct fenwick_tree {
ll N;
ll bit[FENWICK_NMAX];
fenwick_tree(ll n) {
N = n;
for (ll x = 1; x <= N; x++) {
bit[x] = 0;
}
}
void add(ll a, ll w) {
for (ll x = a; x <= N; x += x & -x) {
bit[x] += w;
}
}
ll sum(ll a) {
ll ret = 0;
for (ll x = a; x > 0; x -= x & -x) {
ret += bit[x];
}
return ret;
}
ll range_sum(ll l, ll r) { // [l, r]
return sum(r) - sum(l - 1);
}
};
int main() {
// input
ll n, Q;
cin >> n >> Q;
vector<ll> c(n);
rep(i, 0, n) cin >> c[i];
vector<polka> q(Q);
rep(i, 0, Q) {
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
// sort with right pos
sort(q.begin(), q.end(), [](polka l, polka r) {
if (l.r < r.r)
return true;
else
return false;
});
// query
fenwick_tree t(n + 1);
vector<ll> a(n + 1, -1), res(Q);
ll itr = 0;
rep(i, 0, n) {
if (a[c[i]] > -1)
t.add(a[c[i]], -1);
t.add(i + 1, 1);
a[c[i]] = i + 1;
while (q[itr].r == i + 1) {
cerr << itr << endl;
res[q[itr].id] = t.range_sum(q[itr].l, i + 1);
itr++;
}
}
// print ans
rep(i, 0, Q) cout << res[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
typedef struct omaru {
ll l, r, id;
} polka;
#define FENWICK_NMAX 500010
struct fenwick_tree {
ll N;
ll bit[FENWICK_NMAX];
fenwick_tree(ll n) {
N = n;
for (ll x = 1; x <= N; x++) {
bit[x] = 0;
}
}
void add(ll a, ll w) {
for (ll x = a; x <= N; x += x & -x) {
bit[x] += w;
}
}
ll sum(ll a) {
ll ret = 0;
for (ll x = a; x > 0; x -= x & -x) {
ret += bit[x];
}
return ret;
}
ll range_sum(ll l, ll r) { // [l, r]
return sum(r) - sum(l - 1);
}
};
int main() {
// input
ll n, Q;
cin >> n >> Q;
vector<ll> c(n);
rep(i, 0, n) cin >> c[i];
vector<polka> q(Q);
rep(i, 0, Q) {
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
// sort with right pos
sort(q.begin(), q.end(), [](polka l, polka r) {
if (l.r < r.r)
return true;
else
return false;
});
// query
fenwick_tree t(n + 1);
vector<ll> a(n + 1, -1), res(Q);
ll itr = 0;
rep(i, 0, n) {
if (a[c[i]] > -1)
t.add(a[c[i]], -1);
t.add(i + 1, 1);
a[c[i]] = i + 1;
while (q[itr].r == i + 1) {
res[q[itr].id] = t.range_sum(q[itr].l, i + 1);
itr++;
}
}
// print ans
rep(i, 0, Q) cout << res[i] << endl;
return 0;
} | delete | 66 | 67 | 66 | 66 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int ans = 0;
const int X = 333;
vector<int> a;
vector<int> cnt;
void add(int i) {
ans += (cnt[a[i]] == 0);
cnt[a[i]]++;
}
void del(int i) {
cnt[a[i]]--;
ans -= (cnt[a[i]] == 0);
}
int que(int oldL, int oldR, int L, int R) {
for (int i = oldL; i < L; i++)
del(i);
for (int i = L; i < oldL; i++)
add(i);
for (int i = oldR; i > R; i--)
del(i);
for (int i = R; i > oldR; i--)
add(i);
return ans;
}
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
if (a.first.first / X != b.first.first / X)
return a.first.first / X < b.first.first / X;
return a.first.second < b.first.second;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
a.resize(n), cnt.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
}
vector<pair<pair<int, int>, int>> b(q);
for (int i = 0; i < q; i++) {
cin >> b[i].first.first >> b[i].first.second;
b[i].first.first--, b[i].first.second--;
b[i].second = i;
}
sort(b.begin(), b.end(), cmp);
vector<int> answer(q);
int oldL = 1, oldR = 0;
for (int i = 0; i < q; i++) {
int id = b[i].second;
int L = b[i].first.first;
int R = b[i].first.second;
answer[id] = que(oldL, oldR, L, R);
oldL = L, oldR = R;
}
for (auto &now : answer)
cout << now << '\n';
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int ans = 0;
const int X = 2000;
vector<int> a;
vector<int> cnt;
void add(int i) {
ans += (cnt[a[i]] == 0);
cnt[a[i]]++;
}
void del(int i) {
cnt[a[i]]--;
ans -= (cnt[a[i]] == 0);
}
int que(int oldL, int oldR, int L, int R) {
for (int i = oldL; i < L; i++)
del(i);
for (int i = L; i < oldL; i++)
add(i);
for (int i = oldR; i > R; i--)
del(i);
for (int i = R; i > oldR; i--)
add(i);
return ans;
}
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
if (a.first.first / X != b.first.first / X)
return a.first.first / X < b.first.first / X;
return a.first.second < b.first.second;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
a.resize(n), cnt.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
}
vector<pair<pair<int, int>, int>> b(q);
for (int i = 0; i < q; i++) {
cin >> b[i].first.first >> b[i].first.second;
b[i].first.first--, b[i].first.second--;
b[i].second = i;
}
sort(b.begin(), b.end(), cmp);
vector<int> answer(q);
int oldL = 1, oldR = 0;
for (int i = 0; i < q; i++) {
int id = b[i].second;
int L = b[i].first.first;
int R = b[i].first.second;
answer[id] = que(oldL, oldR, L, R);
oldL = L, oldR = R;
}
for (auto &now : answer)
cout << now << '\n';
return 0;
} | replace | 7 | 8 | 7 | 8 | TLE | |
p02599 | C++ | Time Limit Exceeded | #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1e18;
const ll mod = 1e9 + 7;
struct node {
vector<int> rank0;
vector<int> rank1;
node *l = nullptr;
node *r = nullptr;
};
node *build(vector<int> a, int h) {
if (h == -1)
return nullptr;
vector<int> b[2];
for (int x : a) {
b[(x >> h) & 1].push_back(x); // hビット目が0のものと1のもので振り分け
}
node *res = new node();
res->l = build(b[0], h - 1);
res->r = build(b[1], h - 1);
const int n = a.size();
res->rank0.resize(n + 1);
res->rank1.resize(n + 1);
for (int i = 0; i < n; i++) {
res->rank0[i + 1] = res->rank0[i] + ((a[i] >> h & 1) == 0);
res->rank1[i + 1] = res->rank1[i] + ((a[i] >> h & 1) == 1);
}
return res;
}
int countGE(node *x, int l, int r, int v, int h) {
if (h == -1)
return r - l;
int res = 0;
if (((v >> h) & 1) == 0) {
res += x->rank1[r] - x->rank1[l];
res += countGE(x->l, x->rank0[l], x->rank0[r], v, h - 1);
} else {
res += countGE(x->r, x->rank1[l], x->rank1[r], v, h - 1);
}
return res;
}
int main() {
// 元問題は、「区間[l,r)の中の数字の種類数はいくつか」という問題。
// これは、各項aiについて、biを「aj=aiとなるようなi<jを満たす最小のj」と定義すれば、
//[l,r)におけるr以上のbiの個数を求める問題に置き換えることができる。
int n, q;
cin >> n >> q;
vector<int> c(n);
rep(i, n) cin >> c[i];
vector<int> next(n); // 補助データ
map<int, int> mp;
for (int i = n - 1; i >= 0; i--) {
if (mp.count(c[i])) {
next[i] = mp[c[i]];
} else {
next[i] = n;
}
mp[c[i]] = i;
}
node *t = build(next, 20);
while (q--) {
int l, r;
cin >> l >> r;
l--;
cout << countGE(t, l, r, r, 20) << endl;
}
} | #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1e18;
const ll mod = 1e9 + 7;
struct node {
vector<int> rank0;
vector<int> rank1;
node *l = nullptr;
node *r = nullptr;
};
node *build(vector<int> a, int h) {
if (h == -1)
return nullptr;
vector<int> b[2];
for (int x : a) {
b[(x >> h) & 1].push_back(x); // hビット目が0のものと1のもので振り分け
}
node *res = new node();
res->l = build(b[0], h - 1);
res->r = build(b[1], h - 1);
const int n = a.size();
res->rank0.resize(n + 1);
res->rank1.resize(n + 1);
for (int i = 0; i < n; i++) {
res->rank0[i + 1] = res->rank0[i] + ((a[i] >> h & 1) == 0);
res->rank1[i + 1] = res->rank1[i] + ((a[i] >> h & 1) == 1);
}
return res;
}
int countGE(node *x, int l, int r, int v, int h) {
if (h == -1)
return r - l;
int res = 0;
if (((v >> h) & 1) == 0) {
res += x->rank1[r] - x->rank1[l];
res += countGE(x->l, x->rank0[l], x->rank0[r], v, h - 1);
} else {
res += countGE(x->r, x->rank1[l], x->rank1[r], v, h - 1);
}
return res;
}
int main() {
ios;
// 元問題は、「区間[l,r)の中の数字の種類数はいくつか」という問題。
// これは、各項aiについて、biを「aj=aiとなるようなi<jを満たす最小のj」と定義すれば、
//[l,r)におけるr以上のbiの個数を求める問題に置き換えることができる。
int n, q;
cin >> n >> q;
vector<int> c(n);
rep(i, n) cin >> c[i];
vector<int> next(n); // 補助データ
map<int, int> mp;
for (int i = n - 1; i >= 0; i--) {
if (mp.count(c[i])) {
next[i] = mp[c[i]];
} else {
next[i] = n;
}
mp[c[i]] = i;
}
node *t = build(next, 20);
while (q--) {
int l, r;
cin >> l >> r;
l--;
cout << countGE(t, l, r, r, 20) << endl;
}
}
| insert | 76 | 76 | 76 | 77 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
return (x > y) ? (x = y, true) : false;
};
template <typename Monoid> struct Segment_Tree {
Monoid ope(Monoid a, Monoid b) { return a + b; }
const Monoid unit;
int n;
vector<Monoid> seg;
Segment_Tree(int N, const Monoid &x) : unit(x) {
n = 1;
while (n < N)
n <<= 1;
seg.assign(2 * n, unit);
}
void change(int i, const Monoid &x) {
i += n;
seg[i] = x;
while (i > 0) {
i /= 2;
seg[i] = ope(seg[2 * i], seg[2 * i + 1]);
}
}
Monoid query(int a, int b, int i = 1, int l = 0, int r = -1) {
if (r < 0)
r = n;
if (a >= r || b <= l)
return unit;
if (a <= l && r <= b)
return seg[i];
Monoid vl = query(a, b, 2 * i, l, (l + r) / 2);
Monoid vr = query(a, b, 2 * i + 1, (l + r) / 2, r);
return ope(vl, vr);
}
Monoid operator[](int i) const { return seg[n + i]; }
void clear() { fill(seg.begin(), seg.end(), unit); }
};
int main() {
int N, Q;
cin >> N >> Q;
int c[N];
rep(i, N) cin >> c[i];
int pos[N];
fill(pos, pos + N, -1);
Segment_Tree<int> seg(N, 0);
vector<pii> q[N + 1];
rep(i, Q) {
int l, r;
cin >> l >> r;
l--;
q[r].pb(pii(l, i));
}
int ans[Q];
rep(i, N + 1) {
for (auto &e : q[i]) {
ans[e.second] = seg.query(e.first, i);
}
if (i == N)
break;
if (pos[c[i]] != -1)
seg.change(pos[c[i]], 0);
pos[c[i]] = i;
seg.change(i, 1);
}
rep(i, Q) cout << ans[i] << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
return (x > y) ? (x = y, true) : false;
};
template <typename Monoid> struct Segment_Tree {
Monoid ope(Monoid a, Monoid b) { return a + b; }
const Monoid unit;
int n;
vector<Monoid> seg;
Segment_Tree(int N, const Monoid &x) : unit(x) {
n = 1;
while (n < N)
n <<= 1;
seg.assign(2 * n, unit);
}
void change(int i, const Monoid &x) {
i += n;
seg[i] = x;
while (i > 0) {
i /= 2;
seg[i] = ope(seg[2 * i], seg[2 * i + 1]);
}
}
Monoid query(int a, int b, int i = 1, int l = 0, int r = -1) {
if (r < 0)
r = n;
if (a >= r || b <= l)
return unit;
if (a <= l && r <= b)
return seg[i];
Monoid vl = query(a, b, 2 * i, l, (l + r) / 2);
Monoid vr = query(a, b, 2 * i + 1, (l + r) / 2, r);
return ope(vl, vr);
}
Monoid operator[](int i) const { return seg[n + i]; }
void clear() { fill(seg.begin(), seg.end(), unit); }
};
int main() {
int N, Q;
cin >> N >> Q;
int c[N];
rep(i, N) {
cin >> c[i];
c[i]--;
}
int pos[N];
fill(pos, pos + N, -1);
Segment_Tree<int> seg(N, 0);
vector<pii> q[N + 1];
rep(i, Q) {
int l, r;
cin >> l >> r;
l--;
q[r].pb(pii(l, i));
}
int ans[Q];
rep(i, N + 1) {
for (auto &e : q[i]) {
ans[e.second] = seg.query(e.first, i);
}
if (i == N)
break;
if (pos[c[i]] != -1)
seg.change(pos[c[i]], 0);
pos[c[i]] = i;
seg.change(i, 1);
}
rep(i, Q) cout << ans[i] << endl;
} | replace | 72 | 73 | 72 | 76 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST \
ios_base::sync_with_stdio(0); \
cin.tie(0);
const int maxn = 500010;
typedef pair<pi, int> pii;
const int len = 600;
int n, q;
int A[maxn];
int co[maxn];
int rans = 0;
struct query {
int l, r, index, ans;
void read(int _l, int _r, int _index) {
l = _l;
r = _r;
index = _index;
ans = 0;
}
};
vector<query> queries;
bool cmp(const query &a, query &b) {
int buc1 = a.l / len, buc2 = b.l / len;
if (buc1 == buc2)
return a.r < b.r;
else
return buc1 < buc2;
}
bool cmpind(const query &a, const query &b) { return a.index < b.index; }
void add(int x) {
co[A[x]]++;
if (co[A[x]] == 1)
rans++;
}
void rem(int x) {
co[A[x]]--;
if (co[A[x]] == 0)
rans--;
}
int32_t main() {
FAST cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> A[i];
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
queries.push_back(query());
queries[i].read(--l, --r, i);
}
sort(queries.begin(), queries.end(), cmp);
// cout << "HELLO\n";
// for (auto i: queries) {
// cout << i.l << " " << i.r << "\n";
//}
int ql = 1, qr = 0;
for (int i = 0; i < q; i++) {
int l = queries[i].l, r = queries[i].r;
while (ql > l)
add(--ql);
while (ql < l)
rem(ql++);
while (qr < r)
add(++qr);
while (qr > r)
rem(qr--);
queries[i].ans = rans;
}
sort(queries.begin(), queries.end(), cmpind);
for (int i = 0; i < q; i++) {
cout << queries[i].ans << "\n";
}
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST \
ios_base::sync_with_stdio(0); \
cin.tie(0);
const int maxn = 500010;
typedef pair<pi, int> pii;
const int len = 800;
int n, q;
int A[maxn];
int co[maxn];
int rans = 0;
struct query {
int l, r, index, ans;
void read(int _l, int _r, int _index) {
l = _l;
r = _r;
index = _index;
ans = 0;
}
};
vector<query> queries;
bool cmp(const query &a, query &b) {
int buc1 = a.l / len, buc2 = b.l / len;
if (buc1 == buc2)
return a.r < b.r;
else
return buc1 < buc2;
}
bool cmpind(const query &a, const query &b) { return a.index < b.index; }
void add(int x) {
co[A[x]]++;
if (co[A[x]] == 1)
rans++;
}
void rem(int x) {
co[A[x]]--;
if (co[A[x]] == 0)
rans--;
}
int32_t main() {
FAST cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> A[i];
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
queries.push_back(query());
queries[i].read(--l, --r, i);
}
sort(queries.begin(), queries.end(), cmp);
// cout << "HELLO\n";
// for (auto i: queries) {
// cout << i.l << " " << i.r << "\n";
//}
int ql = 1, qr = 0;
for (int i = 0; i < q; i++) {
int l = queries[i].l, r = queries[i].r;
while (ql > l)
add(--ql);
while (ql < l)
rem(ql++);
while (qr < r)
add(++qr);
while (qr > r)
rem(qr--);
queries[i].ans = rans;
}
sort(queries.begin(), queries.end(), cmpind);
for (int i = 0; i < q; i++) {
cout << queries[i].ans << "\n";
}
}
| replace | 12 | 13 | 12 | 13 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);
int N;
int bit[500005]; // bit 1-index
map<int, int> amap;
void add(int i, int x) {
int tmp;
if (amap[x] == 0) {
tmp = i;
while (tmp <= N) {
bit[tmp]++;
tmp += tmp & -tmp;
}
} else {
tmp = i;
while (tmp <= N) {
bit[tmp]++;
tmp += tmp & -tmp;
}
tmp = amap[x];
while (tmp <= N) {
bit[tmp]--;
tmp += tmp & -tmp;
}
}
amap[x] = i;
}
int sum(int l) {
int ans = 0;
while (l > 0) {
ans += bit[l];
l -= l & -l;
}
return ans;
}
int main() {
int Q;
cin >> N >> Q;
int c[N];
for (int i = 0; i < N; ++i)
cin >> c[i];
vector<tuple<int, int, int>> query(Q);
int l, r;
for (int i = 0; i < Q; ++i) {
cin >> l >> r;
query[i] = make_tuple(r, l, i);
}
sort(query.begin(), query.end());
int ans[Q];
int index = 0;
for (int i = 0; i < Q; ++i) {
l = get<1>(query[i]);
r = get<0>(query[i]);
for (int j = index; j < r; ++j) {
add(j + 1, c[j]);
ans[get<2>(query[i])] = sum(r) - sum(l - 1);
}
}
for (int i = 0; i < Q; ++i)
cout << ans[i] << endl;
}
| #include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);
int N;
int bit[500005]; // bit 1-index
map<int, int> amap;
void add(int i, int x) {
int tmp;
if (amap[x] == 0) {
tmp = i;
while (tmp <= N) {
bit[tmp]++;
tmp += tmp & -tmp;
}
} else {
tmp = i;
while (tmp <= N) {
bit[tmp]++;
tmp += tmp & -tmp;
}
tmp = amap[x];
while (tmp <= N) {
bit[tmp]--;
tmp += tmp & -tmp;
}
}
amap[x] = i;
}
int sum(int l) {
int ans = 0;
while (l > 0) {
ans += bit[l];
l -= l & -l;
}
return ans;
}
int main() {
int Q;
cin >> N >> Q;
int c[N];
for (int i = 0; i < N; ++i)
cin >> c[i];
vector<tuple<int, int, int>> query(Q);
int l, r;
for (int i = 0; i < Q; ++i) {
cin >> l >> r;
query[i] = make_tuple(r, l, i);
}
sort(query.begin(), query.end());
int ans[Q];
int index = 0;
for (int i = 0; i < Q; ++i) {
l = get<1>(query[i]);
r = get<0>(query[i]);
for (int j = index; j < r; ++j) {
add(j + 1, c[j]);
ans[get<2>(query[i])] = sum(r) - sum(l - 1);
}
index = r - 1;
}
for (int i = 0; i < Q; ++i)
cout << ans[i] << endl;
}
| insert | 83 | 83 | 83 | 84 | TLE | |
p02599 | C++ | Runtime Error | #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
// #pragma warning(disable : 4996)
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif
#include <limits.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
// #define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) \
REP(i, v.size()) cout << v[i] << " \n"[i == (int)v.size() - 1]
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
// int dx[4] = { 0,0,-1,1 }, dy[4] = { 1,-1,0,0 };
template <typename T> struct SegmentTree {
int n;
T unit;
vector<T> dat;
function<T(T, T)> func;
SegmentTree(const int N, T _unit, function<T(T, T)> _func)
: unit(_unit), func(_func) {
n = 1;
// 簡単のため、要素数を2のべき乗に
while (n < N)
n *= 2;
dat.assign(2 * n, unit);
}
void update(int k, T a) {
k += n - 1; // 葉の節点
dat[k] = a;
// 上りながら更新
while (k > 0) {
k = (k - 1) / 2;
dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
T _query(int a, int b, int k, int l, int r) {
//[a,b)と[l,r)が交差していなければ、funcに影響を与えない値を返す
if (r <= a || b <= l)
return unit;
//[a,b)が[l,r)を完全に含んでいれば、この節点の値
if (a <= l && r <= b)
return dat[k];
else {
T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
return func(vl, vr);
}
}
//[a,b)
T query(int a, int b) { return _query(a, b, 0, 0, n); }
};
// auto f = [](int a, int b) {return a + b; };
// SegmentTree<int>seg(N,0,f);
int las[200020];
void solve() {
int N, Q;
cin >> N >> Q;
vector<int> A(N);
REP(i, N) cin >> A[i];
vector<int> L(Q), R(Q);
vector<pair<pii, int>> v;
REP(i, Q) {
cin >> L[i] >> R[i];
L[i]--, R[i]--;
v.push_back({{R[i], L[i]}, i});
}
REP(i, N + 1) { las[i] = -1; }
VSORT(v);
vector<int> ids;
REP(i, Q) ids.push_back(v[i].second);
auto f = [](int a, int b) { return a + b; };
SegmentTree<int> seg(N + 1, 0, f);
int idx = 0;
vector<int> ans(Q);
REP(r, N) {
// DEBUG(r);
int pos = las[A[r]];
if (pos != -1) {
seg.update(pos, seg.query(pos, pos + 1) - 1);
}
las[A[r]] = r;
int id = ids[idx];
seg.update(r, seg.query(r, r + 1) + 1);
while (idx < Q && R[id] == r) {
// DEBUG(idx);
// DEBUG(id);
int l = L[id];
ans[id] = seg.query(l, r + 1);
idx++;
if (idx == Q)
break;
id = ids[idx];
}
}
REP(i, Q) print(ans[i]);
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
// int q;
// cin >> q;
// while (q--)
solve();
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
// #pragma warning(disable : 4996)
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif
#include <limits.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
// #define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) \
REP(i, v.size()) cout << v[i] << " \n"[i == (int)v.size() - 1]
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
// int dx[4] = { 0,0,-1,1 }, dy[4] = { 1,-1,0,0 };
template <typename T> struct SegmentTree {
int n;
T unit;
vector<T> dat;
function<T(T, T)> func;
SegmentTree(const int N, T _unit, function<T(T, T)> _func)
: unit(_unit), func(_func) {
n = 1;
// 簡単のため、要素数を2のべき乗に
while (n < N)
n *= 2;
dat.assign(2 * n, unit);
}
void update(int k, T a) {
k += n - 1; // 葉の節点
dat[k] = a;
// 上りながら更新
while (k > 0) {
k = (k - 1) / 2;
dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
T _query(int a, int b, int k, int l, int r) {
//[a,b)と[l,r)が交差していなければ、funcに影響を与えない値を返す
if (r <= a || b <= l)
return unit;
//[a,b)が[l,r)を完全に含んでいれば、この節点の値
if (a <= l && r <= b)
return dat[k];
else {
T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
return func(vl, vr);
}
}
//[a,b)
T query(int a, int b) { return _query(a, b, 0, 0, n); }
};
// auto f = [](int a, int b) {return a + b; };
// SegmentTree<int>seg(N,0,f);
int las[500020];
void solve() {
int N, Q;
cin >> N >> Q;
vector<int> A(N);
REP(i, N) cin >> A[i];
vector<int> L(Q), R(Q);
vector<pair<pii, int>> v;
REP(i, Q) {
cin >> L[i] >> R[i];
L[i]--, R[i]--;
v.push_back({{R[i], L[i]}, i});
}
REP(i, N + 1) { las[i] = -1; }
VSORT(v);
vector<int> ids;
REP(i, Q) ids.push_back(v[i].second);
auto f = [](int a, int b) { return a + b; };
SegmentTree<int> seg(N + 1, 0, f);
int idx = 0;
vector<int> ans(Q);
REP(r, N) {
// DEBUG(r);
int pos = las[A[r]];
if (pos != -1) {
seg.update(pos, seg.query(pos, pos + 1) - 1);
}
las[A[r]] = r;
int id = ids[idx];
seg.update(r, seg.query(r, r + 1) + 1);
while (idx < Q && R[id] == r) {
// DEBUG(idx);
// DEBUG(id);
int l = L[id];
ans[id] = seg.query(l, r + 1);
idx++;
if (idx == Q)
break;
id = ids[idx];
}
}
REP(i, Q) print(ans[i]);
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
// int q;
// cin >> q;
// while (q--)
solve();
}
| replace | 120 | 121 | 120 | 121 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int bl, t, n, m;
int res, a[N], cnt[N], ans[N];
int curR, curL;
struct node {
int l, r, p;
bool operator<(const node &W) const { return l < W.l; }
} e[N];
bool cmp(const node &a, const node &b) {
return (a.l / bl) == (b.l / bl) ? a.r < b.r : a.l > b.l;
}
void remove(int i) {
if ((--cnt[a[i]]) == 0)
res--;
}
void add(int i) {
if ((++cnt[a[i]]) == 1)
res++;
}
int main() {
memset(ans, 0, sizeof(ans));
memset(cnt, 0, sizeof(cnt));
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
bl = sqrt(n);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &e[i].l, &e[i].r);
e[i].p = i;
}
sort(e + 1, e + 1 + m, cmp);
res = 0;
curL = 1;
curR = 0;
for (int i = 1; i <= m; i++) {
int L = e[i].l, R = e[i].r;
while (curL < L)
remove(curL++);
while (curL > L)
add(--curL);
while (curR < R)
add(++curR);
while (curR > R)
remove(curR--);
ans[e[i].p] = res;
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
}
| #pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int bl, t, n, m;
int res, a[N], cnt[N], ans[N];
int curR, curL;
struct node {
int l, r, p;
bool operator<(const node &W) const { return l < W.l; }
} e[N];
bool cmp(const node &a, const node &b) {
return (a.l / bl) == (b.l / bl) ? a.r < b.r : a.l > b.l;
}
void remove(int i) {
if ((--cnt[a[i]]) == 0)
res--;
}
void add(int i) {
if ((++cnt[a[i]]) == 1)
res++;
}
int main() {
memset(ans, 0, sizeof(ans));
memset(cnt, 0, sizeof(cnt));
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
bl = sqrt(n);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &e[i].l, &e[i].r);
e[i].p = i;
}
sort(e + 1, e + 1 + m, cmp);
res = 0;
curL = 1;
curR = 0;
for (int i = 1; i <= m; i++) {
int L = e[i].l, R = e[i].r;
while (curL < L)
remove(curL++);
while (curL > L)
add(--curL);
while (curR < R)
add(++curR);
while (curR > R)
remove(curR--);
ans[e[i].p] = res;
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
}
| insert | 0 | 0 | 0 | 1 | TLE | |
p02599 | C++ | Time Limit Exceeded |
#include <bits/stdc++.h>
using namespace std;
/**
******************* Author:Bisnu sarkar ****************************
**/
#define ull unsigned long long
#define ll long long
#define pii pair<int, int>
#define sit set<int>::iterator
#define vrit vector<int>::reverse iterator
#define ff first
#define ss second
#define endl '\n';
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define IO ios_base::sync_with_stdio(false), cin.tie(NULL);
#define what_is(x) cerr << #x << " is " << x << endl;
///*....Debugger....*///
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
///*....Input....*///
template <typename T> inline void Int(T &n) {
n = 0;
int f = 1;
register int ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-')
f = -1;
for (; isdigit(ch); ch = getchar())
n = (n << 3) + (n << 1) + ch - '0';
n = n * f;
}
template <typename T, typename TT> inline void Int(T &n, TT &m) {
Int(n);
Int(m);
}
template <typename T, typename TT, typename TTT>
inline void Int(T &n, TT &m, TTT &l) {
Int(n, m);
Int(l);
}
double ind() {
double x;
scanf("%lf", &x);
return x;
}
///*....Bitmask....*///
int set_1(int n, int pos) { return n = (n | (1 << pos)); }
int reset_0(int n, int pos) { return n = n & ~(1 << pos); }
bool check_bit(int n, int pos) { return n = n & (1 << pos); }
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
///*....Constraints....*///
const int N = (int)5e5 + 5;
const int M = (int)1e9 + 7;
const double pi = 2 * acos(0.0);
const double eps = 1e-9;
struct query {
int l, r, id;
} q[N];
int a[N], k = 750, l = 1, r = 0;
bool compare(query &a, query &b) {
int b_a = a.l / k;
int b_b = b.l / k;
if (b_a == b_b)
return a.r < b.r;
return b_a < b_b;
}
int cnt[N], ans[N];
int res = 0;
void add(int x) {
cnt[a[x]]++;
if (cnt[a[x]] == 1)
++res;
}
void remove(int x) {
cnt[a[x]]--;
if (cnt[a[x]] == 0)
--res;
}
int main() {
int t = 1, te = 0;
// Int(t);
while (t--) {
int n;
Int(n);
int Q;
Int(Q);
for (int i = 1; i <= n; ++i)
Int(a[i]);
for (int i = 1; i <= Q; ++i) {
Int(q[i].l, q[i].r);
q[i].id = i;
}
sort(q + 1, q + Q + 1, compare);
for (int i = 1; i <= Q; ++i) {
while (l > q[i].l)
add(--l);
while (r < q[i].r)
add(++r);
while (l < q[i].l)
remove(l++);
while (r > q[i].r)
remove(r--);
ans[q[i].id] = res;
}
for (int i = 1; i <= Q; ++i) {
printf("%d\n", ans[i]);
}
// printf("Case %d: ",++te);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
/**
******************* Author:Bisnu sarkar ****************************
**/
#define ull unsigned long long
#define ll long long
#define pii pair<int, int>
#define sit set<int>::iterator
#define vrit vector<int>::reverse iterator
#define ff first
#define ss second
#define endl '\n';
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define IO ios_base::sync_with_stdio(false), cin.tie(NULL);
#define what_is(x) cerr << #x << " is " << x << endl;
///*....Debugger....*///
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
///*....Input....*///
template <typename T> inline void Int(T &n) {
n = 0;
int f = 1;
register int ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-')
f = -1;
for (; isdigit(ch); ch = getchar())
n = (n << 3) + (n << 1) + ch - '0';
n = n * f;
}
template <typename T, typename TT> inline void Int(T &n, TT &m) {
Int(n);
Int(m);
}
template <typename T, typename TT, typename TTT>
inline void Int(T &n, TT &m, TTT &l) {
Int(n, m);
Int(l);
}
double ind() {
double x;
scanf("%lf", &x);
return x;
}
///*....Bitmask....*///
int set_1(int n, int pos) { return n = (n | (1 << pos)); }
int reset_0(int n, int pos) { return n = n & ~(1 << pos); }
bool check_bit(int n, int pos) { return n = n & (1 << pos); }
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
///*....Constraints....*///
const int N = (int)5e5 + 5;
const int M = (int)1e9 + 7;
const double pi = 2 * acos(0.0);
const double eps = 1e-9;
struct query {
int l, r, id;
} q[N];
int a[N], k = 1000, l = 1, r = 0;
bool compare(query &a, query &b) {
int b_a = a.l / k;
int b_b = b.l / k;
if (b_a == b_b)
return a.r < b.r;
return b_a < b_b;
}
int cnt[N], ans[N];
int res = 0;
void add(int x) {
cnt[a[x]]++;
if (cnt[a[x]] == 1)
++res;
}
void remove(int x) {
cnt[a[x]]--;
if (cnt[a[x]] == 0)
--res;
}
int main() {
int t = 1, te = 0;
// Int(t);
while (t--) {
int n;
Int(n);
int Q;
Int(Q);
for (int i = 1; i <= n; ++i)
Int(a[i]);
for (int i = 1; i <= Q; ++i) {
Int(q[i].l, q[i].r);
q[i].id = i;
}
sort(q + 1, q + Q + 1, compare);
for (int i = 1; i <= Q; ++i) {
while (l > q[i].l)
add(--l);
while (r < q[i].r)
add(++r);
while (l < q[i].l)
remove(l++);
while (r > q[i].r)
remove(r--);
ans[q[i].id] = res;
}
for (int i = 1; i <= Q; ++i) {
printf("%d\n", ans[i]);
}
// printf("Case %d: ",++te);
}
return 0;
}
| replace | 78 | 79 | 78 | 79 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define INF (1ll << 62)
#define pb push_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define endl '\n'
class Persistent_Seg_Tree {
private:
int n;
public:
struct Node {
int val;
Node *left, *right;
Node() {}
Node(int v, Node *l, Node *r) : val(v), left(l), right(r) {}
};
#define NEUTRAL_NODE Node(0, nullptr, nullptr);
Node _NEUTRAL_NODE = Node(0, nullptr, nullptr);
vector<Node *> version;
int merge_nodes(const int x, const int y) { return x + y; }
Node *pst_build(Node *node, const int l, const int r,
const vector<int> &arr) {
node = new NEUTRAL_NODE;
if (l == r) {
node->val = arr[l];
return node;
}
int mid = (l + r) / 2;
node->left = pst_build(node->left, l, mid, arr);
node->right = pst_build(node->right, mid + 1, r, arr);
node->val = merge_nodes(node->left->val, node->right->val);
return node;
}
Node *pst_build_empty(Node *node, const int l, const int r) {
node = new NEUTRAL_NODE;
if (l == r)
return node;
int mid = (l + r) / 2;
node->left = pst_build_empty(node->left, l, mid);
node->right = pst_build_empty(node->right, mid + 1, r);
node->val = merge_nodes(node->left->val, node->right->val);
return node;
}
Node *pst_update(Node *cur_tree, Node *prev_tree, const int l, const int r,
const int idx, const int v) {
if (l > idx || r < idx) {
if (cur_tree != nullptr)
return cur_tree;
return prev_tree;
}
if (cur_tree == nullptr)
cur_tree = new Node(prev_tree->val, nullptr, nullptr);
if (l == r) {
cur_tree->val += v;
return cur_tree;
}
int mid = (l + r) / 2;
cur_tree->left =
pst_update(cur_tree->left, prev_tree->left, l, mid, idx, v);
cur_tree->right =
pst_update(cur_tree->right, prev_tree->right, mid + 1, r, idx, v);
cur_tree->val = merge_nodes(cur_tree->left->val, cur_tree->right->val);
return cur_tree;
}
int pst_query(Node *node, const int l, const int r, const int i,
const int j) {
if (l > j || r < i)
return _NEUTRAL_NODE.val;
if (i <= l && r <= j)
return node->val;
int mid = (l + r) / 2;
return merge_nodes(pst_query(node->left, l, mid, i, j),
pst_query(node->right, mid + 1, r, i, j));
}
public:
Persistent_Seg_Tree(const int n, const int number_of_versions) {
this->n = n;
version.resize(number_of_versions);
this->version[0] = this->pst_build_empty(this->version[0], 0, this->n - 1);
}
Persistent_Seg_Tree(const int n, const int number_of_versions,
vector<int> &arr) {
this->n = n;
version.resize(number_of_versions);
this->version[0] = this->pst_build(this->version[0], 0, this->n - 1, arr);
}
void update(const int cur_version, const int prev_version, const int idx,
const int v) {
this->version[cur_version] =
this->pst_update(this->version[cur_version],
this->version[prev_version], 0, this->n - 1, idx, v);
}
int query(const int version, const int l, const int r) {
assert(0 <= l);
assert(l <= r);
assert(r < this->n);
return this->pst_query(this->version[version], 0, this->n - 1, l, r);
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
unordered_map<int, int> last;
vector<int> arr(n + 2);
Persistent_Seg_Tree pst(n + 2, n + 2, arr);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (last.count(x))
pst.update(i, i - 1, last[x], -1);
pst.update(i, i - 1, i, 1);
last[x] = i;
}
while (q--) {
int l, r;
cin >> l >> r;
int ans = pst.query(r, l, r);
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define INF (1ll << 62)
#define pb push_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
// #define int long long
#define tt tuple<int, int, int>
#define endl '\n'
class Persistent_Seg_Tree {
private:
int n;
public:
struct Node {
int val;
Node *left, *right;
Node() {}
Node(int v, Node *l, Node *r) : val(v), left(l), right(r) {}
};
#define NEUTRAL_NODE Node(0, nullptr, nullptr);
Node _NEUTRAL_NODE = Node(0, nullptr, nullptr);
vector<Node *> version;
int merge_nodes(const int x, const int y) { return x + y; }
Node *pst_build(Node *node, const int l, const int r,
const vector<int> &arr) {
node = new NEUTRAL_NODE;
if (l == r) {
node->val = arr[l];
return node;
}
int mid = (l + r) / 2;
node->left = pst_build(node->left, l, mid, arr);
node->right = pst_build(node->right, mid + 1, r, arr);
node->val = merge_nodes(node->left->val, node->right->val);
return node;
}
Node *pst_build_empty(Node *node, const int l, const int r) {
node = new NEUTRAL_NODE;
if (l == r)
return node;
int mid = (l + r) / 2;
node->left = pst_build_empty(node->left, l, mid);
node->right = pst_build_empty(node->right, mid + 1, r);
node->val = merge_nodes(node->left->val, node->right->val);
return node;
}
Node *pst_update(Node *cur_tree, Node *prev_tree, const int l, const int r,
const int idx, const int v) {
if (l > idx || r < idx) {
if (cur_tree != nullptr)
return cur_tree;
return prev_tree;
}
if (cur_tree == nullptr)
cur_tree = new Node(prev_tree->val, nullptr, nullptr);
if (l == r) {
cur_tree->val += v;
return cur_tree;
}
int mid = (l + r) / 2;
cur_tree->left =
pst_update(cur_tree->left, prev_tree->left, l, mid, idx, v);
cur_tree->right =
pst_update(cur_tree->right, prev_tree->right, mid + 1, r, idx, v);
cur_tree->val = merge_nodes(cur_tree->left->val, cur_tree->right->val);
return cur_tree;
}
int pst_query(Node *node, const int l, const int r, const int i,
const int j) {
if (l > j || r < i)
return _NEUTRAL_NODE.val;
if (i <= l && r <= j)
return node->val;
int mid = (l + r) / 2;
return merge_nodes(pst_query(node->left, l, mid, i, j),
pst_query(node->right, mid + 1, r, i, j));
}
public:
Persistent_Seg_Tree(const int n, const int number_of_versions) {
this->n = n;
version.resize(number_of_versions);
this->version[0] = this->pst_build_empty(this->version[0], 0, this->n - 1);
}
Persistent_Seg_Tree(const int n, const int number_of_versions,
vector<int> &arr) {
this->n = n;
version.resize(number_of_versions);
this->version[0] = this->pst_build(this->version[0], 0, this->n - 1, arr);
}
void update(const int cur_version, const int prev_version, const int idx,
const int v) {
this->version[cur_version] =
this->pst_update(this->version[cur_version],
this->version[prev_version], 0, this->n - 1, idx, v);
}
int query(const int version, const int l, const int r) {
assert(0 <= l);
assert(l <= r);
assert(r < this->n);
return this->pst_query(this->version[version], 0, this->n - 1, l, r);
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
unordered_map<int, int> last;
vector<int> arr(n + 2);
Persistent_Seg_Tree pst(n + 2, n + 2, arr);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (last.count(x))
pst.update(i, i - 1, last[x], -1);
pst.update(i, i - 1, i, 1);
last[x] = i;
}
while (q--) {
int l, r;
cin >> l >> r;
int ans = pst.query(r, l, r);
cout << ans << endl;
}
}
| replace | 11 | 12 | 11 | 12 | TLE | |
p02599 | C++ | Time Limit Exceeded | // Mo's algorithm
// 例題:数列の区間内の要素の種類数を求める ABC174 F
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
struct Mo {
vector<int> left, right, index; // offline query
int window; // bucket width
int nl, nr, ptr; // [nl,nr)
vector<int> a; // input
vector<int> res; // output
// add,delで書き換えていく
vector<int> count;
int sum;
Mo(int n, vector<int> aa)
: window((int)sqrt(n)), nl(0), nr(0), ptr(0), count(n, 0), sum(0) {
a = aa;
}
/* query [l,r) 0-indexed */
void push(int l, int r) { left.push_back(l), right.push_back(r); }
/* query sort */
void build() {
index.resize(left.size());
res.resize(left.size());
iota(index.begin(), index.end(), 0);
sort(index.begin(), index.end(), [&](int a, int b) {
if (left[a] / window != left[b] / window)
return left[a] < left[b];
if ((left[a] / window) % 2)
return right[a] > right[b];
return right[a] < right[b];
});
}
/* 問題ごとに書き換える */
void add(int id) {
if (count[a[id]]++ == 0)
sum++;
return;
};
void del(int id) {
if (--count[a[id]] == 0)
sum--;
return;
}
/* 区間を次のquery用に伸縮させる */
void next() {
int i = index[ptr++];
while (left[i] < nl)
add(--nl);
while (nr < right[i])
add(nr++);
while (nl < left[i])
del(nl++);
while (right[i] < nr)
del(--nr);
res[i] = sum;
} // 返り値はquery-index
};
int n, q, l, r;
int main() {
cin >> n >> q;
vector<int> a(n);
rep(i, n) {
cin >> a[i];
a[i]--;
}
Mo mo(n, a);
rep(i, q) {
cin >> l >> r;
mo.push(--l, r);
}
mo.build();
rep(i, q) mo.next();
rep(i, q) cout << mo.res[i] << endl;
return 0;
} | // Mo's algorithm
// 例題:数列の区間内の要素の種類数を求める ABC174 F
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
struct Mo {
vector<int> left, right, index; // offline query
int window; // bucket width
int nl, nr, ptr; // [nl,nr)
vector<int> a; // input
vector<int> res; // output
// add,delで書き換えていく
vector<int> count;
int sum;
Mo(int n, vector<int> aa)
: window((int)sqrt(n)), nl(0), nr(0), ptr(0), count(n, 0), sum(0) {
a = aa;
}
/* query [l,r) 0-indexed */
void push(int l, int r) { left.push_back(l), right.push_back(r); }
/* query sort */
void build() {
index.resize(left.size());
res.resize(left.size());
iota(index.begin(), index.end(), 0);
sort(index.begin(), index.end(), [&](int a, int b) {
if (left[a] / window != left[b] / window)
return left[a] < left[b];
if ((left[a] / window) % 2)
return right[a] > right[b];
return right[a] < right[b];
});
}
/* 問題ごとに書き換える */
void add(int id) {
if (count[a[id]]++ == 0)
sum++;
return;
};
void del(int id) {
if (--count[a[id]] == 0)
sum--;
return;
}
/* 区間を次のquery用に伸縮させる */
void next() {
int i = index[ptr++];
while (left[i] < nl)
add(--nl);
while (nr < right[i])
add(nr++);
while (nl < left[i])
del(nl++);
while (right[i] < nr)
del(--nr);
res[i] = sum;
} // 返り値はquery-index
};
int n, q, l, r;
int main() {
cin >> n >> q;
vector<int> a(n);
rep(i, n) {
cin >> a[i];
a[i]--;
}
Mo mo(n, a);
rep(i, q) {
cin >> l >> r;
mo.push(--l, r);
}
mo.build();
rep(i, q) mo.next();
rep(i, q) cout << mo.res[i] << "\n";
return 0;
} | replace | 84 | 85 | 84 | 85 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()
typedef long long ll;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;
const int M = 1e6 + 10;
const int N = 30000;
const int INF = 1e9;
int pos[M], a[N], nxt[N];
vi t[4 * N];
void build(int v, int tl, int tr) {
if (tl == tr)
t[v].resize(1, nxt[tl]);
else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
merge(all(t[2 * v]), all(t[2 * v + 1]), back_inserter(t[v]));
}
}
int count_less(int v, int tl, int tr, int l, int r, int x) {
if (l <= tl && tr <= r)
return (int)(upper_bound(all(t[v]), x) - t[v].begin());
if (tr < l || r < tl)
return 0;
int tm = (tl + tr) / 2;
return count_less(2 * v, tl, tm, l, r, x) +
count_less(2 * v + 1, tm + 1, tr, l, r, x);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
forn(i, n) cin >> a[i];
fill(pos, pos + M, INF);
fornr(i, n) nxt[i] = pos[a[i]], pos[a[i]] = i;
build(1, 0, n - 1);
while (q--) {
int l, r;
cin >> l >> r;
l--, r--;
cout << (r - l + 1) - count_less(1, 0, n - 1, l, r, r) << "\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()
typedef long long ll;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;
const int M = 1e6 + 10;
const int N = 1e6;
const int INF = 1e9;
int pos[M], a[N], nxt[N];
vi t[4 * N];
void build(int v, int tl, int tr) {
if (tl == tr)
t[v].resize(1, nxt[tl]);
else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
merge(all(t[2 * v]), all(t[2 * v + 1]), back_inserter(t[v]));
}
}
int count_less(int v, int tl, int tr, int l, int r, int x) {
if (l <= tl && tr <= r)
return (int)(upper_bound(all(t[v]), x) - t[v].begin());
if (tr < l || r < tl)
return 0;
int tm = (tl + tr) / 2;
return count_less(2 * v, tl, tm, l, r, x) +
count_less(2 * v + 1, tm + 1, tr, l, r, x);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
forn(i, n) cin >> a[i];
fill(pos, pos + M, INF);
fornr(i, n) nxt[i] = pos[a[i]], pos[a[i]] = i;
build(1, 0, n - 1);
while (q--) {
int l, r;
cin >> l >> r;
l--, r--;
cout << (r - l + 1) - count_less(1, 0, n - 1, l, r, r) << "\n";
}
return 0;
} | replace | 26 | 27 | 26 | 27 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#pragma gcc optimize("O3")
#pragma gcc optimize("unroll-loops")
#ifdef EVAH
#define _WIN32_WINNT 0x0500
#include <windows.h>
#endif
#define openFile(n) \
ShellExecute(nullptr, "open", n, nullptr, nullptr, SW_SHOWNORMAL);
#define closeP(n) system((string("taskkill /im ") + n + " /f").c_str());
#define closeConsole PostMessage(GetConsoleWindow(), WM_CLOSE, 0, 0);
#define opf "output.txt"
#define ipf "input.txt"
#define mem(n, x) memset(n, x, sizeof(n));
#define sf scanf
#define pf printf
#define ff first
#define ss second
#define pb push_back
#define PII pair<int, int>
#define For(x, n) for (int i = x; i < n; i++)
#define stv v + 2 * (mid - tl + 1)
#define LL long long int
#define PI acos(-1.0)
#define N 500001
const int blockSize = 400;
struct query {
int l, r, idx;
bool operator<(query other) const {
if (l / blockSize != other.l / blockSize)
return l < other.l;
return (l / blockSize & 1) ? (r < other.r) : (r > other.r);
}
};
vector<int> clr(N), appeared(N);
int tempAns = 0;
void rmv(int idx) {
appeared[clr[idx]]--;
if (!appeared[clr[idx]])
tempAns--;
}
void add(int idx) {
if (!appeared[clr[idx]])
tempAns++;
appeared[clr[idx]]++;
}
int get_ans() { return tempAns; }
vector<int> mosAlgo(vector<query> queries) {
vector<int> ans(queries.size());
sort(queries.begin(), queries.end());
int curL = 0;
int curR = -1;
for (query q : queries) {
while (curL > q.l) {
curL--;
add(curL);
}
while (curR < q.r) {
curR++;
add(curR);
}
while (curL < q.l) {
rmv(curL);
curL++;
}
while (curR > q.r) {
rmv(curR);
curR--;
}
ans[q.idx] = get_ans();
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef EVAH
// freopen(ipf,"r",stdin);
// openFile(ipf);
// closeConsole;
// freopen(opf,"w",stdout);
#endif
int n, q, l, r;
cin >> n >> q;
// blockSize = sqrt(n);
vector<query> queries(q);
For(0, n) { cin >> clr[i]; }
For(0, q) {
queries[i].idx = i;
cin >> queries[i].l >> queries[i].r;
queries[i].l--;
queries[i].r--;
}
vector<int> ans = mosAlgo(queries);
For(0, ans.size()) { cout << ans[i] << "\n"; }
#ifdef EVAH
// openFile(opf);
// Sleep(3000);
// closeP("notepad.exe");
// closeConsole;
#endif
}
| #include <bits/stdc++.h>
using namespace std;
#pragma gcc optimize("O3")
#pragma gcc optimize("unroll-loops")
#ifdef EVAH
#define _WIN32_WINNT 0x0500
#include <windows.h>
#endif
#define openFile(n) \
ShellExecute(nullptr, "open", n, nullptr, nullptr, SW_SHOWNORMAL);
#define closeP(n) system((string("taskkill /im ") + n + " /f").c_str());
#define closeConsole PostMessage(GetConsoleWindow(), WM_CLOSE, 0, 0);
#define opf "output.txt"
#define ipf "input.txt"
#define mem(n, x) memset(n, x, sizeof(n));
#define sf scanf
#define pf printf
#define ff first
#define ss second
#define pb push_back
#define PII pair<int, int>
#define For(x, n) for (int i = x; i < n; i++)
#define stv v + 2 * (mid - tl + 1)
#define LL long long int
#define PI acos(-1.0)
#define N 500001
const int blockSize = 800;
struct query {
int l, r, idx;
bool operator<(query other) const {
if (l / blockSize != other.l / blockSize)
return l < other.l;
return (l / blockSize & 1) ? (r < other.r) : (r > other.r);
}
};
vector<int> clr(N), appeared(N);
int tempAns = 0;
void rmv(int idx) {
appeared[clr[idx]]--;
if (!appeared[clr[idx]])
tempAns--;
}
void add(int idx) {
if (!appeared[clr[idx]])
tempAns++;
appeared[clr[idx]]++;
}
int get_ans() { return tempAns; }
vector<int> mosAlgo(vector<query> queries) {
vector<int> ans(queries.size());
sort(queries.begin(), queries.end());
int curL = 0;
int curR = -1;
for (query q : queries) {
while (curL > q.l) {
curL--;
add(curL);
}
while (curR < q.r) {
curR++;
add(curR);
}
while (curL < q.l) {
rmv(curL);
curL++;
}
while (curR > q.r) {
rmv(curR);
curR--;
}
ans[q.idx] = get_ans();
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef EVAH
// freopen(ipf,"r",stdin);
// openFile(ipf);
// closeConsole;
// freopen(opf,"w",stdout);
#endif
int n, q, l, r;
cin >> n >> q;
// blockSize = sqrt(n);
vector<query> queries(q);
For(0, n) { cin >> clr[i]; }
For(0, q) {
queries[i].idx = i;
cin >> queries[i].l >> queries[i].r;
queries[i].l--;
queries[i].r--;
}
vector<int> ans = mosAlgo(queries);
For(0, ans.size()) { cout << ans[i] << "\n"; }
#ifdef EVAH
// openFile(opf);
// Sleep(3000);
// closeP("notepad.exe");
// closeConsole;
#endif
}
| replace | 27 | 28 | 27 | 28 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 1 << 30;
constexpr int mod = 1e9 + 7;
constexpr ll infl = 1ll << 60;
constexpr int nr = 800;
vector<pair<int, int>> mo[nr];
array<pair<int, int>, 500050> query;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
vector<int> c(n);
for (int i = 0; i < n; ++i)
cin >> c[i];
for (int i = 0; i < q; ++i) {
cin >> query[i].first >> query[i].second;
query[i].first--;
mo[query[i].first / nr].push_back({query[i].second, i});
}
int x;
array<int, 500050> num{};
array<int, 500050> ret{};
for (int i = 0; i < nr; ++i) {
sort(mo[i].begin(), mo[i].end());
fill(num.begin(), num.end(), 0);
int curl, curr;
curl = curr = 0;
int count = 0;
for (auto &&a : mo[i]) {
x = a.second;
while (curr < query[x].second) {
if (num[c[curr]] == 0) {
count++;
}
num[c[curr]]++;
curr++;
}
while (curl < query[x].first) {
num[c[curl]]--;
if (num[c[curl]] == 0)
count--;
curl++;
}
while (curl > query[x].first) {
curl--;
num[c[curl]]++;
if (num[c[curl]] == 1)
count++;
}
ret[x] = count;
}
}
for (int i = 0; i < q; ++i)
cout << ret[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 1 << 30;
constexpr int mod = 1e9 + 7;
constexpr ll infl = 1ll << 60;
constexpr int nr = 800;
vector<pair<int, int>> mo[nr];
pair<int, int> query[500050];
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
vector<int> c(n);
for (int i = 0; i < n; ++i)
cin >> c[i];
for (int i = 0; i < q; ++i) {
cin >> query[i].first >> query[i].second;
query[i].first--;
mo[query[i].first / nr].push_back({query[i].second, i});
}
int x;
array<int, 500050> num{};
array<int, 500050> ret{};
for (int i = 0; i < nr; ++i) {
sort(mo[i].begin(), mo[i].end());
fill(num.begin(), num.end(), 0);
int curl, curr;
curl = curr = 0;
int count = 0;
for (auto &&a : mo[i]) {
x = a.second;
while (curr < query[x].second) {
if (num[c[curr]] == 0) {
count++;
}
num[c[curr]]++;
curr++;
}
while (curl < query[x].first) {
num[c[curl]]--;
if (num[c[curl]] == 0)
count--;
curl++;
}
while (curl > query[x].first) {
curl--;
num[c[curl]]++;
if (num[c[curl]] == 1)
count++;
}
ret[x] = count;
}
}
for (int i = 0; i < q; ++i)
cout << ret[i] << endl;
return 0;
} | replace | 11 | 12 | 11 | 12 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define BLOCK 750
#define mxn 5000001
using namespace std;
struct query {
int l;
int r;
int i;
};
query Q[mxn];
int ar[mxn], ans[mxn];
int fre[mxn];
int cnt = 0;
bool comp(query a, query b) {
if (a.l / BLOCK != b.l / BLOCK)
return a.l / BLOCK < b.l / BLOCK;
return a.r < b.r;
}
void add(int pos) {
fre[ar[pos]]++;
if (fre[ar[pos]] == 1)
cnt++;
}
void remove(int pos) {
fre[ar[pos]]--;
if (fre[ar[pos]] == 0)
cnt--;
}
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> ar[i];
for (int i = 0; i < q; i++) {
cin >> Q[i].l >> Q[i].r;
Q[i].i = i, Q[i].l--, Q[i].r--;
}
sort(Q, Q + q, comp);
int ML = 0, MR = -1;
for (int i = 0; i < q; i++) {
int L = Q[i].l;
int R = Q[i].r;
while (ML > L)
ML--, add(ML);
while (MR < R)
MR++, add(MR);
while (ML < L)
remove(ML), ML++;
while (MR > R)
remove(MR), MR--;
ans[Q[i].i] = cnt;
}
for (int i = 0; i < q; i++)
cout << ans[i] << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define BLOCK 750
#define mxn 5000001
using namespace std;
struct query {
int l;
int r;
int i;
};
query Q[mxn];
int ar[mxn], ans[mxn];
int fre[mxn];
int cnt = 0;
bool comp(query a, query b) {
if (a.l / BLOCK != b.l / BLOCK)
return a.l / BLOCK < b.l / BLOCK;
return a.r < b.r;
}
void add(int pos) {
fre[ar[pos]]++;
if (fre[ar[pos]] == 1)
cnt++;
}
void remove(int pos) {
fre[ar[pos]]--;
if (fre[ar[pos]] == 0)
cnt--;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> ar[i];
for (int i = 0; i < q; i++) {
cin >> Q[i].l >> Q[i].r;
Q[i].i = i, Q[i].l--, Q[i].r--;
}
sort(Q, Q + q, comp);
int ML = 0, MR = -1;
for (int i = 0; i < q; i++) {
int L = Q[i].l;
int R = Q[i].r;
while (ML > L)
ML--, add(ML);
while (MR < R)
MR++, add(MR);
while (ML < L)
remove(ML), ML++;
while (MR > R)
remove(MR), MR--;
ans[Q[i].i] = cnt;
}
for (int i = 0; i < q; i++)
cout << ans[i] << '\n';
}
| replace | 35 | 37 | 35 | 37 | TLE | |
p02599 | C++ | Time Limit Exceeded | /*** 𝕒𝕜𝕤𝕙𝕒𝕪𝕜𝕦𝕞𝕒𝕣𝟡𝟡 ***/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using id_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS() \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 5e5 + 1;
typedef long long ll;
typedef long double ld;
ll blocksize = 512, n, m, ans = 0;
ll a[N];
ll cont[N];
void remove(ll ind) {
if (cont[a[ind]] == 1)
ans--;
cont[a[ind]]--;
}
void add(ll ind) {
cont[a[ind]]++;
if (cont[a[ind]] == 1)
ans++;
}
ll getanswer() { return ans; }
struct Query {
ll l, r, ind;
bool operator<(Query other) const {
return make_pair(l / blocksize, r) <
make_pair(other.l / blocksize, other.r);
}
};
vector<ll> mosalgo(vector<Query> queries) {
vector<ll> answer(queries.size());
sort(all(queries));
ll curl = 0, curr = -1;
for (Query q : queries) {
while (curl > q.l) {
curl--;
add(curl);
}
while (curr < q.r) {
curr++;
add(curr);
}
while (curl < q.l) {
remove(curl);
curl++;
}
while (curr > q.r) {
remove(curr);
curr--;
}
answer[q.ind] = getanswer();
}
return answer;
}
void test_case() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<Query> v;
for (int i = 0; i < m; i++) {
ll x, y;
cin >> x >> y;
x--;
y--;
v.pu({x, y, i});
}
vector<ll> res = mosalgo(v);
for (int i = 0; i < m; i++)
cout << res[i] << '\n';
}
int main() {
IOS();
int tc = 1;
// cin>>tc;
while (tc--)
test_case();
return 0;
} | /*** 𝕒𝕜𝕤𝕙𝕒𝕪𝕜𝕦𝕞𝕒𝕣𝟡𝟡 ***/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using id_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS() \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 5e5 + 1;
typedef long long ll;
typedef long double ld;
ll blocksize = 2000, n, m, ans = 0;
ll a[N];
ll cont[N];
void remove(ll ind) {
if (cont[a[ind]] == 1)
ans--;
cont[a[ind]]--;
}
void add(ll ind) {
cont[a[ind]]++;
if (cont[a[ind]] == 1)
ans++;
}
ll getanswer() { return ans; }
struct Query {
ll l, r, ind;
bool operator<(Query other) const {
return make_pair(l / blocksize, r) <
make_pair(other.l / blocksize, other.r);
}
};
vector<ll> mosalgo(vector<Query> queries) {
vector<ll> answer(queries.size());
sort(all(queries));
ll curl = 0, curr = -1;
for (Query q : queries) {
while (curl > q.l) {
curl--;
add(curl);
}
while (curr < q.r) {
curr++;
add(curr);
}
while (curl < q.l) {
remove(curl);
curl++;
}
while (curr > q.r) {
remove(curr);
curr--;
}
answer[q.ind] = getanswer();
}
return answer;
}
void test_case() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<Query> v;
for (int i = 0; i < m; i++) {
ll x, y;
cin >> x >> y;
x--;
y--;
v.pu({x, y, i});
}
vector<ll> res = mosalgo(v);
for (int i = 0; i < m; i++)
cout << res[i] << '\n';
}
int main() {
IOS();
int tc = 1;
// cin>>tc;
while (tc--)
test_case();
return 0;
} | replace | 35 | 36 | 35 | 36 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct ii {
int a, b;
bool operator<(ii o) const { return tie(a, b) < tie(o.a, o.b); }
};
struct iii {
int a, b, c;
bool operator<(iii o) const { return tie(a, b, c) < tie(o.a, o.b, o.c); }
};
const int MOD = 1000000007;
const int INF = 1000000000;
const int MAXN = 500005;
int N, sqN, Q, curUnique;
int arr[MAXN], cnt[MAXN], ans[MAXN];
iii query[MAXN];
bool cmp(iii x, iii y) {
return make_tuple(x.b / sqN, x.c / sqN) < make_tuple(y.b / sqN, y.c / sqN);
}
void inc(int idx) {
if (cnt[arr[idx]] == 0)
curUnique++;
cnt[arr[idx]]++;
}
void dec(int idx) {
cnt[arr[idx]]--;
if (cnt[arr[idx]] == 0)
curUnique--;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> Q;
sqN = sqrt(N);
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
for (int i = 0; i < Q; i++) {
query[i].a = i;
cin >> query[i].b >> query[i].c;
query[i].b--;
query[i].c--;
}
sort(query, query + Q, cmp);
int l = 0;
int r = 0;
cnt[arr[l]]++;
curUnique = 1;
for (int i = 0; i < Q; i++) {
while (l < query[i].b)
dec(l++);
while (l > query[i].b)
inc(--l);
while (r < query[i].c)
inc(++r);
while (r > query[i].c)
dec(r--);
ans[query[i].a] = curUnique;
}
for (int i = 0; i < Q; i++)
cout << ans[i] << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct ii {
int a, b;
bool operator<(ii o) const { return tie(a, b) < tie(o.a, o.b); }
};
struct iii {
int a, b, c;
bool operator<(iii o) const { return tie(a, b, c) < tie(o.a, o.b, o.c); }
};
const int MOD = 1000000007;
const int INF = 1000000000;
const int MAXN = 500005;
int N, sqN, Q, curUnique;
int arr[MAXN], cnt[MAXN], ans[MAXN];
iii query[MAXN];
bool cmp(iii x, iii y) {
return make_tuple(x.b / sqN, x.c) < make_tuple(y.b / sqN, y.c);
}
void inc(int idx) {
if (cnt[arr[idx]] == 0)
curUnique++;
cnt[arr[idx]]++;
}
void dec(int idx) {
cnt[arr[idx]]--;
if (cnt[arr[idx]] == 0)
curUnique--;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> Q;
sqN = sqrt(N);
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
for (int i = 0; i < Q; i++) {
query[i].a = i;
cin >> query[i].b >> query[i].c;
query[i].b--;
query[i].c--;
}
sort(query, query + Q, cmp);
int l = 0;
int r = 0;
cnt[arr[l]]++;
curUnique = 1;
for (int i = 0; i < Q; i++) {
while (l < query[i].b)
dec(l++);
while (l > query[i].b)
inc(--l);
while (r < query[i].c)
inc(++r);
while (r > query[i].c)
dec(r--);
ans[query[i].a] = curUnique;
}
for (int i = 0; i < Q; i++)
cout << ans[i] << "\n";
}
| replace | 21 | 22 | 21 | 22 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <math.h>
const int mx = 5e5 + 5;
int n, q, a[mx], blk;
struct node {
int l, r, p;
node() {}
node(int _l, int _r, int _p) : l(_l), r(_r), p(_p) {}
bool operator<(const node &ot) {
if (l / blk != ot.l / blk)
return l / blk < ot.l / blk;
return r > ot.r;
}
} qq[mx];
int ans = 0, cnt[mx], res[mx];
void add(int x) {
if (!cnt[a[x]])
ans++;
cnt[a[x]]++;
}
void remove(int x) {
cnt[a[x]]--;
if (!cnt[a[x]])
ans--;
}
int main() {
scanf("%d %d", &n, &q);
blk = sqrt(n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int l, r, i = 0; i < q; i++) {
scanf("%d %d", &l, &r);
qq[i] = node(l - 1, r - 1, i);
}
std ::sort(qq, qq + q);
int cl = 0, cr = 0;
for (int l, r, i = 0; i < q; i++) {
l = qq[i].l, r = qq[i].r;
while (cl < l)
remove(cl++);
while (cl > l)
add(--cl);
while (cr <= r)
add(cr++);
while (cr > r + 1)
remove(--cr);
res[qq[i].p] = ans;
}
for (int i = 0; i < q; i++)
printf("%d\n", res[i]);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <math.h>
const int mx = 5e5 + 5;
int n, q, a[mx], blk;
struct node {
int l, r, p;
node() {}
node(int _l, int _r, int _p) : l(_l), r(_r), p(_p) {}
bool operator<(const node &ot) {
if (l / blk != ot.l / blk)
return l < ot.l;
return (bool)((r < ot.r) ^ ((l / blk) & 1));
}
} qq[mx];
int ans = 0, cnt[mx], res[mx];
void add(int x) {
if (!cnt[a[x]])
ans++;
cnt[a[x]]++;
}
void remove(int x) {
cnt[a[x]]--;
if (!cnt[a[x]])
ans--;
}
int main() {
scanf("%d %d", &n, &q);
blk = sqrt(n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int l, r, i = 0; i < q; i++) {
scanf("%d %d", &l, &r);
qq[i] = node(l - 1, r - 1, i);
}
std ::sort(qq, qq + q);
int cl = 0, cr = 0;
for (int l, r, i = 0; i < q; i++) {
l = qq[i].l, r = qq[i].r;
while (cl < l)
remove(cl++);
while (cl > l)
add(--cl);
while (cr <= r)
add(cr++);
while (cr > r + 1)
remove(--cr);
res[qq[i].p] = ans;
}
for (int i = 0; i < q; i++)
printf("%d\n", res[i]);
return 0;
} | replace | 12 | 14 | 12 | 14 | TLE | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 10000;
int n = 0, q = 0;
struct Query {
int id, l, r;
bool operator<(Query a) const { return r < a.r; }
} query[maxn];
int v[maxn], sum[maxn], ans[maxn];
int lowbit(int x) { return x & (-x); }
void change(int i, int a) {
while (i <= n) {
sum[i] += a;
i += lowbit(i);
}
}
int getsum(int i) {
int sum_t = 0;
while (i > 0) {
sum_t += sum[i];
i -= lowbit(i);
}
return sum_t;
}
int main() {
scanf("%d", &n);
memset(v, 0, sizeof(v));
memset(sum, 0, sizeof(sum));
memset(ans, 0, sizeof(ans));
scanf("%d", &q);
int t = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &v[i]);
}
for (int i = 1; i <= q; i++) {
scanf("%d%d", &query[i].l, &query[i].r);
query[i].id = i;
}
sort(query + 1, query + q + 1);
int pre = 1;
map<int, int> mymap;
for (int i = 1; i <= q; i++) {
for (int j = pre; j <= query[i].r; j++) {
// 已存在
if (mymap[v[j]]) {
change(mymap[v[j]], -1);
}
mymap[v[j]] = j;
change(j, 1);
}
pre = query[i].r + 1;
ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 5e5 + 10;
int n = 0, q = 0;
struct Query {
int id, l, r;
bool operator<(Query a) const { return r < a.r; }
} query[maxn];
int v[maxn], sum[maxn], ans[maxn];
int lowbit(int x) { return x & (-x); }
void change(int i, int a) {
while (i <= n) {
sum[i] += a;
i += lowbit(i);
}
}
int getsum(int i) {
int sum_t = 0;
while (i > 0) {
sum_t += sum[i];
i -= lowbit(i);
}
return sum_t;
}
int main() {
scanf("%d", &n);
memset(v, 0, sizeof(v));
memset(sum, 0, sizeof(sum));
memset(ans, 0, sizeof(ans));
scanf("%d", &q);
int t = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &v[i]);
}
for (int i = 1; i <= q; i++) {
scanf("%d%d", &query[i].l, &query[i].r);
query[i].id = i;
}
sort(query + 1, query + q + 1);
int pre = 1;
map<int, int> mymap;
for (int i = 1; i <= q; i++) {
for (int j = pre; j <= query[i].r; j++) {
// 已存在
if (mymap[v[j]]) {
change(mymap[v[j]], -1);
}
mymap[v[j]] = j;
change(j, 1);
}
pre = query[i].r + 1;
ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
return 0;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02599 | C++ | Time Limit Exceeded | /*
Created by Rahul Goel.
*/
/*******************************************************************************/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
/*******************************************************************************/
using namespace std;
typedef long long ll;
typedef long double ld;
ll mod = 1000000007;
const int INF = 1e9;
const ll LINF = 1e18;
/*******************************************************************************/
/* For PBDS */
/*Two new methods X.order_of_key() and X.find_by_order()*/
/*
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
*/
/*******************************************************************************/
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define all(c) (c).begin(), (c).end()
#define pii pair<int, int>
#define vi vector<int>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vpii vector<pair<int, int>>
#define vvpii vector<vector<pair<int, int>>>
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(aint(c), x) != (c).end())
#define uniq(c) (c).resize(distance((c).begin(), unique(aint(c))))
#define min_pq(t) priority_queue<t, vector<t>, greater<t>>
#define max_pq(t) priority_queue<t>
#define pb push_back
#define ff first
#define ss second
/*******************************************************************************/
int mod_sum() { return 0; }
template <typename T, typename... Args> T mod_sum(T a, Args... args) {
return ((a + mod_sum(args...)) % mod + mod) % mod;
}
int mod_prod() { return 1; }
template <typename T, typename... Args> T mod_prod(T a, Args... args) {
return (a * mod_prod(args...)) % mod;
}
/*******************************************************************************/
/*
Code begins after this.
*/
int freq[500005];
vi vec;
int cur_query_ans = 0;
int block_size;
struct foo {
ll l, r, id, block_id;
};
vector<foo> queries;
bool cmp(foo &a, foo &b) {
if (a.block_id < b.block_id)
return true;
else if (a.block_id > b.block_id)
return false;
else
return a.r < b.r;
}
void add(int idx) {
if (!freq[vec[idx]])
cur_query_ans++;
freq[vec[idx]]++;
}
void remove(int idx) {
freq[vec[idx]]--;
if (!freq[vec[idx]])
cur_query_ans--;
}
int get_ans() { return cur_query_ans; }
int solve() {
memset(freq, 0, sizeof(freq));
int n, q;
cin >> n >> q;
vi answers(q);
vec.resize(n);
for (int i = 0; i < n; i++) {
cin >> vec[i];
}
block_size = sqrt(n);
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
queries.pb({x - 1, y - 1, i, (x - 1) / block_size});
}
sort(all(queries), cmp);
int cur_l = 0, cur_r = -1;
for (auto qu : queries) {
int ql = qu.l, qr = qu.r;
while (cur_l > ql)
cur_l--, add(cur_l);
while (cur_r < qr)
cur_r++, add(cur_r);
while (cur_l < ql)
remove(cur_l), cur_l++;
while (cur_r > qr)
remove(cur_r), cur_r--;
answers[qu.id] = get_ans();
}
for (int i = 0; i < q; i++)
cout << answers[i] << '\n';
return 0;
}
int main() {
fastio;
int t = 1;
while (t--)
solve();
return 0;
}
| /*
Created by Rahul Goel.
*/
/*******************************************************************************/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
/*******************************************************************************/
using namespace std;
typedef long long ll;
typedef long double ld;
ll mod = 1000000007;
const int INF = 1e9;
const ll LINF = 1e18;
/*******************************************************************************/
/* For PBDS */
/*Two new methods X.order_of_key() and X.find_by_order()*/
/*
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
*/
/*******************************************************************************/
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define all(c) (c).begin(), (c).end()
#define pii pair<int, int>
#define vi vector<int>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vpii vector<pair<int, int>>
#define vvpii vector<vector<pair<int, int>>>
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(aint(c), x) != (c).end())
#define uniq(c) (c).resize(distance((c).begin(), unique(aint(c))))
#define min_pq(t) priority_queue<t, vector<t>, greater<t>>
#define max_pq(t) priority_queue<t>
#define pb push_back
#define ff first
#define ss second
/*******************************************************************************/
int mod_sum() { return 0; }
template <typename T, typename... Args> T mod_sum(T a, Args... args) {
return ((a + mod_sum(args...)) % mod + mod) % mod;
}
int mod_prod() { return 1; }
template <typename T, typename... Args> T mod_prod(T a, Args... args) {
return (a * mod_prod(args...)) % mod;
}
/*******************************************************************************/
/*
Code begins after this.
*/
int freq[500005];
vi vec;
int cur_query_ans = 0;
int block_size;
struct foo {
ll l, r, id, block_id;
};
vector<foo> queries;
bool cmp(foo &a, foo &b) {
if (a.block_id < b.block_id)
return true;
else if (a.block_id > b.block_id)
return false;
else if (a.block_id & 1)
return a.r > b.r;
else
return a.r < b.r;
}
void add(int idx) {
if (!freq[vec[idx]])
cur_query_ans++;
freq[vec[idx]]++;
}
void remove(int idx) {
freq[vec[idx]]--;
if (!freq[vec[idx]])
cur_query_ans--;
}
int get_ans() { return cur_query_ans; }
int solve() {
memset(freq, 0, sizeof(freq));
int n, q;
cin >> n >> q;
vi answers(q);
vec.resize(n);
for (int i = 0; i < n; i++) {
cin >> vec[i];
}
block_size = sqrt(n);
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
queries.pb({x - 1, y - 1, i, (x - 1) / block_size});
}
sort(all(queries), cmp);
int cur_l = 0, cur_r = -1;
for (auto qu : queries) {
int ql = qu.l, qr = qu.r;
while (cur_l > ql)
cur_l--, add(cur_l);
while (cur_r < qr)
cur_r++, add(cur_r);
while (cur_l < ql)
remove(cur_l), cur_l++;
while (cur_r > qr)
remove(cur_r), cur_r--;
answers[qu.id] = get_ans();
}
for (int i = 0; i < q; i++)
cout << answers[i] << '\n';
return 0;
}
int main() {
fastio;
int t = 1;
while (t--)
solve();
return 0;
}
| insert | 92 | 92 | 92 | 94 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
struct query {
ll left;
ll right;
ll index;
};
ll arr[500001];
query Q[500001];
ll blk = 777; // Block-size=sqrt(n)=sqrt(30000).we precompute it so to prevent
// runtime error.
ll frequency[500001]; // array to keep the frquency of the elements.
ll answer[500001]; // array to store the answers for all the given queries.
ll cnt = 0; // tells the distinct-elements for each query.
// we compare according to the left-range of the query.(in ascending order)
bool compare(query a, query b) {
if (a.left / blk != b.left / blk)
return (a.left / blk < b.left / blk);
else
return (a.right < b.right);
}
void add(ll pos) {
frequency[arr[pos]]++;
if (frequency[arr[pos]] == 1)
cnt++;
}
void remove(ll pos) {
frequency[arr[pos]]--;
if (frequency[arr[pos]] == 0)
cnt--;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n{0};
ll q{0};
cin >> n >> q;
for (ll i = 0; i < n; i++)
cin >> arr[i];
for (ll i = 0; i < q; i++) {
cin >> Q[i].left >> Q[i].right;
Q[i].index = i;
Q[i].left--;
Q[i].right--;
}
sort(Q, Q + q, compare); // sorting the queries so we can reduce the movement
// of the global-left and global-right pointers.
ll glo_left =
0; // global-left pointer which point where the previous query started
ll glo_right =
-1; // global-right pointer which point where the previous query ended
for (ll i = 0; i < q; i++) {
ll cur_left = Q[i].left; // current query left
ll cur_right = Q[i].right; // current query right
// to expand the previous range for the current range
while (glo_left > cur_left) {
glo_left--;
add(glo_left);
}
while (glo_right < cur_right) {
glo_right++;
add(glo_right);
}
// to compress the previous range for the current range
while (glo_left < cur_left) {
remove(glo_left);
glo_left++;
}
while (glo_right > cur_right) {
remove(glo_right);
glo_right--;
}
answer[Q[i].index] = cnt;
}
for (ll i = 0; i < q; i++)
cout << answer[i] << endl;
} | #include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
struct query {
ll left;
ll right;
ll index;
};
ll arr[500001];
query Q[500001];
ll blk = 1000; // Block-size=sqrt(n)=sqrt(30000).we precompute it so to prevent
// runtime error.
ll frequency[500001]; // array to keep the frquency of the elements.
ll answer[500001]; // array to store the answers for all the given queries.
ll cnt = 0; // tells the distinct-elements for each query.
// we compare according to the left-range of the query.(in ascending order)
bool compare(query a, query b) {
if (a.left / blk != b.left / blk)
return (a.left / blk < b.left / blk);
else
return (a.right < b.right);
}
void add(ll pos) {
frequency[arr[pos]]++;
if (frequency[arr[pos]] == 1)
cnt++;
}
void remove(ll pos) {
frequency[arr[pos]]--;
if (frequency[arr[pos]] == 0)
cnt--;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n{0};
ll q{0};
cin >> n >> q;
for (ll i = 0; i < n; i++)
cin >> arr[i];
for (ll i = 0; i < q; i++) {
cin >> Q[i].left >> Q[i].right;
Q[i].index = i;
Q[i].left--;
Q[i].right--;
}
sort(Q, Q + q, compare); // sorting the queries so we can reduce the movement
// of the global-left and global-right pointers.
ll glo_left =
0; // global-left pointer which point where the previous query started
ll glo_right =
-1; // global-right pointer which point where the previous query ended
for (ll i = 0; i < q; i++) {
ll cur_left = Q[i].left; // current query left
ll cur_right = Q[i].right; // current query right
// to expand the previous range for the current range
while (glo_left > cur_left) {
glo_left--;
add(glo_left);
}
while (glo_right < cur_right) {
glo_right++;
add(glo_right);
}
// to compress the previous range for the current range
while (glo_left < cur_left) {
remove(glo_left);
glo_left++;
}
while (glo_right > cur_right) {
remove(glo_right);
glo_right--;
}
answer[Q[i].index] = cnt;
}
for (ll i = 0; i < q; i++)
cout << answer[i] << endl;
} | replace | 14 | 16 | 14 | 16 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct _ {
ios_base::Init i;
_() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;
const int sqn = 316;
const int maxn = 5e5;
int n, q;
vector<int> c(maxn + 1, 0);
vector<int> ans(maxn + 1, 0);
vector<int> freq(maxn + 1, 0);
vector<vector<int>> queries(maxn + 1, vector<int>(3, 0));
class myComp {
public:
bool operator()(const vector<int> &a, const vector<int> &b) {
// if ((a[0] / sqn) == (b[0] / sqn)) return a[1] < b[1];
// return a[0] < b[0];
if (a[0] / sqn != b[0] / sqn)
return a[0] < b[0];
return a[0] / sqn % 2 ? a[1] < b[1] : a[1] > b[1];
}
};
int main() {
cin >> n >> q;
for (int i = 0; i <= n - 1; i++)
cin >> c[i];
for (int i = 0; i <= q - 1; i++)
cin >> queries[i][0] >> queries[i][1], queries[i][0]--, queries[i][1]--,
queries[i][2] = i;
sort(queries.begin(), queries.begin() + q, myComp());
int beg = 0, en = 1, cnt = 0;
freq[c[0]]++;
cnt = 1;
for (int i = 0; i <= q - 1; i++) {
int l = queries[i][0], r = queries[i][1];
while (en <= r) {
freq[c[en]]++;
if (freq[c[en]] == 1)
cnt++;
en++;
}
while (beg < l) {
freq[c[beg]]--;
if (freq[c[beg]] == 0)
cnt--;
beg++;
}
while (beg > l) {
beg--;
freq[c[beg]]++;
if (freq[c[beg]] == 1)
cnt++;
}
while (en > r + 1) {
en--;
freq[c[en]]--;
if (freq[c[en]] == 0)
cnt--;
}
ans[queries[i][2]] = cnt;
}
for (int i = 0; i <= q - 1; i++)
cout << ans[i] << '\n';
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct _ {
ios_base::Init i;
_() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;
const int sqn = 710;
const int maxn = 5e5;
int n, q;
vector<int> c(maxn + 1, 0);
vector<int> ans(maxn + 1, 0);
vector<int> freq(maxn + 1, 0);
vector<vector<int>> queries(maxn + 1, vector<int>(3, 0));
class myComp {
public:
bool operator()(const vector<int> &a, const vector<int> &b) {
// if ((a[0] / sqn) == (b[0] / sqn)) return a[1] < b[1];
// return a[0] < b[0];
if (a[0] / sqn != b[0] / sqn)
return a[0] < b[0];
return a[0] / sqn % 2 ? a[1] < b[1] : a[1] > b[1];
}
};
int main() {
cin >> n >> q;
for (int i = 0; i <= n - 1; i++)
cin >> c[i];
for (int i = 0; i <= q - 1; i++)
cin >> queries[i][0] >> queries[i][1], queries[i][0]--, queries[i][1]--,
queries[i][2] = i;
sort(queries.begin(), queries.begin() + q, myComp());
int beg = 0, en = 1, cnt = 0;
freq[c[0]]++;
cnt = 1;
for (int i = 0; i <= q - 1; i++) {
int l = queries[i][0], r = queries[i][1];
while (en <= r) {
freq[c[en]]++;
if (freq[c[en]] == 1)
cnt++;
en++;
}
while (beg < l) {
freq[c[beg]]--;
if (freq[c[beg]] == 0)
cnt--;
beg++;
}
while (beg > l) {
beg--;
freq[c[beg]]++;
if (freq[c[beg]] == 1)
cnt++;
}
while (en > r + 1) {
en--;
freq[c[en]]--;
if (freq[c[en]] == 0)
cnt--;
}
ans[queries[i][2]] = cnt;
}
for (int i = 0; i <= q - 1; i++)
cout << ans[i] << '\n';
return 0;
} | replace | 10 | 11 | 10 | 11 | TLE | |
p02599 | C++ | Time Limit Exceeded | // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
int res = 0;
// Mo のアルゴリズム (以下の条件を満たすときにクエリを高速に処理可能)
// ・オフラインクエリ
// ・クエリの対象となる数列が不変
// ・区間の伸縮にかかる計算量が小さい
struct Mo {
int bucket, nl, nr, ptr;
vector<int> left, right, query_idx;
vector<bool> state;
Mo(int N) : bucket((int)sqrt(N)), nl(0), nr(0), ptr(0), state(N) {
left = right = vector<int>();
left.reserve(500010);
right.reserve(500010);
}
// 区間 [l, r) を追加
void insert(int l, int r) {
left.push_back(l);
right.push_back(r);
}
// ソートする (バケットごとに、バケット同じなら右端小さい順)
void build() {
query_idx = vector<int>(left.size());
iota(query_idx.begin(), query_idx.end(), 0);
sort(query_idx.begin(), query_idx.end(), [&](int a, int b) {
if (left[a] / bucket != left[b] / bucket)
return left[a] < left[b];
return right[a] < right[b];
});
}
// クエリを一つすすめて、そのクエリ id を返す
vector<int> run() {
vector<int> ans(query_idx.size());
auto operate = [&](int idx) {
state[idx].flip();
if (state[idx])
add(idx);
else
del(idx);
};
for (size_t i = 0; i < query_idx.size(); i++) {
int id = query_idx[ptr++];
while (nl > left[id])
operate(--nl);
while (nr < right[id])
operate(nr++);
while (nl < left[id])
operate(nl++);
while (nr > right[id])
operate(--nr);
ans[id] = res;
}
return ans;
}
inline void add(int idx) const;
inline void del(int idx) const;
};
int A[500010], cnt[500010];
inline void Mo::add(int idx) const {
if (cnt[A[idx]]++ == 0)
++res;
}
inline void Mo::del(int idx) const {
if (cnt[A[idx]]-- == 1)
--res;
}
class FastIO {
static const int rdata_sz = (1 << 25), wdata_sz = (1 << 25);
char rdata[rdata_sz], wdata[wdata_sz], *rb, *wb;
char tmp_s[20];
public:
FastIO() {
fread(rdata, 1, rdata_sz, stdin);
rb = rdata;
wb = wdata;
}
~FastIO() { fwrite(wdata, 1, wb - wdata, stdout); }
template <typename T> inline void read_i(T &x) {
bool neg = false;
x = 0;
while ((*rb < '0' || *rb > '9') && *rb != '-')
++rb;
if (*rb == '-') {
neg = true;
++rb;
}
while ('0' <= *rb && *rb <= '9') {
x = 10 * x + (*rb - '0');
++rb;
}
if (neg)
x = -x;
}
#define pc(x) *(wb++) = x
template <typename T> inline void writeln_i(T x) {
if (x == 0) {
pc('0');
pc('\n');
return;
}
if (x < 0) {
pc('-');
x = -x;
}
char *t = tmp_s;
while (x) {
T y = x / 10;
*(t++) = (x - y * 10) + '0';
x = y;
}
while (t != tmp_s)
pc(*(--t));
pc('\n');
}
#undef pc
};
FastIO io;
int main() {
int N;
io.read_i(N);
int Q;
io.read_i(Q);
for (int i = 0; i < N; i++) {
io.read_i(A[i]);
}
Mo mo(N);
for (int i = 0; i < Q; i++) {
int l;
io.read_i(l);
int r;
io.read_i(r);
mo.insert(--l, r);
}
mo.build();
vector<int> ans = mo.run();
for (int i = 0; i < Q; i++) {
io.writeln_i(ans[i]);
}
return 0;
}
| // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
int res = 0;
// Mo のアルゴリズム (以下の条件を満たすときにクエリを高速に処理可能)
// ・オフラインクエリ
// ・クエリの対象となる数列が不変
// ・区間の伸縮にかかる計算量が小さい
struct Mo {
int bucket, nl, nr, ptr;
vector<int> left, right, query_idx;
vector<bool> state;
Mo(int N) : bucket(1500), nl(0), nr(0), ptr(0), state(N) {
left = right = vector<int>();
left.reserve(500010);
right.reserve(500010);
}
// 区間 [l, r) を追加
void insert(int l, int r) {
left.push_back(l);
right.push_back(r);
}
// ソートする (バケットごとに、バケット同じなら右端小さい順)
void build() {
query_idx = vector<int>(left.size());
iota(query_idx.begin(), query_idx.end(), 0);
sort(query_idx.begin(), query_idx.end(), [&](int a, int b) {
if (left[a] / bucket != left[b] / bucket)
return left[a] < left[b];
return right[a] < right[b];
});
}
// クエリを一つすすめて、そのクエリ id を返す
vector<int> run() {
vector<int> ans(query_idx.size());
auto operate = [&](int idx) {
state[idx].flip();
if (state[idx])
add(idx);
else
del(idx);
};
for (size_t i = 0; i < query_idx.size(); i++) {
int id = query_idx[ptr++];
while (nl > left[id])
operate(--nl);
while (nr < right[id])
operate(nr++);
while (nl < left[id])
operate(nl++);
while (nr > right[id])
operate(--nr);
ans[id] = res;
}
return ans;
}
inline void add(int idx) const;
inline void del(int idx) const;
};
int A[500010], cnt[500010];
inline void Mo::add(int idx) const {
if (cnt[A[idx]]++ == 0)
++res;
}
inline void Mo::del(int idx) const {
if (cnt[A[idx]]-- == 1)
--res;
}
class FastIO {
static const int rdata_sz = (1 << 25), wdata_sz = (1 << 25);
char rdata[rdata_sz], wdata[wdata_sz], *rb, *wb;
char tmp_s[20];
public:
FastIO() {
fread(rdata, 1, rdata_sz, stdin);
rb = rdata;
wb = wdata;
}
~FastIO() { fwrite(wdata, 1, wb - wdata, stdout); }
template <typename T> inline void read_i(T &x) {
bool neg = false;
x = 0;
while ((*rb < '0' || *rb > '9') && *rb != '-')
++rb;
if (*rb == '-') {
neg = true;
++rb;
}
while ('0' <= *rb && *rb <= '9') {
x = 10 * x + (*rb - '0');
++rb;
}
if (neg)
x = -x;
}
#define pc(x) *(wb++) = x
template <typename T> inline void writeln_i(T x) {
if (x == 0) {
pc('0');
pc('\n');
return;
}
if (x < 0) {
pc('-');
x = -x;
}
char *t = tmp_s;
while (x) {
T y = x / 10;
*(t++) = (x - y * 10) + '0';
x = y;
}
while (t != tmp_s)
pc(*(--t));
pc('\n');
}
#undef pc
};
FastIO io;
int main() {
int N;
io.read_i(N);
int Q;
io.read_i(Q);
for (int i = 0; i < N; i++) {
io.read_i(A[i]);
}
Mo mo(N);
for (int i = 0; i < Q; i++) {
int l;
io.read_i(l);
int r;
io.read_i(r);
mo.insert(--l, r);
}
mo.build();
vector<int> ans = mo.run();
for (int i = 0; i < Q; i++) {
io.writeln_i(ans[i]);
}
return 0;
}
| replace | 51 | 52 | 51 | 52 | TLE | |
p02599 | C++ | Runtime Error | typedef int ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
template <class Monoid> struct SegTree {
using Func = function<Monoid(Monoid, Monoid)>;
const Func F;
const Monoid UNITY;
int SIZE_R;
vector<Monoid> dat;
SegTree(int n, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
init(n);
}
void init(int n) {
SIZE_R = 1;
while (SIZE_R < n)
SIZE_R *= 2;
dat.assign(SIZE_R * 2, UNITY);
}
/* set, a is 0-indexed */
void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
void build() {
for (int k = SIZE_R - 1; k > 0; --k)
dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
}
/* update a, a is 0-indexed */
void update(int a, const Monoid &v) {
int k = a + SIZE_R;
dat[k] = v;
while (k >>= 1)
dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
}
/* get [a, b), a and b are 0-indexed */
Monoid get(int a, int b) {
Monoid vleft = UNITY, vright = UNITY;
for (int left = a + SIZE_R, right = b + SIZE_R; left < right;
left >>= 1, right >>= 1) {
if (left & 1)
vleft = F(vleft, dat[left++]);
if (right & 1)
vright = F(dat[--right], vright);
}
return F(vleft, vright);
}
inline Monoid operator[](int a) { return dat[a + SIZE_R]; }
/* debug */
void print() {
for (int i = 0; i < SIZE_R; ++i) {
cout << (*this)[i];
if (i != SIZE_R - 1)
cout << ",";
}
cout << endl;
}
};
int main() {
ll n, q;
std::cin >> n >> q;
vector<ll> c(n + 10);
for (int i = 0; i < n; i++) {
std::cin >> c[i];
c[i]--;
}
vector<pair<ll, ll>> ans(n + 10);
map<pair<ll, ll>, ll> vmp;
vector<pair<ll, ll>> qs(q + 10);
for (int i = 0; i < q; i++) {
ll l, r;
std::cin >> l >> r;
l--;
r--;
ans[i] = {l, r};
qs[i] = {l, r};
}
SegTree<ll> seg(
n + 10, [](ll a, ll b) { return a + b; }, 0);
sort(qs.begin(), qs.end(),
[](const auto le, const auto ri) { return le.second > ri.second; });
vector<ll> last(n + 10, -1);
vector<ll> doko(n + 10, -1);
for (int i = 0; i < n; i++) {
last[i] = doko[c[i]];
doko[c[i]] = i;
}
for (int i = 0; i < n; i++) {
if (doko[c[i]] == i) {
seg.update(i, 1);
}
}
ll nowr = n - 1;
for (int i = 0; i < q; i++) {
for (int j = nowr; j > qs[i].second; j--) {
seg.update(j, 0);
if (last[j] != -1) {
seg.update(last[j], 1);
}
}
nowr = qs[i].second;
vmp[qs[i]] = seg.get(qs[i].first, qs[i].second + 1);
}
for (int i = 0; i < q; i++) {
std::cout << vmp[ans[i]] << std::endl;
}
}
| typedef int ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
template <class Monoid> struct SegTree {
using Func = function<Monoid(Monoid, Monoid)>;
const Func F;
const Monoid UNITY;
int SIZE_R;
vector<Monoid> dat;
SegTree(int n, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
init(n);
}
void init(int n) {
SIZE_R = 1;
while (SIZE_R < n)
SIZE_R *= 2;
dat.assign(SIZE_R * 2, UNITY);
}
/* set, a is 0-indexed */
void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
void build() {
for (int k = SIZE_R - 1; k > 0; --k)
dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
}
/* update a, a is 0-indexed */
void update(int a, const Monoid &v) {
int k = a + SIZE_R;
dat[k] = v;
while (k >>= 1)
dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
}
/* get [a, b), a and b are 0-indexed */
Monoid get(int a, int b) {
Monoid vleft = UNITY, vright = UNITY;
for (int left = a + SIZE_R, right = b + SIZE_R; left < right;
left >>= 1, right >>= 1) {
if (left & 1)
vleft = F(vleft, dat[left++]);
if (right & 1)
vright = F(dat[--right], vright);
}
return F(vleft, vright);
}
inline Monoid operator[](int a) { return dat[a + SIZE_R]; }
/* debug */
void print() {
for (int i = 0; i < SIZE_R; ++i) {
cout << (*this)[i];
if (i != SIZE_R - 1)
cout << ",";
}
cout << endl;
}
};
int main() {
ll n, q;
std::cin >> n >> q;
vector<ll> c(n + 10);
for (int i = 0; i < n; i++) {
std::cin >> c[i];
c[i]--;
}
vector<pair<ll, ll>> ans(q + 10);
map<pair<ll, ll>, ll> vmp;
vector<pair<ll, ll>> qs(q + 10);
for (int i = 0; i < q; i++) {
ll l, r;
std::cin >> l >> r;
l--;
r--;
ans[i] = {l, r};
qs[i] = {l, r};
}
SegTree<ll> seg(
n + 10, [](ll a, ll b) { return a + b; }, 0);
sort(qs.begin(), qs.end(),
[](const auto le, const auto ri) { return le.second > ri.second; });
vector<ll> last(n + 10, -1);
vector<ll> doko(n + 10, -1);
for (int i = 0; i < n; i++) {
last[i] = doko[c[i]];
doko[c[i]] = i;
}
for (int i = 0; i < n; i++) {
if (doko[c[i]] == i) {
seg.update(i, 1);
}
}
ll nowr = n - 1;
for (int i = 0; i < q; i++) {
for (int j = nowr; j > qs[i].second; j--) {
seg.update(j, 0);
if (last[j] != -1) {
seg.update(last[j], 1);
}
}
nowr = qs[i].second;
vmp[qs[i]] = seg.get(qs[i].first, qs[i].second + 1);
}
for (int i = 0; i < q; i++) {
std::cout << vmp[ans[i]] << std::endl;
}
}
| replace | 68 | 69 | 68 | 69 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 5;
const int LogN = (int)20;
int L[4 * N * LogN];
int R[N * 4 * LogN];
int T[N * 4 * LogN];
int version;
int root[N * 4 * LogN];
int src[N];
unordered_map<int, int> pos;
void build(int i, int b, int e) {
if (b == e) {
T[i] = 0;
return;
}
L[i] = ++version;
R[i] = ++version;
int mid = (b + e) >> 1;
build(L[i], b, mid);
build(R[i], mid + 1, e);
}
int update(int i, int b, int e, int pos, int v) {
if (b > pos or e < pos)
return i;
int cur = ++version;
if (b == e) {
T[cur] = v;
return cur;
}
int mid = (b + e) >> 1;
L[cur] = update(L[i], b, mid, pos, v);
R[cur] = update(R[i], mid + 1, e, pos, v);
T[cur] = T[L[cur]] + T[R[cur]];
return cur;
}
int query(int i, int b, int e, int l, int r) {
if (e < l or b > r)
return 0;
if (l <= b and e <= r)
return T[i];
int mid = (b + e) >> 1;
return query(L[i], b, mid, l, r) + query(R[i], mid + 1, e, l, r);
}
int main() {
// cout << N * 4 * LogN<< '\n' ;
int test = 1, tc = 0;
// scanf("%d", &test) ;
while (test--) {
int n, q;
scanf("%d %d", &n, &q);
// memset(L, 0, sizeof L) ;
// memset(R, 0, sizeof R) ;
// memset(T, 0, sizeof T) ;
// memset(root, 0, sizeof root) ;
// memset(src, 0, sizeof src) ;
// pos.clear() ;
version = 0;
int cnt = 0;
root[0] = 0;
build(0, 1, n);
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
int k = pos[x];
if (k) {
++cnt;
root[cnt] = update(root[cnt - 1], 1, n, k, 0);
}
++cnt;
root[cnt] = update(root[cnt - 1], 1, n, i, 1);
pos[x] = i;
src[i] = cnt;
}
// printf("Case %d:\n", ++tc);
while (q--) {
int l, r;
scanf("%d %d", &l, &r);
int res = query(root[src[r]], 1, n, l, r);
printf("%d\n", res);
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = (int)5e5 + 5;
const int LogN = (int)22;
int L[4 * N * LogN];
int R[N * 4 * LogN];
int T[N * 4 * LogN];
int version;
int root[N * 4 * LogN];
int src[N];
unordered_map<int, int> pos;
void build(int i, int b, int e) {
if (b == e) {
T[i] = 0;
return;
}
L[i] = ++version;
R[i] = ++version;
int mid = (b + e) >> 1;
build(L[i], b, mid);
build(R[i], mid + 1, e);
}
int update(int i, int b, int e, int pos, int v) {
if (b > pos or e < pos)
return i;
int cur = ++version;
if (b == e) {
T[cur] = v;
return cur;
}
int mid = (b + e) >> 1;
L[cur] = update(L[i], b, mid, pos, v);
R[cur] = update(R[i], mid + 1, e, pos, v);
T[cur] = T[L[cur]] + T[R[cur]];
return cur;
}
int query(int i, int b, int e, int l, int r) {
if (e < l or b > r)
return 0;
if (l <= b and e <= r)
return T[i];
int mid = (b + e) >> 1;
return query(L[i], b, mid, l, r) + query(R[i], mid + 1, e, l, r);
}
int main() {
// cout << N * 4 * LogN<< '\n' ;
int test = 1, tc = 0;
// scanf("%d", &test) ;
while (test--) {
int n, q;
scanf("%d %d", &n, &q);
// memset(L, 0, sizeof L) ;
// memset(R, 0, sizeof R) ;
// memset(T, 0, sizeof T) ;
// memset(root, 0, sizeof root) ;
// memset(src, 0, sizeof src) ;
// pos.clear() ;
version = 0;
int cnt = 0;
root[0] = 0;
build(0, 1, n);
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
int k = pos[x];
if (k) {
++cnt;
root[cnt] = update(root[cnt - 1], 1, n, k, 0);
}
++cnt;
root[cnt] = update(root[cnt - 1], 1, n, i, 1);
pos[x] = i;
src[i] = cnt;
}
// printf("Case %d:\n", ++tc);
while (q--) {
int l, r;
scanf("%d %d", &l, &r);
int res = query(root[src[r]], 1, n, l, r);
printf("%d\n", res);
}
}
return 0;
} | replace | 3 | 5 | 3 | 5 | -11 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ctime>
#include <iostream>
using namespace std;
#define _LL long long
#define rep(i, n) for (_LL i = 0; i < (_LL)(n); i++)
#define vecrep(itr, v) for (auto itr = (v).begin(); itr != (v).end(); itr++)
template <class T> class SegTree {
private:
int _n;
int _d;
template <class U> class SNode {
public:
U _min;
U _max;
U _sum;
bool _used;
SNode() {
_min = 0;
_max = 0;
_sum = 0;
_used = false;
}
SNode(SNode<U> &t) { copy(t); }
void set(SNode<U> &t) {
_min = t._min;
_max = t._max;
_sum = t._sum;
_used = t._used;
}
void set(U v) {
_min = v;
_max = v;
_sum = v;
_used = true;
}
};
SNode<T> *nodes;
int dindex(int d) { return (1 << d); }
void evalnode(int index) {
if (index < 1)
return;
if (nodes[index * 2]._used && nodes[index * 2 + 1]._used) {
nodes[index]._min = min(nodes[index * 2]._min, nodes[index * 2 + 1]._min);
nodes[index]._max = max(nodes[index * 2]._max, nodes[index * 2 + 1]._max);
nodes[index]._sum = nodes[index * 2]._sum + nodes[index * 2 + 1]._sum;
nodes[index]._used = true;
} else if (nodes[index * 2]._used) {
nodes[index].set(nodes[index * 2]);
} else if (nodes[index * 2 + 1]._used) {
nodes[index].set(nodes[index * 2 + 1]);
}
if (nodes[index]._used)
evalnode(index / 2);
}
// 対象ノードが範囲に含まれるか -1:
// 含まれない、 0:部分的に含まれる、1:完全に含む
int check(int l, int r, int index, int d) {
int i = index - (1 << d);
int k = (1 << (_d - d - 1));
if (r <= i * k || l >= (i + 1) * k)
return -1;
if (l <= i * k && r >= (i + 1) * k)
return 1;
return 0;
}
T getminsub(int l, int r, int index, int d) {
if (d == _d - 1)
nodes[index]._min;
if (check(l, r, index, d) == 1)
return nodes[index]._min;
if (check(l, r, index * 2, d + 1) == -1)
return getminsub(l, r, index * 2 + 1, d + 1);
if (check(l, r, index * 2 + 1, d + 1) == -1)
return getminsub(l, r, index * 2, d + 1);
return min(getminsub(l, r, index * 2 + 1, d + 1),
getminsub(l, r, index * 2, d + 1));
}
T getmaxsub(int l, int r, int index, int d) {
if (d == _d - 1)
nodes[index]._max;
if (check(l, r, index, d) == 1)
return nodes[index]._max;
if (check(l, r, index * 2, d + 1) == -1)
return getmaxsub(l, r, index * 2 + 1, d + 1);
if (check(l, r, index * 2 + 1, d + 1) == -1)
return getmaxsub(l, r, index * 2, d + 1);
return max(getmaxsub(l, r, index * 2 + 1, d + 1),
getmaxsub(l, r, index * 2, d + 1));
}
T getsumsub(int l, int r, int index, int d) {
if (d == _d - 1)
return nodes[index]._sum;
if (check(l, r, index, d) == 1)
return nodes[index]._sum;
if (check(l, r, index * 2, d + 1) == -1)
return getsumsub(l, r, index * 2 + 1, d + 1);
if (check(l, r, index * 2 + 1, d + 1) == -1)
return getsumsub(l, r, index * 2, d + 1);
return getsumsub(l, r, index * 2 + 1, d + 1) +
getsumsub(l, r, index * 2, d + 1);
}
public:
SegTree(int n) {
_n = n;
_d = 1;
int bit = 1;
while (bit < _n) {
bit *= 2;
_d++;
}
nodes = new SNode<T>[bit * 2];
}
~SegTree() { delete nodes; }
void set(int i, T value) {
int index = dindex(_d - 1) + i;
nodes[index].set(value);
evalnode(index / 2);
}
// l <= i < r
T getmin(int l, int r) { return getminsub(l, r, 1, 0); }
T getmax(int l, int r) { return getmaxsub(l, r, 1, 0); }
T getsum(int l, int r) { return getsumsub(l, r, 1, 0); }
};
int main() {
int n, q;
cin >> n >> q;
int *c = new int[n];
rep(i, n) cin >> c[i];
vector<tuple<int, int, int>> lr;
rep(i, q) {
int l, r;
cin >> l >> r;
l--;
r--;
lr.emplace_back(r, l, i);
}
sort(lr.begin(), lr.end());
int *last = new int[n];
rep(i, n) last[i] = -1;
SegTree<int> st(n);
int l = 0;
int r = 0;
int *ans = new int[q];
vecrep(itr, lr) {
int tr = get<0>(*itr);
int tl = get<1>(*itr);
while (r <= tr) {
st.set(r, 1);
if (last[c[r]] >= 0) {
st.set(last[c[r]], 0);
}
last[c[r]] = r;
r++;
}
ans[get<2>(*itr)] = st.getsum(tl, r);
}
rep(i, q) { cout << ans[i] << endl; }
return 0;
} | #include <bits/stdc++.h>
#include <ctime>
#include <iostream>
using namespace std;
#define _LL long long
#define rep(i, n) for (_LL i = 0; i < (_LL)(n); i++)
#define vecrep(itr, v) for (auto itr = (v).begin(); itr != (v).end(); itr++)
template <class T> class SegTree {
private:
int _n;
int _d;
template <class U> class SNode {
public:
U _min;
U _max;
U _sum;
bool _used;
SNode() {
_min = 0;
_max = 0;
_sum = 0;
_used = false;
}
SNode(SNode<U> &t) { copy(t); }
void set(SNode<U> &t) {
_min = t._min;
_max = t._max;
_sum = t._sum;
_used = t._used;
}
void set(U v) {
_min = v;
_max = v;
_sum = v;
_used = true;
}
};
SNode<T> *nodes;
int dindex(int d) { return (1 << d); }
void evalnode(int index) {
if (index < 1)
return;
if (nodes[index * 2]._used && nodes[index * 2 + 1]._used) {
nodes[index]._min = min(nodes[index * 2]._min, nodes[index * 2 + 1]._min);
nodes[index]._max = max(nodes[index * 2]._max, nodes[index * 2 + 1]._max);
nodes[index]._sum = nodes[index * 2]._sum + nodes[index * 2 + 1]._sum;
nodes[index]._used = true;
} else if (nodes[index * 2]._used) {
nodes[index].set(nodes[index * 2]);
} else if (nodes[index * 2 + 1]._used) {
nodes[index].set(nodes[index * 2 + 1]);
}
if (nodes[index]._used)
evalnode(index / 2);
}
// 対象ノードが範囲に含まれるか -1:
// 含まれない、 0:部分的に含まれる、1:完全に含む
int check(int l, int r, int index, int d) {
int i = index - (1 << d);
int k = (1 << (_d - d - 1));
if (r <= i * k || l >= (i + 1) * k)
return -1;
if (l <= i * k && r >= (i + 1) * k)
return 1;
return 0;
}
T getminsub(int l, int r, int index, int d) {
if (d == _d - 1)
nodes[index]._min;
if (check(l, r, index, d) == 1)
return nodes[index]._min;
if (check(l, r, index * 2, d + 1) == -1)
return getminsub(l, r, index * 2 + 1, d + 1);
if (check(l, r, index * 2 + 1, d + 1) == -1)
return getminsub(l, r, index * 2, d + 1);
return min(getminsub(l, r, index * 2 + 1, d + 1),
getminsub(l, r, index * 2, d + 1));
}
T getmaxsub(int l, int r, int index, int d) {
if (d == _d - 1)
nodes[index]._max;
if (check(l, r, index, d) == 1)
return nodes[index]._max;
if (check(l, r, index * 2, d + 1) == -1)
return getmaxsub(l, r, index * 2 + 1, d + 1);
if (check(l, r, index * 2 + 1, d + 1) == -1)
return getmaxsub(l, r, index * 2, d + 1);
return max(getmaxsub(l, r, index * 2 + 1, d + 1),
getmaxsub(l, r, index * 2, d + 1));
}
T getsumsub(int l, int r, int index, int d) {
if (d == _d - 1)
return nodes[index]._sum;
if (check(l, r, index, d) == 1)
return nodes[index]._sum;
if (check(l, r, index * 2, d + 1) == -1)
return getsumsub(l, r, index * 2 + 1, d + 1);
if (check(l, r, index * 2 + 1, d + 1) == -1)
return getsumsub(l, r, index * 2, d + 1);
return getsumsub(l, r, index * 2 + 1, d + 1) +
getsumsub(l, r, index * 2, d + 1);
}
public:
SegTree(int n) {
_n = n;
_d = 1;
int bit = 1;
while (bit < _n) {
bit *= 2;
_d++;
}
nodes = new SNode<T>[bit * 2];
}
~SegTree() { delete nodes; }
void set(int i, T value) {
int index = dindex(_d - 1) + i;
nodes[index].set(value);
evalnode(index / 2);
}
// l <= i < r
T getmin(int l, int r) { return getminsub(l, r, 1, 0); }
T getmax(int l, int r) { return getmaxsub(l, r, 1, 0); }
T getsum(int l, int r) { return getsumsub(l, r, 1, 0); }
};
int main() {
int n, q;
cin >> n >> q;
int *c = new int[n];
rep(i, n) {
cin >> c[i];
c[i]--;
}
vector<tuple<int, int, int>> lr;
rep(i, q) {
int l, r;
cin >> l >> r;
l--;
r--;
lr.emplace_back(r, l, i);
}
sort(lr.begin(), lr.end());
int *last = new int[n];
rep(i, n) last[i] = -1;
SegTree<int> st(n);
int l = 0;
int r = 0;
int *ans = new int[q];
vecrep(itr, lr) {
int tr = get<0>(*itr);
int tl = get<1>(*itr);
while (r <= tr) {
st.set(r, 1);
if (last[c[r]] >= 0) {
st.set(last[c[r]], 0);
}
last[c[r]] = r;
r++;
}
ans[get<2>(*itr)] = st.getsum(tl, r);
}
rep(i, q) { cout << ans[i] << endl; }
return 0;
} | replace | 149 | 150 | 149 | 153 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 500005, INF = 1 << 30, D = 3;
int N, Q;
int A[MAX], cnt[MAX], ans[MAX];
int sum = 0, l = -1, r = -1;
struct dat {
int l;
int r;
int id;
};
vector<dat> S;
int main() {
std::ifstream in("text.txt");
std::cin.rdbuf(in.rdbuf());
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> Q;
for (int i = 0; i < N; i++)
cin >> A[i];
S.resize(Q);
for (int i = 0; i < Q; i++) {
cin >> S[i].l >> S[i].r;
S[i].id = i;
S[i].l--;
S[i].r--;
}
sort(all(S), [](auto a, auto b) {
if (a.l / D == b.l / D)
return a.r < b.r;
return a.l / D < b.l / D;
});
for (int i = 0; i < Q; i++) {
while (S[i].r < r) {
cnt[A[r]]--;
if (cnt[A[r]] == 0)
sum--;
r--;
}
while (S[i].r > r) {
cnt[A[r + 1]]++;
if (cnt[A[r + 1]] == 1)
sum++;
r++;
}
while (S[i].l < l) {
cnt[A[l - 1]]++;
if (cnt[A[l - 1]] == 1)
sum++;
l--;
}
while (S[i].l > l) {
cnt[A[l]]--;
if (cnt[A[l]] == 0)
sum--;
l++;
}
ans[S[i].id] = sum;
}
for (int i = 0; i < Q; i++)
cout << ans[i] << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 500005, INF = 1 << 30, D = 700;
int N, Q;
int A[MAX], cnt[MAX], ans[MAX];
int sum = 0, l = -1, r = -1;
struct dat {
int l;
int r;
int id;
};
vector<dat> S;
int main() {
std::ifstream in("text.txt");
std::cin.rdbuf(in.rdbuf());
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> Q;
for (int i = 0; i < N; i++)
cin >> A[i];
S.resize(Q);
for (int i = 0; i < Q; i++) {
cin >> S[i].l >> S[i].r;
S[i].id = i;
S[i].l--;
S[i].r--;
}
sort(all(S), [](auto a, auto b) {
if (a.l / D == b.l / D)
return a.r < b.r;
return a.l / D < b.l / D;
});
for (int i = 0; i < Q; i++) {
while (S[i].r < r) {
cnt[A[r]]--;
if (cnt[A[r]] == 0)
sum--;
r--;
}
while (S[i].r > r) {
cnt[A[r + 1]]++;
if (cnt[A[r + 1]] == 1)
sum++;
r++;
}
while (S[i].l < l) {
cnt[A[l - 1]]++;
if (cnt[A[l - 1]] == 1)
sum++;
l--;
}
while (S[i].l > l) {
cnt[A[l]]--;
if (cnt[A[l]] == 0)
sum--;
l++;
}
ans[S[i].id] = sum;
}
for (int i = 0; i < Q; i++)
cout << ans[i] << "\n";
}
| replace | 22 | 23 | 22 | 23 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " = " << arg1 << " || ";
__f(comma + 1, args...);
}
#else
#define debug(...)
#endif
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
Container const &x) {
os << "{ ";
for (auto &y : x)
os << y << " ; ";
return os << "}";
}
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
return os << "[ " << p.first << ", " << p.second << "]";
}
#define FAST_IO \
std::ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define sz(a) (ll)(a).size()
#define endl "\n"
// priority_queue<ll, vector<ll>, less<ll>>pq; // Max Heap
// priority_queue<ll, vector<ll>, greater<ll>>pq;
const int MAXN = 5e5 + 5;
ll n, q, a[MAXN], nx[MAXN];
vector<ll> adj[MAXN], segtree[MAXN];
void build() {
for (int i = n - 1; i > 0; i--) {
ll p = 0, q = 0;
while (p < segtree[2 * i].size() && q < segtree[2 * i + 1].size()) {
if (segtree[2 * i][p] < segtree[2 * i + 1][q])
segtree[i].pb(segtree[2 * i][p++]);
else
segtree[i].pb(segtree[2 * i + 1][q++]);
}
while (p < segtree[2 * i].size())
segtree[i].pb(segtree[2 * i][p++]);
while (q < segtree[2 * i + 1].size())
segtree[i].pb(segtree[2 * i + 1][q++]);
}
}
ll query(ll l, ll r, ll x) {
ll ans = 0;
for (l += n - 1, r += n; l < r; l /= 2, r /= 2) {
if (l % 2) {
ans += upper_bound(segtree[l].begin(), segtree[l].end(), x) -
segtree[l].begin();
l++;
}
if (r % 2) {
r--;
ans += upper_bound(segtree[r].begin(), segtree[r].end(), x) -
segtree[r].begin();
}
}
return ans;
}
int main() {
FAST_IO;
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = n - 1; i >= 0; i--) {
if (!sz(adj[a[i]]))
nx[i] = n;
else
nx[i] = adj[a[i]].back();
adj[a[i]].pb(i);
}
for (int i = 0; i < n; i++)
segtree[n + i].pb(nx[i]);
build();
while (q--) {
ll l, r;
cin >> l >> r;
cout << r - l + 1 - query(l, r, r - 1) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " = " << arg1 << " || ";
__f(comma + 1, args...);
}
#else
#define debug(...)
#endif
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
Container const &x) {
os << "{ ";
for (auto &y : x)
os << y << " ; ";
return os << "}";
}
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
return os << "[ " << p.first << ", " << p.second << "]";
}
#define FAST_IO \
std::ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define sz(a) (ll)(a).size()
#define endl "\n"
// priority_queue<ll, vector<ll>, less<ll>>pq; // Max Heap
// priority_queue<ll, vector<ll>, greater<ll>>pq;
const int MAXN = 5e5 + 5;
ll n, q, a[MAXN], nx[MAXN];
vector<ll> adj[MAXN], segtree[2 * MAXN];
void build() {
for (int i = n - 1; i > 0; i--) {
ll p = 0, q = 0;
while (p < segtree[2 * i].size() && q < segtree[2 * i + 1].size()) {
if (segtree[2 * i][p] < segtree[2 * i + 1][q])
segtree[i].pb(segtree[2 * i][p++]);
else
segtree[i].pb(segtree[2 * i + 1][q++]);
}
while (p < segtree[2 * i].size())
segtree[i].pb(segtree[2 * i][p++]);
while (q < segtree[2 * i + 1].size())
segtree[i].pb(segtree[2 * i + 1][q++]);
}
}
ll query(ll l, ll r, ll x) {
ll ans = 0;
for (l += n - 1, r += n; l < r; l /= 2, r /= 2) {
if (l % 2) {
ans += upper_bound(segtree[l].begin(), segtree[l].end(), x) -
segtree[l].begin();
l++;
}
if (r % 2) {
r--;
ans += upper_bound(segtree[r].begin(), segtree[r].end(), x) -
segtree[r].begin();
}
}
return ans;
}
int main() {
FAST_IO;
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = n - 1; i >= 0; i--) {
if (!sz(adj[a[i]]))
nx[i] = n;
else
nx[i] = adj[a[i]].back();
adj[a[i]].pb(i);
}
for (int i = 0; i < n; i++)
segtree[n + i].pb(nx[i]);
build();
while (q--) {
ll l, r;
cin >> l >> r;
cout << r - l + 1 - query(l, r, r - 1) << endl;
}
return 0;
} | replace | 50 | 51 | 50 | 51 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 500000 + 500;
const int B = 1000000 + 1000;
const int M = 200000 + 200;
int Yuyuko;
int n, m;
int b[B], a[N], block[N], ans[M], cans = 1;
struct operation {
int l, r, id;
} o[M];
inline int read() {
int a = 0;
char ch;
int f = 1;
while (!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')))
;
if (ch == '-')
f = -1;
else {
a = a * 10;
a += ch - '0';
}
while ((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')) {
a = a * 10;
a += ch - '0';
}
return a * f;
}
inline bool cmp(operation x, operation y) {
if (block[x.l] != block[y.l])
return block[x.l] < block[y.l];
else
return x.r < y.r;
}
inline void add(int x) {
if (!b[a[x]])
++cans;
++b[a[x]];
}
inline void minu(int x) {
--b[a[x]];
if (!b[a[x]])
--cans;
}
int main() {
n = read(), m = read();
Yuyuko = sqrt(n) + 1;
for (int i = 1; i <= n; ++i) {
a[i] = read();
block[i] = (i - 1) / Yuyuko + 1;
}
for (int i = 1; i <= m; ++i) {
o[i].l = read(), o[i].r = read();
o[i].id = i;
}
sort(o + 1, o + 1 + m, cmp);
int L = 1, R = 1;
b[a[1]] = 1;
for (int i = 1; i <= m; ++i) {
while (L < o[i].l)
minu(L++);
while (L > o[i].l)
add(--L);
while (R < o[i].r)
add(++R);
while (R > o[i].r)
minu(R--);
ans[o[i].id] = cans;
}
for (int i = 1; i <= m; ++i)
printf("%d\n", ans[i]);
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 500000 + 500;
const int B = 1000000 + 1000;
const int M = 500000 + 200;
int Yuyuko;
int n, m;
int b[B], a[N], block[N], ans[M], cans = 1;
struct operation {
int l, r, id;
} o[M];
inline int read() {
int a = 0;
char ch;
int f = 1;
while (!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')))
;
if (ch == '-')
f = -1;
else {
a = a * 10;
a += ch - '0';
}
while ((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')) {
a = a * 10;
a += ch - '0';
}
return a * f;
}
inline bool cmp(operation x, operation y) {
if (block[x.l] != block[y.l])
return block[x.l] < block[y.l];
else
return x.r < y.r;
}
inline void add(int x) {
if (!b[a[x]])
++cans;
++b[a[x]];
}
inline void minu(int x) {
--b[a[x]];
if (!b[a[x]])
--cans;
}
int main() {
n = read(), m = read();
Yuyuko = sqrt(n) + 1;
for (int i = 1; i <= n; ++i) {
a[i] = read();
block[i] = (i - 1) / Yuyuko + 1;
}
for (int i = 1; i <= m; ++i) {
o[i].l = read(), o[i].r = read();
o[i].id = i;
}
sort(o + 1, o + 1 + m, cmp);
int L = 1, R = 1;
b[a[1]] = 1;
for (int i = 1; i <= m; ++i) {
while (L < o[i].l)
minu(L++);
while (L > o[i].l)
add(--L);
while (R < o[i].r)
add(++R);
while (R > o[i].r)
minu(R--);
ans[o[i].id] = cans;
}
for (int i = 1; i <= m; ++i)
printf("%d\n", ans[i]);
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};
template <typename T> struct BIT {
int n;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {}
void add(int i, T x = 1) {
for (i++; i <= n; i += i & -i) {
d[i] += x;
}
}
T sum(int i) {
T x = 0;
for (i++; i; i -= i & -i) {
x += d[i];
}
return x;
}
};
int main() {
ll n, q;
cin >> n >> q;
vl c(n), last(5e5 + 10, -1);
rep(i, n) cin >> c[i];
vector<pair<P, ll>> p(q);
rep(i, q) {
ll l, r;
cin >> l >> r;
l--;
r--;
p[i] = {{r, l}, i};
}
sort(all(p));
BIT<ll> b(n + 10);
vl ans(n);
ll k = 0;
rep(i, q) {
ll l = p[i].first.second;
ll r = p[i].first.first;
ll j = p[i].second;
for (; k <= r; k++) {
ll now = last[c[k]];
last[c[k]] = k;
if (now >= 0)
b.add(now, -1);
b.add(k, 1);
}
ans[j] = b.sum(r);
if (l > 0)
ans[j] -= b.sum(l - 1);
}
rep(i, q) cout << ans[i] << '\n';
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};
template <typename T> struct BIT {
int n;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {}
void add(int i, T x = 1) {
for (i++; i <= n; i += i & -i) {
d[i] += x;
}
}
T sum(int i) {
T x = 0;
for (i++; i; i -= i & -i) {
x += d[i];
}
return x;
}
};
int main() {
ll n, q;
cin >> n >> q;
vl c(n), last(5e5 + 10, -1);
rep(i, n) cin >> c[i];
vector<pair<P, ll>> p(q);
rep(i, q) {
ll l, r;
cin >> l >> r;
l--;
r--;
p[i] = {{r, l}, i};
}
sort(all(p));
BIT<ll> b(n + 10);
vl ans(q);
ll k = 0;
rep(i, q) {
ll l = p[i].first.second;
ll r = p[i].first.first;
ll j = p[i].second;
for (; k <= r; k++) {
ll now = last[c[k]];
last[c[k]] = k;
if (now >= 0)
b.add(now, -1);
b.add(k, 1);
}
ans[j] = b.sum(r);
if (l > 0)
ans[j] -= b.sum(l - 1);
}
rep(i, q) cout << ans[i] << '\n';
} | replace | 48 | 49 | 48 | 49 | 0 | |
p02599 | C++ | Runtime Error | /* 実装例 Atcoder Beginers Contest 174-F Range Set query */
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
signed main() {
int n, q;
std::cin >> n >> q;
int sq = 2 * sqrt(n);
std::vector<int> c(n);
rep(i, n) {
std::cin >> c[i];
c[i]--;
}
std::vector<int> l(n), r(n);
rep(i, q) {
std::cin >> l[i] >> r[i];
l[i]--;
}
std::vector<int> qu(q);
rep(i, q) qu[i] = i;
std::sort(ALL(qu), [&](int i, int j) {
if (l[i] / sq != l[j] / sq)
return l[i] < l[j];
else
return r[i] < r[j];
});
std::vector<int> ans(q);
std::vector<int> cnt(n, 0);
int nl = 0, nr = 0;
int res = 0;
for (int i : qu) {
while (nl > l[i]) {
nl--;
if (cnt[c[nl]]++ == 0)
res++;
}
while (nl < l[i]) {
if (--cnt[c[nl]] == 0)
res--;
nl++;
}
while (nr < r[i]) {
if (cnt[c[nr]]++ == 0)
res++;
nr++;
}
while (nr > r[i]) {
nr--;
if (--cnt[c[nr]] == 0)
res--;
}
/* ここから結果 */
ans[i] = res;
}
rep(i, q) std::cout << ans[i] << "\n";
}
| /* 実装例 Atcoder Beginers Contest 174-F Range Set query */
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
signed main() {
int n, q;
std::cin >> n >> q;
int sq = 2 * sqrt(n);
std::vector<int> c(n);
rep(i, n) {
std::cin >> c[i];
c[i]--;
}
std::vector<int> l(q), r(q);
rep(i, q) {
std::cin >> l[i] >> r[i];
l[i]--;
}
std::vector<int> qu(q);
rep(i, q) qu[i] = i;
std::sort(ALL(qu), [&](int i, int j) {
if (l[i] / sq != l[j] / sq)
return l[i] < l[j];
else
return r[i] < r[j];
});
std::vector<int> ans(q);
std::vector<int> cnt(n, 0);
int nl = 0, nr = 0;
int res = 0;
for (int i : qu) {
while (nl > l[i]) {
nl--;
if (cnt[c[nl]]++ == 0)
res++;
}
while (nl < l[i]) {
if (--cnt[c[nl]] == 0)
res--;
nl++;
}
while (nr < r[i]) {
if (cnt[c[nr]]++ == 0)
res++;
nr++;
}
while (nr > r[i]) {
nr--;
if (--cnt[c[nr]] == 0)
res--;
}
/* ここから結果 */
ans[i] = res;
}
rep(i, q) std::cout << ans[i] << "\n";
}
| replace | 17 | 18 | 17 | 18 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define N 511111
#define A 1111111
#define BLOCK 555 // ~sqrt(N)
int cnt[A], a[N], ans[N], answer = 0;
struct node {
int L, R, i;
} q[N];
bool cmp(node x, node y) {
if (x.L / BLOCK != y.L / BLOCK) {
// different blocks, so sort by block.
return x.L / BLOCK < y.L / BLOCK;
}
// same block, so sort by R value
return x.R < y.R;
}
void add(int position) {
cnt[a[position]]++;
if (cnt[a[position]] == 1) {
answer++;
}
}
void remove(int position) {
cnt[a[position]]--;
if (cnt[a[position]] == 0) {
answer--;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < m; i++) {
cin >> q[i].L >> q[i].R;
q[i].L--;
q[i].R--;
q[i].i = i;
}
sort(q, q + m, cmp);
int currentL = 0, currentR = 0;
for (int i = 0; i < m; i++) {
int L = q[i].L, R = q[i].R;
while (currentL < L) {
remove(currentL);
currentL++;
}
while (currentL > L) {
add(currentL - 1);
currentL--;
}
while (currentR <= R) {
add(currentR);
currentR++;
}
while (currentR > R + 1) {
remove(currentR - 1);
currentR--;
}
ans[q[i].i] = answer;
}
for (int i = 0; i < m; i++)
cout << ans[i] << "\n";
} | #include <bits/stdc++.h>
using namespace std;
#define N 511111
#define A 511111
#define BLOCK 710 // ~sqrt(N)
int cnt[A], a[N], ans[N], answer = 0;
struct node {
int L, R, i;
} q[N];
bool cmp(node x, node y) {
if (x.L / BLOCK != y.L / BLOCK) {
// different blocks, so sort by block.
return x.L / BLOCK < y.L / BLOCK;
}
// same block, so sort by R value
return x.R < y.R;
}
void add(int position) {
cnt[a[position]]++;
if (cnt[a[position]] == 1) {
answer++;
}
}
void remove(int position) {
cnt[a[position]]--;
if (cnt[a[position]] == 0) {
answer--;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < m; i++) {
cin >> q[i].L >> q[i].R;
q[i].L--;
q[i].R--;
q[i].i = i;
}
sort(q, q + m, cmp);
int currentL = 0, currentR = 0;
for (int i = 0; i < m; i++) {
int L = q[i].L, R = q[i].R;
while (currentL < L) {
remove(currentL);
currentL++;
}
while (currentL > L) {
add(currentL - 1);
currentL--;
}
while (currentR <= R) {
add(currentR);
currentR++;
}
while (currentR > R + 1) {
remove(currentR - 1);
currentR--;
}
ans[q[i].i] = answer;
}
for (int i = 0; i < m; i++)
cout << ans[i] << "\n";
} | replace | 4 | 6 | 4 | 6 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int MAX_N = 262144;
vector<int64_t> seg(MAX_N * 2 - 1);
int64_t qu(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) {
return 0;
} else if (a <= l && r <= b) {
return seg[k];
} else {
int64_t X = qu(a, b, k * 2 + 1, l, (l + r) / 2);
int64_t Y = qu(a, b, k * 2 + 2, (l + r) / 2, r);
return X + Y;
}
}
void pu(int64_t a, int b) {
int D = MAX_N - 1 + b;
while (D > 0) {
seg[D] += a;
D--;
D /= 2;
}
seg[0] += a;
}
int main() {
int N, Q;
cin >> N >> Q;
vector<int> p(N);
for (int i = 0; i < N; i++) {
cin >> p[i];
p[i]--;
}
vector<tuple<int, int, int>> q(Q);
for (int i = 0; i < Q; i++) {
int a, b;
cin >> a >> b;
q[i] = make_tuple(b, a, i);
}
sort(q.begin(), q.end());
vector<int> la(N, -1);
vector<int> an(Q);
int W = 0;
for (int i = 0; i < Q; i++) {
int a, b, c;
tie(a, b, c) = q[i];
while (W < a) {
if (la[p[W]] != -1) {
pu(-1, la[p[W]]);
}
la[p[W]] = W;
pu(1, W);
W++;
}
an[c] = qu(b - 1, a, 0, 0, MAX_N);
}
for (int i = 0; i < Q; i++) {
cout << an[i] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int MAX_N = 262144 * 2;
vector<int64_t> seg(MAX_N * 2 - 1);
int64_t qu(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) {
return 0;
} else if (a <= l && r <= b) {
return seg[k];
} else {
int64_t X = qu(a, b, k * 2 + 1, l, (l + r) / 2);
int64_t Y = qu(a, b, k * 2 + 2, (l + r) / 2, r);
return X + Y;
}
}
void pu(int64_t a, int b) {
int D = MAX_N - 1 + b;
while (D > 0) {
seg[D] += a;
D--;
D /= 2;
}
seg[0] += a;
}
int main() {
int N, Q;
cin >> N >> Q;
vector<int> p(N);
for (int i = 0; i < N; i++) {
cin >> p[i];
p[i]--;
}
vector<tuple<int, int, int>> q(Q);
for (int i = 0; i < Q; i++) {
int a, b;
cin >> a >> b;
q[i] = make_tuple(b, a, i);
}
sort(q.begin(), q.end());
vector<int> la(N, -1);
vector<int> an(Q);
int W = 0;
for (int i = 0; i < Q; i++) {
int a, b, c;
tie(a, b, c) = q[i];
while (W < a) {
if (la[p[W]] != -1) {
pu(-1, la[p[W]]);
}
la[p[W]] = W;
pu(1, W);
W++;
}
an[c] = qu(b - 1, a, 0, 0, MAX_N);
}
for (int i = 0; i < Q; i++) {
cout << an[i] << endl;
}
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define val second
#define N 100000000
#define fo(i, m, n) for (int i = m; i < n; i++)
#define M 7
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
using namespace std;
using ll = long long;
using ul = unsigned long long;
using pii = pair<int, int>;
using vvi = vector<vector<int>>;
using vi = vector<int>;
using pll = pair<ll, ll>;
using vsi = vector<set<int>>;
using vll = vector<ll>;
ll power(ll a, ll n) {
ll j = 0;
ll x = 1;
while (1ll << j <= n) {
if (1ll << j & n)
x = (x * a) % mod;
a = a * a % mod;
j++;
}
return x;
}
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int lb(vi &a, int x) {
int n = a.size();
int l = 0;
int r = n - 1;
int ind = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (x >= a[mid]) {
l = mid + 1;
ind = mid;
} else
r = mid - 1;
}
return ind + 1;
}
int up(vll &a, ll x) {
int n = a.size();
int l = 0;
int r = n - 1;
int ind = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (a[mid] > x) {
r = mid - 1;
ind = mid;
} else
l = mid + 1;
}
return ind;
}
ll max(ll a, ll b) {
if (a > b)
return a;
else
return b;
}
ll min(ll a, ll b) {
if (a > b)
return b;
else
return a;
}
template <typename T> void pra(T a[], int n) {
fo(i, 0, n) cout << a[i] << ' ';
cout << endl;
}
int b;
bool csf(pair<pii, int> &x, pair<pii, int> &y) {
if (x.first.first / b == y.first.first / b)
return x.first.second < y.first.second;
return x.first.first / b < y.first.first / b;
}
vi c;
void ext(int v[], int &cnt, int l, int r, int cl, int cr) {
for (int i = cl - 1; i >= l; i--) {
v[c[i]]++;
if (v[c[i]] == 1)
cnt++;
}
for (int i = cr + 1; i <= r; i++) {
// cout<<i<<' ';
v[c[i]]++;
if (v[c[i]] == 1)
cnt++;
}
for (int i = cl; i < l; i++) {
v[c[i]]--;
if (v[c[i]] == 0)
cnt--;
}
}
int main() {
fast_io int n, q;
cin >> n >> q;
c = vi(n + 1);
fo(i, 1, n + 1) cin >> c[i];
b = sqrt(q) * 100;
vector<pair<pii, int>> queries;
fo(i, 0, q) {
int l, r;
cin >> l >> r;
queries.push_back({{l, r}, i});
}
sort(queries.begin(), queries.end(), csf);
int v[n + 1];
int cnt = 0;
int cl = 0;
int cr = 0;
fill(v, v + n + 1, 0);
vi ans(q);
fo(i, 0, q) {
int l = queries[i].first.first;
int r = queries[i].first.second;
int ind = queries[i].second;
if (r < cr) {
fill(v, v + n + 1, 0);
cl = l;
cr = l - 1;
cnt = 0;
}
ext(v, cnt, l, r, cl, cr);
cl = l;
cr = r;
// cout<<cnt<<' '<<ind<<endl;
ans[ind] = cnt;
}
fo(i, 0, q) cout << ans[i] << endl;
}
| #include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define val second
#define N 100000000
#define fo(i, m, n) for (int i = m; i < n; i++)
#define M 7
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
using namespace std;
using ll = long long;
using ul = unsigned long long;
using pii = pair<int, int>;
using vvi = vector<vector<int>>;
using vi = vector<int>;
using pll = pair<ll, ll>;
using vsi = vector<set<int>>;
using vll = vector<ll>;
ll power(ll a, ll n) {
ll j = 0;
ll x = 1;
while (1ll << j <= n) {
if (1ll << j & n)
x = (x * a) % mod;
a = a * a % mod;
j++;
}
return x;
}
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int lb(vi &a, int x) {
int n = a.size();
int l = 0;
int r = n - 1;
int ind = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (x >= a[mid]) {
l = mid + 1;
ind = mid;
} else
r = mid - 1;
}
return ind + 1;
}
int up(vll &a, ll x) {
int n = a.size();
int l = 0;
int r = n - 1;
int ind = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (a[mid] > x) {
r = mid - 1;
ind = mid;
} else
l = mid + 1;
}
return ind;
}
ll max(ll a, ll b) {
if (a > b)
return a;
else
return b;
}
ll min(ll a, ll b) {
if (a > b)
return b;
else
return a;
}
template <typename T> void pra(T a[], int n) {
fo(i, 0, n) cout << a[i] << ' ';
cout << endl;
}
int b;
bool csf(pair<pii, int> &x, pair<pii, int> &y) {
if (x.first.first / b == y.first.first / b)
return x.first.second < y.first.second;
return x.first.first / b < y.first.first / b;
}
vi c;
void ext(int v[], int &cnt, int l, int r, int cl, int cr) {
for (int i = cl - 1; i >= l; i--) {
v[c[i]]++;
if (v[c[i]] == 1)
cnt++;
}
for (int i = cr + 1; i <= r; i++) {
// cout<<i<<' ';
v[c[i]]++;
if (v[c[i]] == 1)
cnt++;
}
for (int i = cl; i < l; i++) {
v[c[i]]--;
if (v[c[i]] == 0)
cnt--;
}
}
int main() {
fast_io int n, q;
cin >> n >> q;
c = vi(n + 1);
fo(i, 1, n + 1) cin >> c[i];
b = sqrt(q) + 500;
vector<pair<pii, int>> queries;
fo(i, 0, q) {
int l, r;
cin >> l >> r;
queries.push_back({{l, r}, i});
}
sort(queries.begin(), queries.end(), csf);
int v[n + 1];
int cnt = 0;
int cl = 0;
int cr = 0;
fill(v, v + n + 1, 0);
vi ans(q);
fo(i, 0, q) {
int l = queries[i].first.first;
int r = queries[i].first.second;
int ind = queries[i].second;
if (r < cr) {
fill(v, v + n + 1, 0);
cl = l;
cr = l - 1;
cnt = 0;
}
ext(v, cnt, l, r, cl, cr);
cl = l;
cr = r;
// cout<<cnt<<' '<<ind<<endl;
ans[ind] = cnt;
}
fo(i, 0, q) cout << ans[i] << endl;
}
| replace | 132 | 133 | 132 | 133 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
int n, q, pos;
int a[30002], tree[4 * 30002], idx[1000002], ans[200002];
pair<int, pair<int, int>> qy[200002];
bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
return a.ss.ff < b.ss.ff;
}
void update(ll pos, ll l, ll r, ll idx, ll value) {
if (l == r && l == idx) {
tree[pos] = value;
return;
}
ll mid = (l + r) / 2;
if (idx >= l && idx <= mid)
update(pos * 2, l, mid, idx, value);
if (idx >= (mid + 1) && idx <= r)
update(pos * 2 + 1, mid + 1, r, idx, value);
tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];
}
ll query(ll pos, ll l, ll r, ll L, ll R) {
if (l >= L && r <= R)
return tree[pos];
if (r < L || l > R)
return 0;
ll mid = (l + r) / 2;
ll q1 = query(pos * 2, l, mid, L, R);
ll q2 = query(pos * 2 + 1, mid + 1, r, L, R);
return q1 + q2;
}
int main() {
scanf("%d %d", &n, &q);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 0; i < q; i++) {
scanf("%d %d", &qy[i].ff, &qy[i].ss.ff);
qy[i].ss.ss = i;
}
sort(qy, qy + q, cmp);
pos = 1;
for (int i = 0; i < q; i++) {
while (pos <= qy[i].ss.ff) {
int x = a[pos];
if (idx[x])
update(1, 1, n, idx[x], 0);
update(1, 1, n, pos, 1);
idx[x] = pos;
pos++;
}
ans[qy[i].ss.ss] = query(1, 1, n, qy[i].ff, qy[i].ss.ff);
}
for (int i = 0; i < q; i++)
printf("%d\n", ans[i]);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
int n, q, pos;
int a[500002], tree[4 * 500002], idx[500002], ans[500002];
pair<int, pair<int, int>> qy[500002];
bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
return a.ss.ff < b.ss.ff;
}
void update(ll pos, ll l, ll r, ll idx, ll value) {
if (l == r && l == idx) {
tree[pos] = value;
return;
}
ll mid = (l + r) / 2;
if (idx >= l && idx <= mid)
update(pos * 2, l, mid, idx, value);
if (idx >= (mid + 1) && idx <= r)
update(pos * 2 + 1, mid + 1, r, idx, value);
tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];
}
ll query(ll pos, ll l, ll r, ll L, ll R) {
if (l >= L && r <= R)
return tree[pos];
if (r < L || l > R)
return 0;
ll mid = (l + r) / 2;
ll q1 = query(pos * 2, l, mid, L, R);
ll q2 = query(pos * 2 + 1, mid + 1, r, L, R);
return q1 + q2;
}
int main() {
scanf("%d %d", &n, &q);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 0; i < q; i++) {
scanf("%d %d", &qy[i].ff, &qy[i].ss.ff);
qy[i].ss.ss = i;
}
sort(qy, qy + q, cmp);
pos = 1;
for (int i = 0; i < q; i++) {
while (pos <= qy[i].ss.ff) {
int x = a[pos];
if (idx[x])
update(1, 1, n, idx[x], 0);
update(1, 1, n, pos, 1);
idx[x] = pos;
pos++;
}
ans[qy[i].ss.ss] = query(1, 1, n, qy[i].ff, qy[i].ss.ff);
}
for (int i = 0; i < q; i++)
printf("%d\n", ans[i]);
return 0;
}
| replace | 8 | 10 | 8 | 10 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const unsigned int BIT_ARRAY_SIZE = 100005;
ll BIT_ARRAY[BIT_ARRAY_SIZE];
int l = BIT_ARRAY_SIZE;
void BIT_update(int i, ll val) {
while (i <= l) {
BIT_ARRAY[i] += val;
i = i + (i & (-i));
}
}
ll BIT_query(int i) {
ll s = 0;
while (i > 0) {
s += BIT_ARRAY[i];
i = i - (i & (-i));
}
return s;
}
int lastpos[BIT_ARRAY_SIZE * 10];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, x, y, q;
cin >> n >> q;
vector<int> v(n + 1);
for (int i = 1; i <= n; i++)
cin >> v[i];
vector<pair<pair<int, int>, int>> Q;
vector<int> ans(q + 1);
for (int i = 1; i <= q; i++) {
cin >> x >> y;
Q.push_back(make_pair(make_pair(y, x), i));
}
sort(Q.begin(), Q.end());
int pos = 1;
for (int i = 0; i < q; i++) {
while (pos <= Q[i].first.first) {
if (lastpos[v[pos]] != 0)
BIT_update(lastpos[v[pos]], -1);
BIT_update(pos, 1);
lastpos[v[pos]] = pos;
pos++;
}
ans[Q[i].second] =
BIT_query(Q[i].first.first) - BIT_query(Q[i].first.second - 1);
}
for (int i = 1; i <= q; i++)
cout << ans[i] << "\n";
} | #include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const unsigned int BIT_ARRAY_SIZE = 500005;
ll BIT_ARRAY[BIT_ARRAY_SIZE];
int l = BIT_ARRAY_SIZE;
void BIT_update(int i, ll val) {
while (i <= l) {
BIT_ARRAY[i] += val;
i = i + (i & (-i));
}
}
ll BIT_query(int i) {
ll s = 0;
while (i > 0) {
s += BIT_ARRAY[i];
i = i - (i & (-i));
}
return s;
}
int lastpos[BIT_ARRAY_SIZE * 10];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, x, y, q;
cin >> n >> q;
vector<int> v(n + 1);
for (int i = 1; i <= n; i++)
cin >> v[i];
vector<pair<pair<int, int>, int>> Q;
vector<int> ans(q + 1);
for (int i = 1; i <= q; i++) {
cin >> x >> y;
Q.push_back(make_pair(make_pair(y, x), i));
}
sort(Q.begin(), Q.end());
int pos = 1;
for (int i = 0; i < q; i++) {
while (pos <= Q[i].first.first) {
if (lastpos[v[pos]] != 0)
BIT_update(lastpos[v[pos]], -1);
BIT_update(pos, 1);
lastpos[v[pos]] = pos;
pos++;
}
ans[Q[i].second] =
BIT_query(Q[i].first.first) - BIT_query(Q[i].first.second - 1);
}
for (int i = 1; i <= q; i++)
cout << ans[i] << "\n";
} | replace | 24 | 25 | 24 | 25 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int dx[] = {0, 1, -1, 0, 1, -1, 1, -1}; // i<4:4way i<8:8way
int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};
const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);
template <typename T> struct BIT {
int n;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {}
void add(int i, T x = 1) {
for (i++; i <= n; i += i & -i) {
d[i] += x;
}
}
T sum(int i) {
T x = 0;
for (i++; i; i -= i & -i) {
x += d[i];
}
return x;
}
// 下の2個の操作を使う時は上の2個の操作が使えない事に注意
T getmax(int i) { // get max(d[i]) for i = 0,1,...,i-1,i
T x = 0;
for (i++; i; i -= i & -i) {
x = max(x, d[i]);
}
return x;
}
void setmax(int i, int v) { // set d[i] = max(d[i], v)
for (i++; i <= n; i += i & -i) {
d[i] = max(d[i], v);
}
}
};
vector<int> pos(500005, -1);
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n, q;
cin >> n >> q;
BIT<int> bit(500005);
vector<int> c(n), ans(q);
rep(i, n) { cin >> c[i]; }
vector<tuple<int, int, int>> p;
rep(i, q) {
int l, r;
cin >> l >> r;
p.push_back({l, r, i});
}
sort(ALL(p), [](auto a, auto b) { return get<1>(a) < get<1>(b); });
int nowpos = 0;
rep(i, q) {
int ret = 0;
int l = get<0>(p[i]);
int r = get<1>(p[i]);
for (int j = nowpos; j < r; j++) {
int now = c[j];
if (pos[now] == -1) {
pos[now] = j;
} else {
bit.add(pos[now] + 1, -1);
pos[now] = j;
}
bit.add(j + 1, 1);
}
ans[get<2>(p[i])] = bit.sum(r) - bit.sum(l - 1);
}
for (int i = 0; i < q; i++)
cout << ans[i] << endl;
} | #include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int dx[] = {0, 1, -1, 0, 1, -1, 1, -1}; // i<4:4way i<8:8way
int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};
const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);
template <typename T> struct BIT {
int n;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {}
void add(int i, T x = 1) {
for (i++; i <= n; i += i & -i) {
d[i] += x;
}
}
T sum(int i) {
T x = 0;
for (i++; i; i -= i & -i) {
x += d[i];
}
return x;
}
// 下の2個の操作を使う時は上の2個の操作が使えない事に注意
T getmax(int i) { // get max(d[i]) for i = 0,1,...,i-1,i
T x = 0;
for (i++; i; i -= i & -i) {
x = max(x, d[i]);
}
return x;
}
void setmax(int i, int v) { // set d[i] = max(d[i], v)
for (i++; i <= n; i += i & -i) {
d[i] = max(d[i], v);
}
}
};
vector<int> pos(500005, -1);
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n, q;
cin >> n >> q;
BIT<int> bit(500005);
vector<int> c(n), ans(q);
rep(i, n) { cin >> c[i]; }
vector<tuple<int, int, int>> p;
rep(i, q) {
int l, r;
cin >> l >> r;
p.push_back({l, r, i});
}
sort(ALL(p), [](auto a, auto b) { return get<1>(a) < get<1>(b); });
int nowpos = 0;
rep(i, q) {
int ret = 0;
int l = get<0>(p[i]);
int r = get<1>(p[i]);
for (int j = nowpos; j < r; j++) {
int now = c[j];
if (pos[now] == -1) {
pos[now] = j;
} else {
bit.add(pos[now] + 1, -1);
pos[now] = j;
}
bit.add(j + 1, 1);
}
nowpos = r;
ans[get<2>(p[i])] = bit.sum(r) - bit.sum(l - 1);
}
for (int i = 0; i < q; i++)
cout << ans[i] << endl;
} | insert | 92 | 92 | 92 | 93 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
ll n;
ll dat[200005];
void init(ll n_) {
n = 1;
while (n <= n_)
n *= 2;
}
void update(ll k, ll a) {
k += n - 1;
dat[k] = a;
while (0 < k) {
k = (k - 1) / 2;
dat[k] = (dat[2 * k + 1] + dat[2 * k + 2]);
}
}
ll query(ll a, ll b, ll k, ll l, ll r) {
if (a <= l && r <= b)
return dat[k];
else if (r <= a || b <= l)
return 0;
else {
ll val = query(a, b, 2 * k + 1, l, (l + r) / 2);
ll var = query(a, b, 2 * k + 2, (l + r) / 2, r);
return (val + var);
}
}
ll N, Q;
int main() {
cin >> N >> Q;
init(N + 2);
vector<ll> c(N);
for (int i = 0; i < N; i++)
cin >> c.at(i);
vector<pair<P, ll>> A(Q);
for (int i = 0; i < Q; i++) {
ll l, r;
cin >> l >> r;
P p(r, l);
pair<P, ll> p1(p, i);
A[i] = p1;
}
sort(A.begin(), A.end());
vector<ll> ans(Q);
map<ll, ll> M;
ll I = 0;
for (int i = 0; i < N; i++) {
if (!M.count(c[i])) {
M[c[i]] = i;
update(i, 1);
} else {
update(M.at(c[i]), 0);
M.at(c[i]) = i;
update(i, 1);
}
while (A[I].first.first == i + 1) {
ll l = A[I].first.second;
ll r = A[I].first.first;
ll j = A[I].second;
ll a = query(l - 1, r, 0, 0, n);
ans[j] = a;
I++;
}
}
for (int i = 0; i < Q; i++)
cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
ll n;
ll dat[2000005];
void init(ll n_) {
n = 1;
while (n <= n_)
n *= 2;
}
void update(ll k, ll a) {
k += n - 1;
dat[k] = a;
while (0 < k) {
k = (k - 1) / 2;
dat[k] = (dat[2 * k + 1] + dat[2 * k + 2]);
}
}
ll query(ll a, ll b, ll k, ll l, ll r) {
if (a <= l && r <= b)
return dat[k];
else if (r <= a || b <= l)
return 0;
else {
ll val = query(a, b, 2 * k + 1, l, (l + r) / 2);
ll var = query(a, b, 2 * k + 2, (l + r) / 2, r);
return (val + var);
}
}
ll N, Q;
int main() {
cin >> N >> Q;
init(N + 2);
vector<ll> c(N);
for (int i = 0; i < N; i++)
cin >> c.at(i);
vector<pair<P, ll>> A(Q);
for (int i = 0; i < Q; i++) {
ll l, r;
cin >> l >> r;
P p(r, l);
pair<P, ll> p1(p, i);
A[i] = p1;
}
sort(A.begin(), A.end());
vector<ll> ans(Q);
map<ll, ll> M;
ll I = 0;
for (int i = 0; i < N; i++) {
if (!M.count(c[i])) {
M[c[i]] = i;
update(i, 1);
} else {
update(M.at(c[i]), 0);
M.at(c[i]) = i;
update(i, 1);
}
while (A[I].first.first == i + 1) {
ll l = A[I].first.second;
ll r = A[I].first.first;
ll j = A[I].second;
ll a = query(l - 1, r, 0, 0, n);
ans[j] = a;
I++;
}
}
for (int i = 0; i < Q; i++)
cout << ans[i] << endl;
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
using namespace std;
const int maxn = 300005;
struct node {
int l, r, sum;
} tree[maxn * 4];
struct ques {
int l, r, index;
} ask[maxn];
int a[maxn];
int ans[maxn];
void build(int i, int left, int right) {
tree[i].l = left, tree[i].r = right;
tree[i].sum = 0;
if (left == right)
return;
int mid = left + right >> 1;
build(i << 1, left, mid);
build(i << 1 | 1, mid + 1, right);
}
map<int, int> has;
void update(int i, int pos, int w) {
if (tree[i].l == pos && tree[i].r == pos) {
tree[i].sum += w;
return;
}
int mid = (tree[i].l + tree[i].r) >> 1;
if (pos <= mid)
update(i << 1, pos, w);
else
update(i << 1 | 1, pos, w);
tree[i].sum = tree[i << 1].sum + tree[i << 1 | 1].sum;
}
int query(int i, int left, int right) {
if (tree[i].l == left && tree[i].r == right) {
return tree[i].sum;
}
int mid = (tree[i].l + tree[i].r) >> 1;
if (right <= mid)
return query(i << 1, left, right);
else if (left > mid)
return query(i << 1 | 1, left, right);
else
return query(i << 1, left, mid) + query(i << 1 | 1, mid + 1, right);
}
bool cmp(ques a, ques b) { return a.r < b.r; }
int main() {
int n;
scanf("%d", &n);
int m;
scanf("%d", &m);
build(1, 1, n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) {
ask[i].index = i;
scanf("%d%d", &ask[i].l, &ask[i].r);
}
sort(ask + 1, ask + 1 + m, cmp);
int cur = 1;
for (int i = 1; i <= m; i++) {
for (int j = cur; j <= ask[i].r; j++) {
if (has.find(a[j]) != has.end()) {
int p = has[a[j]];
update(1, p, -1);
}
update(1, j, 1);
has[a[j]] = j;
}
cur = ask[i].r + 1;
ans[ask[i].index] = query(1, ask[i].l, ask[i].r);
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
} | #include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
using namespace std;
const int maxn = 500005;
struct node {
int l, r, sum;
} tree[maxn * 4];
struct ques {
int l, r, index;
} ask[maxn];
int a[maxn];
int ans[maxn];
void build(int i, int left, int right) {
tree[i].l = left, tree[i].r = right;
tree[i].sum = 0;
if (left == right)
return;
int mid = left + right >> 1;
build(i << 1, left, mid);
build(i << 1 | 1, mid + 1, right);
}
map<int, int> has;
void update(int i, int pos, int w) {
if (tree[i].l == pos && tree[i].r == pos) {
tree[i].sum += w;
return;
}
int mid = (tree[i].l + tree[i].r) >> 1;
if (pos <= mid)
update(i << 1, pos, w);
else
update(i << 1 | 1, pos, w);
tree[i].sum = tree[i << 1].sum + tree[i << 1 | 1].sum;
}
int query(int i, int left, int right) {
if (tree[i].l == left && tree[i].r == right) {
return tree[i].sum;
}
int mid = (tree[i].l + tree[i].r) >> 1;
if (right <= mid)
return query(i << 1, left, right);
else if (left > mid)
return query(i << 1 | 1, left, right);
else
return query(i << 1, left, mid) + query(i << 1 | 1, mid + 1, right);
}
bool cmp(ques a, ques b) { return a.r < b.r; }
int main() {
int n;
scanf("%d", &n);
int m;
scanf("%d", &m);
build(1, 1, n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) {
ask[i].index = i;
scanf("%d%d", &ask[i].l, &ask[i].r);
}
sort(ask + 1, ask + 1 + m, cmp);
int cur = 1;
for (int i = 1; i <= m; i++) {
for (int j = cur; j <= ask[i].r; j++) {
if (has.find(a[j]) != has.end()) {
int p = has[a[j]];
update(1, p, -1);
}
update(1, j, 1);
has[a[j]] = j;
}
cur = ask[i].r + 1;
ans[ask[i].index] = query(1, ask[i].l, ask[i].r);
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define dofloat cout << fixed << setprecision(8)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define bitcount __builtin_popcount
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<int, int>> vii;
typedef vector<int> vi;
typedef pair<int, int> ii;
const long long MOD = 1000000007;
const long long MAX = 100005;
const long double PI = 3.14159265359;
const long double G = 9.807;
const long long INF = 1e18;
const long double EPS = 1e-6;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
bool isprime(ll a) {
if (a == 2)
return 1;
if (!(a & 1))
return 0;
for (ll i = 3; i * i <= a; i += 2)
if (a % i == 0)
return 0;
return 1;
}
ll mpow(ll base, ll exponent, ll modulus) {
if (modulus == 1)
return 0;
long long result = 1;
base = base % modulus;
while (exponent) {
if (exponent % 2 == 1)
result = (result * base) % modulus;
exponent = exponent >> 1;
base = (base * base) % modulus;
}
return result;
}
ll minv(ll a, ll mod) {
ll _gcd = gcd(a, mod);
assert(_gcd == 1);
return mpow(a, mod - 2, mod);
}
/*
ll ncr(ll N,ll K){
if(N<K)return 0;
if(K==0)return 1;
if(N==0)return 0;
ll den=1;
for(ll i=1;i<=K;i++)den*=i;
ll num=1;
while(K--){
num*=N;
if(num%den==0){
num/=den;
den=1;
}
N--;
}
return num;
}
*/
const int BZ = 725;
int n, m, a[5 * MAX], ans[5 * MAX], f[5 * MAX];
int cur;
void take(int x) {
if (!f[a[x]])
++cur;
++f[a[x]];
return;
}
void drop(int x) {
--f[a[x]];
if (!f[a[x]])
--cur;
return;
}
int omg(pair<ii, int> x, pair<ii, int> y) {
ii A = x.first;
ii B = y.first;
int L1 = A.first, R1 = A.second;
int L2 = B.first, R2 = B.second;
L1 /= BZ;
L2 /= BZ;
if (L1 != L2)
return L1 < L2;
return R1 < R2;
}
int main() {
FAST;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
vector<pair<ii, int>> qs;
for (int i = 1; i <= m; ++i) {
int l, r;
cin >> l >> r;
qs.pb({{l, r}, i});
}
sort(all(qs), omg);
int mol = 1, mor = 0;
for (int i = 0; i < m; ++i) {
ii p = qs[i].first;
int idx = qs[i].second;
int l = p.first, r = p.second;
while (mor < r) {
take(++mor);
}
while (mor > r) {
drop(mor--);
}
while (mol < l) {
drop(mol++);
}
while (mol > l) {
take(--mol);
}
ans[idx] = cur;
}
for (int i = 1; i <= m; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
/*
WA? check edge cases, N=1,M=1 etc. Read code fully once and check all
variables. Read the question again and see what you might have missed. Consider
changing your solution or find more cases.
*/
| #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define dofloat cout << fixed << setprecision(8)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define bitcount __builtin_popcount
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<int, int>> vii;
typedef vector<int> vi;
typedef pair<int, int> ii;
const long long MOD = 1000000007;
const long long MAX = 100005;
const long double PI = 3.14159265359;
const long double G = 9.807;
const long long INF = 1e18;
const long double EPS = 1e-6;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
bool isprime(ll a) {
if (a == 2)
return 1;
if (!(a & 1))
return 0;
for (ll i = 3; i * i <= a; i += 2)
if (a % i == 0)
return 0;
return 1;
}
ll mpow(ll base, ll exponent, ll modulus) {
if (modulus == 1)
return 0;
long long result = 1;
base = base % modulus;
while (exponent) {
if (exponent % 2 == 1)
result = (result * base) % modulus;
exponent = exponent >> 1;
base = (base * base) % modulus;
}
return result;
}
ll minv(ll a, ll mod) {
ll _gcd = gcd(a, mod);
assert(_gcd == 1);
return mpow(a, mod - 2, mod);
}
/*
ll ncr(ll N,ll K){
if(N<K)return 0;
if(K==0)return 1;
if(N==0)return 0;
ll den=1;
for(ll i=1;i<=K;i++)den*=i;
ll num=1;
while(K--){
num*=N;
if(num%den==0){
num/=den;
den=1;
}
N--;
}
return num;
}
*/
const int BZ = 1000;
int n, m, a[5 * MAX], ans[5 * MAX], f[5 * MAX];
int cur;
void take(int x) {
if (!f[a[x]])
++cur;
++f[a[x]];
return;
}
void drop(int x) {
--f[a[x]];
if (!f[a[x]])
--cur;
return;
}
int omg(pair<ii, int> x, pair<ii, int> y) {
ii A = x.first;
ii B = y.first;
int L1 = A.first, R1 = A.second;
int L2 = B.first, R2 = B.second;
L1 /= BZ;
L2 /= BZ;
if (L1 != L2)
return L1 < L2;
return R1 < R2;
}
int main() {
FAST;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
vector<pair<ii, int>> qs;
for (int i = 1; i <= m; ++i) {
int l, r;
cin >> l >> r;
qs.pb({{l, r}, i});
}
sort(all(qs), omg);
int mol = 1, mor = 0;
for (int i = 0; i < m; ++i) {
ii p = qs[i].first;
int idx = qs[i].second;
int l = p.first, r = p.second;
while (mor < r) {
take(++mor);
}
while (mor > r) {
drop(mor--);
}
while (mol < l) {
drop(mol++);
}
while (mol > l) {
take(--mol);
}
ans[idx] = cur;
}
for (int i = 1; i <= m; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
/*
WA? check edge cases, N=1,M=1 etc. Read code fully once and check all
variables. Read the question again and see what you might have missed. Consider
changing your solution or find more cases.
*/
| replace | 77 | 78 | 77 | 78 | TLE | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 500005;
struct Node {
int sum;
int lson, rson;
} a[maxn * 30];
int tot = 1;
void buildtree(int id, int l, int r) {
if (l == r)
return;
a[id].lson = ++tot;
a[id].rson = ++tot;
int mid = (l + r) / 2;
buildtree(a[id].lson, l, mid);
buildtree(a[id].rson, mid + 1, r);
}
int change(int id, int l, int r, int x, int dl) {
// cout<<"change "<<endl;
int pos = ++tot;
a[pos] = a[id];
// cout<<"sum "<<pos<<' '<<a[pos].sum<<endl;
if (l == r) {
a[pos].sum += x;
return pos;
}
int mid = (l + r) / 2;
if (dl <= mid)
a[pos].lson = change(a[id].lson, l, mid, x, dl);
else
a[pos].rson = change(a[id].rson, mid + 1, r, x, dl);
a[pos].sum = a[a[pos].lson].sum + a[a[pos].rson].sum;
return pos;
}
int query(int id, int l, int r, int dl) {
// cout<<"query "<<min_<<' '<<max_<<' '<<l<<' '<<r<<' '<<x<<endl;
if (l == r)
return a[id].sum;
int mid = (l + r) / 2;
if (dl <= mid)
return a[a[id].rson].sum + query(a[id].lson, l, mid, dl);
return query(a[id].rson, mid + 1, r, dl);
}
int n, m;
int d[maxn];
int book[maxn], rk[maxn];
int belong[maxn];
int main() {
int maxd;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> d[i];
}
buildtree(rk[0], 1, n);
for (int i = 1; i <= n; ++i) {
if (belong[d[i]]) {
int t = change(rk[i - 1], 1, n, -1, belong[d[i]]);
rk[i] = change(t, 1, n, 1, i);
} else {
rk[i] = change(rk[i - 1], 1, n, 1, i);
}
belong[d[i]] = i;
}
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
cout << query(rk[r], 1, n, l) << endl;
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 500005;
struct Node {
int sum;
int lson, rson;
} a[maxn * 60];
int tot = 1;
void buildtree(int id, int l, int r) {
if (l == r)
return;
a[id].lson = ++tot;
a[id].rson = ++tot;
int mid = (l + r) / 2;
buildtree(a[id].lson, l, mid);
buildtree(a[id].rson, mid + 1, r);
}
int change(int id, int l, int r, int x, int dl) {
// cout<<"change "<<endl;
int pos = ++tot;
a[pos] = a[id];
// cout<<"sum "<<pos<<' '<<a[pos].sum<<endl;
if (l == r) {
a[pos].sum += x;
return pos;
}
int mid = (l + r) / 2;
if (dl <= mid)
a[pos].lson = change(a[id].lson, l, mid, x, dl);
else
a[pos].rson = change(a[id].rson, mid + 1, r, x, dl);
a[pos].sum = a[a[pos].lson].sum + a[a[pos].rson].sum;
return pos;
}
int query(int id, int l, int r, int dl) {
// cout<<"query "<<min_<<' '<<max_<<' '<<l<<' '<<r<<' '<<x<<endl;
if (l == r)
return a[id].sum;
int mid = (l + r) / 2;
if (dl <= mid)
return a[a[id].rson].sum + query(a[id].lson, l, mid, dl);
return query(a[id].rson, mid + 1, r, dl);
}
int n, m;
int d[maxn];
int book[maxn], rk[maxn];
int belong[maxn];
int main() {
int maxd;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> d[i];
}
buildtree(rk[0], 1, n);
for (int i = 1; i <= n; ++i) {
if (belong[d[i]]) {
int t = change(rk[i - 1], 1, n, -1, belong[d[i]]);
rk[i] = change(t, 1, n, 1, i);
} else {
rk[i] = change(rk[i - 1], 1, n, 1, i);
}
belong[d[i]] = i;
}
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
cout << query(rk[r], 1, n, l) << endl;
}
return 0;
} | replace | 10 | 11 | 10 | 11 | -11 | |
p02599 | C++ | Time Limit Exceeded | /*
Auther: ghoshashis545 Ashis Ghosh
College: Jalpaiguri Govt Enggineering College
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll int
#define int long long
#define ld long double
#define ff first
#define ss string
#define se second
#define endl "\n"
#define alt(v) v.begin(), v.end()
#define ralt(v) v.rbegin(), v.rend()
#define pb emplace_back
#define mp make_pair
#define fab(i, a, b) for (int i = (a); i < (b); i++)
#define fba(i, a, b) for (int i = (b); i >= (a); i--)
#define level 20
int mod = 1000000007;
// int mod=998244353;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool test = 0;
struct query {
int l, r, i, ans;
};
int n, q, x, y;
void solve() {
cin >> n >> q;
int sqrtn = (int)sqrt(n) + 1;
int a[n + 5];
for (int i = 0; i < n; i++)
cin >> a[i];
query v[q];
for (int i = 0; i < q; i++) {
cin >> v[i].l >> v[i].r;
v[i].i = i;
v[i].l--;
v[i].r--;
}
sort(v, v + q, [&sqrtn](query &a, query &b) {
if (a.l / sqrtn != b.l / sqrtn)
return a.l / sqrtn < b.l / sqrtn;
return a.r < b.r;
});
map<int, int> m;
int curl = 1, curr = 0;
int cnt = 0;
for (int i = 0; i < q; i++) {
while (curr < v[i].r) {
curr++;
if (m[a[curr]] == 0)
cnt++;
m[a[curr]]++;
}
while (curl > v[i].l) {
curl--;
if (m[a[curl]] == 0)
cnt++;
m[a[curl]]++;
}
while (curr > v[i].r) {
if (m[a[curr]] == 1)
cnt--;
m[a[curr]]--;
curr--;
}
while (curl < v[i].l) {
if (m[a[curl]] == 1)
cnt--;
m[a[curl]]--;
curl++;
}
v[i].ans = cnt;
}
sort(v, v + q, [&](query &a, query &b) { return a.i < b.i; });
for (int i = 0; i < q; i++)
cout << v[i].ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
if (test)
cin >> t;
while (t--) {
solve();
}
return 0;
} | /*
Auther: ghoshashis545 Ashis Ghosh
College: Jalpaiguri Govt Enggineering College
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll int
#define int long long
#define ld long double
#define ff first
#define ss string
#define se second
#define endl "\n"
#define alt(v) v.begin(), v.end()
#define ralt(v) v.rbegin(), v.rend()
#define pb emplace_back
#define mp make_pair
#define fab(i, a, b) for (int i = (a); i < (b); i++)
#define fba(i, a, b) for (int i = (b); i >= (a); i--)
#define level 20
int mod = 1000000007;
// int mod=998244353;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool test = 0;
struct query {
int l, r, i, ans;
};
int n, q, x, y;
void solve() {
cin >> n >> q;
int sqrtn = (int)sqrt(n) + 1;
int a[n + 5];
for (int i = 0; i < n; i++)
cin >> a[i];
query v[q];
for (int i = 0; i < q; i++) {
cin >> v[i].l >> v[i].r;
v[i].i = i;
v[i].l--;
v[i].r--;
}
sort(v, v + q, [&sqrtn](query &a, query &b) {
if (a.l / sqrtn != b.l / sqrtn)
return a.l / sqrtn < b.l / sqrtn;
return a.r < b.r;
});
vector<int> m(n + 1);
int curl = 1, curr = 0;
int cnt = 0;
for (int i = 0; i < q; i++) {
while (curr < v[i].r) {
curr++;
if (m[a[curr]] == 0)
cnt++;
m[a[curr]]++;
}
while (curl > v[i].l) {
curl--;
if (m[a[curl]] == 0)
cnt++;
m[a[curl]]++;
}
while (curr > v[i].r) {
if (m[a[curr]] == 1)
cnt--;
m[a[curr]]--;
curr--;
}
while (curl < v[i].l) {
if (m[a[curl]] == 1)
cnt--;
m[a[curl]]--;
curl++;
}
v[i].ans = cnt;
}
sort(v, v + q, [&](query &a, query &b) { return a.i < b.i; });
for (int i = 0; i < q; i++)
cout << v[i].ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
if (test)
cin >> t;
while (t--) {
solve();
}
return 0;
} | replace | 56 | 57 | 56 | 57 | TLE | |
p02599 | C++ | Runtime Error | // C++ code to find number of distinct numbers
// in a subarray
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100001;
// structure to store queries
struct Query {
int l, r, idx;
};
// cmp function to sort queries according to r
bool cmp(Query x, Query y) { return x.r < y.r; }
// updating the bit array
void update(int idx, int val, int bit[], int n) {
for (; idx <= n; idx += idx & -idx)
bit[idx] += val;
}
// querying the bit array
int query(int idx, int bit[], int n) {
int sum = 0;
for (; idx > 0; idx -= idx & -idx)
sum += bit[idx];
return sum;
}
void answeringQueries(int arr[], int n, Query queries[], int q) {
// initialising bit array
int bit[n + 1];
memset(bit, 0, sizeof(bit));
// holds the rightmost index of any number
// as numbers of a[i] are less than or equal to 10^6
int last_visit[MAX];
memset(last_visit, -1, sizeof(last_visit));
// answer for each query
int ans[q];
int query_counter = 0;
for (int i = 0; i < n; i++) {
// If last visit is not -1 update -1 at the
// idx equal to last_visit[arr[i]]
if (last_visit[arr[i]] != -1)
update(last_visit[arr[i]] + 1, -1, bit, n);
// Setting last_visit[arr[i]] as i and updating
// the bit array accordingly
last_visit[arr[i]] = i;
update(i + 1, 1, bit, n);
// If i is equal to r of any query store answer
// for that query in ans[]
while (query_counter < q && queries[query_counter].r == i) {
ans[queries[query_counter].idx] =
query(queries[query_counter].r + 1, bit, n) -
query(queries[query_counter].l, bit, n);
query_counter++;
}
}
// print answer for each query
for (int i = 0; i < q; i++)
cout << ans[i] << endl;
}
// driver code
int main() {
int a[MAX];
int n, q;
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> a[i];
Query queries[q + 2];
for (int i = 0; i < q; i++) {
cin >> queries[i].l >> queries[i].r;
queries[i].l--;
queries[i].r--;
queries[i].idx = i;
}
sort(queries, queries + q, cmp);
answeringQueries(a, n, queries, q);
return 0;
}
| // C++ code to find number of distinct numbers
// in a subarray
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000001;
// structure to store queries
struct Query {
int l, r, idx;
};
// cmp function to sort queries according to r
bool cmp(Query x, Query y) { return x.r < y.r; }
// updating the bit array
void update(int idx, int val, int bit[], int n) {
for (; idx <= n; idx += idx & -idx)
bit[idx] += val;
}
// querying the bit array
int query(int idx, int bit[], int n) {
int sum = 0;
for (; idx > 0; idx -= idx & -idx)
sum += bit[idx];
return sum;
}
void answeringQueries(int arr[], int n, Query queries[], int q) {
// initialising bit array
int bit[n + 1];
memset(bit, 0, sizeof(bit));
// holds the rightmost index of any number
// as numbers of a[i] are less than or equal to 10^6
int last_visit[MAX];
memset(last_visit, -1, sizeof(last_visit));
// answer for each query
int ans[q];
int query_counter = 0;
for (int i = 0; i < n; i++) {
// If last visit is not -1 update -1 at the
// idx equal to last_visit[arr[i]]
if (last_visit[arr[i]] != -1)
update(last_visit[arr[i]] + 1, -1, bit, n);
// Setting last_visit[arr[i]] as i and updating
// the bit array accordingly
last_visit[arr[i]] = i;
update(i + 1, 1, bit, n);
// If i is equal to r of any query store answer
// for that query in ans[]
while (query_counter < q && queries[query_counter].r == i) {
ans[queries[query_counter].idx] =
query(queries[query_counter].r + 1, bit, n) -
query(queries[query_counter].l, bit, n);
query_counter++;
}
}
// print answer for each query
for (int i = 0; i < q; i++)
cout << ans[i] << endl;
}
// driver code
int main() {
int a[MAX];
int n, q;
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> a[i];
Query queries[q + 2];
for (int i = 0; i < q; i++) {
cin >> queries[i].l >> queries[i].r;
queries[i].l--;
queries[i].r--;
queries[i].idx = i;
}
sort(queries, queries + q, cmp);
answeringQueries(a, n, queries, q);
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.