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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02598 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef vector<vpll> vvpll;
typedef vector<vpii> vvpii;
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(c) (c).begin(), (c).end()
#define ff first
#define ss second
#define mp(x, y) make_pair((x), (y))
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
int f(int len, int goal) { return len / goal - (len % goal == 0); }
void solve() {
int n, k;
cin >> n >> k;
int a[n];
for (int &u : a) {
cin >> u;
}
int lo = 0, hi = 1e9;
int ans;
while (lo <= hi) {
int mid = (lo + hi) / 2;
int cnt = 0;
for (int u : a) {
cnt += f(u, mid);
}
if (cnt <= k) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
cout << ans << '\n';
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
boost;
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case #" << i << ": ";
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef vector<vpll> vvpll;
typedef vector<vpii> vvpii;
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(c) (c).begin(), (c).end()
#define ff first
#define ss second
#define mp(x, y) make_pair((x), (y))
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
int f(int len, int goal) { return len / goal - (len % goal == 0); }
void solve() {
int n, k;
cin >> n >> k;
int a[n];
for (int &u : a) {
cin >> u;
}
int lo = 1, hi = 1e9;
int ans;
while (lo <= hi) {
int mid = (lo + hi) / 2;
int cnt = 0;
for (int u : a) {
cnt += f(u, mid);
}
if (cnt <= k) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
cout << ans << '\n';
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
boost;
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case #" << i << ": ";
solve();
}
return 0;
} | replace | 38 | 39 | 38 | 39 | -11 | |
p02598 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
void solve() {
int n;
ll k;
cin >> n >> k;
vector<ll> a(n);
ll ma = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
ma = max(ma, a[i]);
}
ll left = -1;
ll right = ma + 1;
while (right - left > 1) {
const ll mid = (left + right) / 2;
ll sum = 0;
for (int i = 0; i < n; i++) {
sum += (a[i] + mid - 1) / mid - 1;
}
if (sum <= k)
right = mid;
else
left = mid;
}
cout << right << endl;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
void solve() {
int n;
ll k;
cin >> n >> k;
vector<ll> a(n);
ll ma = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
ma = max(ma, a[i]);
}
ll left = 0;
ll right = ma + 1;
while (right - left > 1) {
const ll mid = (left + right) / 2;
ll sum = 0;
for (int i = 0; i < n; i++) {
sum += (a[i] + mid - 1) / mid - 1;
}
if (sum <= k)
right = mid;
else
left = mid;
}
cout << right << endl;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
solve();
return 0;
}
| replace | 18 | 19 | 18 | 19 | 0 | |
p02598 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int &x : a) {
cin >> x;
}
int l = 0, r = *max_element(a.begin(), a.end());
while (l < r) {
int64_t m = (l + r) >> 1;
int64_t tmp = 0;
for (int x : a) {
tmp += (x + m - 1) / m - 1;
}
if (tmp > k) {
l = m + 1;
} else {
r = m;
}
}
cout << r << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int &x : a) {
cin >> x;
}
int l = 1, r = *max_element(a.begin(), a.end());
while (l < r) {
int64_t m = (l + r) >> 1;
int64_t tmp = 0;
for (int x : a) {
tmp += (x + m - 1) / m - 1;
}
if (tmp > k) {
l = m + 1;
} else {
r = m;
}
}
cout << r << endl;
return 0;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p02598 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
typedef long long int ll;
using namespace std;
#define maxn 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
const int mm = 2e5 + 100;
ll d[mm];
ll n, m;
ll check(double u) {
int i;
ll s = 0;
double a;
for (i = 0; i < n; i++) {
a = d[i] / u;
s += ceil(a) - 1;
}
if (s <= m)
return 1;
else
return 0;
}
int main() {
ll i, j, t, a, b, c, p, k, kk;
cin >> n >> m;
double l, r, mid;
for (i = 0; i < n; i++) {
scanf("%lld", &d[i]);
}
l = 0;
r = 1000000000 + 5.0;
while (l + 1e-7 <= r) {
mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid;
}
cout << (ll)ceil(l) << endl;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
typedef long long int ll;
using namespace std;
#define maxn 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
const int mm = 2e5 + 100;
ll d[mm];
ll n, m;
ll check(double u) {
int i;
ll s = 0;
double a;
for (i = 0; i < n; i++) {
a = d[i] / u;
s += ceil(a) - 1;
}
if (s <= m)
return 1;
else
return 0;
}
int main() {
ll i, j, t, a, b, c, p, k, kk;
cin >> n >> m;
double l, r, mid;
for (i = 0; i < n; i++) {
scanf("%lld", &d[i]);
}
l = 0;
r = 1000000000 + 5.0;
while (l + 1e-6 <= r) {
mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid;
}
cout << (ll)ceil(l) << endl;
} | replace | 37 | 38 | 37 | 38 | TLE | |
p02598 | C++ | Runtime Error |
/*
* Created By: 'Present_Sir'
* Created On: Monday 03 August 2020 08:11:06 PM IST
*/
#include <bits/stdc++.h>
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define int long long
using namespace std;
const int N = 2e5 + 10;
int n, k;
vector<int> v(N);
bool valid(int mid) {
int kc = k;
for (int i = 0; i < n; i++) {
if (v[i] <= mid) {
continue;
}
int t = v[i] / mid;
kc -= (t);
}
return kc >= 0;
}
void solve() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int i = 0, j = LLONG_MAX;
int ans = 0;
while (i <= j) {
int mid = (i + j) / 2;
if (valid(mid)) {
j = mid - 1;
ans = mid;
} else {
i = mid + 1;
}
}
cout << ans << endl;
}
int32_t main() {
IOS;
// int t; cin>>t; while(t--)
{ solve(); }
return 0;
}
|
/*
* Created By: 'Present_Sir'
* Created On: Monday 03 August 2020 08:11:06 PM IST
*/
#include <bits/stdc++.h>
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define int long long
using namespace std;
const int N = 2e5 + 10;
int n, k;
vector<int> v(N);
bool valid(int mid) {
int kc = k;
if (mid == 0) {
return false;
}
for (int i = 0; i < n; i++) {
if (v[i] <= mid) {
continue;
}
int t = v[i] / mid;
kc -= (t);
}
return kc >= 0;
}
void solve() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int i = 0, j = LLONG_MAX;
int ans = 0;
while (i <= j) {
int mid = (i + j) / 2;
if (valid(mid)) {
j = mid - 1;
ans = mid;
} else {
i = mid + 1;
}
}
cout << ans << endl;
}
int32_t main() {
IOS;
// int t; cin>>t; while(t--)
{ solve(); }
return 0;
}
| insert | 19 | 19 | 19 | 22 | 0 | |
p02598 | C++ | Runtime Error | #include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define Maxn 200010
#define LL long long
using namespace std;
const double eps = 0.00001;
inline LL read() {
LL x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while ('0' <= c && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
LL n, k, a[Maxn], maxone;
LL calc(int len) {
LL tim = 0;
for (int i = 1; i <= n; ++i) {
tim += (a[i] - 1) / len;
}
return tim;
}
int main() {
n = read();
k = read();
for (int i = 1; i <= n; ++i)
a[i] = read(), maxone = max(maxone, a[i]);
int l = 0, r = maxone;
while (l < r) {
int mid = (l + r) >> 1;
if (calc(mid) > k)
l = mid + 1;
else
r = mid;
}
cout << l << endl;
return 0;
} | #include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define Maxn 200010
#define LL long long
using namespace std;
const double eps = 0.00001;
inline LL read() {
LL x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while ('0' <= c && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
LL n, k, a[Maxn], maxone;
LL calc(int len) {
if (!len)
return 1e9 + 1;
LL tim = 0;
for (int i = 1; i <= n; ++i) {
tim += (a[i] - 1) / len;
}
return tim;
}
int main() {
n = read();
k = read();
for (int i = 1; i <= n; ++i)
a[i] = read(), maxone = max(maxone, a[i]);
int l = 0, r = maxone;
while (l < r) {
int mid = (l + r) >> 1;
if (calc(mid) > k)
l = mid + 1;
else
r = mid;
}
cout << l << endl;
return 0;
} | insert | 30 | 30 | 30 | 32 | 0 | |
p02598 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
// #define DEBUG 100
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = 2e6 + 100, maxm = 1e3 + 100;
const double PI = acos(-1);
ll solve(vector<ll> &vs, int n, ll k) {
ll a = 0, b = oo, best;
while (a <= b) {
ll mid = (a + b) / 2, cnt = 0;
for (auto it : vs) {
cnt += (it / mid - (it % mid == 0 ? 1 : 0));
}
if (cnt > k) {
a = mid + 1;
} else {
best = mid;
b = mid - 1;
}
}
return best;
}
int main() {
ios_base::sync_with_stdio(false);
ll n, k;
cin >> n >> k;
vector<ll> vs(n);
for (int i = 0; i < n; i++) {
cin >> vs[i];
}
ll ans = solve(vs, n, k);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
// #define DEBUG 100
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = 2e6 + 100, maxm = 1e3 + 100;
const double PI = acos(-1);
ll solve(vector<ll> &vs, int n, ll k) {
ll a = 1, b = oo, best;
while (a <= b) {
ll mid = (a + b) / 2, cnt = 0;
for (auto it : vs) {
cnt += (it / mid - (it % mid == 0 ? 1 : 0));
}
if (cnt > k) {
a = mid + 1;
} else {
best = mid;
b = mid - 1;
}
}
return best;
}
int main() {
ios_base::sync_with_stdio(false);
ll n, k;
cin >> n >> k;
vector<ll> vs(n);
for (int i = 0; i < n; i++) {
cin >> vs[i];
}
ll ans = solve(vs, n, k);
cout << ans << endl;
return 0;
} | replace | 17 | 18 | 17 | 18 | 0 | |
p02598 | C++ | Runtime Error | #include <bits/stdc++.h>
#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)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
for (auto &e : u)
fill_v<T>(e, v...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
rep(i, n) cin >> a[i];
int ok = 1e9 + 5, ng = 0;
auto check = [&](int x) {
ll cnt = 0;
rep(i, n) {
int now = a[i] / x;
if (a[i] % x == 0)
now--;
cnt += now;
}
return cnt <= k;
};
rep(i, 100) {
int mid = (ok + ng) >> 1;
if (check(mid))
ok = mid;
else
ng = mid;
}
cout << ok << endl;
}
| #include <bits/stdc++.h>
#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)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
for (auto &e : u)
fill_v<T>(e, v...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
rep(i, n) cin >> a[i];
int ok = 1e9 + 5, ng = 0;
auto check = [&](int x) {
ll cnt = 0;
rep(i, n) {
int now = a[i] / x;
if (a[i] % x == 0)
now--;
cnt += now;
}
return cnt <= k;
};
while (ok - ng > 1) {
int mid = (ok + ng) >> 1;
if (check(mid))
ok = mid;
else
ng = mid;
}
cout << ok << endl;
}
| replace | 55 | 56 | 55 | 56 | 0 | |
p02598 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
typedef pair<ll, ll> Pair;
template <class T> T input() {
T t;
cin >> t;
return t;
}
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define FIND(x, key) x.find(key) != x.end()
#define IN_RANGE_V2(v, k, x, y) \
(0 <= x + dx[k] && x + dx[k] < v[y].size() && 0 <= y + dy[k] && \
y + dy[k] < v.size())
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define PRINT_ARRAY_H(a) \
REPALL(__i, a) { cout << a[__i] << (__i != a.size() - 1 ? " " : ""); } \
newline;
#define PRINT_ARRAY_V(a) \
REPALL(__i, a) { cout << a[__i] << endl; }
#define e1 first
#define e2 second
#define newline putchar('\n')
#define cspace ' '
#define sspace " "
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};
// const VI dx = {1, 1, 0, -1, -1, -1, 0, 1};
// const VI dy = {0, 1, 1, 1, 0, -1, -1, -1};
void solve() {
ll b, c, d, n, m, l, r, k, x, y, z, ans = 0ll;
string s, t;
cin >> n >> k;
VLL a(n);
REP(i, n) cin >> a[i];
l = 0, r = 1e10;
ll mid;
REP(i, 100) {
ll cnt = 0;
mid = (l + r) / 2;
REP(j, n) { cnt += (a[j] + mid - 1) / mid - 1; }
if (cnt <= k) {
r = mid;
} else {
l = mid;
}
// cout<<r<<sspace<<cnt<<endl;
}
cout << r << endl;
}
int main() {
cout << fixed << setprecision(20);
int __t = 1;
// cin>>__t;
REP(i, __t) { solve(); }
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
typedef pair<ll, ll> Pair;
template <class T> T input() {
T t;
cin >> t;
return t;
}
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define FIND(x, key) x.find(key) != x.end()
#define IN_RANGE_V2(v, k, x, y) \
(0 <= x + dx[k] && x + dx[k] < v[y].size() && 0 <= y + dy[k] && \
y + dy[k] < v.size())
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define PRINT_ARRAY_H(a) \
REPALL(__i, a) { cout << a[__i] << (__i != a.size() - 1 ? " " : ""); } \
newline;
#define PRINT_ARRAY_V(a) \
REPALL(__i, a) { cout << a[__i] << endl; }
#define e1 first
#define e2 second
#define newline putchar('\n')
#define cspace ' '
#define sspace " "
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};
// const VI dx = {1, 1, 0, -1, -1, -1, 0, 1};
// const VI dy = {0, 1, 1, 1, 0, -1, -1, -1};
void solve() {
ll b, c, d, n, m, l, r, k, x, y, z, ans = 0ll;
string s, t;
cin >> n >> k;
VLL a(n);
REP(i, n) cin >> a[i];
l = 0, r = 1e10;
ll mid;
REP(i, 100) {
ll cnt = 0;
mid = (l + r) / 2;
if (mid == 0) {
r = 1;
break;
}
REP(j, n) { cnt += (a[j] + mid - 1) / mid - 1; }
if (cnt <= k) {
r = mid;
} else {
l = mid;
}
// cout<<r<<sspace<<cnt<<endl;
}
cout << r << endl;
}
int main() {
cout << fixed << setprecision(20);
int __t = 1;
// cin>>__t;
REP(i, __t) { solve(); }
return 0;
} | insert | 66 | 66 | 66 | 70 | 0 | |
p02598 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define REP(i, x, n) for (ll i = x; i < n; i++)
#define SZ(v) (ll) v.size()
#define endl "\n"
#define ff first
#define ss second
#define PQ_MIN priority_queue<ll, vector<ll>, greater<ll>>
#define PQ priority_queue<int>
#define pbds \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define setbit(x) __builtin_popcount(x)
#define prec(n) fixed << setprecision(n)
#define pb(n) push_back(n)
#define mp(a, b) make_pair(a, b)
#define clr(n) memset(n, 0, sizeof(n))
#define reset(n) memset(n, -1, sizeof(n))
#define ii pair<ll, ll>
#define vll vector<ll>
using namespace std;
using namespace __gnu_pbds;
bool comp(pair<ll, ll> a, pair<ll, ll> b) {}
const ll MOD = 1e9 + 7;
void print(vector<vector<ll>> &v) {
ll n = v.size();
ll m = v[0].size();
cout << "\n";
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < m; j++)
cout << v[i][j] << " ";
cout << "\n";
}
}
void print(vector<ll> &v) {
ll n = v.size();
cout << "\n";
for (ll i = 0; i < n; i++) {
cout << v[i] << " ";
}
cout << "\n";
}
void print(set<ll> &s) {
cout << endl;
set<ll>::iterator it;
for (it = s.begin(); it != s.end(); it++)
cout << *it << " ";
cout << endl;
}
ll __lcm(ll a, ll b) { return ((a * b) / __gcd(a, b)); }
ll power(ll a, ll p) {
ll res = 1;
while (p > 0) {
if (p & 1) {
res = (res * a) % MOD;
p--;
}
a = (a * a) % MOD;
p /= 2;
}
return res % MOD;
}
const ll INF = 1e16;
const ll nxm = 2e5 + 5;
ll n, k;
ll a[nxm];
bool can(ll hig, ll k) {
REP(i, 0, n) {
ll need = a[i] / hig;
if (a[i] <= hig)
need = 1;
else {
need = a[i] / hig;
if (a[i] % hig)
need++;
}
k -= (need - 1);
if (k < 0)
return false;
}
return true;
}
void solve() {
cin >> n >> k;
ll maxi = 0;
REP(i, 0, n) cin >> a[i], maxi = max(maxi, a[i]);
ll l = 0, r = maxi;
while (l <= r) {
ll mid = (l + r) / 2;
if (can(mid, k)) {
r = mid - 1;
} else
l = mid + 1;
}
cout << l << endl;
}
int main() {
// freopen("timber_input", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin>>t;
REP(i, 1, t + 1) {
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
}
// bitset<64>(x).to_ullong() // in my com _ulong()
// cout<<bitset<64>(ans).to_string().substr(64-m)<<endl;
/*
char s[100];
fgets(s,100,stdin);
stringstream stram(s);
string abc;
bool ok1=0,ok2=0;
while(stram>>abc)
*/
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define REP(i, x, n) for (ll i = x; i < n; i++)
#define SZ(v) (ll) v.size()
#define endl "\n"
#define ff first
#define ss second
#define PQ_MIN priority_queue<ll, vector<ll>, greater<ll>>
#define PQ priority_queue<int>
#define pbds \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define setbit(x) __builtin_popcount(x)
#define prec(n) fixed << setprecision(n)
#define pb(n) push_back(n)
#define mp(a, b) make_pair(a, b)
#define clr(n) memset(n, 0, sizeof(n))
#define reset(n) memset(n, -1, sizeof(n))
#define ii pair<ll, ll>
#define vll vector<ll>
using namespace std;
using namespace __gnu_pbds;
bool comp(pair<ll, ll> a, pair<ll, ll> b) {}
const ll MOD = 1e9 + 7;
void print(vector<vector<ll>> &v) {
ll n = v.size();
ll m = v[0].size();
cout << "\n";
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < m; j++)
cout << v[i][j] << " ";
cout << "\n";
}
}
void print(vector<ll> &v) {
ll n = v.size();
cout << "\n";
for (ll i = 0; i < n; i++) {
cout << v[i] << " ";
}
cout << "\n";
}
void print(set<ll> &s) {
cout << endl;
set<ll>::iterator it;
for (it = s.begin(); it != s.end(); it++)
cout << *it << " ";
cout << endl;
}
ll __lcm(ll a, ll b) { return ((a * b) / __gcd(a, b)); }
ll power(ll a, ll p) {
ll res = 1;
while (p > 0) {
if (p & 1) {
res = (res * a) % MOD;
p--;
}
a = (a * a) % MOD;
p /= 2;
}
return res % MOD;
}
const ll INF = 1e16;
const ll nxm = 2e5 + 5;
ll n, k;
ll a[nxm];
bool can(ll hig, ll k) {
REP(i, 0, n) {
ll need = a[i] / hig;
if (a[i] <= hig)
need = 1;
else {
need = a[i] / hig;
if (a[i] % hig)
need++;
}
k -= (need - 1);
if (k < 0)
return false;
}
return true;
}
void solve() {
cin >> n >> k;
ll maxi = 0;
REP(i, 0, n) cin >> a[i], maxi = max(maxi, a[i]);
ll l = 1, r = maxi;
while (l <= r) {
ll mid = (l + r) / 2;
if (can(mid, k)) {
r = mid - 1;
} else
l = mid + 1;
}
cout << l << endl;
}
int main() {
// freopen("timber_input", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin>>t;
REP(i, 1, t + 1) {
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
}
// bitset<64>(x).to_ullong() // in my com _ulong()
// cout<<bitset<64>(ans).to_string().substr(64-m)<<endl;
/*
char s[100];
fgets(s,100,stdin);
stringstream stram(s);
string abc;
bool ok1=0,ok2=0;
while(stram>>abc)
*/
| replace | 98 | 99 | 98 | 99 | 0 | |
p02598 | C++ | Runtime Error | // #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef long double ld;
typedef pair<int, int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || \
(WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed << setprecision(n)
const int INF = 1061109567;
const int NINF = -1044266559;
const LL LINF = 4557430888798830399;
const ld eps = 1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)
#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(), (a).end()
#define pall(a) (a).rbegin(), (a).rend()
#define Log(x, y) log(x) / log(y)
#define SZ(a) ((int)(a).size())
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++)
#define repd(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define repd1(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define repv(itr, v) \
for (__typeof((v).begin()) itr = (v).begin(); itr != (v).end(); itr++)
#define repV(i, v) for (auto i : v)
#define repE(i, v) for (auto &i : v)
#define MS(x, y) memset(x, y, sizeof(x))
#define MC(x) MS(x, 0)
#define MINF(x) MS(x, 63)
#define MCP(x, y) memcpy(x, y, sizeof(y))
#define UN(v) sort(All(v)), v.erase(unique(All(v)), v.end())
#define filein(x) freopen(x, "r", stdin)
#define fileout(x) freopen(x, "w", stdout)
#define fileio(x) \
freopen(x ".in", "r", stdin); \
freopen(x ".out", "w", stdout)
#define filein2(filename, name) ifstream name(filename, ios::in)
#define fileout2(filename, name) ofstream name(filename, ios::out)
#define fileio2(filename, name) fstream name(filename, ios::in | ios::out)
#define sqr(x) ((x) * (x))
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define SF scanf
#define PF printf
const int maxn = 200010;
int n, k, a[maxn];
inline bool check(int x) {
int cnt = 0;
for (int i = 1; i <= n; i++)
cnt += a[i] / x + (bool)(a[i] % x) - 1;
return cnt <= k;
}
int main() {
SF("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
SF("%d", &a[i]);
int l = 0, r = 1e9 + 1; // [l,r)
while (l < r) {
int m = (l + r) / 2;
if (check(m))
r = m;
else
l = m + 1;
}
PF("%d", r);
return 0;
} | // #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef long double ld;
typedef pair<int, int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || \
(WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed << setprecision(n)
const int INF = 1061109567;
const int NINF = -1044266559;
const LL LINF = 4557430888798830399;
const ld eps = 1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)
#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(), (a).end()
#define pall(a) (a).rbegin(), (a).rend()
#define Log(x, y) log(x) / log(y)
#define SZ(a) ((int)(a).size())
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++)
#define repd(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define repd1(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define repv(itr, v) \
for (__typeof((v).begin()) itr = (v).begin(); itr != (v).end(); itr++)
#define repV(i, v) for (auto i : v)
#define repE(i, v) for (auto &i : v)
#define MS(x, y) memset(x, y, sizeof(x))
#define MC(x) MS(x, 0)
#define MINF(x) MS(x, 63)
#define MCP(x, y) memcpy(x, y, sizeof(y))
#define UN(v) sort(All(v)), v.erase(unique(All(v)), v.end())
#define filein(x) freopen(x, "r", stdin)
#define fileout(x) freopen(x, "w", stdout)
#define fileio(x) \
freopen(x ".in", "r", stdin); \
freopen(x ".out", "w", stdout)
#define filein2(filename, name) ifstream name(filename, ios::in)
#define fileout2(filename, name) ofstream name(filename, ios::out)
#define fileio2(filename, name) fstream name(filename, ios::in | ios::out)
#define sqr(x) ((x) * (x))
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define SF scanf
#define PF printf
const int maxn = 200010;
int n, k, a[maxn];
inline bool check(int x) {
int cnt = 0;
for (int i = 1; i <= n; i++)
cnt += a[i] / x + (bool)(a[i] % x) - 1;
return cnt <= k;
}
int main() {
SF("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
SF("%d", &a[i]);
int l = 1, r = 1e9 + 1; // [l,r)
while (l < r) {
int m = (l + r) / 2;
if (check(m))
r = m;
else
l = m + 1;
}
PF("%d", r);
return 0;
} | replace | 88 | 89 | 88 | 89 | 0 | |
p02598 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FILL0(x) memset(x, 0, sizeof(x))
#define FILL1(x) memset(x, -1, sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
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;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
int n, k;
cin >> n;
cin >> k;
int a[n];
rep(i, n) { cin >> a[i]; }
int l = -1;
int r = 1e9;
while (r - l > 1) {
int mid = (r + l) / 2;
auto judge = [&](int val) {
ll count = 0;
rep(i, n) { count += (a[i] - 1) / val; }
return count <= k;
};
if (judge(mid)) {
r = mid;
} else {
l = mid;
}
}
cout << r << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FILL0(x) memset(x, 0, sizeof(x))
#define FILL1(x) memset(x, -1, sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
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;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
int n, k;
cin >> n;
cin >> k;
int a[n];
rep(i, n) { cin >> a[i]; }
int l = 0;
int r = 1e9;
while (r - l > 1) {
int mid = (r + l) / 2;
auto judge = [&](int val) {
ll count = 0;
rep(i, n) { count += (a[i] - 1) / val; }
return count <= k;
};
if (judge(mid)) {
r = mid;
} else {
l = mid;
}
}
cout << r << endl;
return 0;
}
| replace | 39 | 40 | 39 | 40 | 0 | |
p02598 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define inf 1001001001
#define mod 1000000007
#define mod2 998244353
#define pi acos(-1)
#define all(v) v.begin(), v.end()
int k;
ll gcd(ll a, ll b) {
if (b > a)
swap(a, b);
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
ll rep_jijo(ll n, ll x) {
if (x == 0)
return 1;
if (x % 2 == 0) {
ll t = rep_jijo(n, x / 2);
return t * t % mod;
}
return n * rep_jijo(n, x - 1) % mod;
}
int main() {
ll n, k;
cin >> n;
cin >> k;
k += 1;
vector<double> L(n);
rep(i, n) cin >> L[i];
// cin >> k;
double l = 0;
double r = inf;
ll count;
auto judge = [&](double x) {
count = 0;
rep(i, n) count += floor(L[i] / x);
if (count >= k)
return true;
else
return false;
};
for (int j = 0; j < 2000; j++) {
double mid = (l + r) / 2.0;
bool flag;
flag = judge(mid);
if (flag)
l = mid;
else
r = mid;
}
cout << ll(ceil(l)) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define inf 1001001001
#define mod 1000000007
#define mod2 998244353
#define pi acos(-1)
#define all(v) v.begin(), v.end()
int k;
ll gcd(ll a, ll b) {
if (b > a)
swap(a, b);
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
ll rep_jijo(ll n, ll x) {
if (x == 0)
return 1;
if (x % 2 == 0) {
ll t = rep_jijo(n, x / 2);
return t * t % mod;
}
return n * rep_jijo(n, x - 1) % mod;
}
int main() {
ll n, k;
cin >> n;
cin >> k;
k += 1;
vector<double> L(n);
rep(i, n) cin >> L[i];
// cin >> k;
double l = 0;
double r = inf;
ll count;
auto judge = [&](double x) {
count = 0;
rep(i, n) count += floor(L[i] / x);
if (count >= k)
return true;
else
return false;
};
for (int j = 0; j < 100; j++) {
double mid = (l + r) / 2.0;
bool flag;
flag = judge(mid);
if (flag)
l = mid;
else
r = mid;
}
cout << ll(ceil(l)) << endl;
return 0;
} | replace | 55 | 56 | 55 | 56 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\n")
#define priln(n) printf("%d\n", n)
#define lpriln(n) printf("%lld\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
using namespace std;
const ll mod = 1e9 + 7;
const int N = 1e5 + 4;
struct util {
int sum, mx;
};
util tree[4 * N];
int arr[N];
void build(int node, int st, int end) {
if (st == end) {
tree[node].sum = 0;
tree[node].mx = arr[st];
return;
}
int mid = (st + end) / 2;
build(2 * node, st, mid);
build(2 * node + 1, mid + 1, end);
tree[node].mx = max(tree[2 * node].mx, tree[2 * node + 1].mx);
}
void update(int node, int st, int end, int ind, int val) {
if (st == end) {
tree[node].sum += val;
return;
}
int mid = (st + end) / 2;
if (ind <= mid)
update(2 * node, st, mid, ind, val);
else
update(2 * node + 1, mid + 1, end, ind, val);
tree[node].sum = tree[2 * node].sum + tree[2 * node + 1].sum;
}
util query(int node, int st, int end, int l, int r) {
if (st > r or end < l) {
util aux;
aux.sum = aux.mx = 0;
return aux;
}
if (st >= l and end <= r)
return tree[node];
int mid = (st + end) / 2;
util a, b, here;
a = query(2 * node, st, mid, l, r);
b = query(2 * node + 1, mid + 1, end, l, r);
here.sum = a.sum + b.sum;
here.mx = max(a.mx, b.mx);
return here;
}
int main() {
int n, q;
scn(n);
scn(q);
rep(i, 0, n) scn(arr[i]);
build(1, 0, n - 1);
vector<pair<int, int>> v[n];
int ans[q];
int prev[N]; // last[x] denotes the previous index in which x was present
memset(prev, -1, sizeof(prev));
rep(i, 0, q) {
int l, r;
scn(l);
scn(r);
l--;
r--;
// cout<<l<<" "<<r<<" "<<i<<endl;
v[r].emplace_back(l, i);
}
rep(i, 0, n) {
int here = arr[i];
if (prev[here] != -1)
update(1, 0, n - 1, prev[here], -1);
update(1, 0, n - 1, i, 1);
prev[here] = i;
for (auto it : v[i]) {
int hr = i, hl = it.F, hind = it.S;
// cout<<hl<<" "<<hr<<" "<<hind<<endl;
util temp = query(1, 0, n - 1, hl, hr);
ans[hind] = temp.sum;
}
}
rep(i, 0, q) priln(ans[i]);
} | #include <bits/stdc++.h>
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\n")
#define priln(n) printf("%d\n", n)
#define lpriln(n) printf("%lld\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
using namespace std;
const ll mod = 1e9 + 7;
const int N = 5e5 + 4;
struct util {
int sum, mx;
};
util tree[4 * N];
int arr[N];
void build(int node, int st, int end) {
if (st == end) {
tree[node].sum = 0;
tree[node].mx = arr[st];
return;
}
int mid = (st + end) / 2;
build(2 * node, st, mid);
build(2 * node + 1, mid + 1, end);
tree[node].mx = max(tree[2 * node].mx, tree[2 * node + 1].mx);
}
void update(int node, int st, int end, int ind, int val) {
if (st == end) {
tree[node].sum += val;
return;
}
int mid = (st + end) / 2;
if (ind <= mid)
update(2 * node, st, mid, ind, val);
else
update(2 * node + 1, mid + 1, end, ind, val);
tree[node].sum = tree[2 * node].sum + tree[2 * node + 1].sum;
}
util query(int node, int st, int end, int l, int r) {
if (st > r or end < l) {
util aux;
aux.sum = aux.mx = 0;
return aux;
}
if (st >= l and end <= r)
return tree[node];
int mid = (st + end) / 2;
util a, b, here;
a = query(2 * node, st, mid, l, r);
b = query(2 * node + 1, mid + 1, end, l, r);
here.sum = a.sum + b.sum;
here.mx = max(a.mx, b.mx);
return here;
}
int main() {
int n, q;
scn(n);
scn(q);
rep(i, 0, n) scn(arr[i]);
build(1, 0, n - 1);
vector<pair<int, int>> v[n];
int ans[q];
int prev[N]; // last[x] denotes the previous index in which x was present
memset(prev, -1, sizeof(prev));
rep(i, 0, q) {
int l, r;
scn(l);
scn(r);
l--;
r--;
// cout<<l<<" "<<r<<" "<<i<<endl;
v[r].emplace_back(l, i);
}
rep(i, 0, n) {
int here = arr[i];
if (prev[here] != -1)
update(1, 0, n - 1, prev[here], -1);
update(1, 0, n - 1, i, 1);
prev[here] = i;
for (auto it : v[i]) {
int hr = i, hl = it.F, hind = it.S;
// cout<<hl<<" "<<hr<<" "<<hind<<endl;
util temp = query(1, 0, n - 1, hl, hr);
ans[hind] = temp.sum;
}
}
rep(i, 0, q) priln(ans[i]);
} | replace | 19 | 20 | 19 | 20 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define INF 5000005
int t, n, q, x, y;
int a[100005], b[100005], tree[100005];
int cur[1000005], r[100005];
bool vis[1000005];
int ans[500005];
vector<pair<pair<int, int>, int>> v;
// Query
int read(int idx) {
int sum = 0;
while (idx > 0) {
sum += tree[idx];
idx -= (idx & -idx);
}
return sum;
}
// Update
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += (idx & -idx);
}
}
int main() {
// freopen("input.txt","r",stdin);
memset(cur, INF, sizeof(cur));
scanf("%d", &n);
scanf("%d", &q);
// Initialize the tree
a[0] = tree[0] = 0;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = n; i > 0; i--) {
r[i] = cur[a[i]];
cur[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
if (!vis[a[i]]) {
b[i] = b[i - 1] + 1;
vis[a[i]] = true;
} else
b[i] = b[i - 1];
tree[i] = b[i] - b[i - (i & -i)];
}
// Get and Sort Queries
for (int i = 0; i < q; i++) {
scanf("%d%d", &x, &y);
v.pb(mp(mp(x, y), i));
}
sort(v.begin(), v.end());
// Query and Update
int pos = 0;
for (int i = 1; i <= n; i++) {
while ((v[pos].X).X == i) {
ans[(v[pos].Y)] = read((v[pos].X).Y);
pos++;
}
update(i, -1); // Unmark
update(r[i], 1); // Mark
}
for (int i = 0; i < q; i++)
cout << ans[i] << "\n";
} | #include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define INF 5000005
int t, n, q, x, y;
int a[5000005], b[5000005], tree[5000005];
int cur[1000005], r[5000005];
bool vis[1000005];
int ans[500005];
vector<pair<pair<int, int>, int>> v;
// Query
int read(int idx) {
int sum = 0;
while (idx > 0) {
sum += tree[idx];
idx -= (idx & -idx);
}
return sum;
}
// Update
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += (idx & -idx);
}
}
int main() {
// freopen("input.txt","r",stdin);
memset(cur, INF, sizeof(cur));
scanf("%d", &n);
scanf("%d", &q);
// Initialize the tree
a[0] = tree[0] = 0;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = n; i > 0; i--) {
r[i] = cur[a[i]];
cur[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
if (!vis[a[i]]) {
b[i] = b[i - 1] + 1;
vis[a[i]] = true;
} else
b[i] = b[i - 1];
tree[i] = b[i] - b[i - (i & -i)];
}
// Get and Sort Queries
for (int i = 0; i < q; i++) {
scanf("%d%d", &x, &y);
v.pb(mp(mp(x, y), i));
}
sort(v.begin(), v.end());
// Query and Update
int pos = 0;
for (int i = 1; i <= n; i++) {
while ((v[pos].X).X == i) {
ans[(v[pos].Y)] = read((v[pos].X).Y);
pos++;
}
update(i, -1); // Unmark
update(r[i], 1); // Mark
}
for (int i = 0; i < q; i++)
cout << ans[i] << "\n";
} | replace | 17 | 19 | 17 | 19 | 0 | |
p02599 | C++ | Runtime Error | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define cinf(n, x) \
for (int i = 0; i < (n); i++) \
cin >> x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define LB(a, x) lb(all(a), x) - a.begin()
#define UB(a, x) ub(all(a), x) - a.begin()
#define mod 1000000007
#define FS fixed << setprecision(15)
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
template <class T> using V = vector<T>;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const ll INF = 1e18;
const int mx = 200005;
class Segtree {
public:
ll N = 1;
ll MX = 2000000;
V<ll> dat;
explicit Segtree(ll sz) {
while (N < sz)
N *= 2;
}
// Range Minimum Query -------------------------
void min_init() {
for (int i = 0; i < N; i++)
min_update(i, INF);
}
void min_update(ll k, ll a) {
k += N - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]);
}
}
ll min_query(ll a, ll b, ll k, ll l, ll r) {
if (r <= a || b <= l)
return INF;
if (a <= l && r <= b)
return dat[k];
ll m = (l + r) / 2;
ll u = min_query(a, b, 2 * k + 1, l, m);
ll v = min_query(a, b, 2 * k + 2, m, r);
return min(u, v);
}
// Range Maximum Query ---------------------------
void max_init() {
for (int i = 0; i < N; i++)
max_update(i, -INF);
}
void max_update(ll k, ll a) {
k += N - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = max(dat[2 * k + 1], dat[2 * k + 2]);
}
}
ll max_query(ll a, ll b, ll k, ll l, ll r) {
if (r <= a || b <= l)
return -INF;
if (a <= l && r <= b)
return dat[k];
ll m = (l + r) / 2;
ll u = max_query(a, b, 2 * k + 1, l, m);
ll v = max_query(a, b, 2 * k + 2, m, r);
return max(u, v);
}
// Range Sum Query -----------------------------
void sum_update(ll k, ll a) {
k += N - 1;
dat[k] += a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = (dat[2 * k + 1] + dat[2 * k + 2]);
}
}
ll sum_query(ll a, ll b, ll k, ll l, ll r) {
if (r <= a || b <= l)
return 0LL;
if (a <= l && r <= b)
return dat[k];
ll m = (l + r) / 2;
ll u = sum_query(a, b, 2 * k + 1, l, m);
ll v = sum_query(a, b, 2 * k + 2, m, r);
return (u + v);
}
// その他----------------------------------------
/*
ll unit(){
return ;//単位元
}
ll calc(ll a,ll b){
return ;//演算
}
void update(ll k,ll a){
k+=N-1;
dat[k]=a;
while(k>0){
k=(k-1)/2;
dat[k]=calc(dat[2*k+1],dat[2*k+2]);
}
}
ll query(ll a,ll b,ll k,ll l,ll r){
if(r<=a||b<=l)return unit();
if(a<=l&&r<=b)return dat[k];
ll m=(l+r)/2;
ll u=min_query(a,b,2*k+1,l,m);
ll v=min_query(a,b,2*k+2,m,r);
return calc(u,v);
}
*/
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, qq;
cin >> n >> qq;
V<ll> c(n);
Segtree stree(n);
V<ll> last(n, -1);
rep(i, n) {
cin >> c[i];
c[i]--;
}
V<pair<P, ll>> p(qq);
rep(i, qq) {
ll l, r;
cin >> l >> r;
l--;
r--;
p[i] = make_pair(make_pair(r, l), i);
// cout<<how[r]+rhow[l]-M<<'\n';
}
sort(all(p));
V<ll> ans(qq);
ll pos = 0;
rep(i, qq) {
for (int j = pos; j <= p[i].ft.ft; j++) {
stree.sum_update(j, 1);
if (last[c[j]] != -1)
stree.sum_update(last[c[j]], -1);
last[c[j]] = j;
}
ans[p[i].sc] = stree.sum_query(p[i].ft.sc, p[i].ft.ft + 1, 0, 0, stree.N);
pos = p[i].ft.ft + 1;
}
rep(i, qq) cout << ans[i] << '\n';
}
// ペナルティ出しても焦らない ACできると信じろ!!!
// どうしてもわからないときはサンプルで実験 何か見えてくるかも
// 頭で考えてダメなら紙におこせ!!
/*
V,P(大文字)使用不可
乗算などの際にオーバーフローに注意せよ!
(適切にmodをとれ にぶたんで途中で切り上げろ)
制約をよく読め!
{
・全探索できるなら全探索しろ
・異常な制約ならそこに注目
}
stringの計算量(扱い)注意
コーナー注意!(特に数値が小さいものについては要検証)
N行出力のときは'¥n'
グリッド上では行先が範囲内におさまるかif文で確認(RE注意)
if文ではちゃんと比較演算子==を使え('='でもエラー出ない)
配列(vector)の大きさが0か1以上かで場合分けせよ(RE注意)
(vector<int> a(m)でm==0というものはできない)
modはなるべく最後に取れ!
doubleを扱うときには(abs)ではなく'fabs'!!!
*/ | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define cinf(n, x) \
for (int i = 0; i < (n); i++) \
cin >> x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define LB(a, x) lb(all(a), x) - a.begin()
#define UB(a, x) ub(all(a), x) - a.begin()
#define mod 1000000007
#define FS fixed << setprecision(15)
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
template <class T> using V = vector<T>;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const ll INF = 1e18;
const int mx = 200005;
class Segtree {
public:
ll N = 1;
ll MX = 2000000;
V<ll> dat;
explicit Segtree(ll sz) {
while (N < sz)
N *= 2;
dat.reserve(2 * N);
}
// Range Minimum Query -------------------------
void min_init() {
for (int i = 0; i < N; i++)
min_update(i, INF);
}
void min_update(ll k, ll a) {
k += N - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]);
}
}
ll min_query(ll a, ll b, ll k, ll l, ll r) {
if (r <= a || b <= l)
return INF;
if (a <= l && r <= b)
return dat[k];
ll m = (l + r) / 2;
ll u = min_query(a, b, 2 * k + 1, l, m);
ll v = min_query(a, b, 2 * k + 2, m, r);
return min(u, v);
}
// Range Maximum Query ---------------------------
void max_init() {
for (int i = 0; i < N; i++)
max_update(i, -INF);
}
void max_update(ll k, ll a) {
k += N - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = max(dat[2 * k + 1], dat[2 * k + 2]);
}
}
ll max_query(ll a, ll b, ll k, ll l, ll r) {
if (r <= a || b <= l)
return -INF;
if (a <= l && r <= b)
return dat[k];
ll m = (l + r) / 2;
ll u = max_query(a, b, 2 * k + 1, l, m);
ll v = max_query(a, b, 2 * k + 2, m, r);
return max(u, v);
}
// Range Sum Query -----------------------------
void sum_update(ll k, ll a) {
k += N - 1;
dat[k] += a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = (dat[2 * k + 1] + dat[2 * k + 2]);
}
}
ll sum_query(ll a, ll b, ll k, ll l, ll r) {
if (r <= a || b <= l)
return 0LL;
if (a <= l && r <= b)
return dat[k];
ll m = (l + r) / 2;
ll u = sum_query(a, b, 2 * k + 1, l, m);
ll v = sum_query(a, b, 2 * k + 2, m, r);
return (u + v);
}
// その他----------------------------------------
/*
ll unit(){
return ;//単位元
}
ll calc(ll a,ll b){
return ;//演算
}
void update(ll k,ll a){
k+=N-1;
dat[k]=a;
while(k>0){
k=(k-1)/2;
dat[k]=calc(dat[2*k+1],dat[2*k+2]);
}
}
ll query(ll a,ll b,ll k,ll l,ll r){
if(r<=a||b<=l)return unit();
if(a<=l&&r<=b)return dat[k];
ll m=(l+r)/2;
ll u=min_query(a,b,2*k+1,l,m);
ll v=min_query(a,b,2*k+2,m,r);
return calc(u,v);
}
*/
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, qq;
cin >> n >> qq;
V<ll> c(n);
Segtree stree(n);
V<ll> last(n, -1);
rep(i, n) {
cin >> c[i];
c[i]--;
}
V<pair<P, ll>> p(qq);
rep(i, qq) {
ll l, r;
cin >> l >> r;
l--;
r--;
p[i] = make_pair(make_pair(r, l), i);
// cout<<how[r]+rhow[l]-M<<'\n';
}
sort(all(p));
V<ll> ans(qq);
ll pos = 0;
rep(i, qq) {
for (int j = pos; j <= p[i].ft.ft; j++) {
stree.sum_update(j, 1);
if (last[c[j]] != -1)
stree.sum_update(last[c[j]], -1);
last[c[j]] = j;
}
ans[p[i].sc] = stree.sum_query(p[i].ft.sc, p[i].ft.ft + 1, 0, 0, stree.N);
pos = p[i].ft.ft + 1;
}
rep(i, qq) cout << ans[i] << '\n';
}
// ペナルティ出しても焦らない ACできると信じろ!!!
// どうしてもわからないときはサンプルで実験 何か見えてくるかも
// 頭で考えてダメなら紙におこせ!!
/*
V,P(大文字)使用不可
乗算などの際にオーバーフローに注意せよ!
(適切にmodをとれ にぶたんで途中で切り上げろ)
制約をよく読め!
{
・全探索できるなら全探索しろ
・異常な制約ならそこに注目
}
stringの計算量(扱い)注意
コーナー注意!(特に数値が小さいものについては要検証)
N行出力のときは'¥n'
グリッド上では行先が範囲内におさまるかif文で確認(RE注意)
if文ではちゃんと比較演算子==を使え('='でもエラー出ない)
配列(vector)の大きさが0か1以上かで場合分けせよ(RE注意)
(vector<int> a(m)でm==0というものはできない)
modはなるべく最後に取れ!
doubleを扱うときには(abs)ではなく'fabs'!!!
*/ | insert | 50 | 50 | 50 | 51 | -11 | |
p02599 | C++ | Time Limit Exceeded | // mai iss duniyaa ka sbse bada chutiyaaa huu
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mod 1000000007
#define mod2 998244353
#define int long long
#define endl '\n'
#define p_b push_back
#define m_p make_pair
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)
using namespace std;
using namespace __gnu_pbds;
using namespace std::chrono;
template <class T>
using _ost = tree<T, null_type, less_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;
char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
// rectangle intersection
// x5 = max(x1, x3);
// y5 = max(y1, y3);
// x6 = min(x2, x4);
// y6 = min(y2, y4);
const int mx = 500005;
// char get_next(char c) {
// for(int i=0; i<25; i++) if(c == alphabet[i]) return alphabet[i+1];
// return 'a';
// }
struct no {
int l, r, id;
} q[mx];
int bl[mx];
int col[mx];
unordered_map<int, int> mp;
void get_blocks(int n) {
int sqroot = sqrt(n) + 1;
for (int i = 0; i <= n; i++)
bl[i] = i / sqroot;
}
bool mo_sort(no a, no b) {
return bl[a.l] == bl[b.l] ? a.r < b.r : bl[a.l] < bl[b.l];
}
int diff = 0;
void add(int id) {
if (!mp[col[id]])
diff++;
mp[col[id]]++;
}
void rem(int id) {
mp[col[id]]--;
if (mp[col[id]] == 0)
diff--;
}
int32_t main() {
fastIO;
int tc;
// cin >> tc;
tc = 1;
while (tc--) {
int n, qry;
cin >> n >> qry;
get_blocks(n);
for (int i = 0; i < n; i++)
cin >> col[i];
for (int i = 0; i < qry; i++) {
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
int ans[qry];
sort(q, q + qry, mo_sort);
int curl = 0, curr = -1;
for (int i = 0; i < qry; i++) {
int l = q[i].l - 1, r = q[i].r - 1;
while (curl > l) {
curl--;
add(curl);
}
while (curr < r) {
curr++;
add(curr);
}
while (curl < l) {
rem(curl);
curl++;
}
while (curr > r) {
rem(curr);
curr--;
}
ans[q[i].id] = diff;
}
for (int i = 0; i < qry; i++)
cout << ans[i] << endl;
}
}
| // mai iss duniyaa ka sbse bada chutiyaaa huu
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mod 1000000007
#define mod2 998244353
#define int long long
#define endl '\n'
#define p_b push_back
#define m_p make_pair
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)
using namespace std;
using namespace __gnu_pbds;
using namespace std::chrono;
template <class T>
using _ost = tree<T, null_type, less_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;
char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
// rectangle intersection
// x5 = max(x1, x3);
// y5 = max(y1, y3);
// x6 = min(x2, x4);
// y6 = min(y2, y4);
const int mx = 500005;
// char get_next(char c) {
// for(int i=0; i<25; i++) if(c == alphabet[i]) return alphabet[i+1];
// return 'a';
// }
struct no {
int l, r, id;
} q[mx];
int bl[mx];
int col[mx];
int mp[mx];
void get_blocks(int n) {
int sqroot = sqrt(n) + 1;
for (int i = 0; i <= n; i++)
bl[i] = i / sqroot;
}
bool mo_sort(no a, no b) {
return bl[a.l] == bl[b.l] ? a.r < b.r : bl[a.l] < bl[b.l];
}
int diff = 0;
void add(int id) {
if (!mp[col[id]])
diff++;
mp[col[id]]++;
}
void rem(int id) {
mp[col[id]]--;
if (mp[col[id]] == 0)
diff--;
}
int32_t main() {
fastIO;
int tc;
// cin >> tc;
tc = 1;
while (tc--) {
int n, qry;
cin >> n >> qry;
get_blocks(n);
for (int i = 0; i < n; i++)
cin >> col[i];
for (int i = 0; i < qry; i++) {
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
int ans[qry];
sort(q, q + qry, mo_sort);
int curl = 0, curr = -1;
for (int i = 0; i < qry; i++) {
int l = q[i].l - 1, r = q[i].r - 1;
while (curl > l) {
curl--;
add(curl);
}
while (curr < r) {
curr++;
add(curr);
}
while (curl < l) {
rem(curl);
curl++;
}
while (curr > r) {
rem(curr);
curr--;
}
ans[q[i].id] = diff;
}
for (int i = 0; i < qry; i++)
cout << ans[i] << endl;
}
}
| replace | 44 | 45 | 44 | 45 | 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);
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 = 1e6 + 7;
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);
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 | // #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
// #pragma warning(disable : 4996)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define int long long
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define len(s) (int)(s).size()
// #define endl '\n'
inline void accell() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
}
template <class T> ostream &operator<<(ostream &out, const pair<int, int> &X) {
out << X.first << ' ' << X.second << endl;
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &X) {
for (const auto &it : X)
out << it << ' ';
return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &X) {
for (const auto &it : X)
out << it << ' ';
return out;
}
template <class T> void dbg(const T &X) {
cerr << "DEBUG: "
<< ": ";
cerr << X << endl;
}
const int MOD1 = 1e9 + 7;
const int N = 3e5;
const int MOD2 = 998244353;
const int inf = 1e13;
const int mx = (1 << 19) - 1;
const int mod = 1e9 + 7;
int fenv[N];
void add(int v, int x) {
if (v < 0)
return;
for (int i = v; i < N; i |= (i + 1))
fenv[i] += x;
}
int sum(int r) {
int res = 0;
for (int i = r; i >= 0; i = (i & (i + 1)) - 1)
res += fenv[i];
return res;
}
int get(int l, int r) { return sum(r) - sum(l - 1); }
signed main() {
accell();
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, q;
cin >> n >> q;
vector<int> c(n);
for (int i = 0; i < n; ++i) {
cin >> c[i];
--c[i];
}
vector<int> lst(n, -1);
vector<int> cur(n, -1);
for (int i = 0; i < n; ++i) {
cur[i] = lst[c[i]];
lst[c[i]] = i;
}
vector<pair<pair<int, int>, int>> v(q);
for (int i = 0; i < q; ++i) {
cin >> v[i].first.first >> v[i].first.second;
v[i].first.first--, v[i].first.second--;
v[i].second = i;
}
sort(all(v), [&](pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
return x.first.second < y.first.second;
});
int j = 0;
vector<int> ans(q);
for (int i = 0; i < n; ++i) {
add(i, 1);
add(cur[i], -1);
while (j < len(v) && v[j].first.second == i) {
ans[v[j].second] = get(v[j].first.first, v[j].first.second);
++j;
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
| // #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
// #pragma warning(disable : 4996)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define int long long
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define len(s) (int)(s).size()
// #define endl '\n'
inline void accell() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
}
template <class T> ostream &operator<<(ostream &out, const pair<int, int> &X) {
out << X.first << ' ' << X.second << endl;
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &X) {
for (const auto &it : X)
out << it << ' ';
return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &X) {
for (const auto &it : X)
out << it << ' ';
return out;
}
template <class T> void dbg(const T &X) {
cerr << "DEBUG: "
<< ": ";
cerr << X << endl;
}
const int MOD1 = 1e9 + 7;
const int N = 5e5 + 25;
const int MOD2 = 998244353;
const int inf = 1e13;
const int mx = (1 << 19) - 1;
const int mod = 1e9 + 7;
int fenv[N];
void add(int v, int x) {
if (v < 0)
return;
for (int i = v; i < N; i |= (i + 1))
fenv[i] += x;
}
int sum(int r) {
int res = 0;
for (int i = r; i >= 0; i = (i & (i + 1)) - 1)
res += fenv[i];
return res;
}
int get(int l, int r) { return sum(r) - sum(l - 1); }
signed main() {
accell();
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, q;
cin >> n >> q;
vector<int> c(n);
for (int i = 0; i < n; ++i) {
cin >> c[i];
--c[i];
}
vector<int> lst(n, -1);
vector<int> cur(n, -1);
for (int i = 0; i < n; ++i) {
cur[i] = lst[c[i]];
lst[c[i]] = i;
}
vector<pair<pair<int, int>, int>> v(q);
for (int i = 0; i < q; ++i) {
cin >> v[i].first.first >> v[i].first.second;
v[i].first.first--, v[i].first.second--;
v[i].second = i;
}
sort(all(v), [&](pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
return x.first.second < y.first.second;
});
int j = 0;
vector<int> ans(q);
for (int i = 0; i < n; ++i) {
add(i, 1);
add(cur[i], -1);
while (j < len(v) && v[j].first.second == i) {
ans[v[j].second] = get(v[j].first.first, v[j].first.second);
++j;
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
| replace | 48 | 49 | 48 | 49 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define forl(i, a, n) for (int i = a; i < n; i++)
#define rfor(i, n, a) for (int i = n; i >= a; i--)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define ld long double
#define vi vector<int>
#define vll vector<long long>
#define pi pair<int, int>
#define pll pair<long long, long long>
#define mp make_pair
#define B begin()
#define E end()
#define S size()
#define m9 1000000007
using namespace std;
ll gcd(ll x, ll y) {
if (x == 0)
return y;
return gcd(y % x, x);
}
/*
ll gcdextended(ll a, ll b, ll *x, ll *y)
{
if (a == 0)
{
*x = 0, *y = 1;
return b;
}
ll x1, y1;
int gcd = gcdextended(b%a, a, &x1, &y1);
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
///RELATIVE COPRIME
ll modinverse(ll a, ll m)
{
ll x, y;
ll g = gcdextended(a, m, &x, &y);
if (g != 1)
return -1;
else
{
ll res = (x%m + m) % m;
return res;
}
}
*/
ll powmod(ll x, ll y, ll m) {
if (y == 0)
return 1;
ll p = powmod(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
/// M IS PRIME
ll modif(ll x, ll m) { return (powmod(x, m - 2, m)); }
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define pin(a) \
for (auto x : a) \
cout << x << " "; \
cout << endl
#define fck(a) \
cout << a; \
exit(0)
#define vvll vector<vector<ll>>
#define A(b) b.begin(), b.end()
#define vpi std::vector<pll>
#include <array>
#include <stack>
// include<bits/stdc++.h>
bool fa(vll &x, vll &y) { return x[0] < y[0]; }
bool fa1(vll &x, vll &y) { return x[1] < y[1]; }
bool f1(pll &x, pll &y) { return x.second > y.second; }
bool f2(pll &x, pll &y) { return x.first > y.first; }
bool f3(ll &x, ll &y) { return x > y; }
ll te;
ll n, qq;
ll block = 708;
bool meow(array<ll, 3> x, array<ll, 3> &y) {
if (x[0] / block != y[0] / block) {
return x[0] / block < y[0] / block;
} else {
return x[1] < y[1];
}
}
const ll maxn = 5e5 + 2;
ll d[maxn];
ll b[maxn];
ll cnt = 0;
void add(ll pos) {
d[b[pos]]++;
if (d[b[pos]] == 1)
cnt++;
}
void remove(ll pos) {
d[b[pos]]--;
if (d[b[pos]] == 0)
cnt--;
}
int main() {
fastio;
// freopen("test_input (1).xt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n >> qq;
forl(i, 0, n) d[i] = 0;
forl(i, 0, n) cin >> b[i];
std::vector<array<ll, 3>> q(qq);
forl(i, 0, qq) {
cin >> q[i][0] >> q[i][1];
q[i][0]--;
q[i][1]--;
q[i][2] = i;
}
ll ans[qq];
forl(i, 0, qq) ans[i] = -1;
sort(q.B, q.E, meow);
// for(auto x:q){
// p2(x[0]+1,x[1]+1);
// }
ll ml = 0, mr = -1;
forl(i, 0, qq) {
ll l = q[i][0], r = q[i][1];
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][2]] = cnt;
}
for (auto x : ans) {
cout << x << "\n";
}
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define forl(i, a, n) for (int i = a; i < n; i++)
#define rfor(i, n, a) for (int i = n; i >= a; i--)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define ld long double
#define vi vector<int>
#define vll vector<long long>
#define pi pair<int, int>
#define pll pair<long long, long long>
#define mp make_pair
#define B begin()
#define E end()
#define S size()
#define m9 1000000007
using namespace std;
ll gcd(ll x, ll y) {
if (x == 0)
return y;
return gcd(y % x, x);
}
/*
ll gcdextended(ll a, ll b, ll *x, ll *y)
{
if (a == 0)
{
*x = 0, *y = 1;
return b;
}
ll x1, y1;
int gcd = gcdextended(b%a, a, &x1, &y1);
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
///RELATIVE COPRIME
ll modinverse(ll a, ll m)
{
ll x, y;
ll g = gcdextended(a, m, &x, &y);
if (g != 1)
return -1;
else
{
ll res = (x%m + m) % m;
return res;
}
}
*/
ll powmod(ll x, ll y, ll m) {
if (y == 0)
return 1;
ll p = powmod(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
/// M IS PRIME
ll modif(ll x, ll m) { return (powmod(x, m - 2, m)); }
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define pin(a) \
for (auto x : a) \
cout << x << " "; \
cout << endl
#define fck(a) \
cout << a; \
exit(0)
#define vvll vector<vector<ll>>
#define A(b) b.begin(), b.end()
#define vpi std::vector<pll>
#include <array>
#include <stack>
// include<bits/stdc++.h>
bool fa(vll &x, vll &y) { return x[0] < y[0]; }
bool fa1(vll &x, vll &y) { return x[1] < y[1]; }
bool f1(pll &x, pll &y) { return x.second > y.second; }
bool f2(pll &x, pll &y) { return x.first > y.first; }
bool f3(ll &x, ll &y) { return x > y; }
ll te;
ll n, qq;
ll block = 800;
bool meow(array<ll, 3> x, array<ll, 3> &y) {
if (x[0] / block != y[0] / block) {
return x[0] / block < y[0] / block;
} else {
return x[1] < y[1];
}
}
const ll maxn = 5e5 + 2;
ll d[maxn];
ll b[maxn];
ll cnt = 0;
void add(ll pos) {
d[b[pos]]++;
if (d[b[pos]] == 1)
cnt++;
}
void remove(ll pos) {
d[b[pos]]--;
if (d[b[pos]] == 0)
cnt--;
}
int main() {
fastio;
// freopen("test_input (1).xt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n >> qq;
forl(i, 0, n) d[i] = 0;
forl(i, 0, n) cin >> b[i];
std::vector<array<ll, 3>> q(qq);
forl(i, 0, qq) {
cin >> q[i][0] >> q[i][1];
q[i][0]--;
q[i][1]--;
q[i][2] = i;
}
ll ans[qq];
forl(i, 0, qq) ans[i] = -1;
sort(q.B, q.E, meow);
// for(auto x:q){
// p2(x[0]+1,x[1]+1);
// }
ll ml = 0, mr = -1;
forl(i, 0, qq) {
ll l = q[i][0], r = q[i][1];
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][2]] = cnt;
}
for (auto x : ans) {
cout << x << "\n";
}
return 0;
}
| replace | 111 | 112 | 111 | 112 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<ll, Pll> Plp;
template <class T, class S> void cmin(T &a, const S &b) {
if (a > b)
a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
if (a < b)
a = b;
}
template <class A> void PR(A a, ll n) {
rep(i, n) {
if (i)
cout << ' ';
cout << a[i];
}
cout << "\n";
}
ld PI = 3.14159265358979323846;
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;
}
T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};
int main() {
ll N, Q;
cin >> N >> Q;
vector<ll> a(N), l(Q), r(Q), pi(N + 1, -1);
rep(i, N) { cin >> a[i]; }
vector<vector<Pll>> qs(N);
rep(i, Q) {
cin >> l[i] >> r[i];
l[i]--;
r[i]--;
qs[l[i]].push_back(Pll(r[i], i));
}
vector<vector<ll>> ps(N);
rep(i, N) {
ll l = pi[a[i]];
if (l != -1)
ps[l].push_back(i);
pi[a[i]] = i;
}
BIT<ll> d(N);
vector<ll> ans(N);
for (ll i = N - 1; i >= 0; i--) {
for (auto &y : ps[i])
d.add(y, 1);
for (auto &p : qs[i]) {
ans[p.se] = (p.fi - i + 1) - d.sum(p.fi);
}
}
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 REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<ll, Pll> Plp;
template <class T, class S> void cmin(T &a, const S &b) {
if (a > b)
a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
if (a < b)
a = b;
}
template <class A> void PR(A a, ll n) {
rep(i, n) {
if (i)
cout << ' ';
cout << a[i];
}
cout << "\n";
}
ld PI = 3.14159265358979323846;
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;
}
T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};
int main() {
ll N, Q;
cin >> N >> Q;
vector<ll> a(N), l(Q), r(Q), pi(N + 1, -1);
rep(i, N) { cin >> a[i]; }
vector<vector<Pll>> qs(N);
rep(i, Q) {
cin >> l[i] >> r[i];
l[i]--;
r[i]--;
qs[l[i]].push_back(Pll(r[i], i));
}
vector<vector<ll>> ps(N);
rep(i, N) {
ll l = pi[a[i]];
if (l != -1)
ps[l].push_back(i);
pi[a[i]] = i;
}
BIT<ll> d(N);
vector<ll> ans(Q);
for (ll i = N - 1; i >= 0; i--) {
for (auto &y : ps[i])
d.add(y, 1);
for (auto &p : qs[i]) {
ans[p.se] = (p.fi - i + 1) - d.sum(p.fi);
}
}
rep(i, Q) cout << ans[i] << endl;
} | replace | 69 | 70 | 69 | 70 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
using namespace std;
struct req {
int l, r, val, idx;
bool t;
};
bool operator<(req a, req b) {
if (a.r == b.r) {
return a.t < b.t;
}
return a.r < b.r;
}
int bit[500001];
void add(int y, int val) {
while (y <= 500000) {
bit[y] += val;
y += (y & -y);
}
}
int sum(int y) {
int res = 0;
while (y > 0) {
res += bit[y];
y -= (y & -y);
}
return res;
}
bool visited[2000001];
req arr[600000];
int main() {
int N, Q, i;
int *prev = new int[2000001];
for (i = 0; i <= 2000000; ++i) {
visited[i] = false;
prev[i] = 0;
}
scanf("%d", &N);
scanf("%d", &Q);
for (i = 1; i <= N; ++i) {
add(i, 1);
}
for (i = 0; i < N; ++i) {
scanf("%d", &arr[i].val);
arr[i].idx = i + 1;
arr[i].t = 0;
arr[i].r = i + 1;
}
for (i = N; i < N + Q; ++i) {
scanf("%d%d", &arr[i].l, &arr[i].r);
arr[i].idx = i - N;
arr[i].t = 1;
}
sort(arr, arr + N + Q);
int ans[Q];
for (i = 0; i < N + Q; ++i) {
if (arr[i].t) {
ans[arr[i].idx] = sum(arr[i].r) - sum(arr[i].l - 1);
} else {
if (visited[arr[i].val]) {
if (prev[arr[i].val] != 0) {
add(prev[arr[i].val], -1);
}
}
visited[arr[i].val] = true;
prev[arr[i].val] = arr[i].idx;
}
}
for (i = 0; i < Q; ++i) {
printf("%d\n", ans[i]);
}
return 0;
} | #include <algorithm>
#include <cstdio>
using namespace std;
struct req {
int l, r, val, idx;
bool t;
};
bool operator<(req a, req b) {
if (a.r == b.r) {
return a.t < b.t;
}
return a.r < b.r;
}
int bit[500001];
void add(int y, int val) {
while (y <= 500000) {
bit[y] += val;
y += (y & -y);
}
}
int sum(int y) {
int res = 0;
while (y > 0) {
res += bit[y];
y -= (y & -y);
}
return res;
}
bool visited[2000001];
req arr[1000005];
int main() {
int N, Q, i;
int *prev = new int[2000001];
for (i = 0; i <= 2000000; ++i) {
visited[i] = false;
prev[i] = 0;
}
scanf("%d", &N);
scanf("%d", &Q);
for (i = 1; i <= N; ++i) {
add(i, 1);
}
for (i = 0; i < N; ++i) {
scanf("%d", &arr[i].val);
arr[i].idx = i + 1;
arr[i].t = 0;
arr[i].r = i + 1;
}
for (i = N; i < N + Q; ++i) {
scanf("%d%d", &arr[i].l, &arr[i].r);
arr[i].idx = i - N;
arr[i].t = 1;
}
sort(arr, arr + N + Q);
int ans[Q];
for (i = 0; i < N + Q; ++i) {
if (arr[i].t) {
ans[arr[i].idx] = sum(arr[i].r) - sum(arr[i].l - 1);
} else {
if (visited[arr[i].val]) {
if (prev[arr[i].val] != 0) {
add(prev[arr[i].val], -1);
}
}
visited[arr[i].val] = true;
prev[arr[i].val] = arr[i].idx;
}
}
for (i = 0; i < Q; ++i) {
printf("%d\n", ans[i]);
}
return 0;
} | replace | 36 | 37 | 36 | 37 | 0 | |
p02599 | C++ | Runtime Error | // Patwari26
// #include template<t> as mine
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define endl '\n'
#define mii map<ll, ll>
#define pii pair<ll, ll>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define INF (1ll << 60)
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define time \
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
const int N = 500005;
// We will represent each query as three numbers: L, R, idx. Idx is
// the position (in original order) of this query.
pair<pair<int, int>, int> queries[100500];
int BLOCK_SIZE;
int arr[500005];
long long cnt[500005];
// Essential part of Mo's algorithm: comparator, which we will
// use with std::sort. It is a function, which must return True
// if query x must come earlier than query y, and False otherwise.
inline bool mo_cmp(const pair<pair<int, int>, int> &x,
const pair<pair<int, int>, int> &y) {
int block_x = x.first.first / BLOCK_SIZE;
int block_y = y.first.first / BLOCK_SIZE;
if (block_x != block_y)
return block_x < block_y;
return x.first.second < y.first.second;
}
ll val[N];
void solve() {
ll n, q;
cin >> n;
cin >> q;
BLOCK_SIZE = static_cast<int>(sqrt(n));
rep(i, 0, n - 1) cin >> arr[i];
rep(i, 0, q - 1) {
cin >> queries[i].first.first >> queries[i].first.second;
queries[i].second = i;
}
sort(queries, queries + q, mo_cmp);
int mo_left = 0, mo_right = -1;
ll cc = 0;
ll ans[q];
for (int i = 0; i < q; i++) {
// [left, right] is what query we must answer now.
int left = queries[i].first.first - 1; // -1 for zero based indexing
int right = queries[i].first.second - 1; // -1 for zero based indexing
// Usual part of applying Mo's algorithm: moving mo_left
// and mo_right.
while (mo_right < right) {
mo_right++;
cnt[arr[mo_right]]++;
if (cnt[arr[mo_right]] == 1)
cc++;
}
while (mo_right > right) {
if (cnt[arr[mo_right]] == 1)
cc--;
cnt[arr[mo_right]]--;
mo_right--;
}
while (mo_left < left) {
if (cnt[arr[mo_left]] == 1)
cc--;
cnt[arr[mo_left]]--;
mo_left++;
}
while (mo_left > left) {
mo_left--;
cnt[arr[mo_left]]++;
if (cnt[arr[mo_left]] == 1)
cc++;
}
ans[queries[i].second] = cc;
}
rep(i, 0, q - 1) { cout << ans[i] << endl; }
}
signed main() {
ios int TESTS = 1;
// cin>>TESTS;
while (TESTS--) {
solve();
}
// time
return 0;
} | // Patwari26
// #include template<t> as mine
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define endl '\n'
#define mii map<ll, ll>
#define pii pair<ll, ll>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define INF (1ll << 60)
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define time \
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
const int N = 500005;
// We will represent each query as three numbers: L, R, idx. Idx is
// the position (in original order) of this query.
pair<pair<int, int>, int> queries[500500];
int BLOCK_SIZE;
int arr[500005];
long long cnt[500005];
// Essential part of Mo's algorithm: comparator, which we will
// use with std::sort. It is a function, which must return True
// if query x must come earlier than query y, and False otherwise.
inline bool mo_cmp(const pair<pair<int, int>, int> &x,
const pair<pair<int, int>, int> &y) {
int block_x = x.first.first / BLOCK_SIZE;
int block_y = y.first.first / BLOCK_SIZE;
if (block_x != block_y)
return block_x < block_y;
return x.first.second < y.first.second;
}
ll val[N];
void solve() {
ll n, q;
cin >> n;
cin >> q;
BLOCK_SIZE = static_cast<int>(sqrt(n));
rep(i, 0, n - 1) cin >> arr[i];
rep(i, 0, q - 1) {
cin >> queries[i].first.first >> queries[i].first.second;
queries[i].second = i;
}
sort(queries, queries + q, mo_cmp);
int mo_left = 0, mo_right = -1;
ll cc = 0;
ll ans[q];
for (int i = 0; i < q; i++) {
// [left, right] is what query we must answer now.
int left = queries[i].first.first - 1; // -1 for zero based indexing
int right = queries[i].first.second - 1; // -1 for zero based indexing
// Usual part of applying Mo's algorithm: moving mo_left
// and mo_right.
while (mo_right < right) {
mo_right++;
cnt[arr[mo_right]]++;
if (cnt[arr[mo_right]] == 1)
cc++;
}
while (mo_right > right) {
if (cnt[arr[mo_right]] == 1)
cc--;
cnt[arr[mo_right]]--;
mo_right--;
}
while (mo_left < left) {
if (cnt[arr[mo_left]] == 1)
cc--;
cnt[arr[mo_left]]--;
mo_left++;
}
while (mo_left > left) {
mo_left--;
cnt[arr[mo_left]]++;
if (cnt[arr[mo_left]] == 1)
cc++;
}
ans[queries[i].second] = cc;
}
rep(i, 0, q - 1) { cout << ans[i] << endl; }
}
signed main() {
ios int TESTS = 1;
// cin>>TESTS;
while (TESTS--) {
solve();
}
// time
return 0;
} | replace | 30 | 31 | 30 | 31 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
#define fi first
#define se second
#define ll long long
#define dl double long
using namespace std;
const int NN = 1e9 + 7;
const int N = 2e5 + 7;
const int M = 26;
const ll mod = 1e9 + 7;
const int inf = 1e9 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, q;
int c[N];
int t[N];
int res[N];
int last[N];
vector<pair<int, int>> qr[N];
void upd(int x, int y) {
while (x < N) {
t[x] += y;
x += x & -x;
}
}
int get(int x) {
int res = 0;
while (x > 0) {
res += t[x];
x -= x & -x;
}
return res;
}
void solve1() {
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> c[i];
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
qr[r].push_back({l, i});
}
for (int i = 1; i <= n; i++) {
if (last[c[i]] != 0)
upd(last[c[i]], -1);
upd(i, 1);
last[c[i]] = i;
for (auto x : qr[i]) {
res[x.se] = get(i) - get(x.fi - 1);
}
}
for (int i = 1; i <= q; i++) {
cout << res[i] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen( "input.txt" , "r" , stdin );
// freopen( "output.txt" , "w" , stdout );
int cghf = 1; // cin >> cghf;
while (cghf--) {
solve1();
}
}
| #include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
#define fi first
#define se second
#define ll long long
#define dl double long
using namespace std;
const int NN = 1e9 + 7;
const int N = 1e6 + 7;
const int M = 26;
const ll mod = 1e9 + 7;
const int inf = 1e9 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, q;
int c[N];
int t[N];
int res[N];
int last[N];
vector<pair<int, int>> qr[N];
void upd(int x, int y) {
while (x < N) {
t[x] += y;
x += x & -x;
}
}
int get(int x) {
int res = 0;
while (x > 0) {
res += t[x];
x -= x & -x;
}
return res;
}
void solve1() {
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> c[i];
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
qr[r].push_back({l, i});
}
for (int i = 1; i <= n; i++) {
if (last[c[i]] != 0)
upd(last[c[i]], -1);
upd(i, 1);
last[c[i]] = i;
for (auto x : qr[i]) {
res[x.se] = get(i) - get(x.fi - 1);
}
}
for (int i = 1; i <= q; i++) {
cout << res[i] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen( "input.txt" , "r" , stdin );
// freopen( "output.txt" , "w" , stdout );
int cghf = 1; // cin >> cghf;
while (cghf--) {
solve1();
}
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// TEMPLATE
#define pb push_back
#define mp make_pair
#define ll int
#define ld long double
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define F first
#define S second
#define newline printf("\n")
#define minusone printf("-1\n")
#define zeroo printf("0\n")
#define scl1(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld %lld", &a, &b)
#define scl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define prl1(a) printf("%lld\n", a)
#define prl2(a, b) printf("%lld %lld\n", a, b)
#define ssl1(s) scanf(" %[^\n]", s)
#define scd1(a) scanf("%lf", &a)
#define scd2(a, b) scanf("%lf %lf", &a, &b)
#define prd(a) printf("%lf\n", a)
#define prld(a) printf("%Lf\n", a)
#define prcase(cs) printf("Case %lld: ", cs)
#define cin1(a) cin >> a
#define cin2(a, b) cin >> a >> b
#define cin3(a, b, c) cin >> a >> b >> c
#define cin4(a, b, c, d) cin >> a >> b >> c >> d
#define cot1(a) cout << a << "\n"
#define cot2(a, b) cout << a << " " << b << "\n"
#define cot3(a, b, c) cout << a << " " << b << " " << c << "\n"
#define cot4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\n"
#define cotline cout << "\n"
#define cotminus cout << "-1\n"
#define cot0 cout << "0\n"
#define cotyes cout << "YES\n"
#define cotno cout << "NO\n"
#define cotcase(cs) cout << "Case " << cs << ": "
#define reversed(s) reverse(s.begin(), s.end())
#define asort(s) sort(s.begin(), s.end())
#define dsort(s) sort(s.rbegin(), s.rend())
#define all(s) s.begin(), s.end()
#define uniq(s) s.resize(distance(s.begin(), unique(s.begin(), s.end())))
#define found(s, x) (s.find(x) != s.end())
#define for0(i, n) for (i = 0; i < n; i++)
#define for1(i, n) for (i = 1; i <= n; i++)
#define fora(i, a, b) for (i = a; i <= b; i++)
#define forb(i, b, a) for (i = b; i >= a; i--)
#define fori(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define FR \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define ms(a, x) memset(a, x, sizeof a)
#define bitcount(n) __builtin_popcountll(n)
// #define pi 3.1415926536
#define pi acos(-1)
const ll INF = LLONG_MAX;
const ll SZ = 5e5 + 5;
const ll mod = 1e9 + 7;
struct query {
ll l, r, id;
} q[SZ];
ll ans[SZ];
ll cnt[SZ];
ll n, total_q, block_size;
ll l = 0, r = -1, total = 0;
ll a[SZ];
bool cmp(query a, query b) {
ll block_a = a.l / block_size;
ll block_b = b.l / block_size;
if (block_a == block_b)
return a.r < b.r;
return block_a < block_b;
}
void add(ll in) {
cnt[a[in]]++;
if (cnt[a[in]] == 1)
total++;
}
void remove(ll in) {
cnt[a[in]]--;
if (cnt[a[in]] == 0)
total--;
}
int main() {
FR;
ll cs = 0, tc = 1, x, y, z, i, j, k, g, p, sum = 0, c = 0, t = 0;
// ll a, b, d;
string s, s1, s2;
cin2(n, total_q);
for0(i, n) cin1(a[i]);
block_size = 800;
for0(i, total_q) {
cin2(x, y);
x--, y--;
q[i].l = x, q[i].r = y;
q[i].id = i;
}
sort(q, q + total_q, cmp);
for0(i, total_q) {
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] = total;
}
for0(i, total_q) cot1(ans[i]);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// TEMPLATE
#define pb push_back
#define mp make_pair
#define ll int
#define ld long double
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define F first
#define S second
#define newline printf("\n")
#define minusone printf("-1\n")
#define zeroo printf("0\n")
#define scl1(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld %lld", &a, &b)
#define scl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define prl1(a) printf("%lld\n", a)
#define prl2(a, b) printf("%lld %lld\n", a, b)
#define ssl1(s) scanf(" %[^\n]", s)
#define scd1(a) scanf("%lf", &a)
#define scd2(a, b) scanf("%lf %lf", &a, &b)
#define prd(a) printf("%lf\n", a)
#define prld(a) printf("%Lf\n", a)
#define prcase(cs) printf("Case %lld: ", cs)
#define cin1(a) cin >> a
#define cin2(a, b) cin >> a >> b
#define cin3(a, b, c) cin >> a >> b >> c
#define cin4(a, b, c, d) cin >> a >> b >> c >> d
#define cot1(a) cout << a << "\n"
#define cot2(a, b) cout << a << " " << b << "\n"
#define cot3(a, b, c) cout << a << " " << b << " " << c << "\n"
#define cot4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\n"
#define cotline cout << "\n"
#define cotminus cout << "-1\n"
#define cot0 cout << "0\n"
#define cotyes cout << "YES\n"
#define cotno cout << "NO\n"
#define cotcase(cs) cout << "Case " << cs << ": "
#define reversed(s) reverse(s.begin(), s.end())
#define asort(s) sort(s.begin(), s.end())
#define dsort(s) sort(s.rbegin(), s.rend())
#define all(s) s.begin(), s.end()
#define uniq(s) s.resize(distance(s.begin(), unique(s.begin(), s.end())))
#define found(s, x) (s.find(x) != s.end())
#define for0(i, n) for (i = 0; i < n; i++)
#define for1(i, n) for (i = 1; i <= n; i++)
#define fora(i, a, b) for (i = a; i <= b; i++)
#define forb(i, b, a) for (i = b; i >= a; i--)
#define fori(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define FR \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define ms(a, x) memset(a, x, sizeof a)
#define bitcount(n) __builtin_popcountll(n)
// #define pi 3.1415926536
#define pi acos(-1)
const ll INF = LLONG_MAX;
const ll SZ = 5e5 + 5;
const ll mod = 1e9 + 7;
struct query {
ll l, r, id;
} q[SZ];
ll ans[SZ];
ll cnt[SZ];
ll n, total_q, block_size;
ll l = 0, r = -1, total = 0;
ll a[SZ];
bool cmp(query a, query b) {
ll block_a = a.l / block_size;
ll block_b = b.l / block_size;
if (block_a == block_b)
return a.r < b.r;
return block_a < block_b;
}
void add(ll in) {
cnt[a[in]]++;
if (cnt[a[in]] == 1)
total++;
}
void remove(ll in) {
cnt[a[in]]--;
if (cnt[a[in]] == 0)
total--;
}
int main() {
FR;
ll cs = 0, tc = 1, x, y, z, i, j, k, g, p, sum = 0, c = 0, t = 0;
// ll a, b, d;
string s, s1, s2;
cin2(n, total_q);
for0(i, n) cin1(a[i]);
block_size = 1000;
for0(i, total_q) {
cin2(x, y);
x--, y--;
q[i].l = x, q[i].r = y;
q[i].id = i;
}
sort(q, q + total_q, cmp);
for0(i, total_q) {
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] = total;
}
for0(i, total_q) cot1(ans[i]);
return 0;
}
| replace | 108 | 109 | 108 | 109 | TLE | |
p02599 | C++ | Time Limit Exceeded | #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;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<long long, long long> ii;
// typedef complex<long double> point;
ll n;
const ll s = 700;
struct querie {
ll l, r, id;
bool operator<(const querie &rhs) const {
return make_pair(l / s, r) < make_pair(rhs.l / s, rhs.r);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
ll q;
cin >> q;
vector<ll> arr(n);
for (int z = 0; z < n; z++) {
cin >> arr[z];
}
vector<querie> que(q);
for (int z = 0; z < q; z++) {
cin >> que[z].l >> que[z].r;
que[z].l--;
que[z].r--;
que[z].id = z;
}
sort(que.begin(), que.end());
ll l = 0;
ll r = 0;
ll count = 1;
vector<ll> mapp(n + 1);
mapp[arr[0]]++;
vector<ll> ans(q);
for (int z = 0; z < q; z++) {
while (que[z].l < l) {
l--;
mapp[arr[l]]++;
if (mapp[arr[l]] == 1)
count++;
}
while (que[z].l > l) {
mapp[arr[l]]--;
if (mapp[arr[l]] == 0)
count--;
l++;
}
while (que[z].r < r) {
mapp[arr[r]]--;
if (mapp[arr[r]] == 0)
count--;
r--;
}
while (que[z].r > r) {
r++;
mapp[arr[r]]++;
if (mapp[arr[r]] == 1)
count++;
}
ans[que[z].id] = count;
}
for (ll i : ans) {
cout << i << endl;
}
}
| #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;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<long long, long long> ii;
// typedef complex<long double> point;
ll n;
const ll s = 700;
struct querie {
ll l, r, id;
bool operator<(const querie &rhs) const {
if (l / s != rhs.l / s)
return make_pair(l, r) < make_pair(rhs.l, rhs.r);
return (l / s & 1) ? (r < rhs.r) : (r > rhs.r);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
ll q;
cin >> q;
vector<ll> arr(n);
for (int z = 0; z < n; z++) {
cin >> arr[z];
}
vector<querie> que(q);
for (int z = 0; z < q; z++) {
cin >> que[z].l >> que[z].r;
que[z].l--;
que[z].r--;
que[z].id = z;
}
sort(que.begin(), que.end());
ll l = 0;
ll r = 0;
ll count = 1;
vector<ll> mapp(n + 1);
mapp[arr[0]]++;
vector<ll> ans(q);
for (int z = 0; z < q; z++) {
while (que[z].l < l) {
l--;
mapp[arr[l]]++;
if (mapp[arr[l]] == 1)
count++;
}
while (que[z].l > l) {
mapp[arr[l]]--;
if (mapp[arr[l]] == 0)
count--;
l++;
}
while (que[z].r < r) {
mapp[arr[r]]--;
if (mapp[arr[r]] == 0)
count--;
r--;
}
while (que[z].r > r) {
r++;
mapp[arr[r]]++;
if (mapp[arr[r]] == 1)
count++;
}
ans[que[z].id] = count;
}
for (ll i : ans) {
cout << i << endl;
}
}
| replace | 22 | 23 | 22 | 25 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<ll, ll>
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
ll mod = 1e9 + 7;
// const double pi = acos(-1.0);
/*
ll kmp(string x)
{
ll n= x.length();
//cout<<x<<endl;
vector<ll> lcp(x.length(), 0);
for(ll i=1;i<n;i++)
{
ll j=lcp[i-1];
while(j>0 && x[j]!=x[i])
j=lcp[j-1];
if(x[i]==x[j])
j++;
lcp[i]=j;
}
return lcp[n-1];
}
int N = 1000002;
int ar[100002];
void sieve()
{
for(int i=1;i<=N;i++)
ar[i] = 1;
ar[0] = 0;
for(int i=2;i*i<=N;i++)
{
if(ar[i] && i*i<=N)
{
for(int j = i*i; j<=N;j+=i)
ar[j] = 0;
}
}
}
ll modex(ll a,ll b){
if(b<=0){
return 1;
}
if(b%2==0)
{
ll x = modex(a, b/2);
return ((x*x)%mod);
}
else
return ((a%mod * modex(a, b-1))%mod);
}
ll c[1010][1010];
ll combina(ll i, ll j){
for (int i=0;i<=1005;i++)
for (int j=0;j<=i;j++)
{
if (j==i||j==0)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
c[i][j]%=mod;
}
return c[i][j];
}
void find_fac(int x){
vector<ll> di;
for(ll i = 2; i*i <= x; i++)
{
if(x%i==0){
di.push_back(i);
while(x%i==0)
x/=i;
}
}
if(x > 1)
di.push_back(x);
}
*/
vector<int> ar; // Bit Array
vector<int> a;
int la[100001];
ll n;
struct node {
int l, r, in;
};
vector<node> qu;
int comp(node &a, node &b) { return a.r < b.r; }
void update(ll x, ll v) {
// cout<<x<<endl;
// x += 1;
while (x <= (n)) {
// cout<<x<<endl;
ar[x] += v;
x += (x & (-x));
}
}
ll query(ll x) {
// x+=1;
ll an = 0;
while (x > 0) {
an += ar[x];
x = x & (x - 1);
}
return an;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed;
cout.precision(12);
ll q;
cin >> n >> q;
a.resize(n);
for (ll i = 0; i < n; i++)
cin >> a[i];
memset(la, -1, sizeof(la));
ar.assign(n + 1, 0);
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
qu.push_back({l, r, i});
}
sort(qu.begin(), qu.end(), comp);
ll j = 0;
/*for(ll i=0;i<q;i++)
cout<<qu[i].l<<" "<<qu[i].r<<" "<<qu[i].in<<endl;*/
vector<int> an(q);
for (int i = 0; i < n; i++) {
// cout<<a[i]<<" "<<la[a[i]]<<endl;
if (la[a[i]] == -1) {
la[a[i]] = i + 1;
update(la[a[i]], 1);
} else {
update(la[a[i]], -1);
la[a[i]] = i + 1;
update(la[a[i]], 1);
}
while (j < q && qu[j].r == (i + 1)) {
int l = qu[j].l, r = qu[j].r, in = qu[j].in;
// cout<<in<<" "<<query(r)<<" "<<query(l-1)<<endl;
an[in] = query(r) - query(l - 1);
j++;
}
}
for (ll i = 0; i < q; i++)
cout << an[i] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<ll, ll>
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
ll mod = 1e9 + 7;
// const double pi = acos(-1.0);
/*
ll kmp(string x)
{
ll n= x.length();
//cout<<x<<endl;
vector<ll> lcp(x.length(), 0);
for(ll i=1;i<n;i++)
{
ll j=lcp[i-1];
while(j>0 && x[j]!=x[i])
j=lcp[j-1];
if(x[i]==x[j])
j++;
lcp[i]=j;
}
return lcp[n-1];
}
int N = 1000002;
int ar[100002];
void sieve()
{
for(int i=1;i<=N;i++)
ar[i] = 1;
ar[0] = 0;
for(int i=2;i*i<=N;i++)
{
if(ar[i] && i*i<=N)
{
for(int j = i*i; j<=N;j+=i)
ar[j] = 0;
}
}
}
ll modex(ll a,ll b){
if(b<=0){
return 1;
}
if(b%2==0)
{
ll x = modex(a, b/2);
return ((x*x)%mod);
}
else
return ((a%mod * modex(a, b-1))%mod);
}
ll c[1010][1010];
ll combina(ll i, ll j){
for (int i=0;i<=1005;i++)
for (int j=0;j<=i;j++)
{
if (j==i||j==0)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
c[i][j]%=mod;
}
return c[i][j];
}
void find_fac(int x){
vector<ll> di;
for(ll i = 2; i*i <= x; i++)
{
if(x%i==0){
di.push_back(i);
while(x%i==0)
x/=i;
}
}
if(x > 1)
di.push_back(x);
}
*/
vector<int> ar; // Bit Array
vector<int> a;
int la[500001];
ll n;
struct node {
int l, r, in;
};
vector<node> qu;
int comp(node &a, node &b) { return a.r < b.r; }
void update(ll x, ll v) {
// cout<<x<<endl;
// x += 1;
while (x <= (n)) {
// cout<<x<<endl;
ar[x] += v;
x += (x & (-x));
}
}
ll query(ll x) {
// x+=1;
ll an = 0;
while (x > 0) {
an += ar[x];
x = x & (x - 1);
}
return an;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed;
cout.precision(12);
ll q;
cin >> n >> q;
a.resize(n);
for (ll i = 0; i < n; i++)
cin >> a[i];
memset(la, -1, sizeof(la));
ar.assign(n + 1, 0);
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
qu.push_back({l, r, i});
}
sort(qu.begin(), qu.end(), comp);
ll j = 0;
/*for(ll i=0;i<q;i++)
cout<<qu[i].l<<" "<<qu[i].r<<" "<<qu[i].in<<endl;*/
vector<int> an(q);
for (int i = 0; i < n; i++) {
// cout<<a[i]<<" "<<la[a[i]]<<endl;
if (la[a[i]] == -1) {
la[a[i]] = i + 1;
update(la[a[i]], 1);
} else {
update(la[a[i]], -1);
la[a[i]] = i + 1;
update(la[a[i]], 1);
}
while (j < q && qu[j].r == (i + 1)) {
int l = qu[j].l, r = qu[j].r, in = qu[j].in;
// cout<<in<<" "<<query(r)<<" "<<query(l-1)<<endl;
an[in] = query(r) - query(l - 1);
j++;
}
}
for (ll i = 0; i < q; i++)
cout << an[i] << endl;
return 0;
}
| replace | 90 | 91 | 90 | 91 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second
using namespace std;
pair<int, pair<int, int>> q[200001];
int n, m, a[100001], ans[200001];
int st[5 * 100001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
if (a.s.f != b.s.f)
return a.s.f < b.s.f;
return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
if (l == r) {
st[v] = d;
return;
}
int mid = (l + r) / 2;
if (x <= mid)
update(v * 2, l, mid, x, d);
else
update(v * 2 + 1, mid + 1, r, x, d);
st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
if (l > B || r < A)
return 0;
if (A == l && B == r)
return st[v];
int mid = (A + B) / 2;
return Find(v * 2, A, mid, l, min(mid, r)) +
Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++)
scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
sort(q + 1, q + m + 1, cmp);
int j = 1;
for (int i = 1; i <= m; i++) {
while (j <= q[i].s.f) {
if (!used[a[j]]) {
update(1, 1, n, j, 1);
used[a[j]] = j;
} else {
update(1, 1, n, used[a[j]], 0);
update(1, 1, n, j, 1);
used[a[j]] = j;
}
j++;
}
ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second
using namespace std;
pair<int, pair<int, int>> q[500005];
int n, m, a[500005], ans[500005];
int st[25 * 100001], used[5000005];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
if (a.s.f != b.s.f)
return a.s.f < b.s.f;
return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
if (l == r) {
st[v] = d;
return;
}
int mid = (l + r) / 2;
if (x <= mid)
update(v * 2, l, mid, x, d);
else
update(v * 2 + 1, mid + 1, r, x, d);
st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
if (l > B || r < A)
return 0;
if (A == l && B == r)
return st[v];
int mid = (A + B) / 2;
return Find(v * 2, A, mid, l, min(mid, r)) +
Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++)
scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
sort(q + 1, q + m + 1, cmp);
int j = 1;
for (int i = 1; i <= m; i++) {
while (j <= q[i].s.f) {
if (!used[a[j]]) {
update(1, 1, n, j, 1);
used[a[j]] = j;
} else {
update(1, 1, n, used[a[j]], 0);
update(1, 1, n, j, 1);
used[a[j]] = j;
}
j++;
}
ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
} | replace | 8 | 11 | 8 | 11 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int A[MAXN], B[MAXN];
vector<int> T[4 * MAXN];
void build(int i, int l, int r) {
if (l == r)
T[i].push_back(B[r]);
else {
int m = (l + r) / 2;
build(2 * i, l, m);
build(2 * i + 1, m + 1, r);
merge(T[2 * i].begin(), T[2 * i].end(), T[2 * i + 1].begin(),
T[2 * i + 1].end(), back_inserter(T[i]));
}
}
int ask(int i, int l, int r, int ql, int qr) {
if (r < ql || qr < l)
return 0;
if (ql <= l && r <= qr)
return lower_bound(T[i].begin(), T[i].end(), ql) - T[i].begin();
int m = (l + r) / 2;
return ask(2 * i, l, m, ql, qr) + ask(2 * i + 1, m + 1, r, ql, qr);
}
void solver() {
int N, M;
cin >> N >> M;
// cin >> N;
for (int i = 0; i < N; ++i)
cin >> A[i];
memset(B, -1, sizeof(B));
map<int, int> last;
for (int i = 0; i < N; ++i) {
if (!last.count(A[i]))
B[i] = -1;
else
B[i] = last[A[i]];
last[A[i]] = i;
}
build(1, 0, N - 1);
// cin >> M;
while (M-- > 0) {
int left, right;
cin >> left >> right;
cout << ask(1, 0, N - 1, left - 1, right - 1) << '\n';
}
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(NULL);
int t;
// cin >> t;
t = 1;
while (t--)
solver();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 500001;
int A[MAXN], B[MAXN];
vector<int> T[4 * MAXN];
void build(int i, int l, int r) {
if (l == r)
T[i].push_back(B[r]);
else {
int m = (l + r) / 2;
build(2 * i, l, m);
build(2 * i + 1, m + 1, r);
merge(T[2 * i].begin(), T[2 * i].end(), T[2 * i + 1].begin(),
T[2 * i + 1].end(), back_inserter(T[i]));
}
}
int ask(int i, int l, int r, int ql, int qr) {
if (r < ql || qr < l)
return 0;
if (ql <= l && r <= qr)
return lower_bound(T[i].begin(), T[i].end(), ql) - T[i].begin();
int m = (l + r) / 2;
return ask(2 * i, l, m, ql, qr) + ask(2 * i + 1, m + 1, r, ql, qr);
}
void solver() {
int N, M;
cin >> N >> M;
// cin >> N;
for (int i = 0; i < N; ++i)
cin >> A[i];
memset(B, -1, sizeof(B));
map<int, int> last;
for (int i = 0; i < N; ++i) {
if (!last.count(A[i]))
B[i] = -1;
else
B[i] = last[A[i]];
last[A[i]] = i;
}
build(1, 0, N - 1);
// cin >> M;
while (M-- > 0) {
int left, right;
cin >> left >> right;
cout << ask(1, 0, N - 1, left - 1, right - 1) << '\n';
}
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(NULL);
int t;
// cin >> t;
t = 1;
while (t--)
solver();
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int c[maxn], vis[maxn], ans[maxn], tree[maxn];
vector<pair<int, int>> v[maxn];
int n, q;
int lowbit(int x) { return x & (-x); }
void add(int x, int k) {
while (x <= n) {
tree[x] += k;
x += lowbit(x);
}
}
int sum(int x) {
int ans = 0;
while (x != 0) {
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
int main() {
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> c[i];
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
v[r].push_back(make_pair(i, l));
}
for (int i = 1; i <= n; i++) {
if (vis[c[i]] != 0)
add(vis[c[i]], -1);
vis[c[i]] = i;
add(i, 1);
for (auto j : v[i])
ans[j.first] = sum(i) - sum(j.second - 1);
}
for (int i = 1; i <= q; i++)
cout << ans[i] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
const int inf = 0x3f3f3f3f;
int c[maxn], vis[maxn], ans[maxn], tree[maxn];
vector<pair<int, int>> v[maxn];
int n, q;
int lowbit(int x) { return x & (-x); }
void add(int x, int k) {
while (x <= n) {
tree[x] += k;
x += lowbit(x);
}
}
int sum(int x) {
int ans = 0;
while (x != 0) {
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
int main() {
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> c[i];
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
v[r].push_back(make_pair(i, l));
}
for (int i = 1; i <= n; i++) {
if (vis[c[i]] != 0)
add(vis[c[i]], -1);
vis[c[i]] = i;
add(i, 1);
for (auto j : v[i])
ans[j.first] = sum(i) - sum(j.second - 1);
}
for (int i = 1; i <= q; i++)
cout << ans[i] << endl;
return 0;
}
| replace | 2 | 3 | 2 | 3 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define se second
#define fr first
#define int long long int
#define pb push_back
#define inf 1e18
#define slld(x) scanf("%lld", &x)
#define plld(x) printf("%lld\n", x)
#define all(v) v.begin(), v.end()
#define CHAL_BAAP_KO_MT_SIKHA \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define mod 1000000007
#define MOD 998244353
#define endl "\n"
/*
Damn Fast:TheFuckinMastermind
while(How to solve the Question?){
Read the Question Again!!
}
do
Practise
#Motivation::::0/0
*/
const int N = 2e5;
vector<int> seg[4 * N + 1];
int a[N + 1], ans[N + 1];
vector<int> merge(vector<int> v1, vector<int> v2) {
int i = 0, j = 0;
vector<int> v;
while (i < v1.size() and j < v2.size()) {
if (v1[i] < v2[j]) {
v.pb(v1[i]);
i++;
} else {
v.pb(v2[j]);
j++;
}
}
while (i < v1.size()) {
v.pb(v1[i]);
i++;
}
while (j < v2.size()) {
v.pb(v2[j]);
j++;
}
return v;
}
void build(int l, int r, int idx) {
if (l == r) {
seg[idx].pb(ans[l]);
return;
}
int mid = (l + r) / 2;
build(l, mid, 2 * idx + 1);
build(mid + 1, r, 2 * idx + 2);
seg[idx] = merge(seg[2 * idx + 1], seg[2 * idx + 2]);
}
int qury(int l, int r, int ll, int rr, int idx) {
if (l > rr or r < ll)
return 0;
if (ll <= l and rr >= r) {
return (lower_bound(seg[idx].begin(), seg[idx].end(), ll) -
seg[idx].begin());
}
int mid = (l + r) / 2;
return qury(l, mid, ll, rr, 2 * idx + 1) +
qury(mid + 1, r, ll, rr, 2 * idx + 2);
}
signed main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("out.txt","w",stdout);
// #endif
CHAL_BAAP_KO_MT_SIKHA
int n, q;
cin >> n >> q;
// vector<int> ans(n+1,0);
map<int, int> mp;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (mp[a[i]] == 0) {
ans[i] = 0;
} else {
ans[i] = mp[a[i]];
}
mp[a[i]] = i;
}
build(1, n, 0);
while (q--) {
int l, r;
cin >> l >> r;
cout << qury(1, n, l, r, 0) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define se second
#define fr first
#define int long long int
#define pb push_back
#define inf 1e18
#define slld(x) scanf("%lld", &x)
#define plld(x) printf("%lld\n", x)
#define all(v) v.begin(), v.end()
#define CHAL_BAAP_KO_MT_SIKHA \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define mod 1000000007
#define MOD 998244353
#define endl "\n"
/*
Damn Fast:TheFuckinMastermind
while(How to solve the Question?){
Read the Question Again!!
}
do
Practise
#Motivation::::0/0
*/
const int N = 5e5;
vector<int> seg[4 * N + 1];
int a[N + 1], ans[N + 1];
vector<int> merge(vector<int> v1, vector<int> v2) {
int i = 0, j = 0;
vector<int> v;
while (i < v1.size() and j < v2.size()) {
if (v1[i] < v2[j]) {
v.pb(v1[i]);
i++;
} else {
v.pb(v2[j]);
j++;
}
}
while (i < v1.size()) {
v.pb(v1[i]);
i++;
}
while (j < v2.size()) {
v.pb(v2[j]);
j++;
}
return v;
}
void build(int l, int r, int idx) {
if (l == r) {
seg[idx].pb(ans[l]);
return;
}
int mid = (l + r) / 2;
build(l, mid, 2 * idx + 1);
build(mid + 1, r, 2 * idx + 2);
seg[idx] = merge(seg[2 * idx + 1], seg[2 * idx + 2]);
}
int qury(int l, int r, int ll, int rr, int idx) {
if (l > rr or r < ll)
return 0;
if (ll <= l and rr >= r) {
return (lower_bound(seg[idx].begin(), seg[idx].end(), ll) -
seg[idx].begin());
}
int mid = (l + r) / 2;
return qury(l, mid, ll, rr, 2 * idx + 1) +
qury(mid + 1, r, ll, rr, 2 * idx + 2);
}
signed main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("out.txt","w",stdout);
// #endif
CHAL_BAAP_KO_MT_SIKHA
int n, q;
cin >> n >> q;
// vector<int> ans(n+1,0);
map<int, int> mp;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (mp[a[i]] == 0) {
ans[i] = 0;
} else {
ans[i] = mp[a[i]];
}
mp[a[i]] = i;
}
build(1, n, 0);
while (q--) {
int l, r;
cin >> l >> r;
cout << qury(1, n, l, r, 0) << endl;
}
} | replace | 26 | 27 | 26 | 27 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int maxi = 2e5 + 5, n;
vector<int> val(maxi), ano(maxi);
vector<vector<int>> node;
void init() {
long long hei = ceil(log2(n));
hei = 2 * pow(2, hei);
node.clear();
node.resize(hei);
unordered_map<int, int> tmp;
for (int i = n; i; i--) {
if (tmp.find(val[i]) == tmp.end())
ano[i] = n + 1;
else
ano[i] = tmp[val[i]];
tmp[val[i]] = i;
}
}
vector<int> merge(vector<int> &a, vector<int> &b) {
vector<int> to_ret;
int i = 0, j = 0;
while (i < int(a.size()) && j < int(b.size()))
if (a[i] < b[j])
to_ret.emplace_back(a[i++]);
else
to_ret.emplace_back(b[j++]);
while (i < int(a.size()))
to_ret.emplace_back(a[i++]);
while (j < int(b.size()))
to_ret.emplace_back(b[j++]);
return to_ret;
}
void build(int cur, int start, int end) {
if (start == end) {
node[cur].emplace_back(ano[start + 1]);
return;
}
int mid = (start + end) / 2;
build(2 * cur, start, mid);
build(2 * cur + 1, mid + 1, end);
node[cur] = merge(node[2 * cur], node[2 * cur + 1]);
}
int query(int cur, int l, int r, int rl, int rr, int k) {
if (rr < l || r < rl)
return 0;
if (l >= rl && r <= rr)
return node[cur].size() -
(upper_bound(node[cur].begin(), node[cur].end(), k) -
node[cur].begin());
int mid = (l + r) / 2;
return query(2 * cur, l, mid, rl, rr, k) +
query(2 * cur + 1, mid + 1, r, rl, rr, k);
}
int solve2(int l, int r) {
r = min(r, n);
return query(1, 0, n - 1, l - 1, r - 1, r);
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int l, r;
int q;
cin >> n >> q;
for (int j = 1; j <= n; j++)
cin >> val[j];
init();
build(1, 0, n - 1);
while (q--) {
cin >> l >> r;
cout << solve2(l, r) << "\n";
}
} | #include <bits/stdc++.h>
using namespace std;
int maxi = 5e5 + 5, n;
vector<int> val(maxi), ano(maxi);
vector<vector<int>> node;
void init() {
long long hei = ceil(log2(n));
hei = 2 * pow(2, hei);
node.clear();
node.resize(hei);
unordered_map<int, int> tmp;
for (int i = n; i; i--) {
if (tmp.find(val[i]) == tmp.end())
ano[i] = n + 1;
else
ano[i] = tmp[val[i]];
tmp[val[i]] = i;
}
}
vector<int> merge(vector<int> &a, vector<int> &b) {
vector<int> to_ret;
int i = 0, j = 0;
while (i < int(a.size()) && j < int(b.size()))
if (a[i] < b[j])
to_ret.emplace_back(a[i++]);
else
to_ret.emplace_back(b[j++]);
while (i < int(a.size()))
to_ret.emplace_back(a[i++]);
while (j < int(b.size()))
to_ret.emplace_back(b[j++]);
return to_ret;
}
void build(int cur, int start, int end) {
if (start == end) {
node[cur].emplace_back(ano[start + 1]);
return;
}
int mid = (start + end) / 2;
build(2 * cur, start, mid);
build(2 * cur + 1, mid + 1, end);
node[cur] = merge(node[2 * cur], node[2 * cur + 1]);
}
int query(int cur, int l, int r, int rl, int rr, int k) {
if (rr < l || r < rl)
return 0;
if (l >= rl && r <= rr)
return node[cur].size() -
(upper_bound(node[cur].begin(), node[cur].end(), k) -
node[cur].begin());
int mid = (l + r) / 2;
return query(2 * cur, l, mid, rl, rr, k) +
query(2 * cur + 1, mid + 1, r, rl, rr, k);
}
int solve2(int l, int r) {
r = min(r, n);
return query(1, 0, n - 1, l - 1, r - 1, r);
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int l, r;
int q;
cin >> n >> q;
for (int j = 1; j <= n; j++)
cin >> val[j];
init();
build(1, 0, n - 1);
while (q--) {
cin >> l >> r;
cout << solve2(l, r) << "\n";
}
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02599 | C++ | Runtime Error | #ifndef DEBUG
#pragma GCC optimize("Ofast")
#endif
#include <bits/stdc++.h>
using namespace std;
#define hfor(var, s, e) \
for (int var = s; int(s) <= var && var < int(e); ++var) // half-opened range
#define hfori(var, s, e) \
for (int var = e - 1; int(s) <= var && var < int(e); --var) // inversion
#define hforo(var, s, e) \
int var = s; \
for (; int(s) <= var && var < int(e); ++var) // out declaration
#define hforoi(var, s, e) \
int var = e - 1; \
for (; int(s) <= var && var < int(e); --var)
#define cfor(var, s, e) hfor(var, s, (e) + 1) // closed range
#define cfori(var, s, e) hfori(var, s, (e) + 1)
#define cforo(var, s, e) hforo(var, s, (e) + 1)
#define cforoi(var, s, e) hforoi(var, s, (e) + 1)
#define rep(x) hfor(repi, 0, x)
#define all(x) x.begin(), x.end()
#define sum_in(s, e) accumulate(s, e, 0ll)
#define mri(it) make_reverse_iterator(it) //*mri(it) == *prev(it)
#define gcd __gcd
template <typename C, typename RI> RI rerase(C &c, RI ri) {
return next(mri(c.erase(prev(ri.base()))));
}
template <typename T> constexpr T inf() { return numeric_limits<T>::max() / 2; }
using f64 = double;
using i64 = long long;
using u64 = unsigned long long;
const f64 pi = acos(-1), eps = 1e-12;
const int prime = 998244353; // 1073741783 int(2e9+11) int(1e9+9)
const int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // r, cw
const int mod = int(1e9 + 7);
auto _dummy = (ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
(cout << fixed << setprecision(11)), srand((int)time(0)), 0);
#define endl '\n' // not interactive?
// #define int i64//overflow?
struct Q {
int s, e, i;
};
const int N = 5e5;
signed main() {
int n, m;
cin >> n >> m;
int a[N];
hfor(i, 0, n) cin >> a[i];
int bsz = 300;
vector<Q> v[1000];
hfor(i, 0, m) {
int s, e;
cin >> s >> e;
s--;
v[s / bsz].push_back({s, e, i});
}
hfor(i, 0, 1000) sort(all(v[i]), [](auto a, auto b) { return a.e < b.e; });
vector<pair<int, int>> ans;
hfor(i, 0, 1000) {
if (v[i].empty())
continue;
int buf[N + 1] = {
0,
};
int cnt = 0;
int normalized_s = v[i].front().s / bsz * bsz;
int j = normalized_s;
for (auto k : v[i]) {
while (j < k.e)
cnt += !buf[a[j++]]++;
hfor(l, normalized_s, k.s) cnt -= !--buf[a[l]];
ans.push_back({k.i, cnt});
hfor(l, normalized_s, k.s) cnt += !buf[a[l]]++;
}
}
sort(all(ans));
for (auto i : ans)
cout << i.second << endl;
return 0;
}
| #ifndef DEBUG
#pragma GCC optimize("Ofast")
#endif
#include <bits/stdc++.h>
using namespace std;
#define hfor(var, s, e) \
for (int var = s; int(s) <= var && var < int(e); ++var) // half-opened range
#define hfori(var, s, e) \
for (int var = e - 1; int(s) <= var && var < int(e); --var) // inversion
#define hforo(var, s, e) \
int var = s; \
for (; int(s) <= var && var < int(e); ++var) // out declaration
#define hforoi(var, s, e) \
int var = e - 1; \
for (; int(s) <= var && var < int(e); --var)
#define cfor(var, s, e) hfor(var, s, (e) + 1) // closed range
#define cfori(var, s, e) hfori(var, s, (e) + 1)
#define cforo(var, s, e) hforo(var, s, (e) + 1)
#define cforoi(var, s, e) hforoi(var, s, (e) + 1)
#define rep(x) hfor(repi, 0, x)
#define all(x) x.begin(), x.end()
#define sum_in(s, e) accumulate(s, e, 0ll)
#define mri(it) make_reverse_iterator(it) //*mri(it) == *prev(it)
#define gcd __gcd
template <typename C, typename RI> RI rerase(C &c, RI ri) {
return next(mri(c.erase(prev(ri.base()))));
}
template <typename T> constexpr T inf() { return numeric_limits<T>::max() / 2; }
using f64 = double;
using i64 = long long;
using u64 = unsigned long long;
const f64 pi = acos(-1), eps = 1e-12;
const int prime = 998244353; // 1073741783 int(2e9+11) int(1e9+9)
const int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // r, cw
const int mod = int(1e9 + 7);
auto _dummy = (ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
(cout << fixed << setprecision(11)), srand((int)time(0)), 0);
#define endl '\n' // not interactive?
// #define int i64//overflow?
struct Q {
int s, e, i;
};
const int N = 5e5;
signed main() {
int n, m;
cin >> n >> m;
int a[N];
hfor(i, 0, n) cin >> a[i];
int bsz = 1000;
vector<Q> v[1000];
hfor(i, 0, m) {
int s, e;
cin >> s >> e;
s--;
v[s / bsz].push_back({s, e, i});
}
hfor(i, 0, 1000) sort(all(v[i]), [](auto a, auto b) { return a.e < b.e; });
vector<pair<int, int>> ans;
hfor(i, 0, 1000) {
if (v[i].empty())
continue;
int buf[N + 1] = {
0,
};
int cnt = 0;
int normalized_s = v[i].front().s / bsz * bsz;
int j = normalized_s;
for (auto k : v[i]) {
while (j < k.e)
cnt += !buf[a[j++]]++;
hfor(l, normalized_s, k.s) cnt -= !--buf[a[l]];
ans.push_back({k.i, cnt});
hfor(l, normalized_s, k.s) cnt += !buf[a[l]]++;
}
}
sort(all(ans));
for (auto i : ans)
cout << i.second << endl;
return 0;
}
| replace | 54 | 55 | 54 | 55 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int bit[N], p[N], ans[N], a[N];
pair<int, pair<int, int>> pr[N];
void update(int idx, int val) {
while (idx < N) {
bit[idx] += val;
idx += (idx & -idx);
}
}
int read(int idx) {
int sum = 0;
while (idx > 0) {
sum += bit[idx];
idx -= (idx & -idx);
}
return sum;
}
int main() {
int n, q, x;
cin >> n >> q;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 0; i < q; ++i) {
cin >> pr[i].second.first >> pr[i].first;
pr[i].second.second = i;
}
sort(pr, pr + q);
memset(p, -1, sizeof(p));
memset(bit, 0, sizeof(bit));
x = 0;
for (int i = 1; i <= n; ++i) {
if (p[a[i]] != -1)
update(p[a[i]], -1);
p[a[i]] = i;
update(i, 1);
while (x < q and pr[x].first == i) {
ans[pr[x].second.second] =
read(pr[x].first) - read(pr[x].second.first - 1);
x++;
}
}
for (int i = 0; i < q; ++i)
cout << ans[i] << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int bit[N], p[N], ans[N], a[N];
pair<int, pair<int, int>> pr[N];
void update(int idx, int val) {
while (idx < N) {
bit[idx] += val;
idx += (idx & -idx);
}
}
int read(int idx) {
int sum = 0;
while (idx > 0) {
sum += bit[idx];
idx -= (idx & -idx);
}
return sum;
}
int main() {
int n, q, x;
cin >> n >> q;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 0; i < q; ++i) {
cin >> pr[i].second.first >> pr[i].first;
pr[i].second.second = i;
}
sort(pr, pr + q);
memset(p, -1, sizeof(p));
memset(bit, 0, sizeof(bit));
x = 0;
for (int i = 1; i <= n; ++i) {
if (p[a[i]] != -1)
update(p[a[i]], -1);
p[a[i]] = i;
update(i, 1);
while (x < q and pr[x].first == i) {
ans[pr[x].second.second] =
read(pr[x].first) - read(pr[x].second.first - 1);
x++;
}
}
for (int i = 0; i < q; ++i)
cout << ans[i] << "\n";
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
/// ----------------------------- (Debug) ------------------------------
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
vector<char *> tokenizer(const char *args) {
char *token = new char[111];
strcpy(token, args);
token = strtok(token, ", ");
vector<char *> v({token});
while (token = strtok(NULL, ", "))
v.push_back(token);
return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char *> args) { cerr << "\b\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char *> args, Head H, Tail... T) {
debug() << " [" << args.back() << ": " << H << "] ";
args.pop_back();
debugg(args, T...);
}
#define harg(...) #__VA_ARGS__
#ifndef ONLINE_JUDGE
#define dbg(...) \
{ \
debugg(tokenizer(harg(__VA_ARGS__, \b\t-- > Line)), __VA_ARGS__, \
__LINE__); \
cerr << endl; \
}
#else
#define dbg(...) \
{}
#endif
/// -----------------------------------------------------------------------
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define print(x) cout << x << "\n";
#define REP(i, a, b) for (i = a; i <= b; i++)
#define RAP(i, a, b) for (i = a; i >= b; i--)
#define spa << " " <<
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
ll mod = 1e9 + 7;
const int MX = 0x3f3f3f3f;
int seg[int(1e6) + 1];
int c[int(5e5) + 1], last[int(5e5) + 1], ans[int(5e5) + 5];
void update(int v, int l, int r, int idx, int num) {
if (r < idx || l > idx)
return;
else if (l == r)
seg[v] += num;
else {
update(2 * v, l, (l + r) >> 1, idx, num);
update(2 * v + 1, ((l + r) >> 1) + 1, r, idx, num);
seg[v] = seg[2 * v] + seg[2 * v + 1];
}
}
int query(int v, int i, int j, int l, int r) {
if (j < l || i > r)
return 0;
else if (l >= i && r <= j)
return seg[v];
else {
return query(2 * v, i, j, l, (l + r) >> 1) +
query(2 * v + 1, i, j, (l + r >> 1) + 1, r);
}
}
int main() {
memset(last, -1, sizeof(last));
int n, q, i;
cin >> n >> q;
REP(i, 0, n - 1) cin >> c[i];
vector<pair<pair<int, int>, int>> que(q);
REP(i, 0, q - 1) {
cin >> que[i].F.S >> que[i].F.F;
que[i].S = i;
}
sort(all(que));
int j = 0;
dbg(que);
REP(i, 0, n - 1) {
if (last[c[i]] != -1) {
update(1, 0, n - 1, last[c[i]], -1);
}
last[c[i]] = i;
update(1, 0, n - 1, i, 1);
dbg(seg[1]);
while (j < q && que[j].F.F == i + 1) {
dbg(que[j].S);
ans[que[j].S] = query(1, que[j].F.S - 1, que[j].F.F - 1, 0, n - 1);
j++;
}
}
dbg(ans);
REP(i, 0, q - 1) cout << ans[i] << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
/// ----------------------------- (Debug) ------------------------------
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
vector<char *> tokenizer(const char *args) {
char *token = new char[111];
strcpy(token, args);
token = strtok(token, ", ");
vector<char *> v({token});
while (token = strtok(NULL, ", "))
v.push_back(token);
return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char *> args) { cerr << "\b\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char *> args, Head H, Tail... T) {
debug() << " [" << args.back() << ": " << H << "] ";
args.pop_back();
debugg(args, T...);
}
#define harg(...) #__VA_ARGS__
#ifndef ONLINE_JUDGE
#define dbg(...) \
{ \
debugg(tokenizer(harg(__VA_ARGS__, \b\t-- > Line)), __VA_ARGS__, \
__LINE__); \
cerr << endl; \
}
#else
#define dbg(...) \
{}
#endif
/// -----------------------------------------------------------------------
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define print(x) cout << x << "\n";
#define REP(i, a, b) for (i = a; i <= b; i++)
#define RAP(i, a, b) for (i = a; i >= b; i--)
#define spa << " " <<
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
ll mod = 1e9 + 7;
const int MX = 0x3f3f3f3f;
int seg[int(2e6) + 1];
int c[int(5e5) + 1], last[int(5e5) + 5], ans[int(5e5) + 5];
void update(int v, int l, int r, int idx, int num) {
if (r < idx || l > idx)
return;
else if (l == r)
seg[v] += num;
else {
update(2 * v, l, (l + r) >> 1, idx, num);
update(2 * v + 1, ((l + r) >> 1) + 1, r, idx, num);
seg[v] = seg[2 * v] + seg[2 * v + 1];
}
}
int query(int v, int i, int j, int l, int r) {
if (j < l || i > r)
return 0;
else if (l >= i && r <= j)
return seg[v];
else {
return query(2 * v, i, j, l, (l + r) >> 1) +
query(2 * v + 1, i, j, (l + r >> 1) + 1, r);
}
}
int main() {
memset(last, -1, sizeof(last));
int n, q, i;
cin >> n >> q;
REP(i, 0, n - 1) cin >> c[i];
vector<pair<pair<int, int>, int>> que(q);
REP(i, 0, q - 1) {
cin >> que[i].F.S >> que[i].F.F;
que[i].S = i;
}
sort(all(que));
int j = 0;
dbg(que);
REP(i, 0, n - 1) {
if (last[c[i]] != -1) {
update(1, 0, n - 1, last[c[i]], -1);
}
last[c[i]] = i;
update(1, 0, n - 1, i, 1);
dbg(seg[1]);
while (j < q && que[j].F.F == i + 1) {
dbg(que[j].S);
ans[que[j].S] = query(1, que[j].F.S - 1, que[j].F.F - 1, 0, n - 1);
j++;
}
}
dbg(ans);
REP(i, 0, q - 1) cout << ans[i] << "\n";
return 0;
} | replace | 89 | 91 | 89 | 91 | 0 | [que: [((3, 1), 0), ((3, 3), 2), ((4, 2), 1)]] [ -->Line: 94]
[seg[1]: 1] [ -->Line: 101]
[seg[1]: 2] [ -->Line: 101]
[seg[1]: 2] [ -->Line: 101]
[que[j].second: 0] [ -->Line: 103]
[que[j].second: 2] [ -->Line: 103]
[seg[1]: 3] [ -->Line: 101]
[que[j].second: 1] [ -->Line: 103]
[ans: 0x5584fadc7600] [ -->Line: 108]
|
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<ll, int> ii;
typedef pair<ii, int> iii;
const int mod = 1e9 + 7;
const int N = 100005;
struct query {
int i, l, r, ans;
};
void solve() {
int n, qq;
cin >> n >> qq;
vector<int> a(n), see(n + 1);
vector<query> q(qq);
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < qq; i++) {
cin >> q[i].l >> q[i].r;
q[i].i = i;
q[i].l--, q[i].r--;
}
int len = sqrt(n) + 1;
sort(q.begin(), q.end(), [&len](query &a, query &b) {
if (a.l != b.l)
return a.l < b.l;
return a.r < b.r;
});
int l = 0, r = -1, cnt = 0;
for (auto &q : q) {
while (l < q.l) {
see[a[l]]--;
if (see[a[l]] == 0)
cnt--;
l++;
}
while (l > q.l) {
l--;
if (see[a[l]] == 0)
cnt++;
see[a[l]]++;
}
while (r < q.r) {
r++;
if (see[a[r]] == 0)
cnt++;
see[a[r]]++;
}
while (r > q.r) {
see[a[r]]--;
if (see[a[r]] == 0)
cnt--;
r--;
}
q.ans = cnt;
}
sort(q.begin(), q.end(), [](query &a, query &b) { return a.i < b.i; });
for (int i = 0; i < qq; i++)
cout << q[i].ans << '\n';
}
int main() {
fastio;
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<ll, int> ii;
typedef pair<ii, int> iii;
const int mod = 1e9 + 7;
const int N = 100005;
struct query {
int i, l, r, ans;
};
void solve() {
int n, qq;
cin >> n >> qq;
vector<int> a(n), see(n + 1);
vector<query> q(qq);
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < qq; i++) {
cin >> q[i].l >> q[i].r;
q[i].i = i;
q[i].l--, q[i].r--;
}
int len = sqrt(n) + 1;
sort(q.begin(), q.end(),
[&len](query &a, query &b) { // sqrt decomp. trick (mo's)
if (a.l / len != b.l / len)
return a.l / len < b.l / len;
return a.r < b.r;
});
int l = 0, r = -1, cnt = 0;
for (auto &q : q) {
while (l < q.l) {
see[a[l]]--;
if (see[a[l]] == 0)
cnt--;
l++;
}
while (l > q.l) {
l--;
if (see[a[l]] == 0)
cnt++;
see[a[l]]++;
}
while (r < q.r) {
r++;
if (see[a[r]] == 0)
cnt++;
see[a[r]]++;
}
while (r > q.r) {
see[a[r]]--;
if (see[a[r]] == 0)
cnt--;
r--;
}
q.ans = cnt;
}
sort(q.begin(), q.end(), [](query &a, query &b) { return a.i < b.i; });
for (int i = 0; i < qq; i++)
cout << q[i].ans << '\n';
}
int main() {
fastio;
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
| replace | 38 | 43 | 38 | 44 | TLE | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second
using namespace std;
pair<int, pair<int, int>> q[200001];
int n, m, a[100001], ans[200001];
int st[5 * 100001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
if (a.s.f != b.s.f)
return a.s.f < b.s.f;
return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
if (l == r) {
st[v] = d;
return;
}
int mid = (l + r) / 2;
if (x <= mid)
update(v * 2, l, mid, x, d);
else
update(v * 2 + 1, mid + 1, r, x, d);
st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
if (l > B || r < A)
return 0;
if (A == l && B == r)
return st[v];
int mid = (A + B) / 2;
return Find(v * 2, A, mid, l, min(mid, r)) +
Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++)
scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
sort(q + 1, q + m + 1, cmp);
int j = 1;
for (int i = 1; i <= m; i++) {
while (j <= q[i].s.f) {
if (!used[a[j]]) {
update(1, 1, n, j, 1);
used[a[j]] = j;
} else {
update(1, 1, n, used[a[j]], 0);
update(1, 1, n, j, 1);
used[a[j]] = j;
}
j++;
}
ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second
using namespace std;
const int N = 500009;
pair<int, pair<int, int>> q[N];
int n, m, a[N], ans[N];
int st[5 * N], used[5 * N];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
if (a.s.f != b.s.f)
return a.s.f < b.s.f;
return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
if (l == r) {
st[v] = d;
return;
}
int mid = (l + r) / 2;
if (x <= mid)
update(v * 2, l, mid, x, d);
else
update(v * 2 + 1, mid + 1, r, x, d);
st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
if (l > B || r < A)
return 0;
if (A == l && B == r)
return st[v];
int mid = (A + B) / 2;
return Find(v * 2, A, mid, l, min(mid, r)) +
Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++)
scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
sort(q + 1, q + m + 1, cmp);
int j = 1;
for (int i = 1; i <= m; i++) {
while (j <= q[i].s.f) {
if (!used[a[j]]) {
update(1, 1, n, j, 1);
used[a[j]] = j;
} else {
update(1, 1, n, used[a[j]], 0);
update(1, 1, n, j, 1);
used[a[j]] = j;
}
j++;
}
ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
}
| replace | 7 | 11 | 7 | 11 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int c[500000];
int sqrtN = 710;
struct SqrtDecomposition {
int N, K;
vector<long long> data;
vector<long long> bucketSum;
SqrtDecomposition(int n) {
N = n;
K = (N + sqrtN - 1) / sqrtN;
data.assign(N, 0);
bucketSum.assign(K, 0);
}
void add(int x, int y) {
data[x] += y;
bucketSum[x / sqrtN] += y;
}
long long getSum(int x, int y) {
long long ans = 0;
if (y - x < sqrtN) {
for (int i = x; i < y; i++) {
ans += data[i];
}
return ans;
}
for (int i = x / sqrtN + 1; i < y / sqrtN; i++) {
ans += bucketSum[i];
}
for (int i = x; i < (x / sqrtN + 1) * sqrtN; i++) {
ans += data[i];
}
for (int i = y / sqrtN * sqrtN; i < y; i++) {
ans += data[i];
}
return ans;
}
};
int ans[500000];
int last_appeared[500001];
pair<pair<int, int>, int> RL_ind[500000];
int main() {
int N;
int Q;
cin >> N >> Q;
int max_c = 0;
for (int i = 0; i < N; i++) {
cin >> c[i];
max_c = max(max_c, c[i]);
}
for (int i = 0; i < Q; i++) {
int l;
int r;
cin >> l >> r;
RL_ind[i] = make_pair(make_pair(r, l), i);
}
for (int i = 0; i <= max_c; i++) {
last_appeared[i] = -1;
}
sort(RL_ind, RL_ind + Q);
SqrtDecomposition sq(N);
int last_R = 0;
for (int i = 0; i < Q; i++) {
int cur_L = RL_ind[i].first.second;
int cur_R = RL_ind[i].first.first;
int ans_ind = RL_ind[i].second;
for (int j = last_R; j < cur_R; j++) {
if (last_appeared[c[j]] >= 0) {
sq.add(last_appeared[c[j]], -1);
}
last_appeared[c[j]] = j;
sq.add(j, 1);
}
ans[ans_ind] = sq.getSum(cur_L - 1, cur_R);
}
for (int i = 0; i < Q; i++) {
cout << ans[i] << endl;
}
return 0;
}
| #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int c[500000];
int sqrtN = 710;
struct SqrtDecomposition {
int N, K;
vector<long long> data;
vector<long long> bucketSum;
SqrtDecomposition(int n) {
N = n;
K = (N + sqrtN - 1) / sqrtN;
data.assign(N, 0);
bucketSum.assign(K, 0);
}
void add(int x, int y) {
data[x] += y;
bucketSum[x / sqrtN] += y;
}
long long getSum(int x, int y) {
long long ans = 0;
if (y - x < sqrtN) {
for (int i = x; i < y; i++) {
ans += data[i];
}
return ans;
}
for (int i = x / sqrtN + 1; i < y / sqrtN; i++) {
ans += bucketSum[i];
}
for (int i = x; i < (x / sqrtN + 1) * sqrtN; i++) {
ans += data[i];
}
for (int i = y / sqrtN * sqrtN; i < y; i++) {
ans += data[i];
}
return ans;
}
};
int ans[500000];
int last_appeared[500001];
pair<pair<int, int>, int> RL_ind[500000];
int main() {
int N;
int Q;
cin >> N >> Q;
int max_c = 0;
for (int i = 0; i < N; i++) {
cin >> c[i];
max_c = max(max_c, c[i]);
}
for (int i = 0; i < Q; i++) {
int l;
int r;
cin >> l >> r;
RL_ind[i] = make_pair(make_pair(r, l), i);
}
for (int i = 0; i <= max_c; i++) {
last_appeared[i] = -1;
}
sort(RL_ind, RL_ind + Q);
SqrtDecomposition sq(N);
int last_R = 0;
for (int i = 0; i < Q; i++) {
int cur_L = RL_ind[i].first.second;
int cur_R = RL_ind[i].first.first;
int ans_ind = RL_ind[i].second;
for (int j = last_R; j < cur_R; j++) {
if (last_appeared[c[j]] >= 0) {
sq.add(last_appeared[c[j]], -1);
}
last_appeared[c[j]] = j;
sq.add(j, 1);
}
ans[ans_ind] = sq.getSum(cur_L - 1, cur_R);
last_R = cur_R;
}
for (int i = 0; i < Q; i++) {
cout << ans[i] << endl;
}
return 0;
}
| insert | 96 | 96 | 96 | 97 | TLE | |
p02599 | C++ | Runtime Error | // Konrad Paluszek,University of Warsaw(former XIV LO Staszic)
// #STAY AT HOME
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define TIME (chrono::steady_clock::now().time_since_epoch().count())
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define xfm(a, b) a##b
#define xwb(a, b) xfm(a, b)
#define _ xwb(nvj, __LINE__)
#define __ xwb(kjy, __LINE__)
#define ___ xwb(cjm, __LINE__)
#define REP(i, n) for (urs(n) i = 0; i < (n); ++i)
#define UNTIL(t) while (clock() < (t)*CLOCKS_PER_SEC)
#define PER(r...) for (bool _ = 1; _ || next_permutation(r); _ = false)
#define ALL(r) (r).begin(), (r).end()
#define RALL(r) (r).rbegin(), (r).rend()
#define FS(r) r.first, r.second
#define SF(r) r.second, r.first
#define M0(r) memset(r, 0, sizeof(r))
#define sim template <class c
#define ros return o
#define rans return ans
#define forbits(i, m) \
if (m) \
for (urs(m) i = ctz(m), i##nvj = m; i##nvj; \
i##nvj ^= ((urs(m))1 << i), i = ctz(i##nvj))
#define fordbits(i, m) \
if (m) \
for (urs(m) i = 8 * sizeof(m) - clz(m) - 1, i##nxd = m; i##nxd; \
i##nxd ^= ((urs(m))1 << i), i = 8 * sizeof(m) - clz(i##nxd) - 1)
#define ksets(t, m, k, n) \
for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m))
#define urs(r...) typename decay<decltype(r)>::type
#define hur(f, g, r) \
sim > int f(c a) { \
if (sizeof(c) == 16) \
return r; \
if (sizeof(c) == 8) \
return g##ll(a); \
return g(a); \
}
#define pwq(t, i) \
int clz(t x) { return clz<int>(x) - i; }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using spii = set<pii>;
using mii = map<int, int>;
using unt = unsigned int;
sim > using min_queue = priority_queue<c, vector<c>, greater<c>>;
sim, class b,
class cmp =
less<c> > using ordered_map =
tree<c, b, cmp, rb_tree_tag, tree_order_statistics_node_update>;
sim, class cmp = less < c >> using ordered_set = ordered_map<c, null_type, cmp>;
hur(popc, __builtin_popcount, popc<ull>(a) + popc<ull>(a >> 64))
hur(ctz, __builtin_ctz, (ull)a ? ctz<ull>(a) : 64 + ctz<ull>(a >> 64))
hur(clz, __builtin_clz, a >> 64 ? clz<ull>(a >> 64) : 64 + clz<ull>(a))
pwq(short, 16) pwq(uint16_t, 16) pwq(char, 24) pwq(int8_t, 24)
pwq(uint8_t, 24) sim,
class N > bool mini(c &o, const N &h) {
if (o > h)
ros = h, 1;
return 0;
}
sim, class N > bool maxi(c &o, const N &h) {
if (o < h)
ros = h, 1;
return 0;
}
sim, class n > using gyv = c;
#if defined(LOCAL) // || defined(LOCAL2)
#include </home/kjp/headers/debuglib.hpp>
#else
#define loc(...)
#define onl(r...) r
#define debug(...)
#define print_stack(...)
#define mark_stack(...)
#define set_pre(...)
#define reg_it(...)
#define def_op(...) \
struct _ {};
#if !defined(LOCAL) && !defined(LOCAL2)
#define exit my_exit
void my_exit(int x) {
fflush(stdout);
_Exit(x);
}
#endif
#endif
#define next nexT
#define prev preV
#define tree trEE
#define left lefT
#define right righT
#define div diV
#define y1 y_1
#define pow \
do \
not use cmath pow unless you know what you are doing
ull mix(ull o) {
o += 0x9e3779b97f4a7c15;
o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
ros ^ (o >> 31);
}
ull SALT = 0x7a14a4b0881ebf9, tqu = 0x7a14a4b0881ebf9;
ull my_rand() { return tqu = mix(tqu); }
void my_srand(ull x) { SALT = tqu = x; }
const int inf = 1023400000;
const ll llinf = 1234567890000000000ll;
ll fix(ll o, ll m) {
o %= m;
if (o < 0)
o += m;
ros;
}
#define rand my_rand
#define srand my_srand
#define random_shuffle(r...) \
random_shuffle(r, [](int _) { return my_rand() % _; })
sim > inline c nux(c m) {
if (!m)
return numeric_limits<c>::max();
c A = m & -m;
c B = ~((A - 1) ^ m);
c C = B & -B;
c D = (C >> (1 + ctz(A))) - 1;
return C | (m & ~(C - 1)) | D;
}
__attribute__((no_sanitize_undefined)) ll mul(ll a, ll b, ll m) {
ll q = (ll)(a * (ld)b / m);
ll o = a * b - q * m;
o %= m;
if (o < 0)
o += m;
ros;
}
sim > void unq(c &x) { x.resize(unique(ALL(x)) - x.begin()); }
#pragma GCC diagnostic pop
#if ((ULONG_MAX) != (UINT_MAX))
namespace std {
template <> struct is_signed<__int128> : public true_type {};
} // namespace std
#endif
sim, class d > typename common_type<c, d>::type floor_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using floor_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b - (a % b < 0);
}
sim, class d > typename common_type<c, d>::type ceil_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using ceil_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b + (a % b > 0);
}
sim > struct REV {
using value_type = typename c::value_type;
c &x;
using it = typename c::reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > struct CREV {
using value_type = typename c::value_type;
const c &x;
using it = typename c::const_reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > REV<c> reversed(c &x) { return REV<c>{x}; }
sim > CREV<c> reversed(const c &x) { return CREV<c>{x}; }
#define done(r...) exit(0 * printf(r))
#if defined(LOCAL) || defined(LOCAL2)
void __tmi() {
cerr << setprecision(6) << fixed
<< "total time: " << clock() / (ld)CLOCKS_PER_SEC << "s" << endl;
}
int _ = (atexit(__tmi), 0);
#endif
// #STAY AT HOME
const int nax = 5e5 + 44;
const int L = 8;
ll rec(int a, int b, int ord) {
// debug(imie(a), imie(b), imie(ord));
if (ord == 0)
return 0;
int edge = 1 << ord;
assert(a >= 0 && a < edge);
assert(b >= 0 && b < edge);
int half = edge / 2;
ll siz = 1ll << (2 * (ord - 1));
if (a < half && b < half)
return rec(b, a, ord - 1);
if (a < half && b >= half)
return siz + rec(a, b - half, ord - 1);
if (a >= half && b >= half)
return 2 * siz + rec(a - half, b - half, ord - 1);
if (a >= half && b < half)
return 3 * siz + rec(half - b - 1, half - (a - half) - 1, ord - 1);
assert(false);
}
ll cast(tuple<int, int, int> a) {
auto [first, second, _] = a;
swap(first, second);
return rec(first, second, 20);
}
bool cmp(tuple<int, int, int, ll> a, tuple<int, int, int, ll> b) {
return get<3>(a) < get<3>(b);
}
int a[nax];
int cou[nax];
int ans[nax];
int diffs = 0;
void add(int x) {
if (cou[x] == 0)
diffs++;
cou[x]++;
}
void rem(int x) {
cou[x]--;
if (cou[x] == 0)
diffs--;
}
int dist(tuple<int, int, int> a, tuple<int, int, int> b) {
auto [l1, r1, _] = a;
auto [l2, r2, _] = b;
if (l1 > r2 || r2 < l1)
return r1 - l1 + r2 - l2 + 2;
return abs(l1 - l2) + abs(r1 - r2);
}
ll hilbert(int a, int b) {
// debug(imie(a), imie(b));
return rec(a, b, 20);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
vector<tuple<int, int, int, ll>> qs(q);
srand(TIME);
int off1 = rand() % n, off2 = rand() % n;
REP(i, q) {
int l, r;
scanf("%d%d", &l, &r);
qs[i] = {l, r, i, rec(l + off1, r + off2, 19)};
}
sort(ALL(qs), cmp);
auto [l, r, q0, _] = qs[0];
debug(imie(qs));
for (int i = l; i <= r; ++i)
add(a[i]);
ans[q0] = diffs;
for (int i = 1; i < q; ++i) {
auto [l0, r0, _, __] = qs[i - 1];
auto [l, r, ind, __] = qs[i];
if (r < l0 || l > r0) {
for (int j = l0; j <= r0; ++j)
rem(a[j]);
for (int j = l; j <= r; ++j)
add(a[j]);
} else {
while (l < l0)
add(a[--l0]);
while (l > l0)
rem(a[l0++]);
while (r < r0)
rem(a[r0--]);
while (r > r0)
add(a[++r0]);
}
ans[ind] = diffs;
}
REP(i, q) printf("%d\n", ans[i]);
}
// #STAY AT HOME
| // Konrad Paluszek,University of Warsaw(former XIV LO Staszic)
// #STAY AT HOME
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define TIME (chrono::steady_clock::now().time_since_epoch().count())
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define xfm(a, b) a##b
#define xwb(a, b) xfm(a, b)
#define _ xwb(nvj, __LINE__)
#define __ xwb(kjy, __LINE__)
#define ___ xwb(cjm, __LINE__)
#define REP(i, n) for (urs(n) i = 0; i < (n); ++i)
#define UNTIL(t) while (clock() < (t)*CLOCKS_PER_SEC)
#define PER(r...) for (bool _ = 1; _ || next_permutation(r); _ = false)
#define ALL(r) (r).begin(), (r).end()
#define RALL(r) (r).rbegin(), (r).rend()
#define FS(r) r.first, r.second
#define SF(r) r.second, r.first
#define M0(r) memset(r, 0, sizeof(r))
#define sim template <class c
#define ros return o
#define rans return ans
#define forbits(i, m) \
if (m) \
for (urs(m) i = ctz(m), i##nvj = m; i##nvj; \
i##nvj ^= ((urs(m))1 << i), i = ctz(i##nvj))
#define fordbits(i, m) \
if (m) \
for (urs(m) i = 8 * sizeof(m) - clz(m) - 1, i##nxd = m; i##nxd; \
i##nxd ^= ((urs(m))1 << i), i = 8 * sizeof(m) - clz(i##nxd) - 1)
#define ksets(t, m, k, n) \
for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m))
#define urs(r...) typename decay<decltype(r)>::type
#define hur(f, g, r) \
sim > int f(c a) { \
if (sizeof(c) == 16) \
return r; \
if (sizeof(c) == 8) \
return g##ll(a); \
return g(a); \
}
#define pwq(t, i) \
int clz(t x) { return clz<int>(x) - i; }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using spii = set<pii>;
using mii = map<int, int>;
using unt = unsigned int;
sim > using min_queue = priority_queue<c, vector<c>, greater<c>>;
sim, class b,
class cmp =
less<c> > using ordered_map =
tree<c, b, cmp, rb_tree_tag, tree_order_statistics_node_update>;
sim, class cmp = less < c >> using ordered_set = ordered_map<c, null_type, cmp>;
hur(popc, __builtin_popcount, popc<ull>(a) + popc<ull>(a >> 64))
hur(ctz, __builtin_ctz, (ull)a ? ctz<ull>(a) : 64 + ctz<ull>(a >> 64))
hur(clz, __builtin_clz, a >> 64 ? clz<ull>(a >> 64) : 64 + clz<ull>(a))
pwq(short, 16) pwq(uint16_t, 16) pwq(char, 24) pwq(int8_t, 24)
pwq(uint8_t, 24) sim,
class N > bool mini(c &o, const N &h) {
if (o > h)
ros = h, 1;
return 0;
}
sim, class N > bool maxi(c &o, const N &h) {
if (o < h)
ros = h, 1;
return 0;
}
sim, class n > using gyv = c;
#if defined(LOCAL) // || defined(LOCAL2)
#include </home/kjp/headers/debuglib.hpp>
#else
#define loc(...)
#define onl(r...) r
#define debug(...)
#define print_stack(...)
#define mark_stack(...)
#define set_pre(...)
#define reg_it(...)
#define def_op(...) \
struct _ {};
#if !defined(LOCAL) && !defined(LOCAL2)
#define exit my_exit
void my_exit(int x) {
fflush(stdout);
_Exit(x);
}
#endif
#endif
#define next nexT
#define prev preV
#define tree trEE
#define left lefT
#define right righT
#define div diV
#define y1 y_1
#define pow \
do \
not use cmath pow unless you know what you are doing
ull mix(ull o) {
o += 0x9e3779b97f4a7c15;
o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
ros ^ (o >> 31);
}
ull SALT = 0x7a14a4b0881ebf9, tqu = 0x7a14a4b0881ebf9;
ull my_rand() { return tqu = mix(tqu); }
void my_srand(ull x) { SALT = tqu = x; }
const int inf = 1023400000;
const ll llinf = 1234567890000000000ll;
ll fix(ll o, ll m) {
o %= m;
if (o < 0)
o += m;
ros;
}
#define rand my_rand
#define srand my_srand
#define random_shuffle(r...) \
random_shuffle(r, [](int _) { return my_rand() % _; })
sim > inline c nux(c m) {
if (!m)
return numeric_limits<c>::max();
c A = m & -m;
c B = ~((A - 1) ^ m);
c C = B & -B;
c D = (C >> (1 + ctz(A))) - 1;
return C | (m & ~(C - 1)) | D;
}
__attribute__((no_sanitize_undefined)) ll mul(ll a, ll b, ll m) {
ll q = (ll)(a * (ld)b / m);
ll o = a * b - q * m;
o %= m;
if (o < 0)
o += m;
ros;
}
sim > void unq(c &x) { x.resize(unique(ALL(x)) - x.begin()); }
#pragma GCC diagnostic pop
#if ((ULONG_MAX) != (UINT_MAX))
namespace std {
template <> struct is_signed<__int128> : public true_type {};
} // namespace std
#endif
sim, class d > typename common_type<c, d>::type floor_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using floor_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b - (a % b < 0);
}
sim, class d > typename common_type<c, d>::type ceil_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using ceil_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b + (a % b > 0);
}
sim > struct REV {
using value_type = typename c::value_type;
c &x;
using it = typename c::reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > struct CREV {
using value_type = typename c::value_type;
const c &x;
using it = typename c::const_reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > REV<c> reversed(c &x) { return REV<c>{x}; }
sim > CREV<c> reversed(const c &x) { return CREV<c>{x}; }
#define done(r...) exit(0 * printf(r))
#if defined(LOCAL) || defined(LOCAL2)
void __tmi() {
cerr << setprecision(6) << fixed
<< "total time: " << clock() / (ld)CLOCKS_PER_SEC << "s" << endl;
}
int _ = (atexit(__tmi), 0);
#endif
// #STAY AT HOME
const int nax = 5e5 + 44;
const int L = 8;
ll rec(int a, int b, int ord) {
// debug(imie(a), imie(b), imie(ord));
if (ord == 0)
return 0;
int edge = 1 << ord;
assert(a >= 0 && a < edge);
assert(b >= 0 && b < edge);
int half = edge / 2;
ll siz = 1ll << (2 * (ord - 1));
if (a < half && b < half)
return rec(b, a, ord - 1);
if (a < half && b >= half)
return siz + rec(a, b - half, ord - 1);
if (a >= half && b >= half)
return 2 * siz + rec(a - half, b - half, ord - 1);
if (a >= half && b < half)
return 3 * siz + rec(half - b - 1, half - (a - half) - 1, ord - 1);
assert(false);
}
ll cast(tuple<int, int, int> a) {
auto [first, second, _] = a;
swap(first, second);
return rec(first, second, 20);
}
bool cmp(tuple<int, int, int, ll> a, tuple<int, int, int, ll> b) {
return get<3>(a) < get<3>(b);
}
int a[nax];
int cou[nax];
int ans[nax];
int diffs = 0;
void add(int x) {
if (cou[x] == 0)
diffs++;
cou[x]++;
}
void rem(int x) {
cou[x]--;
if (cou[x] == 0)
diffs--;
}
int dist(tuple<int, int, int> a, tuple<int, int, int> b) {
auto [l1, r1, _] = a;
auto [l2, r2, _] = b;
if (l1 > r2 || r2 < l1)
return r1 - l1 + r2 - l2 + 2;
return abs(l1 - l2) + abs(r1 - r2);
}
ll hilbert(int a, int b) {
// debug(imie(a), imie(b));
return rec(a, b, 20);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
vector<tuple<int, int, int, ll>> qs(q);
srand(TIME);
int off1 = rand() % n, off2 = rand() % n;
REP(i, q) {
int l, r;
scanf("%d%d", &l, &r);
qs[i] = {l, r, i, rec(l + off1, r + off2, 20)};
}
sort(ALL(qs), cmp);
auto [l, r, q0, _] = qs[0];
debug(imie(qs));
for (int i = l; i <= r; ++i)
add(a[i]);
ans[q0] = diffs;
for (int i = 1; i < q; ++i) {
auto [l0, r0, _, __] = qs[i - 1];
auto [l, r, ind, __] = qs[i];
if (r < l0 || l > r0) {
for (int j = l0; j <= r0; ++j)
rem(a[j]);
for (int j = l; j <= r; ++j)
add(a[j]);
} else {
while (l < l0)
add(a[--l0]);
while (l > l0)
rem(a[l0++]);
while (r < r0)
rem(a[r0--]);
while (r > r0)
add(a[++r0]);
}
ans[ind] = diffs;
}
REP(i, q) printf("%d\n", ans[i]);
}
// #STAY AT HOME
| replace | 262 | 263 | 262 | 263 | 0 | |
p02599 | C++ | Time Limit Exceeded | // Konrad Paluszek,University of Warsaw(former XIV LO Staszic)
// #STAY AT HOME
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define TIME (chrono::steady_clock::now().time_since_epoch().count())
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define xfm(a, b) a##b
#define xwb(a, b) xfm(a, b)
#define _ xwb(nvj, __LINE__)
#define __ xwb(kjy, __LINE__)
#define ___ xwb(cjm, __LINE__)
#define REP(i, n) for (urs(n) i = 0; i < (n); ++i)
#define UNTIL(t) while (clock() < (t)*CLOCKS_PER_SEC)
#define PER(r...) for (bool _ = 1; _ || next_permutation(r); _ = false)
#define ALL(r) (r).begin(), (r).end()
#define RALL(r) (r).rbegin(), (r).rend()
#define FS(r) r.first, r.second
#define SF(r) r.second, r.first
#define M0(r) memset(r, 0, sizeof(r))
#define sim template <class c
#define ros return o
#define rans return ans
#define forbits(i, m) \
if (m) \
for (urs(m) i = ctz(m), i##nvj = m; i##nvj; \
i##nvj ^= ((urs(m))1 << i), i = ctz(i##nvj))
#define fordbits(i, m) \
if (m) \
for (urs(m) i = 8 * sizeof(m) - clz(m) - 1, i##nxd = m; i##nxd; \
i##nxd ^= ((urs(m))1 << i), i = 8 * sizeof(m) - clz(i##nxd) - 1)
#define ksets(t, m, k, n) \
for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m))
#define urs(r...) typename decay<decltype(r)>::type
#define hur(f, g, r) \
sim > int f(c a) { \
if (sizeof(c) == 16) \
return r; \
if (sizeof(c) == 8) \
return g##ll(a); \
return g(a); \
}
#define pwq(t, i) \
int clz(t x) { return clz<int>(x) - i; }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using spii = set<pii>;
using mii = map<int, int>;
using unt = unsigned int;
sim > using min_queue = priority_queue<c, vector<c>, greater<c>>;
sim, class b,
class cmp =
less<c> > using ordered_map =
tree<c, b, cmp, rb_tree_tag, tree_order_statistics_node_update>;
sim, class cmp = less < c >> using ordered_set = ordered_map<c, null_type, cmp>;
hur(popc, __builtin_popcount, popc<ull>(a) + popc<ull>(a >> 64))
hur(ctz, __builtin_ctz, (ull)a ? ctz<ull>(a) : 64 + ctz<ull>(a >> 64))
hur(clz, __builtin_clz, a >> 64 ? clz<ull>(a >> 64) : 64 + clz<ull>(a))
pwq(short, 16) pwq(uint16_t, 16) pwq(char, 24) pwq(int8_t, 24)
pwq(uint8_t, 24) sim,
class N > bool mini(c &o, const N &h) {
if (o > h)
ros = h, 1;
return 0;
}
sim, class N > bool maxi(c &o, const N &h) {
if (o < h)
ros = h, 1;
return 0;
}
sim, class n > using gyv = c;
#if defined(LOCAL) // || defined(LOCAL2)
#include </home/kjp/headers/debuglib.hpp>
#else
#define loc(...)
#define onl(r...) r
#define debug(...)
#define print_stack(...)
#define mark_stack(...)
#define set_pre(...)
#define reg_it(...)
#define def_op(...) \
struct _ {};
#if !defined(LOCAL) && !defined(LOCAL2)
#define exit my_exit
void my_exit(int x) {
fflush(stdout);
_Exit(x);
}
#endif
#endif
#define next nexT
#define prev preV
#define tree trEE
#define left lefT
#define right righT
#define div diV
#define y1 y_1
#define pow \
do \
not use cmath pow unless you know what you are doing
ull mix(ull o) {
o += 0x9e3779b97f4a7c15;
o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
ros ^ (o >> 31);
}
ull SALT = 0x7a14a4b0881ebf9, tqu = 0x7a14a4b0881ebf9;
ull my_rand() { return tqu = mix(tqu); }
void my_srand(ull x) { SALT = tqu = x; }
const int inf = 1023400000;
const ll llinf = 1234567890000000000ll;
ll fix(ll o, ll m) {
o %= m;
if (o < 0)
o += m;
ros;
}
#define rand my_rand
#define srand my_srand
#define random_shuffle(r...) \
random_shuffle(r, [](int _) { return my_rand() % _; })
sim > inline c nux(c m) {
if (!m)
return numeric_limits<c>::max();
c A = m & -m;
c B = ~((A - 1) ^ m);
c C = B & -B;
c D = (C >> (1 + ctz(A))) - 1;
return C | (m & ~(C - 1)) | D;
}
__attribute__((no_sanitize_undefined)) ll mul(ll a, ll b, ll m) {
ll q = (ll)(a * (ld)b / m);
ll o = a * b - q * m;
o %= m;
if (o < 0)
o += m;
ros;
}
sim > void unq(c &x) { x.resize(unique(ALL(x)) - x.begin()); }
#pragma GCC diagnostic pop
#if ((ULONG_MAX) != (UINT_MAX))
namespace std {
template <> struct is_signed<__int128> : public true_type {};
} // namespace std
#endif
sim, class d > typename common_type<c, d>::type floor_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using floor_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b - (a % b < 0);
}
sim, class d > typename common_type<c, d>::type ceil_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using ceil_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b + (a % b > 0);
}
sim > struct REV {
using value_type = typename c::value_type;
c &x;
using it = typename c::reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > struct CREV {
using value_type = typename c::value_type;
const c &x;
using it = typename c::const_reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > REV<c> reversed(c &x) { return REV<c>{x}; }
sim > CREV<c> reversed(const c &x) { return CREV<c>{x}; }
#define done(r...) exit(0 * printf(r))
#if defined(LOCAL) || defined(LOCAL2)
void __tmi() {
cerr << setprecision(6) << fixed
<< "total time: " << clock() / (ld)CLOCKS_PER_SEC << "s" << endl;
}
int _ = (atexit(__tmi), 0);
#endif
// #STAY AT HOME
const int nax = 5e5 + 44;
const int L = 8;
ll rec(int a, int b, int ord) {
// debug(imie(a), imie(b), imie(ord));
if (ord == 0)
return 0;
int edge = 1 << ord;
assert(a >= 0 && a < edge);
assert(b >= 0 && b < edge);
int half = edge / 2;
ll siz = 1ll << (2 * (ord - 1));
if (a < half && b < half)
return rec(b, a, ord - 1);
if (a < half && b >= half)
return siz + rec(a, b - half, ord - 1);
if (a >= half && b >= half)
return 2 * siz + rec(a - half, b - half, ord - 1);
if (a >= half && b < half)
return 3 * siz + rec(half - b - 1, half - (a - half) - 1, ord - 1);
assert(false);
}
ll cast(tuple<int, int, int> a) {
auto [first, second, _] = a;
swap(first, second);
return rec(first, second, 20);
}
bool cmp(tuple<int, int, int, ll> a, tuple<int, int, int, ll> b) {
return get<3>(a) < get<3>(b);
}
int a[nax];
int cou[nax];
int ans[nax];
int diffs = 0;
void add(int x) {
if (cou[x] == 0)
diffs++;
cou[x]++;
}
void rem(int x) {
cou[x]--;
if (cou[x] == 0)
diffs--;
}
int dist(tuple<int, int, int> a, tuple<int, int, int> b) {
auto [l1, r1, _] = a;
auto [l2, r2, _] = b;
if (l1 > r2 || r2 < l1)
return r1 - l1 + r2 - l2 + 2;
return abs(l1 - l2) + abs(r1 - r2);
}
ll hilbert(int a, int b) {
// debug(imie(a), imie(b));
return rec(a, b, 19);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
vector<tuple<int, int, int, int>> qs(q);
REP(i, q) {
int l, r;
scanf("%d%d", &l, &r);
qs[i] = {l, r, i, rec(l, r, 19)};
}
sort(ALL(qs), cmp);
auto [l, r, q0, _] = qs[0];
debug(imie(qs));
for (int i = l; i <= r; ++i)
add(a[i]);
ans[q0] = diffs;
for (int i = 1; i < q; ++i) {
auto [l0, r0, _, __] = qs[i - 1];
auto [l, r, ind, __] = qs[i];
if (r < l0 || l > r0) {
for (int j = l0; j <= r0; ++j)
rem(a[j]);
for (int j = l; j <= r; ++j)
add(a[j]);
} else {
while (l < l0)
add(a[--l0]);
while (l > l0)
rem(a[l0++]);
while (r < r0)
rem(a[r0--]);
while (r > r0)
add(a[++r0]);
}
ans[ind] = diffs;
}
REP(i, q) printf("%d\n", ans[i]);
}
// #STAY AT HOME
| // Konrad Paluszek,University of Warsaw(former XIV LO Staszic)
// #STAY AT HOME
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define TIME (chrono::steady_clock::now().time_since_epoch().count())
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define xfm(a, b) a##b
#define xwb(a, b) xfm(a, b)
#define _ xwb(nvj, __LINE__)
#define __ xwb(kjy, __LINE__)
#define ___ xwb(cjm, __LINE__)
#define REP(i, n) for (urs(n) i = 0; i < (n); ++i)
#define UNTIL(t) while (clock() < (t)*CLOCKS_PER_SEC)
#define PER(r...) for (bool _ = 1; _ || next_permutation(r); _ = false)
#define ALL(r) (r).begin(), (r).end()
#define RALL(r) (r).rbegin(), (r).rend()
#define FS(r) r.first, r.second
#define SF(r) r.second, r.first
#define M0(r) memset(r, 0, sizeof(r))
#define sim template <class c
#define ros return o
#define rans return ans
#define forbits(i, m) \
if (m) \
for (urs(m) i = ctz(m), i##nvj = m; i##nvj; \
i##nvj ^= ((urs(m))1 << i), i = ctz(i##nvj))
#define fordbits(i, m) \
if (m) \
for (urs(m) i = 8 * sizeof(m) - clz(m) - 1, i##nxd = m; i##nxd; \
i##nxd ^= ((urs(m))1 << i), i = 8 * sizeof(m) - clz(i##nxd) - 1)
#define ksets(t, m, k, n) \
for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m))
#define urs(r...) typename decay<decltype(r)>::type
#define hur(f, g, r) \
sim > int f(c a) { \
if (sizeof(c) == 16) \
return r; \
if (sizeof(c) == 8) \
return g##ll(a); \
return g(a); \
}
#define pwq(t, i) \
int clz(t x) { return clz<int>(x) - i; }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using spii = set<pii>;
using mii = map<int, int>;
using unt = unsigned int;
sim > using min_queue = priority_queue<c, vector<c>, greater<c>>;
sim, class b,
class cmp =
less<c> > using ordered_map =
tree<c, b, cmp, rb_tree_tag, tree_order_statistics_node_update>;
sim, class cmp = less < c >> using ordered_set = ordered_map<c, null_type, cmp>;
hur(popc, __builtin_popcount, popc<ull>(a) + popc<ull>(a >> 64))
hur(ctz, __builtin_ctz, (ull)a ? ctz<ull>(a) : 64 + ctz<ull>(a >> 64))
hur(clz, __builtin_clz, a >> 64 ? clz<ull>(a >> 64) : 64 + clz<ull>(a))
pwq(short, 16) pwq(uint16_t, 16) pwq(char, 24) pwq(int8_t, 24)
pwq(uint8_t, 24) sim,
class N > bool mini(c &o, const N &h) {
if (o > h)
ros = h, 1;
return 0;
}
sim, class N > bool maxi(c &o, const N &h) {
if (o < h)
ros = h, 1;
return 0;
}
sim, class n > using gyv = c;
#if defined(LOCAL) // || defined(LOCAL2)
#include </home/kjp/headers/debuglib.hpp>
#else
#define loc(...)
#define onl(r...) r
#define debug(...)
#define print_stack(...)
#define mark_stack(...)
#define set_pre(...)
#define reg_it(...)
#define def_op(...) \
struct _ {};
#if !defined(LOCAL) && !defined(LOCAL2)
#define exit my_exit
void my_exit(int x) {
fflush(stdout);
_Exit(x);
}
#endif
#endif
#define next nexT
#define prev preV
#define tree trEE
#define left lefT
#define right righT
#define div diV
#define y1 y_1
#define pow \
do \
not use cmath pow unless you know what you are doing
ull mix(ull o) {
o += 0x9e3779b97f4a7c15;
o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
ros ^ (o >> 31);
}
ull SALT = 0x7a14a4b0881ebf9, tqu = 0x7a14a4b0881ebf9;
ull my_rand() { return tqu = mix(tqu); }
void my_srand(ull x) { SALT = tqu = x; }
const int inf = 1023400000;
const ll llinf = 1234567890000000000ll;
ll fix(ll o, ll m) {
o %= m;
if (o < 0)
o += m;
ros;
}
#define rand my_rand
#define srand my_srand
#define random_shuffle(r...) \
random_shuffle(r, [](int _) { return my_rand() % _; })
sim > inline c nux(c m) {
if (!m)
return numeric_limits<c>::max();
c A = m & -m;
c B = ~((A - 1) ^ m);
c C = B & -B;
c D = (C >> (1 + ctz(A))) - 1;
return C | (m & ~(C - 1)) | D;
}
__attribute__((no_sanitize_undefined)) ll mul(ll a, ll b, ll m) {
ll q = (ll)(a * (ld)b / m);
ll o = a * b - q * m;
o %= m;
if (o < 0)
o += m;
ros;
}
sim > void unq(c &x) { x.resize(unique(ALL(x)) - x.begin()); }
#pragma GCC diagnostic pop
#if ((ULONG_MAX) != (UINT_MAX))
namespace std {
template <> struct is_signed<__int128> : public true_type {};
} // namespace std
#endif
sim, class d > typename common_type<c, d>::type floor_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using floor_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b - (a % b < 0);
}
sim, class d > typename common_type<c, d>::type ceil_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using ceil_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b + (a % b > 0);
}
sim > struct REV {
using value_type = typename c::value_type;
c &x;
using it = typename c::reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > struct CREV {
using value_type = typename c::value_type;
const c &x;
using it = typename c::const_reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > REV<c> reversed(c &x) { return REV<c>{x}; }
sim > CREV<c> reversed(const c &x) { return CREV<c>{x}; }
#define done(r...) exit(0 * printf(r))
#if defined(LOCAL) || defined(LOCAL2)
void __tmi() {
cerr << setprecision(6) << fixed
<< "total time: " << clock() / (ld)CLOCKS_PER_SEC << "s" << endl;
}
int _ = (atexit(__tmi), 0);
#endif
// #STAY AT HOME
const int nax = 5e5 + 44;
const int L = 8;
ll rec(int a, int b, int ord) {
// debug(imie(a), imie(b), imie(ord));
if (ord == 0)
return 0;
int edge = 1 << ord;
assert(a >= 0 && a < edge);
assert(b >= 0 && b < edge);
int half = edge / 2;
ll siz = 1ll << (2 * (ord - 1));
if (a < half && b < half)
return rec(b, a, ord - 1);
if (a < half && b >= half)
return siz + rec(a, b - half, ord - 1);
if (a >= half && b >= half)
return 2 * siz + rec(a - half, b - half, ord - 1);
if (a >= half && b < half)
return 3 * siz + rec(half - b - 1, half - (a - half) - 1, ord - 1);
assert(false);
}
ll cast(tuple<int, int, int> a) {
auto [first, second, _] = a;
swap(first, second);
return rec(first, second, 20);
}
bool cmp(tuple<int, int, int, ll> a, tuple<int, int, int, ll> b) {
return get<3>(a) < get<3>(b);
}
int a[nax];
int cou[nax];
int ans[nax];
int diffs = 0;
void add(int x) {
if (cou[x] == 0)
diffs++;
cou[x]++;
}
void rem(int x) {
cou[x]--;
if (cou[x] == 0)
diffs--;
}
int dist(tuple<int, int, int> a, tuple<int, int, int> b) {
auto [l1, r1, _] = a;
auto [l2, r2, _] = b;
if (l1 > r2 || r2 < l1)
return r1 - l1 + r2 - l2 + 2;
return abs(l1 - l2) + abs(r1 - r2);
}
ll hilbert(int a, int b) {
// debug(imie(a), imie(b));
return rec(a, b, 19);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
vector<tuple<int, int, int, ll>> qs(q);
REP(i, q) {
int l, r;
scanf("%d%d", &l, &r);
qs[i] = {l, r, i, rec(l, r, 19)};
}
sort(ALL(qs), cmp);
auto [l, r, q0, _] = qs[0];
debug(imie(qs));
for (int i = l; i <= r; ++i)
add(a[i]);
ans[q0] = diffs;
for (int i = 1; i < q; ++i) {
auto [l0, r0, _, __] = qs[i - 1];
auto [l, r, ind, __] = qs[i];
if (r < l0 || l > r0) {
for (int j = l0; j <= r0; ++j)
rem(a[j]);
for (int j = l; j <= r; ++j)
add(a[j]);
} else {
while (l < l0)
add(a[--l0]);
while (l > l0)
rem(a[l0++]);
while (r < r0)
rem(a[r0--]);
while (r > r0)
add(a[++r0]);
}
ans[ind] = diffs;
}
REP(i, q) printf("%d\n", ans[i]);
}
// #STAY AT HOME
| replace | 256 | 257 | 256 | 257 | TLE | |
p02599 | C++ | Time Limit Exceeded | // Konrad Paluszek,University of Warsaw(former XIV LO Staszic)
// #STAY AT HOME
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define TIME (chrono::steady_clock::now().time_since_epoch().count())
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define xfm(a, b) a##b
#define xwb(a, b) xfm(a, b)
#define _ xwb(nvj, __LINE__)
#define __ xwb(kjy, __LINE__)
#define ___ xwb(cjm, __LINE__)
#define REP(i, n) for (urs(n) i = 0; i < (n); ++i)
#define UNTIL(t) while (clock() < (t)*CLOCKS_PER_SEC)
#define PER(r...) for (bool _ = 1; _ || next_permutation(r); _ = false)
#define ALL(r) (r).begin(), (r).end()
#define RALL(r) (r).rbegin(), (r).rend()
#define FS(r) r.first, r.second
#define SF(r) r.second, r.first
#define M0(r) memset(r, 0, sizeof(r))
#define sim template <class c
#define ros return o
#define rans return ans
#define forbits(i, m) \
if (m) \
for (urs(m) i = ctz(m), i##nvj = m; i##nvj; \
i##nvj ^= ((urs(m))1 << i), i = ctz(i##nvj))
#define fordbits(i, m) \
if (m) \
for (urs(m) i = 8 * sizeof(m) - clz(m) - 1, i##nxd = m; i##nxd; \
i##nxd ^= ((urs(m))1 << i), i = 8 * sizeof(m) - clz(i##nxd) - 1)
#define ksets(t, m, k, n) \
for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m))
#define urs(r...) typename decay<decltype(r)>::type
#define hur(f, g, r) \
sim > int f(c a) { \
if (sizeof(c) == 16) \
return r; \
if (sizeof(c) == 8) \
return g##ll(a); \
return g(a); \
}
#define pwq(t, i) \
int clz(t x) { return clz<int>(x) - i; }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using spii = set<pii>;
using mii = map<int, int>;
using unt = unsigned int;
sim > using min_queue = priority_queue<c, vector<c>, greater<c>>;
sim, class b,
class cmp =
less<c> > using ordered_map =
tree<c, b, cmp, rb_tree_tag, tree_order_statistics_node_update>;
sim, class cmp = less < c >> using ordered_set = ordered_map<c, null_type, cmp>;
hur(popc, __builtin_popcount, popc<ull>(a) + popc<ull>(a >> 64))
hur(ctz, __builtin_ctz, (ull)a ? ctz<ull>(a) : 64 + ctz<ull>(a >> 64))
hur(clz, __builtin_clz, a >> 64 ? clz<ull>(a >> 64) : 64 + clz<ull>(a))
pwq(short, 16) pwq(uint16_t, 16) pwq(char, 24) pwq(int8_t, 24)
pwq(uint8_t, 24) sim,
class N > bool mini(c &o, const N &h) {
if (o > h)
ros = h, 1;
return 0;
}
sim, class N > bool maxi(c &o, const N &h) {
if (o < h)
ros = h, 1;
return 0;
}
sim, class n > using gyv = c;
#if defined(LOCAL) // || defined(LOCAL2)
#include </home/kjp/headers/debuglib.hpp>
#else
#define loc(...)
#define onl(r...) r
#define debug(...)
#define print_stack(...)
#define mark_stack(...)
#define set_pre(...)
#define reg_it(...)
#define def_op(...) \
struct _ {};
#if !defined(LOCAL) && !defined(LOCAL2)
#define exit my_exit
void my_exit(int x) {
fflush(stdout);
_Exit(x);
}
#endif
#endif
#define next nexT
#define prev preV
#define tree trEE
#define left lefT
#define right righT
#define div diV
#define y1 y_1
#define pow \
do \
not use cmath pow unless you know what you are doing
ull mix(ull o) {
o += 0x9e3779b97f4a7c15;
o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
ros ^ (o >> 31);
}
ull SALT = 0x7a14a4b0881ebf9, tqu = 0x7a14a4b0881ebf9;
ull my_rand() { return tqu = mix(tqu); }
void my_srand(ull x) { SALT = tqu = x; }
const int inf = 1023400000;
const ll llinf = 1234567890000000000ll;
ll fix(ll o, ll m) {
o %= m;
if (o < 0)
o += m;
ros;
}
#define rand my_rand
#define srand my_srand
#define random_shuffle(r...) \
random_shuffle(r, [](int _) { return my_rand() % _; })
sim > inline c nux(c m) {
if (!m)
return numeric_limits<c>::max();
c A = m & -m;
c B = ~((A - 1) ^ m);
c C = B & -B;
c D = (C >> (1 + ctz(A))) - 1;
return C | (m & ~(C - 1)) | D;
}
__attribute__((no_sanitize_undefined)) ll mul(ll a, ll b, ll m) {
ll q = (ll)(a * (ld)b / m);
ll o = a * b - q * m;
o %= m;
if (o < 0)
o += m;
ros;
}
sim > void unq(c &x) { x.resize(unique(ALL(x)) - x.begin()); }
#pragma GCC diagnostic pop
#if ((ULONG_MAX) != (UINT_MAX))
namespace std {
template <> struct is_signed<__int128> : public true_type {};
} // namespace std
#endif
sim, class d > typename common_type<c, d>::type floor_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using floor_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b - (a % b < 0);
}
sim, class d > typename common_type<c, d>::type ceil_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using ceil_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b + (a % b > 0);
}
sim > struct REV {
using value_type = typename c::value_type;
c &x;
using it = typename c::reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > struct CREV {
using value_type = typename c::value_type;
const c &x;
using it = typename c::const_reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > REV<c> reversed(c &x) { return REV<c>{x}; }
sim > CREV<c> reversed(const c &x) { return CREV<c>{x}; }
#define done(r...) exit(0 * printf(r))
#if defined(LOCAL) || defined(LOCAL2)
void __tmi() {
cerr << setprecision(6) << fixed
<< "total time: " << clock() / (ld)CLOCKS_PER_SEC << "s" << endl;
}
int _ = (atexit(__tmi), 0);
#endif
// #STAY AT HOME
const int nax = 5e5 + 44;
const int ROOT = 500;
const int L = 8;
tuple<int, int, int> cast(tuple<int, int, int> a) {
auto [first, second, _] = a;
return {first / ROOT, (first / ROOT) & 1 ? second : second, first};
}
bool cmp(tuple<int, int, int> a, tuple<int, int, int> b) {
return cast(a) < cast(b);
}
int a[nax];
int cou[nax];
int ans[nax];
int diffs = 0;
void add(int x) {
if (cou[x] == 0)
diffs++;
cou[x]++;
}
void rem(int x) {
cou[x]--;
if (cou[x] == 0)
diffs--;
}
int dist(tuple<int, int, int> a, tuple<int, int, int> b) {
auto [l1, r1, _] = a;
auto [l2, r2, _] = b;
if (l1 > r2 || r2 < l1)
return r1 - l1 + r2 - l2 + 2;
return abs(l1 - l2) + abs(r1 - r2);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
vector<tuple<int, int, int>> qs(q);
REP(i, q) {
int l, r;
scanf("%d%d", &l, &r);
qs[i] = {l, r, i};
}
sort(ALL(qs), cmp);
if (false)
REP(_, 3) {
REP(i, q - 3)
if (dist(qs[i], qs[i + 1]) + dist(qs[i + 2], qs[i + 3]) >=
dist(qs[i], qs[i + 2]) + dist(qs[i + 1], qs[i + 3]))
swap(qs[i + 1], qs[i + 2]);
}
auto [l, r, q0] = qs[0];
for (int i = l; i <= r; ++i)
add(a[i]);
ans[q0] = diffs;
for (int i = 1; i < q; ++i) {
auto [l0, r0, _] = qs[i - 1];
auto [l, r, ind] = qs[i];
if (r < l0 || l > r0) {
for (int j = l0; j <= r0; ++j)
rem(a[j]);
for (int j = l; j <= r; ++j)
add(a[j]);
} else {
while (l < l0)
add(a[--l0]);
while (l > l0)
rem(a[l0++]);
while (r < r0)
rem(a[r0--]);
while (r > r0)
add(a[++r0]);
}
ans[ind] = diffs;
}
REP(i, q) printf("%d\n", ans[i]);
}
// #STAY AT HOME
| // Konrad Paluszek,University of Warsaw(former XIV LO Staszic)
// #STAY AT HOME
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define TIME (chrono::steady_clock::now().time_since_epoch().count())
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define xfm(a, b) a##b
#define xwb(a, b) xfm(a, b)
#define _ xwb(nvj, __LINE__)
#define __ xwb(kjy, __LINE__)
#define ___ xwb(cjm, __LINE__)
#define REP(i, n) for (urs(n) i = 0; i < (n); ++i)
#define UNTIL(t) while (clock() < (t)*CLOCKS_PER_SEC)
#define PER(r...) for (bool _ = 1; _ || next_permutation(r); _ = false)
#define ALL(r) (r).begin(), (r).end()
#define RALL(r) (r).rbegin(), (r).rend()
#define FS(r) r.first, r.second
#define SF(r) r.second, r.first
#define M0(r) memset(r, 0, sizeof(r))
#define sim template <class c
#define ros return o
#define rans return ans
#define forbits(i, m) \
if (m) \
for (urs(m) i = ctz(m), i##nvj = m; i##nvj; \
i##nvj ^= ((urs(m))1 << i), i = ctz(i##nvj))
#define fordbits(i, m) \
if (m) \
for (urs(m) i = 8 * sizeof(m) - clz(m) - 1, i##nxd = m; i##nxd; \
i##nxd ^= ((urs(m))1 << i), i = 8 * sizeof(m) - clz(i##nxd) - 1)
#define ksets(t, m, k, n) \
for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m))
#define urs(r...) typename decay<decltype(r)>::type
#define hur(f, g, r) \
sim > int f(c a) { \
if (sizeof(c) == 16) \
return r; \
if (sizeof(c) == 8) \
return g##ll(a); \
return g(a); \
}
#define pwq(t, i) \
int clz(t x) { return clz<int>(x) - i; }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using spii = set<pii>;
using mii = map<int, int>;
using unt = unsigned int;
sim > using min_queue = priority_queue<c, vector<c>, greater<c>>;
sim, class b,
class cmp =
less<c> > using ordered_map =
tree<c, b, cmp, rb_tree_tag, tree_order_statistics_node_update>;
sim, class cmp = less < c >> using ordered_set = ordered_map<c, null_type, cmp>;
hur(popc, __builtin_popcount, popc<ull>(a) + popc<ull>(a >> 64))
hur(ctz, __builtin_ctz, (ull)a ? ctz<ull>(a) : 64 + ctz<ull>(a >> 64))
hur(clz, __builtin_clz, a >> 64 ? clz<ull>(a >> 64) : 64 + clz<ull>(a))
pwq(short, 16) pwq(uint16_t, 16) pwq(char, 24) pwq(int8_t, 24)
pwq(uint8_t, 24) sim,
class N > bool mini(c &o, const N &h) {
if (o > h)
ros = h, 1;
return 0;
}
sim, class N > bool maxi(c &o, const N &h) {
if (o < h)
ros = h, 1;
return 0;
}
sim, class n > using gyv = c;
#if defined(LOCAL) // || defined(LOCAL2)
#include </home/kjp/headers/debuglib.hpp>
#else
#define loc(...)
#define onl(r...) r
#define debug(...)
#define print_stack(...)
#define mark_stack(...)
#define set_pre(...)
#define reg_it(...)
#define def_op(...) \
struct _ {};
#if !defined(LOCAL) && !defined(LOCAL2)
#define exit my_exit
void my_exit(int x) {
fflush(stdout);
_Exit(x);
}
#endif
#endif
#define next nexT
#define prev preV
#define tree trEE
#define left lefT
#define right righT
#define div diV
#define y1 y_1
#define pow \
do \
not use cmath pow unless you know what you are doing
ull mix(ull o) {
o += 0x9e3779b97f4a7c15;
o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
ros ^ (o >> 31);
}
ull SALT = 0x7a14a4b0881ebf9, tqu = 0x7a14a4b0881ebf9;
ull my_rand() { return tqu = mix(tqu); }
void my_srand(ull x) { SALT = tqu = x; }
const int inf = 1023400000;
const ll llinf = 1234567890000000000ll;
ll fix(ll o, ll m) {
o %= m;
if (o < 0)
o += m;
ros;
}
#define rand my_rand
#define srand my_srand
#define random_shuffle(r...) \
random_shuffle(r, [](int _) { return my_rand() % _; })
sim > inline c nux(c m) {
if (!m)
return numeric_limits<c>::max();
c A = m & -m;
c B = ~((A - 1) ^ m);
c C = B & -B;
c D = (C >> (1 + ctz(A))) - 1;
return C | (m & ~(C - 1)) | D;
}
__attribute__((no_sanitize_undefined)) ll mul(ll a, ll b, ll m) {
ll q = (ll)(a * (ld)b / m);
ll o = a * b - q * m;
o %= m;
if (o < 0)
o += m;
ros;
}
sim > void unq(c &x) { x.resize(unique(ALL(x)) - x.begin()); }
#pragma GCC diagnostic pop
#if ((ULONG_MAX) != (UINT_MAX))
namespace std {
template <> struct is_signed<__int128> : public true_type {};
} // namespace std
#endif
sim, class d > typename common_type<c, d>::type floor_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using floor_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b - (a % b < 0);
}
sim, class d > typename common_type<c, d>::type ceil_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using ceil_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b + (a % b > 0);
}
sim > struct REV {
using value_type = typename c::value_type;
c &x;
using it = typename c::reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > struct CREV {
using value_type = typename c::value_type;
const c &x;
using it = typename c::const_reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > REV<c> reversed(c &x) { return REV<c>{x}; }
sim > CREV<c> reversed(const c &x) { return CREV<c>{x}; }
#define done(r...) exit(0 * printf(r))
#if defined(LOCAL) || defined(LOCAL2)
void __tmi() {
cerr << setprecision(6) << fixed
<< "total time: " << clock() / (ld)CLOCKS_PER_SEC << "s" << endl;
}
int _ = (atexit(__tmi), 0);
#endif
// #STAY AT HOME
const int nax = 5e5 + 44;
const int ROOT = 500;
const int L = 8;
tuple<int, int, int> cast(tuple<int, int, int> a) {
auto [first, second, _] = a;
swap(first, second);
return {first / ROOT, (first / ROOT) & 1 ? second : -second, first};
}
bool cmp(tuple<int, int, int> a, tuple<int, int, int> b) {
return cast(a) < cast(b);
}
int a[nax];
int cou[nax];
int ans[nax];
int diffs = 0;
void add(int x) {
if (cou[x] == 0)
diffs++;
cou[x]++;
}
void rem(int x) {
cou[x]--;
if (cou[x] == 0)
diffs--;
}
int dist(tuple<int, int, int> a, tuple<int, int, int> b) {
auto [l1, r1, _] = a;
auto [l2, r2, _] = b;
if (l1 > r2 || r2 < l1)
return r1 - l1 + r2 - l2 + 2;
return abs(l1 - l2) + abs(r1 - r2);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
vector<tuple<int, int, int>> qs(q);
REP(i, q) {
int l, r;
scanf("%d%d", &l, &r);
qs[i] = {l, r, i};
}
sort(ALL(qs), cmp);
if (false)
REP(_, 3) {
REP(i, q - 3)
if (dist(qs[i], qs[i + 1]) + dist(qs[i + 2], qs[i + 3]) >=
dist(qs[i], qs[i + 2]) + dist(qs[i + 1], qs[i + 3]))
swap(qs[i + 1], qs[i + 2]);
}
auto [l, r, q0] = qs[0];
for (int i = l; i <= r; ++i)
add(a[i]);
ans[q0] = diffs;
for (int i = 1; i < q; ++i) {
auto [l0, r0, _] = qs[i - 1];
auto [l, r, ind] = qs[i];
if (r < l0 || l > r0) {
for (int j = l0; j <= r0; ++j)
rem(a[j]);
for (int j = l; j <= r; ++j)
add(a[j]);
} else {
while (l < l0)
add(a[--l0]);
while (l > l0)
rem(a[l0++]);
while (r < r0)
rem(a[r0--]);
while (r > r0)
add(a[++r0]);
}
ans[ind] = diffs;
}
REP(i, q) printf("%d\n", ans[i]);
}
// #STAY AT HOME
| replace | 202 | 203 | 202 | 204 | TLE | |
p02599 | C++ | Time Limit Exceeded | // Konrad Paluszek,University of Warsaw(former XIV LO Staszic)
// #STAY AT HOME
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define TIME (chrono::steady_clock::now().time_since_epoch().count())
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define xfm(a, b) a##b
#define xwb(a, b) xfm(a, b)
#define _ xwb(nvj, __LINE__)
#define __ xwb(kjy, __LINE__)
#define ___ xwb(cjm, __LINE__)
#define REP(i, n) for (urs(n) i = 0; i < (n); ++i)
#define UNTIL(t) while (clock() < (t)*CLOCKS_PER_SEC)
#define PER(r...) for (bool _ = 1; _ || next_permutation(r); _ = false)
#define ALL(r) (r).begin(), (r).end()
#define RALL(r) (r).rbegin(), (r).rend()
#define FS(r) r.first, r.second
#define SF(r) r.second, r.first
#define M0(r) memset(r, 0, sizeof(r))
#define sim template <class c
#define ros return o
#define rans return ans
#define forbits(i, m) \
if (m) \
for (urs(m) i = ctz(m), i##nvj = m; i##nvj; \
i##nvj ^= ((urs(m))1 << i), i = ctz(i##nvj))
#define fordbits(i, m) \
if (m) \
for (urs(m) i = 8 * sizeof(m) - clz(m) - 1, i##nxd = m; i##nxd; \
i##nxd ^= ((urs(m))1 << i), i = 8 * sizeof(m) - clz(i##nxd) - 1)
#define ksets(t, m, k, n) \
for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m))
#define urs(r...) typename decay<decltype(r)>::type
#define hur(f, g, r) \
sim > int f(c a) { \
if (sizeof(c) == 16) \
return r; \
if (sizeof(c) == 8) \
return g##ll(a); \
return g(a); \
}
#define pwq(t, i) \
int clz(t x) { return clz<int>(x) - i; }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using spii = set<pii>;
using mii = map<int, int>;
using unt = unsigned int;
sim > using min_queue = priority_queue<c, vector<c>, greater<c>>;
sim, class b,
class cmp =
less<c> > using ordered_map =
tree<c, b, cmp, rb_tree_tag, tree_order_statistics_node_update>;
sim, class cmp = less < c >> using ordered_set = ordered_map<c, null_type, cmp>;
hur(popc, __builtin_popcount, popc<ull>(a) + popc<ull>(a >> 64))
hur(ctz, __builtin_ctz, (ull)a ? ctz<ull>(a) : 64 + ctz<ull>(a >> 64))
hur(clz, __builtin_clz, a >> 64 ? clz<ull>(a >> 64) : 64 + clz<ull>(a))
pwq(short, 16) pwq(uint16_t, 16) pwq(char, 24) pwq(int8_t, 24)
pwq(uint8_t, 24) sim,
class N > bool mini(c &o, const N &h) {
if (o > h)
ros = h, 1;
return 0;
}
sim, class N > bool maxi(c &o, const N &h) {
if (o < h)
ros = h, 1;
return 0;
}
sim, class n > using gyv = c;
#if defined(LOCAL) // || defined(LOCAL2)
#include </home/kjp/headers/debuglib.hpp>
#else
#define loc(...)
#define onl(r...) r
#define debug(...)
#define print_stack(...)
#define mark_stack(...)
#define set_pre(...)
#define reg_it(...)
#define def_op(...) \
struct _ {};
#if !defined(LOCAL) && !defined(LOCAL2)
#define exit my_exit
void my_exit(int x) {
fflush(stdout);
_Exit(x);
}
#endif
#endif
#define next nexT
#define prev preV
#define tree trEE
#define left lefT
#define right righT
#define div diV
#define y1 y_1
#define pow \
do \
not use cmath pow unless you know what you are doing
ull mix(ull o) {
o += 0x9e3779b97f4a7c15;
o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
ros ^ (o >> 31);
}
ull SALT = 0x7a14a4b0881ebf9, tqu = 0x7a14a4b0881ebf9;
ull my_rand() { return tqu = mix(tqu); }
void my_srand(ull x) { SALT = tqu = x; }
const int inf = 1023400000;
const ll llinf = 1234567890000000000ll;
ll fix(ll o, ll m) {
o %= m;
if (o < 0)
o += m;
ros;
}
#define rand my_rand
#define srand my_srand
#define random_shuffle(r...) \
random_shuffle(r, [](int _) { return my_rand() % _; })
sim > inline c nux(c m) {
if (!m)
return numeric_limits<c>::max();
c A = m & -m;
c B = ~((A - 1) ^ m);
c C = B & -B;
c D = (C >> (1 + ctz(A))) - 1;
return C | (m & ~(C - 1)) | D;
}
__attribute__((no_sanitize_undefined)) ll mul(ll a, ll b, ll m) {
ll q = (ll)(a * (ld)b / m);
ll o = a * b - q * m;
o %= m;
if (o < 0)
o += m;
ros;
}
sim > void unq(c &x) { x.resize(unique(ALL(x)) - x.begin()); }
#pragma GCC diagnostic pop
#if ((ULONG_MAX) != (UINT_MAX))
namespace std {
template <> struct is_signed<__int128> : public true_type {};
} // namespace std
#endif
sim, class d > typename common_type<c, d>::type floor_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using floor_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b - (a % b < 0);
}
sim, class d > typename common_type<c, d>::type ceil_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using ceil_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b + (a % b > 0);
}
sim > struct REV {
using value_type = typename c::value_type;
c &x;
using it = typename c::reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > struct CREV {
using value_type = typename c::value_type;
const c &x;
using it = typename c::const_reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > REV<c> reversed(c &x) { return REV<c>{x}; }
sim > CREV<c> reversed(const c &x) { return CREV<c>{x}; }
#define done(r...) exit(0 * printf(r))
#if defined(LOCAL) || defined(LOCAL2)
void __tmi() {
cerr << setprecision(6) << fixed
<< "total time: " << clock() / (ld)CLOCKS_PER_SEC << "s" << endl;
}
int _ = (atexit(__tmi), 0);
#endif
// #STAY AT HOME
const int nax = 5e5 + 44;
const int ROOT = 270;
const int L = 8;
tuple<int, int, int> cast(tuple<int, int, int> a) {
auto [first, second, _] = a;
return {first / ROOT, (first / ROOT) & 1 ? second : -second, first};
}
bool cmp(tuple<int, int, int> a, tuple<int, int, int> b) {
return cast(a) < cast(b);
}
int a[nax];
int cou[nax];
int ans[nax];
int diffs = 0;
void add(int x) {
if (cou[x] == 0)
diffs++;
cou[x]++;
}
void rem(int x) {
cou[x]--;
if (cou[x] == 0)
diffs--;
}
int dist(tuple<int, int, int> a, tuple<int, int, int> b) {
auto [l1, r1, _] = a;
auto [l2, r2, _] = b;
if (l1 > r2 || r2 < l1)
return r1 - l1 + r2 - l2 + 2;
return abs(l1 - l2) + abs(r1 - r2);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
vector<tuple<int, int, int>> qs(q);
REP(i, q) {
int l, r;
scanf("%d%d", &l, &r);
qs[i] = {l, r, i};
}
sort(ALL(qs), cmp);
REP(_, 3) {
REP(i, q - 3)
if (dist(qs[i], qs[i + 1]) + dist(qs[i + 2], qs[i + 3]) >=
dist(qs[i], qs[i + 2]) + dist(qs[i + 1], qs[i + 3]))
swap(qs[i + 1], qs[i + 2]);
}
auto [l, r, q0] = qs[0];
for (int i = l; i <= r; ++i)
add(a[i]);
ans[q0] = diffs;
for (int i = 1; i < q; ++i) {
auto [l0, r0, _] = qs[i - 1];
auto [l, r, ind] = qs[i];
if (r < l0 || l > r0) {
for (int j = l0; j <= r0; ++j)
rem(a[j]);
for (int j = l; j <= r; ++j)
add(a[j]);
} else {
while (l < l0)
add(a[--l0]);
while (l > l0)
rem(a[l0++]);
while (r < r0)
rem(a[r0--]);
while (r > r0)
add(a[++r0]);
}
ans[ind] = diffs;
}
REP(i, q) printf("%d\n", ans[i]);
}
// #STAY AT HOME
| // Konrad Paluszek,University of Warsaw(former XIV LO Staszic)
// #STAY AT HOME
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define TIME (chrono::steady_clock::now().time_since_epoch().count())
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define xfm(a, b) a##b
#define xwb(a, b) xfm(a, b)
#define _ xwb(nvj, __LINE__)
#define __ xwb(kjy, __LINE__)
#define ___ xwb(cjm, __LINE__)
#define REP(i, n) for (urs(n) i = 0; i < (n); ++i)
#define UNTIL(t) while (clock() < (t)*CLOCKS_PER_SEC)
#define PER(r...) for (bool _ = 1; _ || next_permutation(r); _ = false)
#define ALL(r) (r).begin(), (r).end()
#define RALL(r) (r).rbegin(), (r).rend()
#define FS(r) r.first, r.second
#define SF(r) r.second, r.first
#define M0(r) memset(r, 0, sizeof(r))
#define sim template <class c
#define ros return o
#define rans return ans
#define forbits(i, m) \
if (m) \
for (urs(m) i = ctz(m), i##nvj = m; i##nvj; \
i##nvj ^= ((urs(m))1 << i), i = ctz(i##nvj))
#define fordbits(i, m) \
if (m) \
for (urs(m) i = 8 * sizeof(m) - clz(m) - 1, i##nxd = m; i##nxd; \
i##nxd ^= ((urs(m))1 << i), i = 8 * sizeof(m) - clz(i##nxd) - 1)
#define ksets(t, m, k, n) \
for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m))
#define urs(r...) typename decay<decltype(r)>::type
#define hur(f, g, r) \
sim > int f(c a) { \
if (sizeof(c) == 16) \
return r; \
if (sizeof(c) == 8) \
return g##ll(a); \
return g(a); \
}
#define pwq(t, i) \
int clz(t x) { return clz<int>(x) - i; }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using spii = set<pii>;
using mii = map<int, int>;
using unt = unsigned int;
sim > using min_queue = priority_queue<c, vector<c>, greater<c>>;
sim, class b,
class cmp =
less<c> > using ordered_map =
tree<c, b, cmp, rb_tree_tag, tree_order_statistics_node_update>;
sim, class cmp = less < c >> using ordered_set = ordered_map<c, null_type, cmp>;
hur(popc, __builtin_popcount, popc<ull>(a) + popc<ull>(a >> 64))
hur(ctz, __builtin_ctz, (ull)a ? ctz<ull>(a) : 64 + ctz<ull>(a >> 64))
hur(clz, __builtin_clz, a >> 64 ? clz<ull>(a >> 64) : 64 + clz<ull>(a))
pwq(short, 16) pwq(uint16_t, 16) pwq(char, 24) pwq(int8_t, 24)
pwq(uint8_t, 24) sim,
class N > bool mini(c &o, const N &h) {
if (o > h)
ros = h, 1;
return 0;
}
sim, class N > bool maxi(c &o, const N &h) {
if (o < h)
ros = h, 1;
return 0;
}
sim, class n > using gyv = c;
#if defined(LOCAL) // || defined(LOCAL2)
#include </home/kjp/headers/debuglib.hpp>
#else
#define loc(...)
#define onl(r...) r
#define debug(...)
#define print_stack(...)
#define mark_stack(...)
#define set_pre(...)
#define reg_it(...)
#define def_op(...) \
struct _ {};
#if !defined(LOCAL) && !defined(LOCAL2)
#define exit my_exit
void my_exit(int x) {
fflush(stdout);
_Exit(x);
}
#endif
#endif
#define next nexT
#define prev preV
#define tree trEE
#define left lefT
#define right righT
#define div diV
#define y1 y_1
#define pow \
do \
not use cmath pow unless you know what you are doing
ull mix(ull o) {
o += 0x9e3779b97f4a7c15;
o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
ros ^ (o >> 31);
}
ull SALT = 0x7a14a4b0881ebf9, tqu = 0x7a14a4b0881ebf9;
ull my_rand() { return tqu = mix(tqu); }
void my_srand(ull x) { SALT = tqu = x; }
const int inf = 1023400000;
const ll llinf = 1234567890000000000ll;
ll fix(ll o, ll m) {
o %= m;
if (o < 0)
o += m;
ros;
}
#define rand my_rand
#define srand my_srand
#define random_shuffle(r...) \
random_shuffle(r, [](int _) { return my_rand() % _; })
sim > inline c nux(c m) {
if (!m)
return numeric_limits<c>::max();
c A = m & -m;
c B = ~((A - 1) ^ m);
c C = B & -B;
c D = (C >> (1 + ctz(A))) - 1;
return C | (m & ~(C - 1)) | D;
}
__attribute__((no_sanitize_undefined)) ll mul(ll a, ll b, ll m) {
ll q = (ll)(a * (ld)b / m);
ll o = a * b - q * m;
o %= m;
if (o < 0)
o += m;
ros;
}
sim > void unq(c &x) { x.resize(unique(ALL(x)) - x.begin()); }
#pragma GCC diagnostic pop
#if ((ULONG_MAX) != (UINT_MAX))
namespace std {
template <> struct is_signed<__int128> : public true_type {};
} // namespace std
#endif
sim, class d > typename common_type<c, d>::type floor_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using floor_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b - (a % b < 0);
}
sim, class d > typename common_type<c, d>::type ceil_div(c a, d b) {
static_assert(is_signed<c>::value == is_signed<d>::value,
"using ceil_div with different signedness");
if (b < 0)
b = -b, a = -a;
return a / b + (a % b > 0);
}
sim > struct REV {
using value_type = typename c::value_type;
c &x;
using it = typename c::reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > struct CREV {
using value_type = typename c::value_type;
const c &x;
using it = typename c::const_reverse_iterator;
it begin() { return x.rbegin(); }
it end() { return x.rend(); }
};
sim > REV<c> reversed(c &x) { return REV<c>{x}; }
sim > CREV<c> reversed(const c &x) { return CREV<c>{x}; }
#define done(r...) exit(0 * printf(r))
#if defined(LOCAL) || defined(LOCAL2)
void __tmi() {
cerr << setprecision(6) << fixed
<< "total time: " << clock() / (ld)CLOCKS_PER_SEC << "s" << endl;
}
int _ = (atexit(__tmi), 0);
#endif
// #STAY AT HOME
const int nax = 5e5 + 44;
const int ROOT = 500;
const int L = 8;
tuple<int, int, int> cast(tuple<int, int, int> a) {
auto [first, second, _] = a;
return {first / ROOT, (first / ROOT) & 1 ? second : -second, first};
}
bool cmp(tuple<int, int, int> a, tuple<int, int, int> b) {
return cast(a) < cast(b);
}
int a[nax];
int cou[nax];
int ans[nax];
int diffs = 0;
void add(int x) {
if (cou[x] == 0)
diffs++;
cou[x]++;
}
void rem(int x) {
cou[x]--;
if (cou[x] == 0)
diffs--;
}
int dist(tuple<int, int, int> a, tuple<int, int, int> b) {
auto [l1, r1, _] = a;
auto [l2, r2, _] = b;
if (l1 > r2 || r2 < l1)
return r1 - l1 + r2 - l2 + 2;
return abs(l1 - l2) + abs(r1 - r2);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
vector<tuple<int, int, int>> qs(q);
REP(i, q) {
int l, r;
scanf("%d%d", &l, &r);
qs[i] = {l, r, i};
}
sort(ALL(qs), cmp);
REP(_, 3) {
REP(i, q - 3)
if (dist(qs[i], qs[i + 1]) + dist(qs[i + 2], qs[i + 3]) >=
dist(qs[i], qs[i + 2]) + dist(qs[i + 1], qs[i + 3]))
swap(qs[i + 1], qs[i + 2]);
}
auto [l, r, q0] = qs[0];
for (int i = l; i <= r; ++i)
add(a[i]);
ans[q0] = diffs;
for (int i = 1; i < q; ++i) {
auto [l0, r0, _] = qs[i - 1];
auto [l, r, ind] = qs[i];
if (r < l0 || l > r0) {
for (int j = l0; j <= r0; ++j)
rem(a[j]);
for (int j = l; j <= r; ++j)
add(a[j]);
} else {
while (l < l0)
add(a[--l0]);
while (l > l0)
rem(a[l0++]);
while (r < r0)
rem(a[r0--]);
while (r > r0)
add(a[++r0]);
}
ans[ind] = diffs;
}
REP(i, q) printf("%d\n", ans[i]);
}
// #STAY AT HOME
| replace | 198 | 199 | 198 | 199 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, r, l) for (int i = r; i >= l; i--)
const int MX = 2e5 + 7;
const int mod = 998244353;
const double pi = 3.1415926535897932384;
double isp = 1e-13;
using namespace std;
ll qpow(ll a, ll b, ll MOD = mod) {
for (ll ans = 1;; a = a * a % MOD, b >>= 1) {
if (b & 1)
ans = ans * a % MOD;
if (!b)
return ans;
}
}
ll inv(ll a, ll MOD = mod) { return qpow(a, MOD - 2, MOD); } // 要求MOD为质数
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
ll ret = exgcd(b, a % b, y, x);
y -= a / b * x;
return ret;
}
ll getInv(int a, int mod) {
ll x, y;
ll d = exgcd(a, mod, x, y);
return d == 1 ? (x % mod + mod) % mod : -1;
} // 求a在mod下的逆元,不存在逆元返回-1,不要求MOD为质数
int n, c[MX], a[MX], ans[MX];
void upd(int pos, int val) {
for (int x = pos; x <= n; x += x & -x)
c[x] += val;
}
int Sum(int pos) /// 从0位置到pos位置的区间和
{
int ans = 0;
for (int x = pos; x; x -= x & -x)
ans += c[x];
return ans;
}
unordered_map<int, int> vis;
vector<pair<int, int>> mp[MX];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int q;
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
mp[r].push_back(make_pair(l, i));
}
for (int i = 1; i <= n; i++) {
// cout<<vis[a[i]]<<endl;
if (vis[a[i]])
upd(vis[a[i]], -1);
upd(i, 1);
vis[a[i]] = i;
for (auto x : mp[i]) {
int l = x.first;
ans[x.second] = Sum(i) - Sum(l - 1);
}
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
}
| #include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, r, l) for (int i = r; i >= l; i--)
const int MX = 5e5 + 7;
const int mod = 998244353;
const double pi = 3.1415926535897932384;
double isp = 1e-13;
using namespace std;
ll qpow(ll a, ll b, ll MOD = mod) {
for (ll ans = 1;; a = a * a % MOD, b >>= 1) {
if (b & 1)
ans = ans * a % MOD;
if (!b)
return ans;
}
}
ll inv(ll a, ll MOD = mod) { return qpow(a, MOD - 2, MOD); } // 要求MOD为质数
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
ll ret = exgcd(b, a % b, y, x);
y -= a / b * x;
return ret;
}
ll getInv(int a, int mod) {
ll x, y;
ll d = exgcd(a, mod, x, y);
return d == 1 ? (x % mod + mod) % mod : -1;
} // 求a在mod下的逆元,不存在逆元返回-1,不要求MOD为质数
int n, c[MX], a[MX], ans[MX];
void upd(int pos, int val) {
for (int x = pos; x <= n; x += x & -x)
c[x] += val;
}
int Sum(int pos) /// 从0位置到pos位置的区间和
{
int ans = 0;
for (int x = pos; x; x -= x & -x)
ans += c[x];
return ans;
}
unordered_map<int, int> vis;
vector<pair<int, int>> mp[MX];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int q;
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
mp[r].push_back(make_pair(l, i));
}
for (int i = 1; i <= n; i++) {
// cout<<vis[a[i]]<<endl;
if (vis[a[i]])
upd(vis[a[i]], -1);
upd(i, 1);
vis[a[i]] = i;
for (auto x : mp[i]) {
int l = x.first;
ans[x.second] = Sum(i) - Sum(l - 1);
}
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define C continue;
#define R return
#define D double
#define I insert
#define ll long long
#define ld long double
#define ull unsigned long long
#define ui unsigned int
#define pb push_back
#define pf push_front
#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vb vector<bool>
#define vd vector<D>
#define vll vector<ll>
#define vull vector<ull>
#define vld vector<ld>
#define PQ priority_queue
#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vvc vector<vector<char>>
#define vvs vector<vs>
#define vvll vector<vector<ll>>
#define vvd vector<vector<D>>
#define vvld vector<vector<ld>>
#define all(v) (v).begin(), (v).end()
#define allrev(v) (v).rbegin(), (v).rend()
#define allcomp(v) v.begin(), v.end(), comp
#define allrevcomp(v) v.rbegin(), v.rend(), comp
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pDD pair<D, D>
#define vpld vector<pld>
#define vpii vector<pii>
#define vpll vector<pll>
#define vpDD vector<pDD>
#define vvpii vector<vector<pii>>
#define F first
#define S second
#define mp make_pair
#define dist(a, b, p, q) sqrt((p - a) * (p - a) + (q - b) * (q - b))
#define pp(n) printf("%.10Lf", n);
#define line cout << "\n";
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const ld pie = 3.1415926535897932384626;
const ll mod = 1e9 + 7;
/// Tip : If a and b are positive integers ; we may say - ceil (a/b) = 1 + floor
/// ( (a-1)/b ) .
const int N = 5e5 + 10;
int v[N];
vvi qry;
int n, q;
void read() {
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> v[i];
qry.resize(q);
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
qry[i] = {l, r, i};
}
}
int z;
bool comp(const vi &a, const vi &b) {
if (a[0] / z != b[0] / z)
return a < b;
return a[1] < b[1];
}
int cnt = 0;
int h[N];
void add(const int &indx) {
int val = v[indx];
h[val]++;
if (h[val] == 1)
cnt++;
}
void del(const int &indx) {
int val = v[indx];
h[val]--;
if (h[val] == 0)
cnt--;
}
void solve(int test_case) {
read();
z = sqrt(n);
if (z * z < n)
z++;
sort(allcomp(qry));
int i = 0, j = -1;
vi ans(q);
for (int indx = 0; indx < q; indx++) {
int l = qry[indx][0], r = qry[indx][1];
// cout << "l and r : " << l + 1 << " " << r + 1 ; line ;
while (j < r)
add(++j);
while (j > r)
del(j--);
while (i < l)
del(i++);
while (i > l)
add(--i);
// cout << "i : " << i + 1 << " and j : " << j + 1 << " and cnt : "
// << cnt ; line ;
ans[qry[indx][2]] = cnt;
}
for (auto &i : ans)
cout << i, line;
// line ;
}
int main() {
fast int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
solve(i);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define C continue;
#define R return
#define D double
#define I insert
#define ll long long
#define ld long double
#define ull unsigned long long
#define ui unsigned int
#define pb push_back
#define pf push_front
#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vb vector<bool>
#define vd vector<D>
#define vll vector<ll>
#define vull vector<ull>
#define vld vector<ld>
#define PQ priority_queue
#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vvc vector<vector<char>>
#define vvs vector<vs>
#define vvll vector<vector<ll>>
#define vvd vector<vector<D>>
#define vvld vector<vector<ld>>
#define all(v) (v).begin(), (v).end()
#define allrev(v) (v).rbegin(), (v).rend()
#define allcomp(v) v.begin(), v.end(), comp
#define allrevcomp(v) v.rbegin(), v.rend(), comp
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pDD pair<D, D>
#define vpld vector<pld>
#define vpii vector<pii>
#define vpll vector<pll>
#define vpDD vector<pDD>
#define vvpii vector<vector<pii>>
#define F first
#define S second
#define mp make_pair
#define dist(a, b, p, q) sqrt((p - a) * (p - a) + (q - b) * (q - b))
#define pp(n) printf("%.10Lf", n);
#define line cout << "\n";
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const ld pie = 3.1415926535897932384626;
const ll mod = 1e9 + 7;
/// Tip : If a and b are positive integers ; we may say - ceil (a/b) = 1 + floor
/// ( (a-1)/b ) .
const int N = 5e5 + 10;
int v[N];
vvi qry;
int n, q;
void read() {
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> v[i];
qry.resize(q);
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
qry[i] = {l, r, i};
}
}
int z;
bool comp(const vi &a, const vi &b) {
if (a[0] / z != b[0] / z)
return a < b;
if (((a[0] / z) & 1))
return a[1] > b[1];
return a[1] < b[1];
}
int cnt = 0;
int h[N];
void add(const int &indx) {
int val = v[indx];
h[val]++;
if (h[val] == 1)
cnt++;
}
void del(const int &indx) {
int val = v[indx];
h[val]--;
if (h[val] == 0)
cnt--;
}
void solve(int test_case) {
read();
z = sqrt(n);
if (z * z < n)
z++;
sort(allcomp(qry));
int i = 0, j = -1;
vi ans(q);
for (int indx = 0; indx < q; indx++) {
int l = qry[indx][0], r = qry[indx][1];
// cout << "l and r : " << l + 1 << " " << r + 1 ; line ;
while (j < r)
add(++j);
while (j > r)
del(j--);
while (i < l)
del(i++);
while (i > l)
add(--i);
// cout << "i : " << i + 1 << " and j : " << j + 1 << " and cnt : "
// << cnt ; line ;
ans[qry[indx][2]] = cnt;
}
for (auto &i : ans)
cout << i, line;
// line ;
}
int main() {
fast int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
solve(i);
}
return 0;
}
| insert | 111 | 111 | 111 | 114 | TLE | |
p02599 | C++ | Runtime Error | /*
Author:loceaner
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define lowbit(x) (x & (-x));
using namespace std;
const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for (; !isdigit(c); c = getchar())
if (c == '-')
f = -1;
for (; isdigit(c); c = getchar())
x = x * 10 + (c ^ 48);
return x * f;
}
struct node {
int l, r, pos;
} ask[A];
int n, m, c[A], num[A], tree[A], booll[A], nnn[A], ww;
void change(int x, int k) {
while (x <= n) {
c[x] += k;
x += lowbit(x);
}
}
int query(int x) {
int ans = 0;
while (x) {
ans += c[x];
x -= lowbit(x);
}
return ans;
}
bool cmp(node a, node b) { return a.r < b.r; }
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= n; i++)
scanf("%d", &num[i]);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &ask[i].l, &ask[i].r);
ask[i].pos = i;
}
sort(ask + 1, ask + 1 + m, cmp);
int next = 1;
for (int i = 1; i <= m; i++) {
for (int j = next; j <= ask[i].r; j++) {
if (booll[num[j]])
change(booll[num[j]], -1);
change(j, 1), booll[num[j]] = j;
}
next = ask[i].r + 1;
nnn[ask[i].pos] = query(ask[i].r) - query(ask[i].l - 1);
}
for (int i = 1; i <= m; i++)
cout << nnn[i] << "\n";
return 0;
}
| /*
Author:loceaner
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define lowbit(x) (x & (-x));
using namespace std;
const int A = 1e6 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for (; !isdigit(c); c = getchar())
if (c == '-')
f = -1;
for (; isdigit(c); c = getchar())
x = x * 10 + (c ^ 48);
return x * f;
}
struct node {
int l, r, pos;
} ask[A];
int n, m, c[A], num[A], tree[A], booll[A], nnn[A], ww;
void change(int x, int k) {
while (x <= n) {
c[x] += k;
x += lowbit(x);
}
}
int query(int x) {
int ans = 0;
while (x) {
ans += c[x];
x -= lowbit(x);
}
return ans;
}
bool cmp(node a, node b) { return a.r < b.r; }
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= n; i++)
scanf("%d", &num[i]);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &ask[i].l, &ask[i].r);
ask[i].pos = i;
}
sort(ask + 1, ask + 1 + m, cmp);
int next = 1;
for (int i = 1; i <= m; i++) {
for (int j = next; j <= ask[i].r; j++) {
if (booll[num[j]])
change(booll[num[j]], -1);
change(j, 1), booll[num[j]] = j;
}
next = ask[i].r + 1;
nnn[ask[i].pos] = query(ask[i].r) - query(ask[i].l - 1);
}
for (int i = 1; i <= m; i++)
cout << nnn[i] << "\n";
return 0;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> inline bool chmax(T &a, const T &b) {
return b > a ? a = b, true : false;
}
template <class T> inline bool chmin(T &a, const T &b) {
return b < a ? a = b, true : false;
}
template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) {
sort(begin(v), end(v), f);
v.resize(unique(begin(v), end(v)) - begin(v));
}
const int N = 2e5 + 5;
int A[N], L[N], R[N];
set<int> dp[N][18];
int n, qq;
map<int, int> seen;
// fenwick
int fenw[N];
void upd(int k, int x) {
for (int i = k; i <= n; i += i & -i) {
fenw[i] += x;
}
}
int get(int k) {
int res = 0;
for (int i = k; i; i -= i & -i) {
res += fenw[i];
}
return res;
}
// 用BIT記錄i之前,distinct value的數量
// 如果在j位置(j<i)已經出現A[i],先將j位置-1,在i位置+1
// fenw[i]=1,代表A[i]在位置i最後出現
// 將query按照[l,r]的r升冪排序,
// 再遍歷array A時,如果對到達的r,query BIT,計算distinct value
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> qq;
REP(i, n) cin >> A[i];
REP(i, qq) cin >> L[i] >> R[i];
VI Q(qq), ans(qq);
iota(ALL(Q), 0);
sort(ALL(Q),
[&](int i, int j) { return R[i] == R[j] ? L[i] < L[j] : R[i] < R[j]; });
int k = 0;
REP(i, n) {
if (seen.count(A[i])) {
upd(seen[A[i]], -1);
}
upd(i + 1, 1);
seen[A[i]] = i + 1;
while (k < qq && R[Q[k]] - 1 == i) {
ans[Q[k]] = get(R[Q[k]]) - get(L[Q[k]] - 1);
k++;
}
}
for (int i : ans) {
cout << i << '\n';
}
return 0;
}
| #include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> inline bool chmax(T &a, const T &b) {
return b > a ? a = b, true : false;
}
template <class T> inline bool chmin(T &a, const T &b) {
return b < a ? a = b, true : false;
}
template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) {
sort(begin(v), end(v), f);
v.resize(unique(begin(v), end(v)) - begin(v));
}
const int N = 5e5 + 5;
int A[N], L[N], R[N];
set<int> dp[N][18];
int n, qq;
map<int, int> seen;
// fenwick
int fenw[N];
void upd(int k, int x) {
for (int i = k; i <= n; i += i & -i) {
fenw[i] += x;
}
}
int get(int k) {
int res = 0;
for (int i = k; i; i -= i & -i) {
res += fenw[i];
}
return res;
}
// 用BIT記錄i之前,distinct value的數量
// 如果在j位置(j<i)已經出現A[i],先將j位置-1,在i位置+1
// fenw[i]=1,代表A[i]在位置i最後出現
// 將query按照[l,r]的r升冪排序,
// 再遍歷array A時,如果對到達的r,query BIT,計算distinct value
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> qq;
REP(i, n) cin >> A[i];
REP(i, qq) cin >> L[i] >> R[i];
VI Q(qq), ans(qq);
iota(ALL(Q), 0);
sort(ALL(Q),
[&](int i, int j) { return R[i] == R[j] ? L[i] < L[j] : R[i] < R[j]; });
int k = 0;
REP(i, n) {
if (seen.count(A[i])) {
upd(seen[A[i]], -1);
}
upd(i + 1, 1);
seen[A[i]] = i + 1;
while (k < qq && R[Q[k]] - 1 == i) {
ans[Q[k]] = get(R[Q[k]]) - get(L[Q[k]] - 1);
k++;
}
}
for (int i : ans) {
cout << i << '\n';
}
return 0;
}
| replace | 24 | 25 | 24 | 25 | -11 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)((n)-1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define fi first
#define se second
#define debug(...) \
{ \
cerr << "[L" << __LINE__ << "] "; \
_debug(__VA_ARGS__); \
}
template <typename T> string join(const vector<T> &v, string del = ", ") {
stringstream s;
for (auto x : v)
s << del << x;
return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
if (v.size())
o << "[" << join(v) << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
int l = vv.size();
if (l) {
o << endl;
rep(i, l) o << (i == 0 ? "[ " : ",\n ") << vv[i]
<< (i == l - 1 ? " ]" : "");
}
return o;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &p) {
return o << "(" << p.first << ", " << p.second << ")";
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
cerr << first << " ";
_debug(rest...);
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;
inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }
template <typename T> struct BIT {
int n;
vector<T> dat;
BIT(int n) : n(n), dat(n + 1, 0) {}
void add(int x, T val) {
for (int i = x + 1; i <= n; i += (i & -i))
dat[i] += val;
}
T sum(int x) {
T ret = 0;
for (int i = x + 1; i > 0; i -= (i & -i))
ret += dat[i];
return ret;
}
T sum(int a, int b) { return sum(b) - sum(a - 1); }
};
int main() {
ios_base::sync_with_stdio(0);
// finput("./input");
int n, q;
cin >> n >> q;
vi C(n + 1);
rep(i, n) cin >> C[i + 1];
vector<tuple<ll, ll, ll>> Q;
rep(i, q) {
int l, r;
cin >> l >> r;
Q.emplace_back(r, l, i);
}
Q.emplace_back(n + 10, 0, 0);
sort(all(Q));
auto bit = BIT<ll>(n + 10);
vi last(n + 1, -1);
int pos = 0;
vi ans(n);
rep(i, q) {
int qr = get<0>(Q[i]);
int ql = get<1>(Q[i]);
int idx = get<2>(Q[i]);
while (pos <= qr) {
if (last[C[pos]] != -1) {
bit.add(last[C[pos]], -1);
}
bit.add(pos, 1);
last[C[pos]] = pos;
pos++;
}
ans[idx] = bit.sum(ql, qr);
}
rep(i, q) cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)((n)-1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define fi first
#define se second
#define debug(...) \
{ \
cerr << "[L" << __LINE__ << "] "; \
_debug(__VA_ARGS__); \
}
template <typename T> string join(const vector<T> &v, string del = ", ") {
stringstream s;
for (auto x : v)
s << del << x;
return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
if (v.size())
o << "[" << join(v) << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
int l = vv.size();
if (l) {
o << endl;
rep(i, l) o << (i == 0 ? "[ " : ",\n ") << vv[i]
<< (i == l - 1 ? " ]" : "");
}
return o;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &p) {
return o << "(" << p.first << ", " << p.second << ")";
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
cerr << first << " ";
_debug(rest...);
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;
inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }
template <typename T> struct BIT {
int n;
vector<T> dat;
BIT(int n) : n(n), dat(n + 1, 0) {}
void add(int x, T val) {
for (int i = x + 1; i <= n; i += (i & -i))
dat[i] += val;
}
T sum(int x) {
T ret = 0;
for (int i = x + 1; i > 0; i -= (i & -i))
ret += dat[i];
return ret;
}
T sum(int a, int b) { return sum(b) - sum(a - 1); }
};
int main() {
ios_base::sync_with_stdio(0);
// finput("./input");
int n, q;
cin >> n >> q;
vi C(n + 1);
rep(i, n) cin >> C[i + 1];
vector<tuple<ll, ll, ll>> Q;
rep(i, q) {
int l, r;
cin >> l >> r;
Q.emplace_back(r, l, i);
}
Q.emplace_back(n + 10, 0, 0);
sort(all(Q));
auto bit = BIT<ll>(n + 10);
vi last(n + 1, -1);
int pos = 0;
vi ans(q);
rep(i, q) {
int qr = get<0>(Q[i]);
int ql = get<1>(Q[i]);
int idx = get<2>(Q[i]);
while (pos <= qr) {
if (last[C[pos]] != -1) {
bit.add(last[C[pos]], -1);
}
bit.add(pos, 1);
last[C[pos]] = pos;
pos++;
}
ans[idx] = bit.sum(ql, qr);
}
rep(i, q) cout << ans[i] << endl;
return 0;
} | replace | 103 | 104 | 103 | 104 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second
using namespace std;
pair<int, pair<int, int>> q[200001];
int n, m, a[100001], ans[200001];
int st[5 * 100001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
if (a.s.f != b.s.f)
return a.s.f < b.s.f;
return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
if (l == r) {
st[v] = d;
return;
}
int mid = (l + r) / 2;
if (x <= mid)
update(v * 2, l, mid, x, d);
else
update(v * 2 + 1, mid + 1, r, x, d);
st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
if (l > B || r < A)
return 0;
if (A == l && B == r)
return st[v];
int mid = (A + B) / 2;
return Find(v * 2, A, mid, l, min(mid, r)) +
Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++)
scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
sort(q + 1, q + m + 1, cmp);
int j = 1;
for (int i = 1; i <= m; i++) {
while (j <= q[i].s.f) {
if (!used[a[j]]) {
update(1, 1, n, j, 1);
used[a[j]] = j;
} else {
update(1, 1, n, used[a[j]], 0);
update(1, 1, n, j, 1);
used[a[j]] = j;
}
j++;
}
ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second
using namespace std;
pair<int, pair<int, int>> q[1000001];
int n, m, a[500001], ans[1000001];
int st[5 * 500001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
if (a.s.f != b.s.f)
return a.s.f < b.s.f;
return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
if (l == r) {
st[v] = d;
return;
}
int mid = (l + r) / 2;
if (x <= mid)
update(v * 2, l, mid, x, d);
else
update(v * 2 + 1, mid + 1, r, x, d);
st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
if (l > B || r < A)
return 0;
if (A == l && B == r)
return st[v];
int mid = (A + B) / 2;
return Find(v * 2, A, mid, l, min(mid, r)) +
Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++)
scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
sort(q + 1, q + m + 1, cmp);
int j = 1;
for (int i = 1; i <= m; i++) {
while (j <= q[i].s.f) {
if (!used[a[j]]) {
update(1, 1, n, j, 1);
used[a[j]] = j;
} else {
update(1, 1, n, used[a[j]], 0);
update(1, 1, n, j, 1);
used[a[j]] = j;
}
j++;
}
ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
} | replace | 8 | 11 | 8 | 11 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
struct query {
int l;
int r;
int i;
int ans;
};
signed main() {
int n, t;
cin >> n >> t;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<query> q(t);
for (int i = 0; i < t; ++i) {
cin >> q[i].l >> q[i].r;
q[i].i = i;
q[i].l--;
q[i].r--;
}
int sqrtn = sqrt(n) + 1;
sort(q.begin(), q.end(), [&sqrtn](query &a, query &b) {
if (a.r == b.r)
return a.l < b.l;
return a.r < b.r;
});
vector<int> m(n + 1);
int l = 0;
int r = -1;
int cnt = 0;
for (auto &q : q) {
if (r < q.l || q.r < l) {
m.assign(n + 1, 0);
cnt = 0;
l = q.l;
r = q.l - 1;
}
while (r < q.r) {
r++;
if (m[a[r]] == 0)
cnt++;
m[a[r]]++;
}
while (r > q.r) {
m[a[r]]--;
if (m[a[r]] == 0)
cnt--;
r--;
}
while (l < q.l) {
m[a[l]]--;
if (m[a[l]] == 0)
cnt--;
l++;
}
while (l > q.l) {
l--;
if (m[a[l]] == 0)
cnt++;
m[a[l]]++;
}
q.ans = cnt;
}
sort(q.begin(), q.end(), [](query &a, query &b) { return a.i < b.i; });
for (auto &q : q) {
cout << q.ans << '\n';
}
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
struct query {
int l;
int r;
int i;
int ans;
};
signed main() {
int n, t;
cin >> n >> t;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<query> q(t);
for (int i = 0; i < t; ++i) {
cin >> q[i].l >> q[i].r;
q[i].i = i;
q[i].l--;
q[i].r--;
}
int sqrtn = sqrt(n) + 1;
sort(q.begin(), q.end(), [&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 l = 0;
int r = -1;
int cnt = 0;
for (auto &q : q) {
if (r < q.l || q.r < l) {
m.assign(n + 1, 0);
cnt = 0;
l = q.l;
r = q.l - 1;
}
while (r < q.r) {
r++;
if (m[a[r]] == 0)
cnt++;
m[a[r]]++;
}
while (r > q.r) {
m[a[r]]--;
if (m[a[r]] == 0)
cnt--;
r--;
}
while (l < q.l) {
m[a[l]]--;
if (m[a[l]] == 0)
cnt--;
l++;
}
while (l > q.l) {
l--;
if (m[a[l]] == 0)
cnt++;
m[a[l]]++;
}
q.ans = cnt;
}
sort(q.begin(), q.end(), [](query &a, query &b) { return a.i < b.i; });
for (auto &q : q) {
cout << q.ans << '\n';
}
} | replace | 35 | 37 | 35 | 38 | TLE | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int k = 1000;
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
if (a.first.first / k < b.first.first / k)
return true;
if (a.first.first / k == b.first.first / k && a.first.second < b.first.second)
return true;
return false;
}
int cnt[300001];
int a[300001];
int ans[300001];
int answ = 0;
void add(int i) {
if (cnt[a[i]] == 0) {
++answ;
}
cnt[a[i]]++;
}
void del(int i) {
if (cnt[a[i]] == 1) {
--answ;
}
cnt[a[i]]--;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vector<int> f;
for (int i = 0; i < n; ++i) {
cin >> a[i];
f.push_back(a[i]);
}
sort(f.begin(), f.end());
f.resize(unique(f.begin(), f.end()) - f.begin());
unordered_map<int, int> d;
for (int i = 0; i < f.size(); ++i) {
d[f[i]] = i;
}
for (int i = 0; i < n; ++i) {
a[i] = d[a[i]];
}
vector<pair<pair<int, int>, int>> qq;
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l;
--r;
qq.push_back(make_pair(make_pair(l, r), i));
}
sort(qq.begin(), qq.end(), cmp);
int L = 0, R = -1;
for (int i = 0; i < q; ++i) {
int l = qq[i].first.first;
int r = qq[i].first.second;
while (L > l)
add(--L);
while (R < r)
add(++R);
while (L < l)
del(L++);
while (R > r)
del(R--);
// cout << R << " RR " << answ << endl;
// cout << L << " LL " << answ << endl;
ans[qq[i].second] = answ;
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
} | #include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int k = 1000;
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
if (a.first.first / k < b.first.first / k)
return true;
if (a.first.first / k == b.first.first / k && a.first.second < b.first.second)
return true;
return false;
}
int cnt[500001];
int a[500001];
int ans[500001];
int answ = 0;
void add(int i) {
if (cnt[a[i]] == 0) {
++answ;
}
cnt[a[i]]++;
}
void del(int i) {
if (cnt[a[i]] == 1) {
--answ;
}
cnt[a[i]]--;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vector<int> f;
for (int i = 0; i < n; ++i) {
cin >> a[i];
f.push_back(a[i]);
}
sort(f.begin(), f.end());
f.resize(unique(f.begin(), f.end()) - f.begin());
unordered_map<int, int> d;
for (int i = 0; i < f.size(); ++i) {
d[f[i]] = i;
}
for (int i = 0; i < n; ++i) {
a[i] = d[a[i]];
}
vector<pair<pair<int, int>, int>> qq;
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l;
--r;
qq.push_back(make_pair(make_pair(l, r), i));
}
sort(qq.begin(), qq.end(), cmp);
int L = 0, R = -1;
for (int i = 0; i < q; ++i) {
int l = qq[i].first.first;
int r = qq[i].first.second;
while (L > l)
add(--L);
while (R < r)
add(++R);
while (L < l)
del(L++);
while (R > r)
del(R--);
// cout << R << " RR " << answ << endl;
// cout << L << " LL " << answ << endl;
ans[qq[i].second] = answ;
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
} | replace | 17 | 20 | 17 | 20 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#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)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
for (auto &e : u)
fill_v<T>(e, v...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n);
rep(i, n) cin >> a[i], a[i]--;
vector<int> ql(q), qr(q), qi(q);
rep(i, q) cin >> ql[i] >> qr[i], ql[i]--;
iota(qi.begin(), qi.end(), 0);
int width = sqrt(q / 2 + 1);
sort(qi.begin(), qi.end(), [&](int i, int j) {
if (ql[i] / width != ql[j] / width)
return ql[i] < ql[j];
return qr[i] < qr[j];
});
vector<int> cnt(n), ans(q);
int sum = 0;
int l = 0, r = 0;
auto add = [&](int i) {
if (cnt[a[i]]++ == 0)
sum++;
};
auto del = [&](int i) {
if (--cnt[a[i]] == 0)
--sum;
};
for (int i : qi) {
while (ql[i] < l)
add(--l);
while (r < qr[i])
add(r++);
while (l < ql[i])
del(l++);
while (qr[i] < r)
del(--r);
ans[i] = sum;
}
rep(i, q) cout << ans[i] << endl;
}
| #include <bits/stdc++.h>
#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)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
for (auto &e : u)
fill_v<T>(e, v...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n);
rep(i, n) cin >> a[i], a[i]--;
vector<int> ql(q), qr(q), qi(q);
rep(i, q) cin >> ql[i] >> qr[i], ql[i]--;
iota(qi.begin(), qi.end(), 0);
int width = sqrt(q);
sort(qi.begin(), qi.end(), [&](int i, int j) {
if (ql[i] / width != ql[j] / width)
return ql[i] < ql[j];
return qr[i] < qr[j];
});
vector<int> cnt(n), ans(q);
int sum = 0;
int l = 0, r = 0;
auto add = [&](int i) {
if (cnt[a[i]]++ == 0)
sum++;
};
auto del = [&](int i) {
if (--cnt[a[i]] == 0)
--sum;
};
for (int i : qi) {
while (ql[i] < l)
add(--l);
while (r < qr[i])
add(r++);
while (l < ql[i])
del(l++);
while (qr[i] < r)
del(--r);
ans[i] = sum;
}
rep(i, q) cout << ans[i] << endl;
}
| replace | 46 | 47 | 46 | 47 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
// ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// clock_t start=clock();clock_t
// end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef pair<pii, int> ppii;
typedef pair<int, pii> pipi;
typedef pair<ll, ll> pll;
typedef pair<pll, ll> ppll;
typedef pair<ll, pll> plpl;
typedef pair<pii, pii> pippi;
typedef tuple<ll, ll, ll> tl;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> mat;
ll mod = 1000000007;
ll mod2 = 998244353;
ll mod3 = 1000003;
ll mod4 = 998244853;
ll mod5 = 1000000009;
ll inf = 1LL << 61;
int iinf = 1 << 30;
double pi = 3.14159265358979323846;
double pi2 = pi / 2.0;
double eps = 1e-8;
#define rep(i, m, n) for (ll i = m; i < n; i++)
#define rrep(i, n, m) for (ll i = n; i >= m; i--)
#define srep(itr, st) for (auto itr = st.begin(); itr != st.end(); itr++)
#define mrep(itr, mp) for (auto &itr : mp)
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
// #define endl "\n"
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};
int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
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);
}
};
#define umh unordered_map<int, int, custom_hash>
ll gcd(ll a, ll b) {
if (a < 0)
a = -a;
if (b < 0)
b = -b;
if (a < b)
swap(a, b);
if (b == 0)
return a;
if (a % b == 0)
return b;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll c = gcd(a, b);
return a * b / c;
}
ll Pow(ll n, ll k) {
if (k < 0)
return 0;
ll ret = 1;
ll now = n;
while (k > 0) {
if (k & 1)
ret *= now;
now *= now;
k /= 2;
}
return ret;
}
ll beki(ll n, ll k, ll md) {
ll ret = 1;
ll now = n;
now %= md;
while (k > 0) {
if (k % 2 == 1) {
ret *= now;
ret %= md;
}
now *= now;
now %= md;
k /= 2;
}
return ret;
}
ll gyaku(ll n, ll md) { return beki(n, md - 2, md); }
ll popcount(ll n) {
ll ret = 0;
ll u = n;
while (u > 0) {
ret += u % 2;
u /= 2;
}
return ret;
}
struct BIT {
private:
ll u;
vector<ll> bit;
public:
BIT(ll n) {
u = 1;
while (u < n)
u *= 2;
bit.resize(u + 10, 0);
}
void add(ll n, ll x) {
ll i = n;
bit[i] += x;
// bit[i]%=mod;
while (i < u) {
i += i & (-i);
bit[i] += x;
// bit[i]%=mod;
}
}
ll sum(ll n) {
ll i = n;
ll ret = 0;
while (i > 0) {
ret += bit[i];
i -= i & (-i);
// ret%=mod;
}
return ret;
}
ll lb(ll w) {
if (w <= 0)
return 0;
ll x = 0;
for (ll k = u; k > 0; k /= 2) {
if (x + k <= u && bit[x + k] < w) {
w -= bit[x + k];
x += k;
}
}
return x + 1;
}
};
// vector<BIT> b(26,n+1);
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n, q;
cin >> n >> q;
ll c[n + 1];
vector<ll> v[n + 1];
rep(i, 1, n + 1) {
cin >> c[i];
v[c[i]].push_back(i);
}
ll nex[n];
BIT b(n + 1);
rep(i, 1, n + 1) {
rep(j, 0, v[i].size()) {
if (j == v[i].size() - 1)
nex[v[i][j]] = n + 1;
else
nex[v[i][j]] = v[i][j + 1];
}
if (v[i].size() > 0)
b.add(v[i][0], 1);
}
vector<pll> w[n + 1];
rep(i, 0, q) {
ll a, b;
cin >> a >> b;
w[a].push_back({b, i});
}
ll ans[q];
rep(i, 1, n + 1) {
rep(j, 0, w[i].size()) { ans[w[i][j].second] = b.sum(w[i][j].first); }
b.add(i, -1);
b.add(nex[i], 1);
}
rep(i, 0, q) cout << ans[i] << endl;
}
| #include <bits/stdc++.h>
// ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// clock_t start=clock();clock_t
// end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef pair<pii, int> ppii;
typedef pair<int, pii> pipi;
typedef pair<ll, ll> pll;
typedef pair<pll, ll> ppll;
typedef pair<ll, pll> plpl;
typedef pair<pii, pii> pippi;
typedef tuple<ll, ll, ll> tl;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> mat;
ll mod = 1000000007;
ll mod2 = 998244353;
ll mod3 = 1000003;
ll mod4 = 998244853;
ll mod5 = 1000000009;
ll inf = 1LL << 61;
int iinf = 1 << 30;
double pi = 3.14159265358979323846;
double pi2 = pi / 2.0;
double eps = 1e-8;
#define rep(i, m, n) for (ll i = m; i < n; i++)
#define rrep(i, n, m) for (ll i = n; i >= m; i--)
#define srep(itr, st) for (auto itr = st.begin(); itr != st.end(); itr++)
#define mrep(itr, mp) for (auto &itr : mp)
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
// #define endl "\n"
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};
int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
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);
}
};
#define umh unordered_map<int, int, custom_hash>
ll gcd(ll a, ll b) {
if (a < 0)
a = -a;
if (b < 0)
b = -b;
if (a < b)
swap(a, b);
if (b == 0)
return a;
if (a % b == 0)
return b;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll c = gcd(a, b);
return a * b / c;
}
ll Pow(ll n, ll k) {
if (k < 0)
return 0;
ll ret = 1;
ll now = n;
while (k > 0) {
if (k & 1)
ret *= now;
now *= now;
k /= 2;
}
return ret;
}
ll beki(ll n, ll k, ll md) {
ll ret = 1;
ll now = n;
now %= md;
while (k > 0) {
if (k % 2 == 1) {
ret *= now;
ret %= md;
}
now *= now;
now %= md;
k /= 2;
}
return ret;
}
ll gyaku(ll n, ll md) { return beki(n, md - 2, md); }
ll popcount(ll n) {
ll ret = 0;
ll u = n;
while (u > 0) {
ret += u % 2;
u /= 2;
}
return ret;
}
struct BIT {
private:
ll u;
vector<ll> bit;
public:
BIT(ll n) {
u = 1;
while (u < n)
u *= 2;
bit.resize(u + 10, 0);
}
void add(ll n, ll x) {
ll i = n;
bit[i] += x;
// bit[i]%=mod;
while (i < u) {
i += i & (-i);
bit[i] += x;
// bit[i]%=mod;
}
}
ll sum(ll n) {
ll i = n;
ll ret = 0;
while (i > 0) {
ret += bit[i];
i -= i & (-i);
// ret%=mod;
}
return ret;
}
ll lb(ll w) {
if (w <= 0)
return 0;
ll x = 0;
for (ll k = u; k > 0; k /= 2) {
if (x + k <= u && bit[x + k] < w) {
w -= bit[x + k];
x += k;
}
}
return x + 1;
}
};
// vector<BIT> b(26,n+1);
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n, q;
cin >> n >> q;
ll c[n + 1];
vector<ll> v[n + 1];
rep(i, 1, n + 1) {
cin >> c[i];
v[c[i]].push_back(i);
}
ll nex[n + 1];
BIT b(n + 1);
rep(i, 1, n + 1) {
rep(j, 0, v[i].size()) {
if (j == v[i].size() - 1)
nex[v[i][j]] = n + 1;
else
nex[v[i][j]] = v[i][j + 1];
}
if (v[i].size() > 0)
b.add(v[i][0], 1);
}
vector<pll> w[n + 1];
rep(i, 0, q) {
ll a, b;
cin >> a >> b;
w[a].push_back({b, i});
}
ll ans[q];
rep(i, 1, n + 1) {
rep(j, 0, w[i].size()) { ans[w[i][j].second] = b.sum(w[i][j].first); }
b.add(i, -1);
b.add(nex[i], 1);
}
rep(i, 0, q) cout << ans[i] << endl;
}
| replace | 168 | 169 | 168 | 169 | -11 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template <class Key>
using rb_tree_set =
__gnu_pbds::tree<Key, __gnu_pbds::null_type, std::less<Key>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i)
#ifdef ENABLE_DEBUG
template <typename T> void debug(T value) { cerr << value; }
template <typename T, typename... Ts> void debug(T value, Ts... args) {
cerr << value << ", ";
debug(args...);
}
#define DEBUG(...) \
do { \
cerr << " \033[33m (L" << __LINE__ << ") "; \
cerr << #__VA_ARGS__ << ":\033[0m "; \
debug(__VA_ARGS__); \
cerr << endl; \
} while (0)
#else
#define debug(...)
#define DEBUG(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> colors(N);
REP(i, N) cin >> colors[i];
vector<int> lefts(Q), rights(Q), ids(Q);
REP(i, Q) {
cin >> lefts[i] >> rights[i];
--lefts[i];
--rights[i];
ids[i] = i;
}
sort(ids.begin(), ids.end(),
[&](int i, int j) { return rights[i] < rights[j]; });
rb_tree_set<pair<int, int>> st;
vector<int> prev(500'005, -1);
vector<int> res(Q);
int j = 0;
for (int i = 0; i < N; ++i) {
int pc = prev[colors[i]];
if (pc >= 0)
st.insert({pc, i});
for (; j < Q && rights[ids[j]] == i; ++j) {
auto it = st.lower_bound(make_pair(lefts[ids[j]], -1));
int dup_count = distance(it, st.end());
res[ids[j]] = rights[ids[j]] - lefts[ids[j]] + 1 - dup_count;
DEBUG(j, ids[j], lefts[ids[j]], rights[ids[j]], dup_count, res[ids[j]]);
}
prev[colors[i]] = i;
}
for (int i = 0; i < Q; ++i)
cout << res[i] << '\n';
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template <class Key>
using rb_tree_set =
__gnu_pbds::tree<Key, __gnu_pbds::null_type, std::less<Key>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i)
#ifdef ENABLE_DEBUG
template <typename T> void debug(T value) { cerr << value; }
template <typename T, typename... Ts> void debug(T value, Ts... args) {
cerr << value << ", ";
debug(args...);
}
#define DEBUG(...) \
do { \
cerr << " \033[33m (L" << __LINE__ << ") "; \
cerr << #__VA_ARGS__ << ":\033[0m "; \
debug(__VA_ARGS__); \
cerr << endl; \
} while (0)
#else
#define debug(...)
#define DEBUG(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> colors(N);
REP(i, N) cin >> colors[i];
vector<int> lefts(Q), rights(Q), ids(Q);
REP(i, Q) {
cin >> lefts[i] >> rights[i];
--lefts[i];
--rights[i];
ids[i] = i;
}
sort(ids.begin(), ids.end(),
[&](int i, int j) { return rights[i] < rights[j]; });
rb_tree_set<pair<int, int>> st;
vector<int> prev(500'005, -1);
vector<int> res(Q);
int j = 0;
for (int i = 0; i < N; ++i) {
int pc = prev[colors[i]];
if (pc >= 0)
st.insert({pc, i});
for (; j < Q && rights[ids[j]] == i; ++j) {
auto it = st.lower_bound(make_pair(lefts[ids[j]], -1));
int dup_count = 0;
if (it != st.end()) {
dup_count = st.size() - st.order_of_key(*it);
}
res[ids[j]] = rights[ids[j]] - lefts[ids[j]] + 1 - dup_count;
DEBUG(j, ids[j], lefts[ids[j]], rights[ids[j]], dup_count, res[ids[j]]);
}
prev[colors[i]] = i;
}
for (int i = 0; i < Q; ++i)
cout << res[i] << '\n';
}
| replace | 62 | 63 | 62 | 66 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#pragma region Macros
#define ios \
ios::sync_with_stdio(false); \
cin.tie(nullptr);
#define VEC(type, name, size) \
V<type> name(size); \
IN(name)
#define VVEC(type, name, h, w) \
VV<type> name(h, V<type>(w)); \
IN(name)
#define INT(...) \
int __VA_ARGS__; \
IN(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
IN(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
IN(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
IN(__VA_ARGS__)
#define DOUBLE(...) \
DOUBLE __VA_ARGS__; \
IN(__VA_ARGS__)
#define LD(...) \
LD __VA_ARGS__; \
IN(__VA_ARGS__)
template <class T> void scan(T &a) { cin >> a; }
template <class T> void scan(vector<T> &a) {
for (auto &i : a)
scan(i);
}
template <class T, class L> void scan(pair<T, L> &p) {
scan(p.first);
scan(p.second);
}
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
scan(head);
IN(tail...);
}
template <class T> inline void print(T x) { cout << x << '\n'; }
#define ll long long
#define ld long double
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
if (b) {
cout << -1 << '\n';
exit(0);
}
}
template <class T> inline void fin(bool b = true, T e = 0) {
if (b) {
cout << e << '\n';
exit(0);
}
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
template <typename T> T pow(T a, long long n, T e = 1) {
T ret = e;
while (n) {
if (n & 1)
ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
const ll INF = 1e18;
#pragma endregion
// Binary Indexed Tree (Fenwick Tree)
template <typename T> struct BIT {
int n, p;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {
p = 1;
while (p < n)
p *= 2;
}
// 更新
void add(int i, T x = 1) {
for (i++; i <= n; i += i & -i)
d[i] += x;
}
//[0,i)の和
T sum(int i) {
T res = 0;
for (; i; i -= i & -i)
res += d[i];
return res;
}
//[l,r)の和
T sum(int l, int r) { return sum(r) - sum(l); }
// v0+v1+…vx>=mを満たすmin(x)を返す
int lower_bound(T w) {
if (w <= 0)
return 0;
T x = 0;
for (int i = p; i; i /= 2) {
if (i + x <= n && d[i + x] < w) {
w -= d[i + x];
x += i;
}
}
return x;
}
};
int main() {
ios;
LL(n, q);
VEC(ll, c, n);
REP(i, n) c[i]--;
V<> last(n, -1);
BIT<ll> BT(n);
V<pair<pair<int, int>, int>> vec(q);
REP(i, q) {
int l, r;
cin >> l >> r;
l--;
r--;
vec[i] = {{r, l}, i};
}
sort(all(vec));
V<> ans(q);
ll now = -1;
REP(i, q) {
ll r = vec[i].first.first;
for (int i = now + 1; i <= r; i++) {
if (last[c[i]] != -1)
BT.add(last[c[i]], -1);
last[c[i]] = i;
BT.add(last[c[i]], 1);
}
ans[vec[i].second] = BT.sum(vec[i].first.second, vec[i].first.first + 1);
}
REP(i, q) print(ans[i]);
} | #include <bits/stdc++.h>
using namespace std;
#pragma region Macros
#define ios \
ios::sync_with_stdio(false); \
cin.tie(nullptr);
#define VEC(type, name, size) \
V<type> name(size); \
IN(name)
#define VVEC(type, name, h, w) \
VV<type> name(h, V<type>(w)); \
IN(name)
#define INT(...) \
int __VA_ARGS__; \
IN(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
IN(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
IN(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
IN(__VA_ARGS__)
#define DOUBLE(...) \
DOUBLE __VA_ARGS__; \
IN(__VA_ARGS__)
#define LD(...) \
LD __VA_ARGS__; \
IN(__VA_ARGS__)
template <class T> void scan(T &a) { cin >> a; }
template <class T> void scan(vector<T> &a) {
for (auto &i : a)
scan(i);
}
template <class T, class L> void scan(pair<T, L> &p) {
scan(p.first);
scan(p.second);
}
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
scan(head);
IN(tail...);
}
template <class T> inline void print(T x) { cout << x << '\n'; }
#define ll long long
#define ld long double
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
if (b) {
cout << -1 << '\n';
exit(0);
}
}
template <class T> inline void fin(bool b = true, T e = 0) {
if (b) {
cout << e << '\n';
exit(0);
}
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
template <typename T> T pow(T a, long long n, T e = 1) {
T ret = e;
while (n) {
if (n & 1)
ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
const ll INF = 1e18;
#pragma endregion
// Binary Indexed Tree (Fenwick Tree)
template <typename T> struct BIT {
int n, p;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {
p = 1;
while (p < n)
p *= 2;
}
// 更新
void add(int i, T x = 1) {
for (i++; i <= n; i += i & -i)
d[i] += x;
}
//[0,i)の和
T sum(int i) {
T res = 0;
for (; i; i -= i & -i)
res += d[i];
return res;
}
//[l,r)の和
T sum(int l, int r) { return sum(r) - sum(l); }
// v0+v1+…vx>=mを満たすmin(x)を返す
int lower_bound(T w) {
if (w <= 0)
return 0;
T x = 0;
for (int i = p; i; i /= 2) {
if (i + x <= n && d[i + x] < w) {
w -= d[i + x];
x += i;
}
}
return x;
}
};
int main() {
ios;
LL(n, q);
VEC(ll, c, n);
REP(i, n) c[i]--;
V<> last(n, -1);
BIT<ll> BT(n);
V<pair<pair<int, int>, int>> vec(q);
REP(i, q) {
int l, r;
cin >> l >> r;
l--;
r--;
vec[i] = {{r, l}, i};
}
sort(all(vec));
V<> ans(q);
ll now = -1;
REP(i, q) {
ll r = vec[i].first.first;
for (int i = now + 1; i <= r; i++) {
if (last[c[i]] != -1)
BT.add(last[c[i]], -1);
last[c[i]] = i;
BT.add(last[c[i]], 1);
now = r;
}
ans[vec[i].second] = BT.sum(vec[i].first.second, vec[i].first.first + 1);
}
REP(i, q) print(ans[i]);
}
| insert | 165 | 165 | 165 | 166 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using pll = pair<ll, ll>;
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
template <typename T> using vv = vector<vector<T>>;
void solve() {
int n, q;
cin >> n >> q;
vi a(n);
for (auto &i : a)
cin >> i, i--;
;
vector<pair<pll, int>> qr(q);
int cc = 0;
for (auto &e : qr)
cin >> e.first.first >> e.first.second, e.first.first--, e.first.second--,
e.second = cc++;
sort(all(qr), [&](pair<pll, int> aa, pair<pll, int> bb) {
auto a = aa.first, b = bb.first;
if (a.first / 1000 != b.first / 1000)
return a.first / 1000 < b.first;
return a.second < b.second;
});
vi cnt(n);
ll tmp = 0;
ll l = 0, r = -1;
vi res(q);
for (auto e : qr) {
ll nl, nr;
tie(nl, nr) = e.first;
while (r < nr) {
r++;
if (cnt[a[r]]++ == 0)
tmp++;
}
while (l > nl) {
l--;
if (cnt[a[l]]++ == 0)
tmp++;
}
while (r > nr) {
if (--cnt[a[r--]] == 0)
tmp--;
}
while (l < nl) {
if (--cnt[a[l++]] == 0)
tmp--;
}
res[e.second] = tmp;
}
for (auto i : res)
cout << i << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using pll = pair<ll, ll>;
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
template <typename T> using vv = vector<vector<T>>;
void solve() {
int n, q;
cin >> n >> q;
vi a(n);
for (auto &i : a)
cin >> i, i--;
;
vector<pair<pll, int>> qr(q);
int cc = 0;
for (auto &e : qr)
cin >> e.first.first >> e.first.second, e.first.first--, e.first.second--,
e.second = cc++;
sort(all(qr), [&](pair<pll, int> aa, pair<pll, int> bb) {
auto a = aa.first, b = bb.first;
if (a.first / 1000 != b.first / 1000)
return a.first / 1000 < b.first / 1000;
return a.second < b.second;
});
vi cnt(n);
ll tmp = 0;
ll l = 0, r = -1;
vi res(q);
for (auto e : qr) {
ll nl, nr;
tie(nl, nr) = e.first;
while (r < nr) {
r++;
if (cnt[a[r]]++ == 0)
tmp++;
}
while (l > nl) {
l--;
if (cnt[a[l]]++ == 0)
tmp++;
}
while (r > nr) {
if (--cnt[a[r--]] == 0)
tmp--;
}
while (l < nl) {
if (--cnt[a[l++]] == 0)
tmp--;
}
res[e.second] = tmp;
}
for (auto i : res)
cout << i << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
| replace | 31 | 32 | 31 | 32 | 0 | |
p02599 | C++ | Runtime Error | // Author: HarshKumar
#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll N = 1e6 + 1;
ll root[N], L[16 * N], R[16 * N], sum[16 * N];
ll rt = 1, sz = 1;
ll lpos[N];
ll copy(ll v, ll &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(ll pos, ll x, ll v = root[rt - 1], ll l = 0, ll r = N) {
sum[v] += x;
if (r - l == 1)
return;
ll 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);
}
ll get(ll a, ll b, ll v, ll l = 0, ll r = N) {
if (a <= l && r <= b)
return sum[v];
if (r <= a || b <= l)
return 0;
ll 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(false), cin.tie(nullptr);
rt = sz = 1;
ll n, q;
cin >> n >> q;
for (ll i = 1; i <= n; i++) {
ll t;
cin >> t;
make_root();
add(lpos[t], -1);
lpos[t] = i;
add(lpos[t], 1);
}
while (q--) {
ll l, r;
cin >> l >> r;
cout << get(l, r + 1, root[r]) << '\n';
}
}
| // Author: HarshKumar
#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll N = 15e5 + 1;
ll root[N], L[16 * N], R[16 * N], sum[16 * N];
ll rt = 1, sz = 1;
ll lpos[N];
ll copy(ll v, ll &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(ll pos, ll x, ll v = root[rt - 1], ll l = 0, ll r = N) {
sum[v] += x;
if (r - l == 1)
return;
ll 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);
}
ll get(ll a, ll b, ll v, ll l = 0, ll r = N) {
if (a <= l && r <= b)
return sum[v];
if (r <= a || b <= l)
return 0;
ll 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(false), cin.tie(nullptr);
rt = sz = 1;
ll n, q;
cin >> n >> q;
for (ll i = 1; i <= n; i++) {
ll t;
cin >> t;
make_root();
add(lpos[t], -1);
lpos[t] = i;
add(lpos[t], 1);
}
while (q--) {
ll l, r;
cin >> l >> r;
cout << get(l, r + 1, root[r]) << '\n';
}
}
| replace | 4 | 5 | 4 | 5 | -11 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 200010;
template <typename T> void inline read(T &x) {
T f = 1;
char ch = getchar();
for (; '0' > ch || ch > '9'; ch = getchar())
if (ch == '-')
f = -1;
for (x = 0; '0' <= ch && ch <= '9'; ch = getchar())
x = x * 10 + ch - '0';
x *= f;
}
template <typename T> void write(T x) {
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
template <typename T> void print(T x) {
if (x < 0)
putchar('-'), x = -x;
write(x);
putchar('\n');
}
int n, m;
int col[N];
int len, bl[N];
int ans[N];
struct node {
int l, r, id;
friend bool operator<(node a, node b) {
if (bl[a.l] != bl[b.l])
return bl[a.l] < bl[b.l];
if (bl[a.l] % 2)
return a.r < b.r;
return a.r > b.r;
}
} q[N];
int nowl = 1, nowr = 0, nowans;
int cnt[1000010];
void add(int c) {
if (!cnt[c])
nowans++;
cnt[c]++;
}
void del(int c) {
cnt[c]--;
if (!cnt[c])
nowans--;
}
int main() {
read(n);
read(m);
for (int i = 1; i <= n; i++)
read(col[i]);
len = pow(n, 1.0 / 2.0);
for (int i = 1; i <= n; i++)
bl[i] = (i - 1) / len + 1;
for (int i = 1; i <= m; i++) {
read(q[i].l);
read(q[i].r);
q[i].id = i;
}
sort(q + 1, q + 1 + m);
for (int i = 1; i <= m; i++) {
while (nowl > q[i].l)
add(col[--nowl]);
while (nowr < q[i].r)
add(col[++nowr]);
while (nowl < q[i].l)
del(col[nowl++]);
while (nowr > q[i].r)
del(col[nowr--]);
ans[q[i].id] = nowans;
}
for (int i = 1; i <= m; i++)
print(ans[i]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 500010;
template <typename T> void inline read(T &x) {
T f = 1;
char ch = getchar();
for (; '0' > ch || ch > '9'; ch = getchar())
if (ch == '-')
f = -1;
for (x = 0; '0' <= ch && ch <= '9'; ch = getchar())
x = x * 10 + ch - '0';
x *= f;
}
template <typename T> void write(T x) {
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
template <typename T> void print(T x) {
if (x < 0)
putchar('-'), x = -x;
write(x);
putchar('\n');
}
int n, m;
int col[N];
int len, bl[N];
int ans[N];
struct node {
int l, r, id;
friend bool operator<(node a, node b) {
if (bl[a.l] != bl[b.l])
return bl[a.l] < bl[b.l];
if (bl[a.l] % 2)
return a.r < b.r;
return a.r > b.r;
}
} q[N];
int nowl = 1, nowr = 0, nowans;
int cnt[1000010];
void add(int c) {
if (!cnt[c])
nowans++;
cnt[c]++;
}
void del(int c) {
cnt[c]--;
if (!cnt[c])
nowans--;
}
int main() {
read(n);
read(m);
for (int i = 1; i <= n; i++)
read(col[i]);
len = pow(n, 1.0 / 2.0);
for (int i = 1; i <= n; i++)
bl[i] = (i - 1) / len + 1;
for (int i = 1; i <= m; i++) {
read(q[i].l);
read(q[i].r);
q[i].id = i;
}
sort(q + 1, q + 1 + m);
for (int i = 1; i <= m; i++) {
while (nowl > q[i].l)
add(col[--nowl]);
while (nowr < q[i].r)
add(col[++nowr]);
while (nowl < q[i].l)
del(col[nowl++]);
while (nowr > q[i].r)
del(col[nowr--]);
ans[q[i].id] = nowans;
}
for (int i = 1; i <= m; i++)
print(ans[i]);
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// using namespace __gnu_pbds;
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define fill(a, val) memset(a, val, sizeof(a))
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define endl "\n"
#define int long long
#define printclock \
cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const ll MOD2 = 998244353;
const ll N = 5000 * 100 + 10;
const ll N2 = 70;
const ld PI = 3.141592653589793;
// template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
ll gcd(ll a, ll b) {
if (!b)
return a;
return gcd(b, a % b);
}
ll power(ll x, ll y, ll p = LLONG_MAX) {
ll res = 1;
x %= p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int freq[N] = {0};
int block;
struct Query {
int L, R, index, result;
};
bool compare(Query x, Query y) {
if (x.L / block != y.L / block)
return x.L / block < y.L / block;
return x.R < y.R;
}
bool compare1(Query x, Query y) { return x.index < y.index; }
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;
// 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;
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++;
}
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;
}
}
signed main() {
fastio();
// cout<<fixed<<setprecision(20);
// CHECK for LONG LONG and LONG DOUBLE
//*comment for all except cc/cf
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif //*/
int n, q;
cin >> n >> q;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int m = q;
int i = 0;
Query qq[q];
while (q--) {
int l, r;
cin >> l >> r;
l--;
r--;
qq[i].L = l;
qq[i].R = r;
qq[i].index = i;
i++;
}
queryResults(a, n, qq, m);
printResults(qq, m);
printclock;
return 0;
}
| #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// using namespace __gnu_pbds;
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define fill(a, val) memset(a, val, sizeof(a))
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define endl "\n"
#define printclock \
cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const ll MOD2 = 998244353;
const ll N = 5000 * 100 + 10;
const ll N2 = 70;
const ld PI = 3.141592653589793;
// template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
ll gcd(ll a, ll b) {
if (!b)
return a;
return gcd(b, a % b);
}
ll power(ll x, ll y, ll p = LLONG_MAX) {
ll res = 1;
x %= p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int freq[N] = {0};
int block;
struct Query {
int L, R, index, result;
};
bool compare(Query x, Query y) {
if (x.L / block != y.L / block)
return x.L / block < y.L / block;
return x.R < y.R;
}
bool compare1(Query x, Query y) { return x.index < y.index; }
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;
// 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;
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++;
}
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;
}
}
signed main() {
fastio();
// cout<<fixed<<setprecision(20);
// CHECK for LONG LONG and LONG DOUBLE
//*comment for all except cc/cf
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif //*/
int n, q;
cin >> n >> q;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int m = q;
int i = 0;
Query qq[q];
while (q--) {
int l, r;
cin >> l >> r;
l--;
r--;
qq[i].L = l;
qq[i].R = r;
qq[i].index = i;
i++;
}
queryResults(a, n, qq, m);
printResults(qq, m);
printclock;
return 0;
}
| delete | 24 | 25 | 24 | 24 | TLE | |
p02599 | C++ | Time Limit Exceeded | // Program to compute sum of ranges for different range
// queries
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll block, n, m;
ll a[500001];
struct Query {
ll L, R, i;
};
Query q[500001];
bool compare(Query x, Query y) {
if (x.L / block != y.L / block)
return x.L / block < y.L / block;
return x.R < y.R;
}
void queryResults() {
block = (ll)sqrt(n);
sort(q, q + m, compare);
ll currL = 0, currR = 0;
ll currSum = 0;
ll ans = 0;
ll aa[m];
ll f[500001] = {0};
for (ll i = 0; i < m; i++) {
ll L = q[i].L, R = q[i].R;
while (currL < L) {
f[a[currL]]--;
if (f[a[currL]] == 0)
ans--;
currL++;
}
while (currL > L) {
if (f[a[currL - 1]] == 0)
ans++;
f[a[currL - 1]]++;
currL--;
}
while (currR <= R) {
if (f[a[currR]] == 0)
ans++;
f[a[currR]]++;
currR++;
}
while (currR > R + 1) {
f[a[currR - 1]]--;
if (f[a[currR - 1]] == 0)
ans--;
currR--;
}
aa[q[i].i] = ans;
}
for (ll i = 0; i < m; i++)
cout << aa[i] << endl;
}
// Driver program
int main() {
ll i, j, k;
cin >> n >> m;
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < m; i++) {
cin >> j >> k;
q[i].L = j - 1;
q[i].R = k - 1;
q[i].i = i;
}
queryResults();
} | // Program to compute sum of ranges for different range
// queries
#include <bits/stdc++.h>
using namespace std;
#define ll int
ll block, n, m;
ll a[500001];
struct Query {
ll L, R, i;
};
Query q[500001];
bool compare(Query x, Query y) {
if (x.L / block != y.L / block)
return x.L / block < y.L / block;
return x.R < y.R;
}
void queryResults() {
block = (ll)sqrt(n);
sort(q, q + m, compare);
ll currL = 0, currR = 0;
ll currSum = 0;
ll ans = 0;
ll aa[m];
ll f[500001] = {0};
for (ll i = 0; i < m; i++) {
ll L = q[i].L, R = q[i].R;
while (currL < L) {
f[a[currL]]--;
if (f[a[currL]] == 0)
ans--;
currL++;
}
while (currL > L) {
if (f[a[currL - 1]] == 0)
ans++;
f[a[currL - 1]]++;
currL--;
}
while (currR <= R) {
if (f[a[currR]] == 0)
ans++;
f[a[currR]]++;
currR++;
}
while (currR > R + 1) {
f[a[currR - 1]]--;
if (f[a[currR - 1]] == 0)
ans--;
currR--;
}
aa[q[i].i] = ans;
}
for (ll i = 0; i < m; i++)
cout << aa[i] << endl;
}
// Driver program
int main() {
ll i, j, k;
cin >> n >> m;
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < m; i++) {
cin >> j >> k;
q[i].L = j - 1;
q[i].R = k - 1;
q[i].i = i;
}
queryResults();
} | replace | 4 | 5 | 4 | 5 | TLE | |
p02599 | C++ | Runtime Error | /* #region header */
#ifdef LOCAL
#include "/Users/takakurashokichi/Desktop/atcoder/cxx-prettyprint-master/prettyprint.hpp"
#define debug(x) cout << x << endl
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// types
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
typedef pair<ll, ll> Pl;
typedef pair<int, int> Pi;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
template <typename T> using mat = vector<vector<T>>;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef vector<vector<char>> vvc;
template <int mod> struct modint {
int x;
modint() : x(0) {}
modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
modint &operator+=(const modint &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
modint &operator-=(const modint &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
modint &operator*=(const modint &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
modint &operator/=(const modint &p) {
*this *= p.inverse();
return *this;
}
modint operator-() const { return modint(-x); }
modint operator+(const modint &p) const { return modint(*this) += p; }
modint operator-(const modint &p) const { return modint(*this) -= p; }
modint operator*(const modint &p) const { return modint(*this) *= p; }
modint operator/(const modint &p) const { return modint(*this) /= p; }
bool operator==(const modint &p) const { return x == p.x; }
bool operator!=(const modint &p) const { return x != p.x; }
modint inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return modint(u);
}
modint pow(int64_t n) const {
modint ret(1), mul(x);
while (n > 0) {
if (n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }
friend istream &operator>>(istream &is, modint &a) {
int64_t t;
is >> t;
a = modint<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
// abreviations
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep_(i, a_, b_, a, b, ...) \
for (int i = (a), max_i = (b); i < max_i; i++)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define rrep_(i, a_, b_, a, b, ...) \
for (int i = (b - 1), min_i = (a); i >= min_i; i--)
#define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp make_pair
#define print(x) cout << x << endl
#define vprint(x) \
rep(i, x.size()) cout << x[i] << ' '; \
cout << endl
#define vsum(x) accumulate(all(x), 0LL)
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
// functions
// gcd(0, x) fails.
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
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 <typename T> T mypow(T x, ll n) {
T ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
}
return ret;
}
ll modpow(ll x, ll n, const ll mod) {
ll ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
x %= mod;
ret %= mod;
}
return ret;
}
template <typename T> uint64_t my_rand(void) {
static uint64_t x = 88172645463325252ULL;
x = x ^ (x << 13);
x = x ^ (x >> 7);
return x = x ^ (x << 17);
}
int popcnt(ull x) { return __builtin_popcountll(x); }
// graph template
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
bool operator<(const edge<T> &r) const { return cost < r.cost; }
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
struct Timer {
clock_t start_time;
void start() { start_time = clock(); }
int lap() {
// return x ms.
return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
}
};
/* #endregion*/
// constant
#define inf 1000000005
#define INF 4000000004000000000LL
#define mod 1000000007LL
#define endl '\n'
typedef modint<mod> mint;
const long double eps = 0.000001;
const long double PI = 3.141592653589793;
// library
struct Mo {
int width;
vector<int> left, right, order;
vector<bool> v;
Mo(int N, int Q) : width((int)sqrt(N)), order(Q), v(N) {
iota(begin(order), end(order), 0);
}
void add(int l, int r) { /* [l, r) */
left.emplace_back(l);
right.emplace_back(r);
}
template <typename ADD, typename DEL, typename REM>
void run(const ADD &add, const DEL &del, const REM &rem) {
assert(left.size() == order.size());
sort(begin(order), end(order), [&](int a, int b) {
int ablock = left[a] / width, bblock = left[b] / width;
if (ablock != bblock)
return ablock < bblock;
if (ablock & 1)
return right[a] < right[b];
return right[a] > right[b];
});
int nl = 0, nr = 0;
auto push = [&](int idx) {
v[idx].flip();
if (v[idx])
add(idx);
else
del(idx);
};
for (auto idx : order) {
while (nl > left[idx])
push(--nl);
while (nr < right[idx])
push(nr++);
while (nl < left[idx])
push(nl++);
while (nr > right[idx])
push(--nr);
rem(idx);
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << setprecision(20);
vi cnt(50001);
int res = 0;
int n, q;
scanf("%d", &n);
scanf("%d", &q);
vi c(n);
rep(i, n) scanf("%d", &c[i]);
Mo mo(n, q);
vi ans(q);
rep(i, q) {
int l, r;
scanf("%d", &l);
scanf("%d", &r);
mo.add(l - 1, r);
}
auto f = [&](int i) {
if (!cnt[c[i]])
++res;
++cnt[c[i]];
};
auto g = [&](int i) {
if (cnt[c[i]])
--cnt[c[i]];
if (!cnt[c[i]])
--res;
};
auto h = [&](int i) { ans[i] = res; };
mo.run(f, g, h);
rep(i, q) printf("%d\n", ans[i]);
}
| /* #region header */
#ifdef LOCAL
#include "/Users/takakurashokichi/Desktop/atcoder/cxx-prettyprint-master/prettyprint.hpp"
#define debug(x) cout << x << endl
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// types
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
typedef pair<ll, ll> Pl;
typedef pair<int, int> Pi;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
template <typename T> using mat = vector<vector<T>>;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef vector<vector<char>> vvc;
template <int mod> struct modint {
int x;
modint() : x(0) {}
modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
modint &operator+=(const modint &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
modint &operator-=(const modint &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
modint &operator*=(const modint &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
modint &operator/=(const modint &p) {
*this *= p.inverse();
return *this;
}
modint operator-() const { return modint(-x); }
modint operator+(const modint &p) const { return modint(*this) += p; }
modint operator-(const modint &p) const { return modint(*this) -= p; }
modint operator*(const modint &p) const { return modint(*this) *= p; }
modint operator/(const modint &p) const { return modint(*this) /= p; }
bool operator==(const modint &p) const { return x == p.x; }
bool operator!=(const modint &p) const { return x != p.x; }
modint inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return modint(u);
}
modint pow(int64_t n) const {
modint ret(1), mul(x);
while (n > 0) {
if (n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }
friend istream &operator>>(istream &is, modint &a) {
int64_t t;
is >> t;
a = modint<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
// abreviations
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep_(i, a_, b_, a, b, ...) \
for (int i = (a), max_i = (b); i < max_i; i++)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define rrep_(i, a_, b_, a, b, ...) \
for (int i = (b - 1), min_i = (a); i >= min_i; i--)
#define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp make_pair
#define print(x) cout << x << endl
#define vprint(x) \
rep(i, x.size()) cout << x[i] << ' '; \
cout << endl
#define vsum(x) accumulate(all(x), 0LL)
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
// functions
// gcd(0, x) fails.
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
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 <typename T> T mypow(T x, ll n) {
T ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
}
return ret;
}
ll modpow(ll x, ll n, const ll mod) {
ll ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
x %= mod;
ret %= mod;
}
return ret;
}
template <typename T> uint64_t my_rand(void) {
static uint64_t x = 88172645463325252ULL;
x = x ^ (x << 13);
x = x ^ (x >> 7);
return x = x ^ (x << 17);
}
int popcnt(ull x) { return __builtin_popcountll(x); }
// graph template
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
bool operator<(const edge<T> &r) const { return cost < r.cost; }
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
struct Timer {
clock_t start_time;
void start() { start_time = clock(); }
int lap() {
// return x ms.
return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
}
};
/* #endregion*/
// constant
#define inf 1000000005
#define INF 4000000004000000000LL
#define mod 1000000007LL
#define endl '\n'
typedef modint<mod> mint;
const long double eps = 0.000001;
const long double PI = 3.141592653589793;
// library
struct Mo {
int width;
vector<int> left, right, order;
vector<bool> v;
Mo(int N, int Q) : width((int)sqrt(N)), order(Q), v(N) {
iota(begin(order), end(order), 0);
}
void add(int l, int r) { /* [l, r) */
left.emplace_back(l);
right.emplace_back(r);
}
template <typename ADD, typename DEL, typename REM>
void run(const ADD &add, const DEL &del, const REM &rem) {
assert(left.size() == order.size());
sort(begin(order), end(order), [&](int a, int b) {
int ablock = left[a] / width, bblock = left[b] / width;
if (ablock != bblock)
return ablock < bblock;
if (ablock & 1)
return right[a] < right[b];
return right[a] > right[b];
});
int nl = 0, nr = 0;
auto push = [&](int idx) {
v[idx].flip();
if (v[idx])
add(idx);
else
del(idx);
};
for (auto idx : order) {
while (nl > left[idx])
push(--nl);
while (nr < right[idx])
push(nr++);
while (nl < left[idx])
push(nl++);
while (nr > right[idx])
push(--nr);
rem(idx);
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << setprecision(20);
vi cnt(500001);
int res = 0;
int n, q;
scanf("%d", &n);
scanf("%d", &q);
vi c(n);
rep(i, n) scanf("%d", &c[i]);
Mo mo(n, q);
vi ans(q);
rep(i, q) {
int l, r;
scanf("%d", &l);
scanf("%d", &r);
mo.add(l - 1, r);
}
auto f = [&](int i) {
if (!cnt[c[i]])
++res;
++cnt[c[i]];
};
auto g = [&](int i) {
if (cnt[c[i]])
--cnt[c[i]];
if (!cnt[c[i]])
--res;
};
auto h = [&](int i) { ans[i] = res; };
mo.run(f, g, h);
rep(i, q) printf("%d\n", ans[i]);
}
| replace | 260 | 261 | 260 | 261 | 0 | |
p02599 | C++ | Time Limit Exceeded | /* #region header */
#ifdef LOCAL
#include "/Users/takakurashokichi/Desktop/atcoder/cxx-prettyprint-master/prettyprint.hpp"
#define debug(x) cout << x << endl
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// types
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
typedef pair<ll, ll> Pl;
typedef pair<int, int> Pi;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
template <typename T> using mat = vector<vector<T>>;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef vector<vector<char>> vvc;
template <int mod> struct modint {
int x;
modint() : x(0) {}
modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
modint &operator+=(const modint &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
modint &operator-=(const modint &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
modint &operator*=(const modint &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
modint &operator/=(const modint &p) {
*this *= p.inverse();
return *this;
}
modint operator-() const { return modint(-x); }
modint operator+(const modint &p) const { return modint(*this) += p; }
modint operator-(const modint &p) const { return modint(*this) -= p; }
modint operator*(const modint &p) const { return modint(*this) *= p; }
modint operator/(const modint &p) const { return modint(*this) /= p; }
bool operator==(const modint &p) const { return x == p.x; }
bool operator!=(const modint &p) const { return x != p.x; }
modint inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return modint(u);
}
modint pow(int64_t n) const {
modint ret(1), mul(x);
while (n > 0) {
if (n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }
friend istream &operator>>(istream &is, modint &a) {
int64_t t;
is >> t;
a = modint<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
// abreviations
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep_(i, a_, b_, a, b, ...) for (ll i = (a), max_i = (b); i < max_i; i++)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define rrep_(i, a_, b_, a, b, ...) \
for (ll i = (b - 1), min_i = (a); i >= min_i; i--)
#define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp make_pair
#define print(x) cout << x << endl
#define vprint(x) \
rep(i, x.size()) cout << x[i] << ' '; \
cout << endl
#define vsum(x) accumulate(all(x), 0LL)
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
// functions
// gcd(0, x) fails.
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
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 <typename T> T mypow(T x, ll n) {
T ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
}
return ret;
}
ll modpow(ll x, ll n, const ll mod) {
ll ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
x %= mod;
ret %= mod;
}
return ret;
}
template <typename T> uint64_t my_rand(void) {
static uint64_t x = 88172645463325252ULL;
x = x ^ (x << 13);
x = x ^ (x >> 7);
return x = x ^ (x << 17);
}
int popcnt(ull x) { return __builtin_popcountll(x); }
// graph template
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
bool operator<(const edge<T> &r) const { return cost < r.cost; }
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
struct Timer {
clock_t start_time;
void start() { start_time = clock(); }
int lap() {
// return x ms.
return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
}
};
/* #endregion*/
// constant
#define inf 1000000005
#define INF 4000000004000000000LL
#define mod 1000000007LL
#define endl '\n'
typedef modint<mod> mint;
const long double eps = 0.000001;
const long double PI = 3.141592653589793;
// library
template <typename T> struct BIT {
vector<T> data;
BIT(int sz) { data.assign(++sz, 0); }
//[0, k)
T sum(int k) {
T ret = 0;
for (; k > 0; k -= k & -k)
ret += data[k];
return (ret);
}
void add(int k, T x) {
for (++k; k < data.size(); k += k & -k)
data[k] += x;
}
// 0-indexedでk番目の値を返す。
int search(long long k) {
++k;
int res = 0;
int N = 1;
while (N < (int)data.size())
N *= 2;
for (int i = N / 2; i > 0; i /= 2) {
if (res + i < (int)data.size() && data[res + i] < k) {
k = k - data[res + i];
res = res + i;
}
}
return res;
}
// for debug
void show() {
rep(i, SZ(data) - 1) cout << sum(i + 1) - sum(i) << ' ';
cout << endl;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << setprecision(20);
int n, q;
cin >> n >> q;
vi c(n);
rep(i, n) cin >> c[i];
vi ans(q);
vi l(q), r(q);
rep(i, q) {
cin >> l[i] >> r[i];
l[i]--;
}
vi id(q);
iota(all(id), 0);
sort(all(id), [&](int i, int j) { return r[i] < r[j]; });
vi last(vmax(c) + 1, -1);
BIT<int> bit(n);
int now = 0;
for (int i : id) {
rep(j, now, r[i]) {
if (last[c[j]] >= 0)
bit.add(last[c[j]], -1);
last[c[j]] = j;
bit.add(j, 1);
}
ans[i] = bit.sum(r[i]) - bit.sum(l[i]);
}
rep(i, q) print(ans[i]);
} | /* #region header */
#ifdef LOCAL
#include "/Users/takakurashokichi/Desktop/atcoder/cxx-prettyprint-master/prettyprint.hpp"
#define debug(x) cout << x << endl
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// types
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
typedef pair<ll, ll> Pl;
typedef pair<int, int> Pi;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
template <typename T> using mat = vector<vector<T>>;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef vector<vector<char>> vvc;
template <int mod> struct modint {
int x;
modint() : x(0) {}
modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
modint &operator+=(const modint &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
modint &operator-=(const modint &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
modint &operator*=(const modint &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
modint &operator/=(const modint &p) {
*this *= p.inverse();
return *this;
}
modint operator-() const { return modint(-x); }
modint operator+(const modint &p) const { return modint(*this) += p; }
modint operator-(const modint &p) const { return modint(*this) -= p; }
modint operator*(const modint &p) const { return modint(*this) *= p; }
modint operator/(const modint &p) const { return modint(*this) /= p; }
bool operator==(const modint &p) const { return x == p.x; }
bool operator!=(const modint &p) const { return x != p.x; }
modint inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return modint(u);
}
modint pow(int64_t n) const {
modint ret(1), mul(x);
while (n > 0) {
if (n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }
friend istream &operator>>(istream &is, modint &a) {
int64_t t;
is >> t;
a = modint<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
// abreviations
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep_(i, a_, b_, a, b, ...) for (ll i = (a), max_i = (b); i < max_i; i++)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define rrep_(i, a_, b_, a, b, ...) \
for (ll i = (b - 1), min_i = (a); i >= min_i; i--)
#define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp make_pair
#define print(x) cout << x << endl
#define vprint(x) \
rep(i, x.size()) cout << x[i] << ' '; \
cout << endl
#define vsum(x) accumulate(all(x), 0LL)
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
// functions
// gcd(0, x) fails.
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
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 <typename T> T mypow(T x, ll n) {
T ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
}
return ret;
}
ll modpow(ll x, ll n, const ll mod) {
ll ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
x %= mod;
ret %= mod;
}
return ret;
}
template <typename T> uint64_t my_rand(void) {
static uint64_t x = 88172645463325252ULL;
x = x ^ (x << 13);
x = x ^ (x >> 7);
return x = x ^ (x << 17);
}
int popcnt(ull x) { return __builtin_popcountll(x); }
// graph template
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
bool operator<(const edge<T> &r) const { return cost < r.cost; }
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
struct Timer {
clock_t start_time;
void start() { start_time = clock(); }
int lap() {
// return x ms.
return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
}
};
/* #endregion*/
// constant
#define inf 1000000005
#define INF 4000000004000000000LL
#define mod 1000000007LL
#define endl '\n'
typedef modint<mod> mint;
const long double eps = 0.000001;
const long double PI = 3.141592653589793;
// library
template <typename T> struct BIT {
vector<T> data;
BIT(int sz) { data.assign(++sz, 0); }
//[0, k)
T sum(int k) {
T ret = 0;
for (; k > 0; k -= k & -k)
ret += data[k];
return (ret);
}
void add(int k, T x) {
for (++k; k < data.size(); k += k & -k)
data[k] += x;
}
// 0-indexedでk番目の値を返す。
int search(long long k) {
++k;
int res = 0;
int N = 1;
while (N < (int)data.size())
N *= 2;
for (int i = N / 2; i > 0; i /= 2) {
if (res + i < (int)data.size() && data[res + i] < k) {
k = k - data[res + i];
res = res + i;
}
}
return res;
}
// for debug
void show() {
rep(i, SZ(data) - 1) cout << sum(i + 1) - sum(i) << ' ';
cout << endl;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << setprecision(20);
int n, q;
cin >> n >> q;
vi c(n);
rep(i, n) cin >> c[i];
vi ans(q);
vi l(q), r(q);
rep(i, q) {
cin >> l[i] >> r[i];
l[i]--;
}
vi id(q);
iota(all(id), 0);
sort(all(id), [&](int i, int j) { return r[i] < r[j]; });
vi last(vmax(c) + 1, -1);
BIT<int> bit(n);
int now = 0;
for (int i : id) {
rep(j, now, r[i]) {
if (last[c[j]] >= 0)
bit.add(last[c[j]], -1);
last[c[j]] = j;
bit.add(j, 1);
}
now = r[i];
ans[i] = bit.sum(r[i]) - bit.sum(l[i]);
}
rep(i, q) print(ans[i]);
} | insert | 273 | 273 | 273 | 274 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
namespace fast {
const int BSZ = 1 << 15; ////// INPUT
char ibuf[BSZ];
int ipos, ilen;
char nc() { // next char
if (ipos == ilen) {
ipos = 0;
ilen = fread(ibuf, 1, BSZ, stdin);
if (!ilen)
return EOF;
}
return ibuf[ipos++];
}
void rs(string &x) { // read string
char ch;
while (isspace(ch = nc()))
;
do {
x += ch;
} while (!isspace(ch = nc()) && ch != EOF);
}
template <class T> void ri(T &x) { // read int or ll
char ch;
int sgn = 1;
while (!isdigit(ch = nc()))
if (ch == '-')
sgn *= -1;
x = ch - '0';
while (isdigit(ch = nc()))
x = x * 10 + (ch - '0');
x *= sgn;
}
template <class T, class... Ts> void ri(T &t, Ts &...ts) {
ri(t);
ri(ts...);
} // read ints
////// OUTPUT (call initO() at start)
char obuf[BSZ], numBuf[100];
int opos;
void flushOut() {
fwrite(obuf, 1, opos, stdout);
opos = 0;
}
void wc(char c) { // write char
if (opos == BSZ)
flushOut();
obuf[opos++] = c;
}
void ws(string s) {
for (char c : s)
wc(c);
} // write string
template <class T> void wi(T x, char after = '\0') { /// write int
if (x < 0)
wc('-'), x *= -1;
int len = 0;
for (; x >= 10; x /= 10)
numBuf[len++] = '0' + (x % 10);
wc('0' + x);
for (int i = len - 1; i >= 0; i--)
wc(numBuf[i]);
if (after)
wc(after);
}
void initO() { assert(atexit(flushOut) == 0); } /// auto-flush output
} // namespace fast
using namespace fast;
// Globals
const int mxn = 200005;
int a[mxn], freq[mxn] = {};
class query {
public:
int s, e, i, b;
query() { s = e = i = b = INT_MAX; }
query(int start, int end, int index, int block) {
s = start, e = end, i = index, b = block;
}
};
class querySolver {
public:
int sz;
vector<query> queries;
querySolver(int q) {
sz = 0;
queries.resize(q);
}
void addQuery(int s, int e, int i, int b) {
queries[sz++] = query(s, e, i, b);
}
void solve() {
sort(queries.begin(), queries.end(), [](query &q1, query &q2) -> bool {
if (q1.b == q2.b)
return q1.e < q2.e;
return q1.b < q2.b;
});
vector<int> res(sz);
int l = queries[0].s, r = queries[0].e, cnt = 0;
for (int i = l; i <= r; i++) {
freq[a[i]]++;
if (freq[a[i]] == 1)
cnt++;
}
res[queries[0].i] = cnt;
for (int i = 1; i < sz; i++) {
int nL = queries[i].s, nR = queries[i].e;
if (l < nL) {
// Remove elements
while (l < nL) {
freq[a[l]]--;
if (!freq[a[l]])
cnt--;
l++;
}
} else if (l > nL) {
// Add elements
while (l > nL) {
l--;
freq[a[l]]++;
if (freq[a[l]] == 1)
cnt++;
}
}
if (r < nR) {
// Add elements
while (r < nR) {
r++;
freq[a[r]]++;
if (freq[a[r]] == 1)
cnt++;
}
} else if (r > nR) {
// Remove elements
while (r > nR) {
freq[a[r]]--;
if (!freq[a[r]])
cnt--;
r--;
}
}
res[queries[i].i] = cnt;
}
for (auto &ans : res)
wi(ans, '\n');
}
};
int main() {
initO();
int n, q;
ri(n, q);
// Coordinate Compression
unordered_map<int, int> compressor;
for (int i = 0, nVal = 1; i < n; i++) {
ri(a[i]);
if (compressor.count(a[i])) {
a[i] = compressor[a[i]];
} else {
compressor[a[i]] = nVal;
a[i] = nVal++;
}
}
// Queries
auto qSolver = querySolver(q);
int block = sqrt(n);
for (int i = 0, start, end; i < q; i++) {
ri(start, end);
start--, end--;
qSolver.addQuery(start, end, i, start / block);
}
qSolver.solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
namespace fast {
const int BSZ = 1 << 15; ////// INPUT
char ibuf[BSZ];
int ipos, ilen;
char nc() { // next char
if (ipos == ilen) {
ipos = 0;
ilen = fread(ibuf, 1, BSZ, stdin);
if (!ilen)
return EOF;
}
return ibuf[ipos++];
}
void rs(string &x) { // read string
char ch;
while (isspace(ch = nc()))
;
do {
x += ch;
} while (!isspace(ch = nc()) && ch != EOF);
}
template <class T> void ri(T &x) { // read int or ll
char ch;
int sgn = 1;
while (!isdigit(ch = nc()))
if (ch == '-')
sgn *= -1;
x = ch - '0';
while (isdigit(ch = nc()))
x = x * 10 + (ch - '0');
x *= sgn;
}
template <class T, class... Ts> void ri(T &t, Ts &...ts) {
ri(t);
ri(ts...);
} // read ints
////// OUTPUT (call initO() at start)
char obuf[BSZ], numBuf[100];
int opos;
void flushOut() {
fwrite(obuf, 1, opos, stdout);
opos = 0;
}
void wc(char c) { // write char
if (opos == BSZ)
flushOut();
obuf[opos++] = c;
}
void ws(string s) {
for (char c : s)
wc(c);
} // write string
template <class T> void wi(T x, char after = '\0') { /// write int
if (x < 0)
wc('-'), x *= -1;
int len = 0;
for (; x >= 10; x /= 10)
numBuf[len++] = '0' + (x % 10);
wc('0' + x);
for (int i = len - 1; i >= 0; i--)
wc(numBuf[i]);
if (after)
wc(after);
}
void initO() { assert(atexit(flushOut) == 0); } /// auto-flush output
} // namespace fast
using namespace fast;
// Globals
const int mxn = 500005;
int a[mxn], freq[mxn] = {};
class query {
public:
int s, e, i, b;
query() { s = e = i = b = INT_MAX; }
query(int start, int end, int index, int block) {
s = start, e = end, i = index, b = block;
}
};
class querySolver {
public:
int sz;
vector<query> queries;
querySolver(int q) {
sz = 0;
queries.resize(q);
}
void addQuery(int s, int e, int i, int b) {
queries[sz++] = query(s, e, i, b);
}
void solve() {
sort(queries.begin(), queries.end(), [](query &q1, query &q2) -> bool {
if (q1.b == q2.b)
return q1.e < q2.e;
return q1.b < q2.b;
});
vector<int> res(sz);
int l = queries[0].s, r = queries[0].e, cnt = 0;
for (int i = l; i <= r; i++) {
freq[a[i]]++;
if (freq[a[i]] == 1)
cnt++;
}
res[queries[0].i] = cnt;
for (int i = 1; i < sz; i++) {
int nL = queries[i].s, nR = queries[i].e;
if (l < nL) {
// Remove elements
while (l < nL) {
freq[a[l]]--;
if (!freq[a[l]])
cnt--;
l++;
}
} else if (l > nL) {
// Add elements
while (l > nL) {
l--;
freq[a[l]]++;
if (freq[a[l]] == 1)
cnt++;
}
}
if (r < nR) {
// Add elements
while (r < nR) {
r++;
freq[a[r]]++;
if (freq[a[r]] == 1)
cnt++;
}
} else if (r > nR) {
// Remove elements
while (r > nR) {
freq[a[r]]--;
if (!freq[a[r]])
cnt--;
r--;
}
}
res[queries[i].i] = cnt;
}
for (auto &ans : res)
wi(ans, '\n');
}
};
int main() {
initO();
int n, q;
ri(n, q);
// Coordinate Compression
unordered_map<int, int> compressor;
for (int i = 0, nVal = 1; i < n; i++) {
ri(a[i]);
if (compressor.count(a[i])) {
a[i] = compressor[a[i]];
} else {
compressor[a[i]] = nVal;
a[i] = nVal++;
}
}
// Queries
auto qSolver = querySolver(q);
int block = sqrt(n);
for (int i = 0, start, end; i < q; i++) {
ri(start, end);
start--, end--;
qSolver.addQuery(start, end, i, start / block);
}
qSolver.solve();
return 0;
} | replace | 81 | 82 | 81 | 82 | 0 | |
p02599 | C++ | Runtime Error | // luogu-judger-enable-o2
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define rg register
typedef long long ll;
template <typename T> bool read(T &n) {
T ans = 0, flag = 1;
char ch;
while ((ch = getchar()) < '0' || ch > '9')
if (ch == '-')
flag = -1;
else if (ch == EOF)
return false;
ans = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9')
ans = ans * 10 + ch - '0';
n = ans * flag;
return true;
}
template <typename T> T read() {
T ans = 0, flag = 1;
char ch;
while ((ch = getchar()) < '0' || ch > '9')
if (ch == '-')
flag = -1;
else if (ch == EOF)
return false;
ans = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9')
ans = ans * 10 + ch - '0';
return ans * flag;
};
struct QUE {
int l, r, id;
} que[50004];
int sz, belong[50004], n, m, pre[50004], cnt[50004];
ll fz[50004], fm[50004], ans;
bool cmp(QUE a, QUE b) {
return belong[a.l] == belong[b.l] ? (belong[a.l] % 2 ? a.r < b.r : a.r > b.r)
: belong[a.l] < belong[b.l];
}
int main() {
read(n), read(m);
sz = sqrt(n);
for (int i = 1; i <= n; i++)
read(pre[i]);
for (int i = 1; i <= m; i++)
read(que[i].l), read(que[i].r), que[i].id = i, belong[i] = (i - 1) / sz + 1;
sort(que + 1, que + m + 1, cmp);
int nl = que[1].l, nr = que[1].l;
cnt[pre[nl]]++;
ans = 1;
for (int i = 1; i <= m; i++) {
while (nl < que[i].l) {
if (cnt[pre[nl]] == 1)
ans--;
cnt[pre[nl]]--, nl++;
}
while (nr > que[i].r) {
if (cnt[pre[nr]] == 1)
ans--;
cnt[pre[nr]]--, nr--;
}
while (nl > que[i].l) {
nl--;
if (cnt[pre[nl]] == 0)
ans++;
cnt[pre[nl]]++;
}
while (nr < que[i].r) {
nr++;
if (cnt[pre[nr]] == 0)
ans++;
cnt[pre[nr]]++;
}
fz[que[i].id] = ans;
}
for (int i = 1; i <= m; i++) {
printf("%lld\n", fz[i]);
}
return 0;
} | // luogu-judger-enable-o2
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define rg register
typedef long long ll;
template <typename T> bool read(T &n) {
T ans = 0, flag = 1;
char ch;
while ((ch = getchar()) < '0' || ch > '9')
if (ch == '-')
flag = -1;
else if (ch == EOF)
return false;
ans = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9')
ans = ans * 10 + ch - '0';
n = ans * flag;
return true;
}
template <typename T> T read() {
T ans = 0, flag = 1;
char ch;
while ((ch = getchar()) < '0' || ch > '9')
if (ch == '-')
flag = -1;
else if (ch == EOF)
return false;
ans = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9')
ans = ans * 10 + ch - '0';
return ans * flag;
};
struct QUE {
int l, r, id;
} que[500004];
int sz, belong[500004], n, m, pre[500004], cnt[500004];
ll fz[500004], fm[500004], ans;
bool cmp(QUE a, QUE b) {
return belong[a.l] == belong[b.l] ? (belong[a.l] % 2 ? a.r < b.r : a.r > b.r)
: belong[a.l] < belong[b.l];
}
int main() {
read(n), read(m);
sz = sqrt(n);
for (int i = 1; i <= n; i++)
read(pre[i]);
for (int i = 1; i <= m; i++)
read(que[i].l), read(que[i].r), que[i].id = i, belong[i] = (i - 1) / sz + 1;
sort(que + 1, que + m + 1, cmp);
int nl = que[1].l, nr = que[1].l;
cnt[pre[nl]]++;
ans = 1;
for (int i = 1; i <= m; i++) {
while (nl < que[i].l) {
if (cnt[pre[nl]] == 1)
ans--;
cnt[pre[nl]]--, nl++;
}
while (nr > que[i].r) {
if (cnt[pre[nr]] == 1)
ans--;
cnt[pre[nr]]--, nr--;
}
while (nl > que[i].l) {
nl--;
if (cnt[pre[nl]] == 0)
ans++;
cnt[pre[nl]]++;
}
while (nr < que[i].r) {
nr++;
if (cnt[pre[nr]] == 0)
ans++;
cnt[pre[nr]]++;
}
fz[que[i].id] = ans;
}
for (int i = 1; i <= m; i++) {
printf("%lld\n", fz[i]);
}
return 0;
} | replace | 39 | 42 | 39 | 42 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline void prn(vector<T> &v) {
int n = sz(v);
REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
int n = sz(v);
if (n == 0)
cout << endl;
REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
for (auto u : v)
printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
int n = sz(v);
REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
}
class BIT {
public:
vector<int> bit;
int M;
BIT(int M) : bit(vector<int>(M + 1, 0)), M(M) {}
int sum(int i) {
if (!i)
return 0;
return bit[i] + sum(i - (i & -i));
}
void add(int i, int x) {
if (i > M)
return;
bit[i] += x;
add(i + (i & -i), x);
}
};
void solve() {
int n, q;
cin >> n >> q;
vi c(n);
REP(i, n) {
cin >> c[i];
c[i]--;
}
BIT bit(n + 1);
vi s(n, -1);
vector<tuple<int, int, int>> query(q);
int l, r;
REP(i, q) {
cin >> l >> r;
l--;
r--;
query.emplace_back(r, l, i);
}
SORT(query);
int p = -1;
vi ans(q);
int idx;
for (auto m : query) {
tie(r, l, idx) = m;
FOR(i, p + 1, r + 1) {
if (s[c[i]] != -1)
bit.add(s[c[i]] + 1, -1);
bit.add(i + 1, 1);
s[c[i]] = i;
}
ans[idx] = bit.sum(r + 1 + 1) - bit.sum(l);
}
printlnv(ans);
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline void prn(vector<T> &v) {
int n = sz(v);
REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
int n = sz(v);
if (n == 0)
cout << endl;
REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
for (auto u : v)
printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
int n = sz(v);
REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
}
class BIT {
public:
vector<int> bit;
int M;
BIT(int M) : bit(vector<int>(M + 1, 0)), M(M) {}
int sum(int i) {
if (!i)
return 0;
return bit[i] + sum(i - (i & -i));
}
void add(int i, int x) {
if (i > M)
return;
bit[i] += x;
add(i + (i & -i), x);
}
};
void solve() {
int n, q;
cin >> n >> q;
vi c(n);
REP(i, n) {
cin >> c[i];
c[i]--;
}
BIT bit(n + 1);
vi s(n, -1);
vector<tuple<int, int, int>> query(q);
int l, r;
REP(i, q) {
cin >> l >> r;
l--;
r--;
query.emplace_back(r, l, i);
}
SORT(query);
int p = -1;
vi ans(q);
int idx;
for (auto m : query) {
tie(r, l, idx) = m;
FOR(i, p + 1, r + 1) {
if (s[c[i]] != -1)
bit.add(s[c[i]] + 1, -1);
bit.add(i + 1, 1);
s[c[i]] = i;
}
p = r;
ans[idx] = bit.sum(r + 1 + 1) - bit.sum(l);
}
printlnv(ans);
}
| insert | 122 | 122 | 122 | 123 | TLE | |
p02599 | C++ | Time Limit Exceeded | #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"
using namespace std;
const long long MOD = (long long)1e9 + 7;
int n, q, c[500009], l, r, ans[500009], cnt[500009];
pair<pair<int, int>, int> dat[500009];
int seg, cur;
bool comp(const pair<pair<int, int>, int> &x,
const pair<pair<int, int>, int> &y) {
if (x.fi.fi / seg < y.fi.fi / seg)
return true;
else if (x.fi.fi / seg > y.fi.fi / seg)
return false;
return x.fi.se < y.fi.se;
}
void fun(int a, int b, int x, int y) {
if (a < x) {
for (int i = a; i < x; i++) {
cnt[c[i]]--;
if (cnt[c[i]] == 0)
cur--;
}
} else {
for (int i = a - 1; i >= x; i--) {
if (cnt[c[i]] == 0)
cur++;
cnt[c[i]]++;
}
}
if (b < y) {
for (int i = b + 1; i <= y; i++) {
if (cnt[c[i]] == 0)
cur++;
cnt[c[i]]++;
}
} else {
for (int i = b; i > y; i--) {
cnt[c[i]]--;
if (cnt[c[i]] == 0)
cur--;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 0; i < q; i++) {
cin >> l >> r;
pair<int, int> tmp = {l, r};
dat[i] = {tmp, i};
}
seg = 1;
while (seg * seg < n)
seg++;
sort(dat, dat + 1, comp);
for (int i = dat[0].fi.fi; i <= dat[0].fi.se; i++) {
if (cnt[c[i]] == 0)
cur++;
cnt[c[i]]++;
}
ans[dat[0].se] = cur;
for (int i = 1; i < q; i++) {
fun(dat[i - 1].fi.fi, dat[i - 1].fi.se, dat[i].fi.fi, dat[i].fi.se);
ans[dat[i].se] = cur;
}
for (int i = 0; i < q; i++) {
cout << ans[i] << E;
}
// system("pause");
return 0;
} | #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"
using namespace std;
const long long MOD = (long long)1e9 + 7;
int n, q, c[500009], l, r, ans[500009], cnt[500009];
pair<pair<int, int>, int> dat[500009];
int seg, cur;
bool comp(const pair<pair<int, int>, int> &x,
const pair<pair<int, int>, int> &y) {
if (x.fi.fi / seg < y.fi.fi / seg)
return true;
else if (x.fi.fi / seg > y.fi.fi / seg)
return false;
return x.fi.se < y.fi.se;
}
void fun(int a, int b, int x, int y) {
if (a < x) {
for (int i = a; i < x; i++) {
cnt[c[i]]--;
if (cnt[c[i]] == 0)
cur--;
}
} else {
for (int i = a - 1; i >= x; i--) {
if (cnt[c[i]] == 0)
cur++;
cnt[c[i]]++;
}
}
if (b < y) {
for (int i = b + 1; i <= y; i++) {
if (cnt[c[i]] == 0)
cur++;
cnt[c[i]]++;
}
} else {
for (int i = b; i > y; i--) {
cnt[c[i]]--;
if (cnt[c[i]] == 0)
cur--;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 0; i < q; i++) {
cin >> l >> r;
pair<int, int> tmp = {l, r};
dat[i] = {tmp, i};
}
seg = 1;
while (seg * seg < n)
seg++;
sort(dat, dat + q, comp);
for (int i = dat[0].fi.fi; i <= dat[0].fi.se; i++) {
if (cnt[c[i]] == 0)
cur++;
cnt[c[i]]++;
}
ans[dat[0].se] = cur;
for (int i = 1; i < q; i++) {
fun(dat[i - 1].fi.fi, dat[i - 1].fi.se, dat[i].fi.fi, dat[i].fi.se);
ans[dat[i].se] = cur;
}
for (int i = 0; i < q; i++) {
cout << ans[i] << E;
}
// system("pause");
return 0;
} | replace | 68 | 69 | 68 | 69 | TLE | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define mem(x) memset(x, 0, sizeof(x))
#define sca(x) \
if (scanf("%d", &x)) { \
}
#define scaa(x, y) \
if (scanf("%d%d", &x, &y)) { \
}
#define prt(x) printf("%d\n", x)
#define inf 0x7f7f7f7fconst int maxn = 1e5 + 5;
using namespace std;
const int maxn = 5e5 + 1000;
int n, m;
struct Tree {
int ls, rs, val; // 左右儿子的编号, 和维护的一个值.
} tre[maxn * 40];
int idx = 0, root[maxn];
int build(int l, int r) {
int nod = ++idx;
tre[nod].val = 0;
if (l == r)
return nod;
int mid = (l + r) >> 1;
tre[nod].ls = build(l, mid);
tre[nod].rs = build(mid + 1, r);
return nod;
}
int update(int pre, int l, int r, int pos, int v) {
int nod = ++idx;
tre[nod] = tre[pre];
tre[nod].val += v;
if (l == r)
return nod;
int mid = (l + r) >> 1;
if (pos <= mid)
tre[nod].ls = update(tre[pre].ls, l, mid, pos, v);
else
tre[nod].rs = update(tre[pre].rs, mid + 1, r, pos, v);
return nod;
}
int query(int id, int l, int r, int pos) {
if (l == r)
return tre[id].val;
int mid = (l + r) >> 1;
if (pos <= mid) {
return tre[tre[id].rs].val + query(tre[id].ls, l, mid, pos);
} else
return query(tre[id].rs, mid + 1, r, pos);
}
int a[maxn], vis[maxn * 10];
int main() {
scaa(n, m);
for (int i = 1; i <= n; i++) {
sca(a[i]);
}
root[0] = build(1, n); // 根节点
for (int i = 1; i <= n; i++) {
if (!vis[a[i]])
root[i] = update(root[i - 1], 1, n, i, 1);
else {
int t = update(root[i - 1], 1, n, vis[a[i]], -1);
root[i] = update(t, 1, n, i, 1);
}
vis[a[i]] = i;
}
while (m--) {
int l, r;
scaa(l, r);
int tmp = query(root[r], 1, n, l);
printf("%d\n", tmp);
}
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define mem(x) memset(x, 0, sizeof(x))
#define sca(x) \
if (scanf("%d", &x)) { \
}
#define scaa(x, y) \
if (scanf("%d%d", &x, &y)) { \
}
#define prt(x) printf("%d\n", x)
#define inf 0x7f7f7f7fconst int maxn = 1e5 + 5;
using namespace std;
const int maxn = 9e5 + 1000;
int n, m;
struct Tree {
int ls, rs, val; // 左右儿子的编号, 和维护的一个值.
} tre[maxn * 40];
int idx = 0, root[maxn];
int build(int l, int r) {
int nod = ++idx;
tre[nod].val = 0;
if (l == r)
return nod;
int mid = (l + r) >> 1;
tre[nod].ls = build(l, mid);
tre[nod].rs = build(mid + 1, r);
return nod;
}
int update(int pre, int l, int r, int pos, int v) {
int nod = ++idx;
tre[nod] = tre[pre];
tre[nod].val += v;
if (l == r)
return nod;
int mid = (l + r) >> 1;
if (pos <= mid)
tre[nod].ls = update(tre[pre].ls, l, mid, pos, v);
else
tre[nod].rs = update(tre[pre].rs, mid + 1, r, pos, v);
return nod;
}
int query(int id, int l, int r, int pos) {
if (l == r)
return tre[id].val;
int mid = (l + r) >> 1;
if (pos <= mid) {
return tre[tre[id].rs].val + query(tre[id].ls, l, mid, pos);
} else
return query(tre[id].rs, mid + 1, r, pos);
}
int a[maxn], vis[maxn * 10];
int main() {
scaa(n, m);
for (int i = 1; i <= n; i++) {
sca(a[i]);
}
root[0] = build(1, n); // 根节点
for (int i = 1; i <= n; i++) {
if (!vis[a[i]])
root[i] = update(root[i - 1], 1, n, i, 1);
else {
int t = update(root[i - 1], 1, n, vis[a[i]], -1);
root[i] = update(t, 1, n, i, 1);
}
vis[a[i]] = i;
}
while (m--) {
int l, r;
scaa(l, r);
int tmp = query(root[r], 1, n, l);
printf("%d\n", tmp);
}
}
| replace | 19 | 20 | 19 | 20 | -11 | |
p02599 | C++ | Runtime Error | #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;
#define ff first
#define ss second
#define ll int64_t
#define ld long double
#define nl cout << "\n"
#define all(v) v.begin(), v.end()
#define mset(a, v) memset((a), (v), sizeof(a))
#define forn(i, a, b) for (int64_t i = int64_t(a); i < int64_t(b); ++i)
#define forb(i, a, b) for (int64_t i = int64_t(a); i >= int64_t(b); --i)
#define fastio() \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define mod 1000000007
#define mod2 998244353
#define inf 1000000000000007
#define pi 3.14159265358979323846
template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> &p) {
return out << "(" << p.ff << ", " << p.ss << ")";
}
template <class T> ostream &operator<<(ostream &out, vector<T> &v) {
out << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
out << ", ";
out << *it;
}
return out << "]";
}
template <class T> ostream &operator<<(ostream &out, deque<T> &v) {
out << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
out << ", ";
out << *it;
}
return out << "]";
}
template <class T> ostream &operator<<(ostream &out, set<T> &s) {
out << "{";
for (auto it = s.begin(); it != s.end(); ++it) {
if (it != s.begin())
out << ", ";
out << *it;
}
return out << "}";
}
template <class L, class R> ostream &operator<<(ostream &out, map<L, R> &m) {
out << "{";
for (auto it = m.begin(); it != m.end(); ++it) {
if (it != m.begin())
out << ", ";
out << *it;
}
return out << "}";
}
void dbg_out() { cerr << "]\n"; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cerr << H;
if (sizeof...(Tail))
cerr << ", ";
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] = [", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
ll count_digits(ll n) {
ll dig = floor(log10(n) + 1);
return dig;
}
ll mpow(ll x, ll y, ll p) {
ll r = 1;
x = x % p;
while (y > 0) {
if (y & 1)
r = (r * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return r;
}
ll mod_inv(ll a, ll m) {
ll m0 = m, y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
ll q = a / m, t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
/* const ll MAXN=1000007;ll spf[MAXN+5];vector<ll> prime; */
/* void sieve(ll n) {forn(i,2,MAXN+1)spf[i]=i;for(ll
* i=2;i*i<=n;i++)if(spf[i]==i)for(ll
* j=i*i;j<=n;j+=i)if(spf[j]==j)spf[j]=i;forn(i,2,MAXN+1)if(spf[i]==i)prime.push_back(i);}
*/
//-------------------------------L_I_Erq34-------------------------------
vector<ll> a, b;
vector<vector<ll>> segtree;
void build(ll n) {
forn(i, 0, n) segtree[i + n] = {a[i]};
forb(i, n - 1, 0) {
segtree[i].resize(segtree[i << 1].size() + segtree[i << 1 | 1].size());
merge(all(segtree[i << 1]), all(segtree[i << 1 | 1]), segtree[i].begin());
}
}
ll query(ll n, ll l, ll r) {
ll res = 0, val = r - 1;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
res += segtree[l].end() - upper_bound(all(segtree[l]), val);
l++;
}
if (r & 1) {
r--;
res += segtree[r].end() - upper_bound(all(segtree[r]), val);
}
}
return res;
}
int main() {
fastio();
ll z, n, m, t, k, i, j, l, d, h, r;
cin >> n >> m;
a = b = vector<ll>(n, inf);
for (ll &i : a) {
cin >> i;
i--;
}
forb(i, n - 1, 0) {
r = b[a[i]];
b[a[i]] = i;
a[i] = r;
}
segtree = vector<vector<ll>>(n << 1);
build(n);
while (m--) {
cin >> l >> r;
cout << query(n, l - 1, r) << "\n";
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| #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;
#define ff first
#define ss second
#define ll int64_t
#define ld long double
#define nl cout << "\n"
#define all(v) v.begin(), v.end()
#define mset(a, v) memset((a), (v), sizeof(a))
#define forn(i, a, b) for (int64_t i = int64_t(a); i < int64_t(b); ++i)
#define forb(i, a, b) for (int64_t i = int64_t(a); i >= int64_t(b); --i)
#define fastio() \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define mod 1000000007
#define mod2 998244353
#define inf 1000000000000007
#define pi 3.14159265358979323846
template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> &p) {
return out << "(" << p.ff << ", " << p.ss << ")";
}
template <class T> ostream &operator<<(ostream &out, vector<T> &v) {
out << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
out << ", ";
out << *it;
}
return out << "]";
}
template <class T> ostream &operator<<(ostream &out, deque<T> &v) {
out << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
out << ", ";
out << *it;
}
return out << "]";
}
template <class T> ostream &operator<<(ostream &out, set<T> &s) {
out << "{";
for (auto it = s.begin(); it != s.end(); ++it) {
if (it != s.begin())
out << ", ";
out << *it;
}
return out << "}";
}
template <class L, class R> ostream &operator<<(ostream &out, map<L, R> &m) {
out << "{";
for (auto it = m.begin(); it != m.end(); ++it) {
if (it != m.begin())
out << ", ";
out << *it;
}
return out << "}";
}
void dbg_out() { cerr << "]\n"; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cerr << H;
if (sizeof...(Tail))
cerr << ", ";
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] = [", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
ll count_digits(ll n) {
ll dig = floor(log10(n) + 1);
return dig;
}
ll mpow(ll x, ll y, ll p) {
ll r = 1;
x = x % p;
while (y > 0) {
if (y & 1)
r = (r * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return r;
}
ll mod_inv(ll a, ll m) {
ll m0 = m, y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
ll q = a / m, t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
/* const ll MAXN=1000007;ll spf[MAXN+5];vector<ll> prime; */
/* void sieve(ll n) {forn(i,2,MAXN+1)spf[i]=i;for(ll
* i=2;i*i<=n;i++)if(spf[i]==i)for(ll
* j=i*i;j<=n;j+=i)if(spf[j]==j)spf[j]=i;forn(i,2,MAXN+1)if(spf[i]==i)prime.push_back(i);}
*/
//-------------------------------L_I_Erq34-------------------------------
vector<ll> a, b;
vector<vector<ll>> segtree;
void build(ll n) {
forn(i, 0, n) segtree[i + n] = {a[i]};
forb(i, n - 1, 1) {
segtree[i].resize(segtree[i << 1].size() + segtree[i << 1 | 1].size());
merge(all(segtree[i << 1]), all(segtree[i << 1 | 1]), segtree[i].begin());
}
}
ll query(ll n, ll l, ll r) {
ll res = 0, val = r - 1;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
res += segtree[l].end() - upper_bound(all(segtree[l]), val);
l++;
}
if (r & 1) {
r--;
res += segtree[r].end() - upper_bound(all(segtree[r]), val);
}
}
return res;
}
int main() {
fastio();
ll z, n, m, t, k, i, j, l, d, h, r;
cin >> n >> m;
a = b = vector<ll>(n, inf);
for (ll &i : a) {
cin >> i;
i--;
}
forb(i, n - 1, 0) {
r = b[a[i]];
b[a[i]] = i;
a[i] = r;
}
segtree = vector<vector<ll>>(n << 1);
build(n);
while (m--) {
cin >> l >> r;
cout << query(n, l - 1, r) << "\n";
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| replace | 125 | 126 | 125 | 126 | 0 |
Time elapsed: 28ms
|
p02599 | C++ | Time Limit Exceeded | #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;
#define ff first
#define ss second
#define ll int64_t
#define ld long double
#define nl cout << "\n"
#define dbg(x) cerr << #x << " = " << x << "\n"
#define dbg2(x, y) cerr << #x << " = " << x << " " << #y << " = " << y << "\n"
#define all(v) v.begin(), v.end()
#define mset(a, v) memset((a), (v), sizeof(a))
#define forn(i, a, b) for (int64_t i = int64_t(a); i < int64_t(b); ++i)
#define forb(i, a, b) for (int64_t i = int64_t(a); i >= int64_t(b); --i)
#define fastio() \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define mod 1000000007
#define mod2 998244353
#define inf 1000000000000007
#define pi 3.14159265358979323846
template <typename T, typename U> static inline void amin(T &x, U y) {
if (y < x)
x = y;
}
template <typename T, typename U> static inline void amax(T &x, U y) {
if (x < y)
x = y;
}
template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> &p) {
return out << "(" << p.ff << ", " << p.ss << ")";
}
template <class T> ostream &operator<<(ostream &out, vector<T> &v) {
out << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
out << ", ";
out << *it;
}
return out << "]";
}
template <class T> ostream &operator<<(ostream &out, deque<T> &v) {
out << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
out << ", ";
out << *it;
}
return out << "]";
}
template <class T> ostream &operator<<(ostream &out, set<T> &s) {
out << "{";
for (auto it = s.begin(); it != s.end(); ++it) {
if (it != s.begin())
out << ", ";
out << *it;
}
return out << "}";
}
template <class L, class R> ostream &operator<<(ostream &out, map<L, R> &m) {
out << "{";
for (auto it = m.begin(); it != m.end(); ++it) {
if (it != m.begin())
out << ", ";
out << *it;
}
return out << "}";
}
ll count_digits(ll n) {
ll dig = floor(log10(n) + 1);
return dig;
}
ll mpow(ll x, ll y, ll p) {
ll r = 1;
x = x % p;
while (y > 0) {
if (y & 1)
r = (r * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return r;
}
string dec_bin_32(ll n) {
string s;
for (ll i = 31; i >= 0; i--) {
ll k = n >> i;
s += (k & 1) ? '1' : '0';
}
return s;
}
string dec_bin_64(ll n) {
string s;
for (ll i = 63; i >= 0; i--) {
ll k = n >> i;
s += (k & 1) ? '1' : '0';
}
return s;
}
ll mod_inv(ll a, ll m) {
ll m0 = m, y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
ll q = a / m, t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
/* const ll MAXN=10000007;ll spf[MAXN+5];vector<ll> prime; */
/* void sieve(ll n) {forn(i,2,MAXN+1)spf[i]=i;for(ll
* i=2;i*i<=n;i++)if(spf[i]==i)for(ll
* j=i*i;j<=n;j+=i)if(spf[j]==j)spf[j]=i;forn(i,2,MAXN+1)if(spf[i]==i)prime.push_back(i);}
*/
//-------------------------------L_I_Erq34-------------------------------
vector<vector<ll>> segtree;
vector<ll> b;
void construct(ll st, ll end, ll i) {
if (st == end) {
segtree[i].push_back(b[st]);
return;
}
ll mid = (st + end) >> 1;
construct(st, mid, 2 * i + 1);
construct(mid + 1, end, 2 * i + 2);
segtree[i].resize(segtree[2 * i + 1].size() + segtree[2 * i + 2].size());
merge(all(segtree[2 * i + 1]), all(segtree[2 * i + 2]), segtree[i].begin());
}
ll query(ll l, ll h, ll st, ll end, ll i, ll k) {
if (l <= st and h >= end)
return upper_bound(all(segtree[i]), k) - segtree[i].begin();
if (l > end or h < st)
return 0;
ll mid = (st + end) >> 1;
return query(l, h, st, mid, 2 * i + 1, k) +
query(l, h, mid + 1, end, 2 * i + 2, k);
}
int main() {
fastio();
ll z, n, m, t, k, i, j, l, d, h, r;
ll q;
cin >> n >> q;
vector<ll> a(n), ind(n + 1, inf);
b.resize(n);
for (ll &i : a)
cin >> i;
forb(i, n - 1, 0) {
b[i] = ind[a[i]];
ind[a[i]] = i;
}
dbg(b);
segtree.resize(n << 2);
construct(0, n - 1, 0);
while (q--) {
cin >> l >> r;
l--, r--;
k = r - l + 1 - query(l, r, 0, n - 1, 0, r);
cout << k << "\n";
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| #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;
#define ff first
#define ss second
#define ll int64_t
#define ld long double
#define nl cout << "\n"
#define dbg(x) cerr << #x << " = " << x << "\n"
#define dbg2(x, y) cerr << #x << " = " << x << " " << #y << " = " << y << "\n"
#define all(v) v.begin(), v.end()
#define mset(a, v) memset((a), (v), sizeof(a))
#define forn(i, a, b) for (int64_t i = int64_t(a); i < int64_t(b); ++i)
#define forb(i, a, b) for (int64_t i = int64_t(a); i >= int64_t(b); --i)
#define fastio() \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define mod 1000000007
#define mod2 998244353
#define inf 1000000000000007
#define pi 3.14159265358979323846
template <typename T, typename U> static inline void amin(T &x, U y) {
if (y < x)
x = y;
}
template <typename T, typename U> static inline void amax(T &x, U y) {
if (x < y)
x = y;
}
template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> &p) {
return out << "(" << p.ff << ", " << p.ss << ")";
}
template <class T> ostream &operator<<(ostream &out, vector<T> &v) {
out << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
out << ", ";
out << *it;
}
return out << "]";
}
template <class T> ostream &operator<<(ostream &out, deque<T> &v) {
out << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
out << ", ";
out << *it;
}
return out << "]";
}
template <class T> ostream &operator<<(ostream &out, set<T> &s) {
out << "{";
for (auto it = s.begin(); it != s.end(); ++it) {
if (it != s.begin())
out << ", ";
out << *it;
}
return out << "}";
}
template <class L, class R> ostream &operator<<(ostream &out, map<L, R> &m) {
out << "{";
for (auto it = m.begin(); it != m.end(); ++it) {
if (it != m.begin())
out << ", ";
out << *it;
}
return out << "}";
}
ll count_digits(ll n) {
ll dig = floor(log10(n) + 1);
return dig;
}
ll mpow(ll x, ll y, ll p) {
ll r = 1;
x = x % p;
while (y > 0) {
if (y & 1)
r = (r * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return r;
}
string dec_bin_32(ll n) {
string s;
for (ll i = 31; i >= 0; i--) {
ll k = n >> i;
s += (k & 1) ? '1' : '0';
}
return s;
}
string dec_bin_64(ll n) {
string s;
for (ll i = 63; i >= 0; i--) {
ll k = n >> i;
s += (k & 1) ? '1' : '0';
}
return s;
}
ll mod_inv(ll a, ll m) {
ll m0 = m, y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
ll q = a / m, t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
/* const ll MAXN=10000007;ll spf[MAXN+5];vector<ll> prime; */
/* void sieve(ll n) {forn(i,2,MAXN+1)spf[i]=i;for(ll
* i=2;i*i<=n;i++)if(spf[i]==i)for(ll
* j=i*i;j<=n;j+=i)if(spf[j]==j)spf[j]=i;forn(i,2,MAXN+1)if(spf[i]==i)prime.push_back(i);}
*/
//-------------------------------L_I_Erq34-------------------------------
vector<vector<ll>> segtree;
vector<ll> b;
void construct(ll st, ll end, ll i) {
if (st == end) {
segtree[i].push_back(b[st]);
return;
}
ll mid = (st + end) >> 1;
construct(st, mid, 2 * i + 1);
construct(mid + 1, end, 2 * i + 2);
segtree[i].resize(segtree[2 * i + 1].size() + segtree[2 * i + 2].size());
merge(all(segtree[2 * i + 1]), all(segtree[2 * i + 2]), segtree[i].begin());
}
ll query(ll l, ll h, ll st, ll end, ll i, ll k) {
if (l <= st and h >= end)
return upper_bound(all(segtree[i]), k) - segtree[i].begin();
if (l > end or h < st)
return 0;
ll mid = (st + end) >> 1;
return query(l, h, st, mid, 2 * i + 1, k) +
query(l, h, mid + 1, end, 2 * i + 2, k);
}
int main() {
fastio();
ll z, n, m, t, k, i, j, l, d, h, r;
ll q;
cin >> n >> q;
vector<ll> a(n), ind(n + 1, inf);
b.resize(n);
for (ll &i : a)
cin >> i;
forb(i, n - 1, 0) {
b[i] = ind[a[i]];
ind[a[i]] = i;
}
segtree.resize(n << 2);
construct(0, n - 1, 0);
while (q--) {
cin >> l >> r;
l--, r--;
k = r - l + 1 - query(l, r, 0, n - 1, 0, r);
cout << k << "\n";
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
} | delete | 171 | 172 | 171 | 171 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define endl "\n"
#define inf (long long)1000000007
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define bigint 1e18
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUF(v) shuffle(aint(v), RNG);
// Use mt19937_64 for 64 bit random numbers.
template <typename T> T ceil(T a, T b) { return (a + b - 1) / b; }
template <typename T> T binpow(T a, T b, T m) {
a %= m;
T res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
const int BLOCK = 600;
struct Query {
int l, r, idx;
};
bool cmp(Query x, Query 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;
}
int arr[511111], cnt[511111], ans = 0;
void add(int idx) {
cnt[arr[idx]]++;
if (cnt[arr[idx]] == 1) {
ans++;
}
}
void remove(int idx) {
cnt[arr[idx]]--;
if (cnt[arr[idx]] == 0) {
ans--;
}
}
vector<int> mo_s_algorithm(vector<Query> &queries) {
vector<int> res(queries.size());
sort(all(queries), cmp);
int cur_l = 0;
int cur_r = -1;
for (Query q : queries) {
while (cur_l > q.l) {
cur_l--;
add(cur_l);
}
while (cur_r < q.r) {
cur_r++;
add(cur_r);
}
while (cur_l < q.l) {
remove(cur_l);
cur_l++;
}
while (cur_r > q.r) {
remove(cur_r);
cur_r--;
}
res[q.idx] = ans;
}
return res;
}
void solve() {
int n, a, b, q;
cin >> n >> q;
FOR(i, 0, n) cin >> arr[i];
Query qq;
vector<Query> qu;
FOR(i, 0, q) {
cin >> a >> b;
a--, b--;
qq.l = a, qq.r = b, qq.idx = i;
qu.pb(qq);
}
vector<int> res = mo_s_algorithm(qu);
for (int x : res) {
cout << x << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin>>t;
FOR(i, 1, t + 1) {
// cout<<"Case #"<<i<<": ";
solve();
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define endl "\n"
#define inf (long long)1000000007
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define bigint 1e18
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUF(v) shuffle(aint(v), RNG);
// Use mt19937_64 for 64 bit random numbers.
template <typename T> T ceil(T a, T b) { return (a + b - 1) / b; }
template <typename T> T binpow(T a, T b, T m) {
a %= m;
T res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
const int BLOCK = 700;
struct Query {
int l, r, idx;
};
bool cmp(Query x, Query 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;
}
int arr[511111], cnt[511111], ans = 0;
void add(int idx) {
cnt[arr[idx]]++;
if (cnt[arr[idx]] == 1) {
ans++;
}
}
void remove(int idx) {
cnt[arr[idx]]--;
if (cnt[arr[idx]] == 0) {
ans--;
}
}
vector<int> mo_s_algorithm(vector<Query> &queries) {
vector<int> res(queries.size());
sort(all(queries), cmp);
int cur_l = 0;
int cur_r = -1;
for (Query q : queries) {
while (cur_l > q.l) {
cur_l--;
add(cur_l);
}
while (cur_r < q.r) {
cur_r++;
add(cur_r);
}
while (cur_l < q.l) {
remove(cur_l);
cur_l++;
}
while (cur_r > q.r) {
remove(cur_r);
cur_r--;
}
res[q.idx] = ans;
}
return res;
}
void solve() {
int n, a, b, q;
cin >> n >> q;
FOR(i, 0, n) cin >> arr[i];
Query qq;
vector<Query> qu;
FOR(i, 0, q) {
cin >> a >> b;
a--, b--;
qq.l = a, qq.r = b, qq.idx = i;
qu.pb(qq);
}
vector<int> res = mo_s_algorithm(qu);
for (int x : res) {
cout << x << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin>>t;
FOR(i, 1, t + 1) {
// cout<<"Case #"<<i<<": ";
solve();
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| replace | 29 | 30 | 29 | 30 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #undef DEBUG // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T> void debug(T value) { std::cerr << value; }
template <typename T, typename... Ts> void debug(T value, Ts... args) {
std::cerr << value << ", ";
debug(args...);
}
#define dbg(...) \
do { \
cerr << #__VA_ARGS__ << ": "; \
debug(__VA_ARGS__); \
cerr << " (L" << __LINE__ << ")" << endl; \
} while (0)
#else
#define dbg(...)
#endif
void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
std::cin >> value;
read_from_cin(args...);
}
#define in(type, ...) \
type __VA_ARGS__; \
read_from_cin(__VA_ARGS__);
template <typename T> void write_to_cout(const T &value) {
std::cout << value << '\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T &value, const Ts &...args) {
std::cout << value << ' ';
write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using ll = long long;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, class DS> class Mo {
public:
Mo(DS &ds) : ds_(ds), index_(0), prev_(-1) {}
std::pair<int, T> ProcessQuery() {
if (prev_ == -1) {
int bin = sqrt(index_);
sort(queries_.begin(), queries_.end(),
[&bin](const Query &a, const Query &b) {
int ba = a.begin / bin, bb = b.begin / bin;
if (ba != bb)
return ba < bb;
return a.end < b.end;
if (ba % 2 == 0) {
return a.end < b.end;
} else {
return a.end > b.end;
}
});
prev_ = 0;
const Query &q = queries_[0];
Add(q.begin, q.end);
return {q.index, ds_.Get()};
}
const Query &p = queries_[prev_];
const Query &c = queries_[prev_ + 1];
++prev_;
Add(c.begin, p.begin);
Del(p.begin, c.begin);
Add(p.end, c.end);
Del(c.end, p.end);
return {c.index, ds_.Get()};
}
void AddQuery(int begin, int end) {
queries_.push_back({begin, end, index_++});
}
private:
struct Query {
int begin, end, index;
};
void Add(int begin, int end) {
for (int i = begin; i < end; ++i) {
ds_.Add(i);
}
}
void Del(int begin, int end) {
for (int i = begin; i < end; ++i) {
ds_.Del(i);
}
}
DS &ds_;
int index_, prev_;
std::vector<Query> queries_;
};
using namespace std;
struct DS {
DS(int n) : c(n), kinds(n), unique_kinds(0) {}
void Add(int i) {
if (kinds[c[i] - 1]++ == 0) {
++unique_kinds;
}
}
void Del(int i) {
if (--kinds[c[i] - 1] == 0) {
--unique_kinds;
}
}
int Get() const { return unique_kinds; }
V<int> c;
V<int> kinds;
int unique_kinds;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
in(int, n, q);
DS ds(n);
rep(i, n) cin >> ds.c[i];
Mo<int, DS> mo(ds);
rep(i, q) {
in(int, l, r);
mo.AddQuery(l - 1, r);
}
vector<int> ans(q);
rep(i, q) {
auto [idx, x] = mo.ProcessQuery();
ans[idx] = x;
}
rep(i, q) out(ans[i]);
}
| #include <bits/stdc++.h>
// #undef DEBUG // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T> void debug(T value) { std::cerr << value; }
template <typename T, typename... Ts> void debug(T value, Ts... args) {
std::cerr << value << ", ";
debug(args...);
}
#define dbg(...) \
do { \
cerr << #__VA_ARGS__ << ": "; \
debug(__VA_ARGS__); \
cerr << " (L" << __LINE__ << ")" << endl; \
} while (0)
#else
#define dbg(...)
#endif
void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
std::cin >> value;
read_from_cin(args...);
}
#define in(type, ...) \
type __VA_ARGS__; \
read_from_cin(__VA_ARGS__);
template <typename T> void write_to_cout(const T &value) {
std::cout << value << '\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T &value, const Ts &...args) {
std::cout << value << ' ';
write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using ll = long long;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, class DS> class Mo {
public:
Mo(DS &ds) : ds_(ds), index_(0), prev_(-1) {}
std::pair<int, T> ProcessQuery() {
if (prev_ == -1) {
int bin = sqrt(index_);
sort(queries_.begin(), queries_.end(),
[&bin](const Query &a, const Query &b) {
int ba = a.begin / bin, bb = b.begin / bin;
if (ba != bb)
return ba < bb;
// return a.end < b.end;
if (ba % 2 == 0) {
return a.end < b.end;
} else {
return a.end > b.end;
}
});
prev_ = 0;
const Query &q = queries_[0];
Add(q.begin, q.end);
return {q.index, ds_.Get()};
}
const Query &p = queries_[prev_];
const Query &c = queries_[prev_ + 1];
++prev_;
Add(c.begin, p.begin);
Del(p.begin, c.begin);
Add(p.end, c.end);
Del(c.end, p.end);
return {c.index, ds_.Get()};
}
void AddQuery(int begin, int end) {
queries_.push_back({begin, end, index_++});
}
private:
struct Query {
int begin, end, index;
};
void Add(int begin, int end) {
for (int i = begin; i < end; ++i) {
ds_.Add(i);
}
}
void Del(int begin, int end) {
for (int i = begin; i < end; ++i) {
ds_.Del(i);
}
}
DS &ds_;
int index_, prev_;
std::vector<Query> queries_;
};
using namespace std;
struct DS {
DS(int n) : c(n), kinds(n), unique_kinds(0) {}
void Add(int i) {
if (kinds[c[i] - 1]++ == 0) {
++unique_kinds;
}
}
void Del(int i) {
if (--kinds[c[i] - 1] == 0) {
--unique_kinds;
}
}
int Get() const { return unique_kinds; }
V<int> c;
V<int> kinds;
int unique_kinds;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
in(int, n, q);
DS ds(n);
rep(i, n) cin >> ds.c[i];
Mo<int, DS> mo(ds);
rep(i, q) {
in(int, l, r);
mo.AddQuery(l - 1, r);
}
vector<int> ans(q);
rep(i, q) {
auto [idx, x] = mo.ProcessQuery();
ans[idx] = x;
}
rep(i, q) out(ans[i]);
}
| replace | 58 | 59 | 58 | 59 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #undef DEBUG // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T> void debug(T value) { std::cerr << value; }
template <typename T, typename... Ts> void debug(T value, Ts... args) {
std::cerr << value << ", ";
debug(args...);
}
#define dbg(...) \
do { \
cerr << #__VA_ARGS__ << ": "; \
debug(__VA_ARGS__); \
cerr << " (L" << __LINE__ << ")" << endl; \
} while (0)
#else
#define dbg(...)
#endif
void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
std::cin >> value;
read_from_cin(args...);
}
#define in(type, ...) \
type __VA_ARGS__; \
read_from_cin(__VA_ARGS__);
template <typename T> void write_to_cout(const T &value) {
std::cout << value << std::endl;
}
template <typename T, typename... Ts>
void write_to_cout(const T &value, const Ts &...args) {
std::cout << value << ' ';
write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using ll = long long;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;
using namespace std;
using P = pair<int, int>;
struct Q {
P lr;
int bucket;
int i;
int kinds;
};
void Erase(map<int, int> &m, int v) {
auto it = m.find(v);
assert(it != m.end());
--it->second;
if (it->second == 0) {
m.erase(it);
}
}
int main() {
in(int, n, q);
V<int> c(n);
rep(i, n) cin >> c[i];
auto bucket = [](int i) { return int(sqrt(i)); };
V<Q> qs(q);
rep(i, q) {
cin >> qs[i].lr.first >> qs[i].lr.second;
--qs[i].lr.first;
--qs[i].lr.second;
qs[i].i = i;
qs[i].bucket = bucket(qs[i].lr.first);
}
sort(all(qs), [](const Q &l, const Q &r) {
if (l.bucket != r.bucket) {
return l.bucket < r.bucket;
}
return l.lr.second < r.lr.second;
});
vector<int> kinds(n + 1);
int n_kinds = 0;
auto add = [&](int i) {
if (kinds[c[i]] == 0) {
++n_kinds;
}
++kinds[c[i]];
};
auto del = [&](int i) {
--kinds[c[i]];
if (kinds[c[i]] == 0) {
--n_kinds;
}
};
rep(i, q) {
auto [L, R] = qs[i].lr;
if (i == 0) {
for (int j = L; j <= R; ++j) {
add(j);
}
} else {
auto [PL, PR] = qs[i - 1].lr;
if (PL < L) {
for (int j = PL; j < L; ++j) {
del(j);
}
} else {
for (int j = L; j < PL; ++j) {
add(j);
}
}
if (PR < R) {
for (int j = R; j > PR; --j) {
add(j);
}
} else {
for (int j = PR; j > R; --j) {
del(j);
}
}
}
qs[i].kinds = n_kinds;
}
sort(all(qs), [](const Q &l, const Q &r) { return l.i < r.i; });
rep(i, q) { out(qs[i].kinds); }
/*
rep(i, n) {
int b = bucket(i);
if (i > 0) {
int pb = bucket(i - 1);
if (pb < b) {
buckets[b] = buckets[pb];
}
}
buckets[b].insert(c[i]);
}
return 0;
int lri = 0;
while (lri < q) {
}
*/
} | #include <bits/stdc++.h>
// #undef DEBUG // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T> void debug(T value) { std::cerr << value; }
template <typename T, typename... Ts> void debug(T value, Ts... args) {
std::cerr << value << ", ";
debug(args...);
}
#define dbg(...) \
do { \
cerr << #__VA_ARGS__ << ": "; \
debug(__VA_ARGS__); \
cerr << " (L" << __LINE__ << ")" << endl; \
} while (0)
#else
#define dbg(...)
#endif
void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
std::cin >> value;
read_from_cin(args...);
}
#define in(type, ...) \
type __VA_ARGS__; \
read_from_cin(__VA_ARGS__);
template <typename T> void write_to_cout(const T &value) {
std::cout << value << std::endl;
}
template <typename T, typename... Ts>
void write_to_cout(const T &value, const Ts &...args) {
std::cout << value << ' ';
write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using ll = long long;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;
using namespace std;
using P = pair<int, int>;
struct Q {
P lr;
int bucket;
int i;
int kinds;
};
void Erase(map<int, int> &m, int v) {
auto it = m.find(v);
assert(it != m.end());
--it->second;
if (it->second == 0) {
m.erase(it);
}
}
int main() {
in(int, n, q);
V<int> c(n);
rep(i, n) cin >> c[i];
auto bucket = [](int i) { return int(sqrt(i)); };
V<Q> qs(q);
rep(i, q) {
cin >> qs[i].lr.first >> qs[i].lr.second;
--qs[i].lr.first;
--qs[i].lr.second;
qs[i].i = i;
qs[i].bucket = bucket(qs[i].lr.first);
}
sort(all(qs), [](const Q &l, const Q &r) {
if (l.bucket != r.bucket) {
return l.bucket < r.bucket;
}
return l.lr.second < r.lr.second;
});
vector<int> kinds(n + 1);
int n_kinds = 0;
auto add = [&](int i) {
if (kinds[c[i]] == 0) {
++n_kinds;
}
++kinds[c[i]];
};
auto del = [&](int i) {
--kinds[c[i]];
if (kinds[c[i]] == 0) {
--n_kinds;
}
};
rep(i, q) {
auto [L, R] = qs[i].lr;
if (i == 0 || qs[i].bucket != qs[i - 1].bucket) {
n_kinds = 0;
rep(i, n) kinds[i + 1] = 0;
for (int j = L; j <= R; ++j) {
add(j);
}
} else {
auto [PL, PR] = qs[i - 1].lr;
if (PL < L) {
for (int j = PL; j < L; ++j) {
del(j);
}
} else {
for (int j = L; j < PL; ++j) {
add(j);
}
}
if (PR < R) {
for (int j = R; j > PR; --j) {
add(j);
}
} else {
for (int j = PR; j > R; --j) {
del(j);
}
}
}
qs[i].kinds = n_kinds;
}
sort(all(qs), [](const Q &l, const Q &r) { return l.i < r.i; });
rep(i, q) { out(qs[i].kinds); }
/*
rep(i, n) {
int b = bucket(i);
if (i > 0) {
int pb = bucket(i - 1);
if (pb < b) {
buckets[b] = buckets[pb];
}
}
buckets[b].insert(c[i]);
}
return 0;
int lri = 0;
while (lri < q) {
}
*/
} | replace | 105 | 106 | 105 | 108 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pi pair<int, int>
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define ln(x) ((int)x.length())
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define dbg(x) cout << #x << ": " << x << endl
#define clr(x, v) memset(x, v, sizeof(x))
#define FASTIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
const int MAXN = 5e5 + 5;
struct query {
int l, r, idx;
};
int arr[MAXN], t[4 * MAXN], last[MAXN], ans[MAXN];
vector<query> queries;
int update(int v, int tl, int tr, int pos, int val) {
if (tl == tr)
t[v] += val;
else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(2 * v, tl, tm, pos, val);
else
update(2 * v + 1, tm + 1, tr, pos, val);
t[v] = t[2 * v] + t[2 * v + 1];
}
}
int sum(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (tl == l && tr == r) {
return t[v];
} else {
int tm = (tl + tr) / 2;
return sum(2 * v, tl, tm, l, min(r, tm)) +
sum(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}
}
void cp() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--, r--;
queries.pb({l, r, i});
}
sort(all(queries), [&](query a, query b) { return a.r < b.r; });
clr(last, -1);
int pos = 0;
for (int i = 0; i < n; i++) {
if (last[arr[i]] != -1)
update(1, 0, n - 1, last[arr[i]], -1);
// reduce 1 at pos last[arr[i]]
last[arr[i]] = i;
update(1, 0, n - 1, last[arr[i]], 1);
while (pos < q && queries[pos].r == i)
ans[queries[pos].idx] = sum(1, 0, n - 1, queries[pos].l, queries[pos].r),
pos++;
}
for (int i = 0; i < q; i++)
cout << ans[i] << endl;
}
int main() {
FASTIO;
int t;
t = 1;
// cin >> t;
while (t--) {
cp();
}
return 0;
} | #include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pi pair<int, int>
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define ln(x) ((int)x.length())
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define dbg(x) cout << #x << ": " << x << endl
#define clr(x, v) memset(x, v, sizeof(x))
#define FASTIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
const int MAXN = 5e5 + 5;
struct query {
int l, r, idx;
};
int arr[MAXN], t[4 * MAXN], last[MAXN], ans[MAXN];
vector<query> queries;
void update(int v, int tl, int tr, int pos, int val) {
if (tl == tr)
t[v] += val;
else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(2 * v, tl, tm, pos, val);
else
update(2 * v + 1, tm + 1, tr, pos, val);
t[v] = t[2 * v] + t[2 * v + 1];
}
}
int sum(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (tl == l && tr == r) {
return t[v];
} else {
int tm = (tl + tr) / 2;
return sum(2 * v, tl, tm, l, min(r, tm)) +
sum(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}
}
void cp() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--, r--;
queries.pb({l, r, i});
}
sort(all(queries), [&](query a, query b) { return a.r < b.r; });
clr(last, -1);
int pos = 0;
for (int i = 0; i < n; i++) {
if (last[arr[i]] != -1)
update(1, 0, n - 1, last[arr[i]], -1);
// reduce 1 at pos last[arr[i]]
last[arr[i]] = i;
update(1, 0, n - 1, last[arr[i]], 1);
while (pos < q && queries[pos].r == i)
ans[queries[pos].idx] = sum(1, 0, n - 1, queries[pos].l, queries[pos].r),
pos++;
}
for (int i = 0; i < q; i++)
cout << ans[i] << endl;
}
int main() {
FASTIO;
int t;
t = 1;
// cin >> t;
while (t--) {
cp();
}
return 0;
} | replace | 32 | 33 | 32 | 33 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
struct node {
int l, r, s, d;
} t[500005];
void pushup(int x) {
t[x].s = t[x << 1].s + t[(x << 1) + 1].s;
return;
}
void pushdown(int x) {
int mid = (t[x].l + t[x].r) >> 1;
int d = t[x].d;
t[x << 1].d += d;
t[(x << 1) + 1].d += d;
t[x << 1].s += (mid - t[x].l + 1) * d;
t[(x << 1) + 1].s += (t[x].r - mid) * d;
return;
}
void build(int i, int l, int r) {
t[i].l = l;
t[i].r = r;
t[i].d = 0;
if (l == r) {
t[i].s = 0;
return;
}
int mid = l + r >> 1;
build(i << 1, l, mid);
build((i << 1) + 1, mid + 1, r);
pushup(i);
return;
}
int getsum(int i, int l, int r) {
int mid = t[i].l + t[i].r >> 1;
if (l <= t[i].l && t[i].r <= r) {
return t[i].s;
}
int d = t[i].d;
if (!d)
pushdown(i);
int ret = 0;
if (mid >= l)
ret += getsum(i << 1, l, r);
if (mid < r)
ret += getsum((i << 1) + 1, l, r);
return ret;
}
void update(int i, int l, int r, int del) {
if (t[i].l == t[i].r) {
t[i].d += del;
t[i].s += (t[i].l - t[i].r + 1) * del;
return;
}
int mid = t[i].l + t[i].r >> 1;
if (!t[i].d)
pushdown(i);
if (mid >= l)
update(i << 1, l, r, del);
if (mid < r)
update((i << 1) + 1, l, r, del);
pushup(i);
}
int main() {
int n, q;
cin >> n >> q;
vector<int> vc(n + 1, 0);
for (int i = 0; i < n; i++) {
scanf("%d", &vc[i + 1]);
}
vector<pair<pair<int, int>, int>> vp;
vector<int> res(q + 1, 0);
for (int i = 0; i < q; i++) {
int l, r;
scanf("%d%d", &l, &r);
vp.push_back(make_pair(make_pair(r, l), i + 1));
}
sort(vp.begin(), vp.end());
build(1, 1, n);
vector<int> last(n + 1, -1);
int id = 1;
for (int i = 0; i < q; i++) {
int l = vp[i].first.second, r = vp[i].first.first;
int index = vp[i].second;
while (id <= r) {
int c = vc[id];
if (last[c] == -1) {
last[c] = id;
// update_tree(um, id, 1, 1, n);
update(1, id, id, 1);
} else {
// update_tree(um, last[c], -1, 1, n);
// update_tree(um, id, 1, 1, n);
update(1, last[c], last[c], -1);
update(1, id, id, 1);
last[c] = id;
}
id++;
}
// res[index] = query_tree(um, l, r, 1, n);
res[index] = getsum(1, l, r);
}
for (int i = 1; i <= q; i++) {
printf("%d\n", res[i]);
}
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
struct node {
int l, r, s, d;
} t[2000005];
void pushup(int x) {
t[x].s = t[x << 1].s + t[(x << 1) + 1].s;
return;
}
void pushdown(int x) {
int mid = (t[x].l + t[x].r) >> 1;
int d = t[x].d;
t[x << 1].d += d;
t[(x << 1) + 1].d += d;
t[x << 1].s += (mid - t[x].l + 1) * d;
t[(x << 1) + 1].s += (t[x].r - mid) * d;
return;
}
void build(int i, int l, int r) {
t[i].l = l;
t[i].r = r;
t[i].d = 0;
if (l == r) {
t[i].s = 0;
return;
}
int mid = l + r >> 1;
build(i << 1, l, mid);
build((i << 1) + 1, mid + 1, r);
pushup(i);
return;
}
int getsum(int i, int l, int r) {
int mid = t[i].l + t[i].r >> 1;
if (l <= t[i].l && t[i].r <= r) {
return t[i].s;
}
int d = t[i].d;
if (!d)
pushdown(i);
int ret = 0;
if (mid >= l)
ret += getsum(i << 1, l, r);
if (mid < r)
ret += getsum((i << 1) + 1, l, r);
return ret;
}
void update(int i, int l, int r, int del) {
if (t[i].l == t[i].r) {
t[i].d += del;
t[i].s += (t[i].l - t[i].r + 1) * del;
return;
}
int mid = t[i].l + t[i].r >> 1;
if (!t[i].d)
pushdown(i);
if (mid >= l)
update(i << 1, l, r, del);
if (mid < r)
update((i << 1) + 1, l, r, del);
pushup(i);
}
int main() {
int n, q;
cin >> n >> q;
vector<int> vc(n + 1, 0);
for (int i = 0; i < n; i++) {
scanf("%d", &vc[i + 1]);
}
vector<pair<pair<int, int>, int>> vp;
vector<int> res(q + 1, 0);
for (int i = 0; i < q; i++) {
int l, r;
scanf("%d%d", &l, &r);
vp.push_back(make_pair(make_pair(r, l), i + 1));
}
sort(vp.begin(), vp.end());
build(1, 1, n);
vector<int> last(n + 1, -1);
int id = 1;
for (int i = 0; i < q; i++) {
int l = vp[i].first.second, r = vp[i].first.first;
int index = vp[i].second;
while (id <= r) {
int c = vc[id];
if (last[c] == -1) {
last[c] = id;
// update_tree(um, id, 1, 1, n);
update(1, id, id, 1);
} else {
// update_tree(um, last[c], -1, 1, n);
// update_tree(um, id, 1, 1, n);
update(1, last[c], last[c], -1);
update(1, id, id, 1);
last[c] = id;
}
id++;
}
// res[index] = query_tree(um, l, r, 1, n);
res[index] = getsum(1, l, r);
}
for (int i = 1; i <= q; i++) {
printf("%d\n", res[i]);
}
}
| replace | 14 | 15 | 14 | 15 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define ll long long
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000; // 1e18
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = acos(-1.0);
const double EPS = 1e-10;
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;
}
// template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};
/* ----------------------- DEBUG FUNCTION ---------------------------- */
#define DUMPOUT cerr
void dump_function() { DUMPOUT << ' '; }
void dump_function(bool a) { DUMPOUT << a; }
void dump_function(int a) { DUMPOUT << a; }
void dump_function(long long a) { DUMPOUT << a; }
void dump_function(char a) { DUMPOUT << a; }
void dump_function(string &a) { DUMPOUT << a; }
void dump_function(double a) { DUMPOUT << a; }
template <class T> void dump_function(const vector<T> &);
template <class T, size_t size> void dump_function(const array<T, size> &);
template <class T, class L> void dump_function(const pair<T, L> &p);
template <class T, size_t size> void dump_function(const T (&)[size]);
template <class T> void dump_function(const vector<T> &a) {
if (a.empty())
return;
dump_function(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
DUMPOUT << " ";
dump_function(*i);
}
DUMPOUT << endl;
}
template <class T> void dump_function(const deque<T> &a) {
if (a.empty())
return;
dump_function(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
DUMPOUT << " ";
dump_function(*i);
}
}
template <class T, size_t size> void dump_function(const array<T, size> &a) {
dump_function(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
DUMPOUT << " ";
dump_function(*i);
}
}
template <class T, class L> void dump_function(const pair<T, L> &p) {
DUMPOUT << '(';
dump_function(p.first);
DUMPOUT << ",";
dump_function(p.second);
DUMPOUT << ')';
}
template <class T> void dump_function(set<T> &x) {
for (auto e : x)
dump_function(e), DUMPOUT << " ";
DUMPOUT << endl;
}
template <class T> void dump_function(multiset<T> &x) {
for (auto e : x)
dump_function(e), DUMPOUT << " ";
DUMPOUT << endl;
}
template <class T, size_t size> void dump_function(const T (&a)[size]) {
dump_function(a[0]);
for (auto i = a; ++i != end(a);) {
DUMPOUT << " ";
dump_function(*i);
}
}
template <class T> void dump_function(const T &a) { DUMPOUT << a; }
int dump_out() {
DUMPOUT << '\n';
return 0;
}
template <class T> int dump_out(const T &t) {
dump_function(t);
DUMPOUT << '\n';
return 0;
}
template <class Head, class... Tail>
int dump_out(const Head &head, const Tail &...tail) {
dump_function(head);
DUMPOUT << ' ';
dump_out(tail...);
return 0;
}
#ifdef DEBUG_
#define dump(x) \
DUMPOUT << #x << ": "; \
dump_function(x); \
DUMPOUT << endl;
void dumps() {}
template <class T> void dumps(const T &t) {
dump_function(t);
DUMPOUT << " ";
}
template <class Head, class... Tail>
void dumps(const Head &head, const Tail &...tail) {
dump_function(head);
DUMPOUT << ' ';
dump_out(tail...);
}
#else
#define dump(x)
template <class... T> void dumps(const T &...) {}
#endif
/* ----------------------- DEBUG FUNCTION ---------------------------- */
template <int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0)
val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
constexpr Fp &operator+=(const Fp &r) noexcept {
val += r.val;
if (val >= MOD)
val -= MOD;
return *this;
}
constexpr Fp &operator-=(const Fp &r) noexcept {
val -= r.val;
if (val < 0)
val += MOD;
return *this;
}
constexpr Fp &operator*=(const Fp &r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp &operator/=(const Fp &r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
constexpr bool operator==(const Fp &r) const noexcept {
return this->val == r.val;
}
constexpr bool operator!=(const Fp &r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0)
return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1)
t = t * a;
return t;
}
};
template <class T> struct BiCoef {
vector<T> fact_, inv_, finv_;
constexpr BiCoef() {}
constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
init(n);
}
constexpr void init(int n) noexcept {
fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
int MOD = fact_[0].getmod();
for (int i = 2; i < n; i++) {
fact_[i] = fact_[i - 1] * i;
inv_[i] = -inv_[MOD % i] * (MOD / i);
finv_[i] = finv_[i - 1] * inv_[i];
}
}
constexpr T com(int n, int k) const noexcept {
if (n < k || n < 0 || k < 0)
return 0;
return fact_[n] * finv_[k] * finv_[n - k];
}
constexpr T fact(int n) const noexcept {
if (n < 0)
return 0;
return fact_[n];
}
constexpr T inv(int n) const noexcept {
if (n < 0)
return 0;
return inv_[n];
}
constexpr T finv(int n) const noexcept {
if (n < 0)
return 0;
return finv_[n];
}
constexpr T perm(int n, int k) const noexcept {
if (n < k || n < 0 || k < 0)
return 0;
return fact_[n] * finv_[n - k];
}
};
using mint = Fp<MOD>;
BiCoef<mint> bc;
// using vec = vector<mint>;
// using mat = vector<vec>;
struct Mo {
vector<int> left, right, order;
vector<bool> v;
vector<int> A;
vector<int> cnt;
vector<int> ans;
int num;
int width;
int nl, nr;
Mo(int n, int q, vector<int> a)
: width((int)sqrt(n)), order(q), cnt(n, 0), A(a), ans(q), nl(0), nr(0),
num(0), v(n) {
iota(begin(order), end(order), 0);
}
/* [l, r) */
void insert(int l, int r) {
left.push_back(l);
right.push_back(r);
}
/* クエリをソートし、操作を行う */
void build() {
assert(left.size() == order.size());
sort(begin(order), end(order), [&](int a, int b) {
if (left[a] / width != left[b] / width)
return left[a] < left[b];
return right[a] < right[b];
});
for (auto idx : order)
process(idx);
}
/* クエリを1つ分進めて, ansに入れる */
void process(int idx) {
// if(ptr == order.size()) return (-1);
// const auto id = order[ptr];
while (nl > left[idx])
distribute(--nl);
while (nr < right[idx])
distribute(nr++);
while (nl < left[idx])
distribute(nl++);
while (nr > right[idx])
distribute(--nr);
ans[idx] = num;
// return (order[ptr++]);
}
inline void distribute(int idx) {
v[idx].flip();
if (v[idx])
add(idx);
else
del(idx);
}
/* add と delを問題ごとに書く */
/* add(idx) : A[idx]を区間に新たに加えるとどうなるかを更新する */
/* del(idx) : A[idx]を区間から除くとどうなるかを更新 */
void add(int idx) {
if (cnt[A[idx]]++ == 0)
++num;
}
void del(int idx) {
if (--cnt[A[idx]] == 0)
--num;
}
};
void solve() {
int N, Q;
cin >> N >> Q;
vector<int> A(N);
REP(i, N) cin >> A[i];
Mo mo(N, Q, A);
REP(i, Q) {
int l, r;
cin >> l >> r;
mo.insert(--l, r);
}
mo.build();
for (auto x : mo.ans)
cout << x << '\n';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
// int T; cin >> T; REP(t,T) solve();
} | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define ll long long
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000; // 1e18
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = acos(-1.0);
const double EPS = 1e-10;
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;
}
// template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};
/* ----------------------- DEBUG FUNCTION ---------------------------- */
#define DUMPOUT cerr
void dump_function() { DUMPOUT << ' '; }
void dump_function(bool a) { DUMPOUT << a; }
void dump_function(int a) { DUMPOUT << a; }
void dump_function(long long a) { DUMPOUT << a; }
void dump_function(char a) { DUMPOUT << a; }
void dump_function(string &a) { DUMPOUT << a; }
void dump_function(double a) { DUMPOUT << a; }
template <class T> void dump_function(const vector<T> &);
template <class T, size_t size> void dump_function(const array<T, size> &);
template <class T, class L> void dump_function(const pair<T, L> &p);
template <class T, size_t size> void dump_function(const T (&)[size]);
template <class T> void dump_function(const vector<T> &a) {
if (a.empty())
return;
dump_function(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
DUMPOUT << " ";
dump_function(*i);
}
DUMPOUT << endl;
}
template <class T> void dump_function(const deque<T> &a) {
if (a.empty())
return;
dump_function(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
DUMPOUT << " ";
dump_function(*i);
}
}
template <class T, size_t size> void dump_function(const array<T, size> &a) {
dump_function(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
DUMPOUT << " ";
dump_function(*i);
}
}
template <class T, class L> void dump_function(const pair<T, L> &p) {
DUMPOUT << '(';
dump_function(p.first);
DUMPOUT << ",";
dump_function(p.second);
DUMPOUT << ')';
}
template <class T> void dump_function(set<T> &x) {
for (auto e : x)
dump_function(e), DUMPOUT << " ";
DUMPOUT << endl;
}
template <class T> void dump_function(multiset<T> &x) {
for (auto e : x)
dump_function(e), DUMPOUT << " ";
DUMPOUT << endl;
}
template <class T, size_t size> void dump_function(const T (&a)[size]) {
dump_function(a[0]);
for (auto i = a; ++i != end(a);) {
DUMPOUT << " ";
dump_function(*i);
}
}
template <class T> void dump_function(const T &a) { DUMPOUT << a; }
int dump_out() {
DUMPOUT << '\n';
return 0;
}
template <class T> int dump_out(const T &t) {
dump_function(t);
DUMPOUT << '\n';
return 0;
}
template <class Head, class... Tail>
int dump_out(const Head &head, const Tail &...tail) {
dump_function(head);
DUMPOUT << ' ';
dump_out(tail...);
return 0;
}
#ifdef DEBUG_
#define dump(x) \
DUMPOUT << #x << ": "; \
dump_function(x); \
DUMPOUT << endl;
void dumps() {}
template <class T> void dumps(const T &t) {
dump_function(t);
DUMPOUT << " ";
}
template <class Head, class... Tail>
void dumps(const Head &head, const Tail &...tail) {
dump_function(head);
DUMPOUT << ' ';
dump_out(tail...);
}
#else
#define dump(x)
template <class... T> void dumps(const T &...) {}
#endif
/* ----------------------- DEBUG FUNCTION ---------------------------- */
template <int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0)
val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
constexpr Fp &operator+=(const Fp &r) noexcept {
val += r.val;
if (val >= MOD)
val -= MOD;
return *this;
}
constexpr Fp &operator-=(const Fp &r) noexcept {
val -= r.val;
if (val < 0)
val += MOD;
return *this;
}
constexpr Fp &operator*=(const Fp &r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp &operator/=(const Fp &r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
constexpr bool operator==(const Fp &r) const noexcept {
return this->val == r.val;
}
constexpr bool operator!=(const Fp &r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0)
return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1)
t = t * a;
return t;
}
};
template <class T> struct BiCoef {
vector<T> fact_, inv_, finv_;
constexpr BiCoef() {}
constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
init(n);
}
constexpr void init(int n) noexcept {
fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
int MOD = fact_[0].getmod();
for (int i = 2; i < n; i++) {
fact_[i] = fact_[i - 1] * i;
inv_[i] = -inv_[MOD % i] * (MOD / i);
finv_[i] = finv_[i - 1] * inv_[i];
}
}
constexpr T com(int n, int k) const noexcept {
if (n < k || n < 0 || k < 0)
return 0;
return fact_[n] * finv_[k] * finv_[n - k];
}
constexpr T fact(int n) const noexcept {
if (n < 0)
return 0;
return fact_[n];
}
constexpr T inv(int n) const noexcept {
if (n < 0)
return 0;
return inv_[n];
}
constexpr T finv(int n) const noexcept {
if (n < 0)
return 0;
return finv_[n];
}
constexpr T perm(int n, int k) const noexcept {
if (n < k || n < 0 || k < 0)
return 0;
return fact_[n] * finv_[n - k];
}
};
using mint = Fp<MOD>;
BiCoef<mint> bc;
// using vec = vector<mint>;
// using mat = vector<vec>;
struct Mo {
vector<int> left, right, order;
vector<bool> v;
vector<int> A;
vector<int> cnt;
vector<int> ans;
int num;
int width;
int nl, nr;
Mo(int n, int q, vector<int> a)
: width((int)sqrt(n)), order(q), cnt(n, 0), A(a), ans(q), nl(0), nr(0),
num(0), v(n) {
iota(begin(order), end(order), 0);
}
/* [l, r) */
void insert(int l, int r) {
left.push_back(l);
right.push_back(r);
}
/* クエリをソートし、操作を行う */
void build() {
assert(left.size() == order.size());
sort(begin(order), end(order), [&](int a, int b) {
if (left[a] / width != left[b] / width)
return left[a] < left[b];
if ((left[a] / width) & 1)
return right[a] < right[b];
return right[a] > right[b];
});
for (auto idx : order)
process(idx);
}
/* クエリを1つ分進めて, ansに入れる */
void process(int idx) {
// if(ptr == order.size()) return (-1);
// const auto id = order[ptr];
while (nl > left[idx])
distribute(--nl);
while (nr < right[idx])
distribute(nr++);
while (nl < left[idx])
distribute(nl++);
while (nr > right[idx])
distribute(--nr);
ans[idx] = num;
// return (order[ptr++]);
}
inline void distribute(int idx) {
v[idx].flip();
if (v[idx])
add(idx);
else
del(idx);
}
/* add と delを問題ごとに書く */
/* add(idx) : A[idx]を区間に新たに加えるとどうなるかを更新する */
/* del(idx) : A[idx]を区間から除くとどうなるかを更新 */
void add(int idx) {
if (cnt[A[idx]]++ == 0)
++num;
}
void del(int idx) {
if (--cnt[A[idx]] == 0)
--num;
}
};
void solve() {
int N, Q;
cin >> N >> Q;
vector<int> A(N);
REP(i, N) cin >> A[i];
Mo mo(N, Q, A);
REP(i, Q) {
int l, r;
cin >> l >> r;
mo.insert(--l, r);
}
mo.build();
for (auto x : mo.ans)
cout << x << '\n';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
// int T; cin >> T; REP(t,T) solve();
} | replace | 271 | 272 | 271 | 274 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define ios \
; \
ios::sync_with_stdio(false); \
cin.tie(0);
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define ls(x) (x << 1)
#define rs(x) (ls(x) | 1)
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef double db;
mt19937 rnd(random_device{}());
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
const int M = 2e6 + 5;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline ll ksm(ll x, ll y) {
ll res = 1;
while (y) {
if (y & 1)
res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 20], sum[N * 20], vis[N * 20];
int n, q, k;
int id;
void build(int &o, int l, int r) {
o = ++id;
sum[o] = 0;
if (l == r)
return;
int m = (l + r) >> 1;
build(ls[o], l, m);
build(rs[o], m + 1, r);
}
void update(int &o, int l, int r, int last, int pos, int val) {
o = ++id;
ls[o] = ls[last];
rs[o] = rs[last];
sum[o] = sum[last] + val;
if (l == r)
return;
int m = (l + r) >> 1;
if (pos <= m)
update(ls[o], l, m, ls[last], pos, val);
else
update(rs[o], m + 1, r, rs[last], pos, val);
}
int query(int pos, int root, int l, int r) {
if (l == r)
return sum[root];
int m = (l + r) >> 1;
if (m >= pos)
return sum[rs[root]] + query(pos, ls[root], l, m);
else
return query(pos, rs[root], m + 1, r);
}
void solve() {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", query(l, rt[r], 1, n));
}
int main() {
memset(vis, 0, sizeof(vis));
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
id = 0;
build(rt[0], 1, n);
for (int i = 1; i <= n; i++) {
if (!vis[a[i]])
update(rt[i], 1, n, rt[i - 1], i, 1);
else {
int pos = vis[a[i]];
int temp;
update(temp, 1, n, rt[i - 1], pos, -1);
update(rt[i], 1, n, temp, i, 1);
}
vis[a[i]] = i;
}
while (q--)
solve();
return 0;
}
/*
a b a+b+c*2
a a+b*2+c*4 a+b+c*2
a+b+c*4
*/
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
| #include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define ios \
; \
ios::sync_with_stdio(false); \
cin.tie(0);
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define ls(x) (x << 1)
#define rs(x) (ls(x) | 1)
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef double db;
mt19937 rnd(random_device{}());
const int mod = 1e9 + 7;
const int N = 2e6 + 5;
const int M = 2e6 + 5;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline ll ksm(ll x, ll y) {
ll res = 1;
while (y) {
if (y & 1)
res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 20], sum[N * 20], vis[N * 20];
int n, q, k;
int id;
void build(int &o, int l, int r) {
o = ++id;
sum[o] = 0;
if (l == r)
return;
int m = (l + r) >> 1;
build(ls[o], l, m);
build(rs[o], m + 1, r);
}
void update(int &o, int l, int r, int last, int pos, int val) {
o = ++id;
ls[o] = ls[last];
rs[o] = rs[last];
sum[o] = sum[last] + val;
if (l == r)
return;
int m = (l + r) >> 1;
if (pos <= m)
update(ls[o], l, m, ls[last], pos, val);
else
update(rs[o], m + 1, r, rs[last], pos, val);
}
int query(int pos, int root, int l, int r) {
if (l == r)
return sum[root];
int m = (l + r) >> 1;
if (m >= pos)
return sum[rs[root]] + query(pos, ls[root], l, m);
else
return query(pos, rs[root], m + 1, r);
}
void solve() {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", query(l, rt[r], 1, n));
}
int main() {
memset(vis, 0, sizeof(vis));
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
id = 0;
build(rt[0], 1, n);
for (int i = 1; i <= n; i++) {
if (!vis[a[i]])
update(rt[i], 1, n, rt[i - 1], i, 1);
else {
int pos = vis[a[i]];
int temp;
update(temp, 1, n, rt[i - 1], pos, -1);
update(rt[i], 1, n, temp, i, 1);
}
vis[a[i]] = i;
}
while (q--)
solve();
return 0;
}
/*
a b a+b+c*2
a a+b*2+c*4 a+b+c*2
a+b+c*4
*/
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
| replace | 26 | 27 | 26 | 27 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 5e5 + 4;
int n, m;
int root[N], sum[N * 100], lc[N * 100], rc[N * 100];
int tim;
int nxt[N];
inline int read() {
int x = 0, f = 1;
char c = getchar();
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 pushup(int rt) { sum[rt] = sum[lc[rt]] + sum[rc[rt]]; }
inline void build(int &rt, int l, int r) {
rt = ++tim;
if (l == r)
return;
int mid = l + r >> 1;
build(lc[rt], l, mid);
build(rc[rt], mid + 1, r);
}
inline void modify(int &rt, int pre, int l, int r, int pos, int val) {
rt = ++tim;
lc[rt] = lc[pre];
rc[rt] = rc[pre];
sum[rt] = sum[pre] + val;
if (l == r)
return;
int mid = l + r >> 1;
if (pos <= mid)
modify(lc[rt], lc[pre], l, mid, pos, val);
else
modify(rc[rt], rc[pre], mid + 1, r, pos, val);
pushup(rt);
}
inline int query(int rt, int l, int r, int L) {
if (l == r)
return sum[rt];
int mid = l + r >> 1;
if (mid >= L)
return sum[rc[rt]] + query(lc[rt], l, mid, L);
else
return query(rc[rt], mid + 1, r, L);
}
int main() {
freopen("f3.txt", "r", stdin);
n = read(), m = read();
build(root[0], 1, n);
for (register int i = 1; i <= n; ++i) {
int x = read();
if (!nxt[x]) {
modify(root[i], root[i - 1], 1, n, i, 1);
} else {
int temp;
modify(temp, root[i - 1], 1, n, nxt[x], -1);
modify(root[i], temp, 1, n, i, 1);
}
nxt[x] = i;
}
while (m--) {
int x = read(), y = read();
printf("%d\n", query(root[y], 1, n, x));
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 5e5 + 4;
int n, m;
int root[N], sum[N * 100], lc[N * 100], rc[N * 100];
int tim;
int nxt[N];
inline int read() {
int x = 0, f = 1;
char c = getchar();
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 pushup(int rt) { sum[rt] = sum[lc[rt]] + sum[rc[rt]]; }
inline void build(int &rt, int l, int r) {
rt = ++tim;
if (l == r)
return;
int mid = l + r >> 1;
build(lc[rt], l, mid);
build(rc[rt], mid + 1, r);
}
inline void modify(int &rt, int pre, int l, int r, int pos, int val) {
rt = ++tim;
lc[rt] = lc[pre];
rc[rt] = rc[pre];
sum[rt] = sum[pre] + val;
if (l == r)
return;
int mid = l + r >> 1;
if (pos <= mid)
modify(lc[rt], lc[pre], l, mid, pos, val);
else
modify(rc[rt], rc[pre], mid + 1, r, pos, val);
pushup(rt);
}
inline int query(int rt, int l, int r, int L) {
if (l == r)
return sum[rt];
int mid = l + r >> 1;
if (mid >= L)
return sum[rc[rt]] + query(lc[rt], l, mid, L);
else
return query(rc[rt], mid + 1, r, L);
}
int main() {
// freopen("f3.txt","r",stdin);
n = read(), m = read();
build(root[0], 1, n);
for (register int i = 1; i <= n; ++i) {
int x = read();
if (!nxt[x]) {
modify(root[i], root[i - 1], 1, n, i, 1);
} else {
int temp;
modify(temp, root[i - 1], 1, n, nxt[x], -1);
modify(root[i], temp, 1, n, i, 1);
}
nxt[x] = i;
}
while (m--) {
int x = read(), y = read();
printf("%d\n", query(root[y], 1, n, x));
}
return 0;
} | replace | 56 | 57 | 56 | 57 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3e4, MAX_Q = 2e5, MAX_AI = 1e6, OFFSET = 10;
int N, Q, BLOCK_SIZE, current_ans, arr[MAX_N + OFFSET], ans[MAX_Q + OFFSET],
frec[MAX_AI + OFFSET];
struct Query {
int left, right, idx;
Query(){};
Query(int left_, int right_, int idx_)
: left(left_), right(right_), idx(idx_) {}
bool operator<(const Query &other) const {
int this_block = this->left / BLOCK_SIZE;
int other_block = other.left / BLOCK_SIZE;
if (this_block != other_block)
return this_block < other_block;
return this->right < other.right;
}
} query[MAX_Q + OFFSET];
void print() {
for (int i = 0; i < Q; i++)
printf("%d\n", ans[i]);
}
void add(const int &num) {
if (0 == frec[num])
current_ans++;
frec[num]++;
}
void remove(const int &num) {
frec[num]--;
if (0 == frec[num])
current_ans--;
}
void MoAlgorithm() {
BLOCK_SIZE = sqrt(N);
sort(query, query + Q);
int mo_left = 0, mo_right = -1;
for (int i = 0; i < Q; i++) {
while (mo_right < query[i].right)
add(arr[++mo_right]);
while (mo_right > query[i].right)
remove(arr[mo_right--]);
while (mo_left < query[i].left)
remove(arr[mo_left++]);
while (mo_left > query[i].left)
add(arr[--mo_left]);
ans[query[i].idx] = current_ans;
}
}
void readInput() {
scanf("%d %d", &N, &Q);
for (int i = 0; i < N; i++)
scanf("%d", arr + i);
for (int i = 0, left_, right_; i < Q; i++) {
scanf("%d %d", &left_, &right_);
query[i] = Query(--left_, --right_, i);
}
}
int main() {
readInput();
MoAlgorithm();
print();
return (0);
}
| #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e5 + 1, MAX_Q = 5e5 + 1, MAX_AI = 5e5 + 1, OFFSET = 10;
int N, Q, BLOCK_SIZE, current_ans, arr[MAX_N + OFFSET], ans[MAX_Q + OFFSET],
frec[MAX_AI + OFFSET];
struct Query {
int left, right, idx;
Query(){};
Query(int left_, int right_, int idx_)
: left(left_), right(right_), idx(idx_) {}
bool operator<(const Query &other) const {
int this_block = this->left / BLOCK_SIZE;
int other_block = other.left / BLOCK_SIZE;
if (this_block != other_block)
return this_block < other_block;
return this->right < other.right;
}
} query[MAX_Q + OFFSET];
void print() {
for (int i = 0; i < Q; i++)
printf("%d\n", ans[i]);
}
void add(const int &num) {
if (0 == frec[num])
current_ans++;
frec[num]++;
}
void remove(const int &num) {
frec[num]--;
if (0 == frec[num])
current_ans--;
}
void MoAlgorithm() {
BLOCK_SIZE = sqrt(N);
sort(query, query + Q);
int mo_left = 0, mo_right = -1;
for (int i = 0; i < Q; i++) {
while (mo_right < query[i].right)
add(arr[++mo_right]);
while (mo_right > query[i].right)
remove(arr[mo_right--]);
while (mo_left < query[i].left)
remove(arr[mo_left++]);
while (mo_left > query[i].left)
add(arr[--mo_left]);
ans[query[i].idx] = current_ans;
}
}
void readInput() {
scanf("%d %d", &N, &Q);
for (int i = 0; i < N; i++)
scanf("%d", arr + i);
for (int i = 0, left_, right_; i < Q; i++) {
scanf("%d %d", &left_, &right_);
query[i] = Query(--left_, --right_, i);
}
}
int main() {
readInput();
MoAlgorithm();
print();
return (0);
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02599 | C++ | Runtime Error | // This problem is the same as DQUERY: https://www.spoj.com/problems/DQUERY/
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int Int;
typedef pair<Int, Int> pii;
typedef pair<Int, double> pid;
typedef pair<double, double> pdd;
typedef pair<Int, pii> pip;
typedef pair<pii, Int> ppi;
typedef pair<double, Int> pdp;
typedef vector<Int> veci;
typedef vector<double> vecd;
typedef vector<int> veci;
typedef vector<ll> vecll;
typedef vector<double> vecd;
typedef vector<pii> vecpii;
typedef vector<ppi> vecppi;
typedef vector<veci> mati;
typedef vector<vecll> matll;
typedef vector<vecd> matd;
#define PB(x) push_back(x)
#define EB(x) emplace_back(x)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (x).size()
#define CLR(x) memset(x, 0, sizeof x)
#define pdebug() printf("%d\n", __LINE__)
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define FORO(i, n) REP(i, 0, (int)n - 1)
#define FORI(i, n) REP(i, 1, (int)n)
#define FORIT(i, t) for (auto i = t.begin(); i != t.end(); i++)
#define eps 1e-6
#define sqr(x) ((x) * (x))
#define dist(_a, _b) sqrt(sqr(_a.A - _b.A) + sqr(_a.B - _b.B))
#define norm(_a) sqrt(sqr(_a.A) + sqr(_a.B))
#define DEBUG 1
#if DEBUG
#define DUMP(a) \
do { \
std::cout << #a " = " << (a) << ", "; \
} while (false)
#define DUMPLN(a) \
do { \
std::cout << #a " = " << (a) << std::endl; \
} while (false)
#else
#define DUMP(a)
#define DUMPLN(a)
#endif
template <typename T, typename U> inline void getMin(T &a, U b) {
if (a > b)
a = b;
}
template <typename T, typename U> inline void getMax(T &a, U b) {
if (a < b)
a = b;
}
template <typename T> vector<T> getVector(const int n) { return vector<T>(n); }
template <typename T> vector<T> getVector(const int n, const T a) {
return vector<T>(n, a);
}
template <typename T> vector<T> getEmptyVector() { return vector<T>(); }
template <typename T> void appendAll(vector<T> &a, vector<T> b) {
a.insert(a.end(), b.begin(), b.end());
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
return os << "(" << a.first << ", " << a.second << ")";
};
#define X first
#define Y second
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const ll MOD = 1000000007;
const int inf = 1 << 27;
const ll llinf = 1LL << 50;
const int NIL = -inf;
int popcount(int x) { return __builtin_popcount(x); }
const int MAXN = 1 << 19;
const int MAXLOGN = 19;
int pos[MAXN];
int tree[MAXN];
struct NODE {
int v;
int L, R;
} dat[2 * MAXN * MAXLOGN];
int ndat;
int build(int l, int r) {
int idx = ndat++;
if (r - l == 1) {
dat[idx].v = 0;
dat[idx].L = dat[idx].R = -1;
} else {
dat[idx].v = 0;
dat[idx].L = build(l, (r + l) / 2);
dat[idx].R = build((r + l) / 2, r);
}
return idx;
}
int update(int ix, int l, int r, int k, int v) {
if (k == -1)
return -1;
int idx = ndat++;
int mid = (l + r) / 2;
if (ix < mid) {
dat[idx].L = update(ix, l, (l + r) / 2, dat[k].L, v);
dat[idx].R = dat[k].R;
} else {
dat[idx].L = dat[k].L;
dat[idx].R = update(ix, (l + r) / 2, r, dat[k].R, v);
}
dat[idx].v = dat[k].v + v;
return idx;
}
int query(int lo, int l, int r, int id) {
if (r <= lo)
return 0;
if (lo <= l) {
return dat[id].v;
} else {
return query(lo, l, (l + r) / 2, dat[id].L) +
query(lo, (l + r) / 2, r, dat[id].R);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, Q;
cin >> N >> Q;
tree[0] = build(1, N + 1);
for (int i = 1; i <= N; i++) {
int x;
cin >> x;
int posx = pos[x];
if (posx != 0) {
tree[i] = update(i, 1, N + 1, update(posx, 1, N + 1, tree[i - 1], -1), 1);
} else {
tree[i] = update(i, 1, N + 1, tree[i - 1], 1);
}
pos[x] = i;
}
for (int qq = 0; qq < Q; qq++) {
int l, r;
cin >> l >> r;
cout << query(l, 1, N + 1, tree[r]) << endl;
}
return 0;
} | // This problem is the same as DQUERY: https://www.spoj.com/problems/DQUERY/
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int Int;
typedef pair<Int, Int> pii;
typedef pair<Int, double> pid;
typedef pair<double, double> pdd;
typedef pair<Int, pii> pip;
typedef pair<pii, Int> ppi;
typedef pair<double, Int> pdp;
typedef vector<Int> veci;
typedef vector<double> vecd;
typedef vector<int> veci;
typedef vector<ll> vecll;
typedef vector<double> vecd;
typedef vector<pii> vecpii;
typedef vector<ppi> vecppi;
typedef vector<veci> mati;
typedef vector<vecll> matll;
typedef vector<vecd> matd;
#define PB(x) push_back(x)
#define EB(x) emplace_back(x)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (x).size()
#define CLR(x) memset(x, 0, sizeof x)
#define pdebug() printf("%d\n", __LINE__)
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define FORO(i, n) REP(i, 0, (int)n - 1)
#define FORI(i, n) REP(i, 1, (int)n)
#define FORIT(i, t) for (auto i = t.begin(); i != t.end(); i++)
#define eps 1e-6
#define sqr(x) ((x) * (x))
#define dist(_a, _b) sqrt(sqr(_a.A - _b.A) + sqr(_a.B - _b.B))
#define norm(_a) sqrt(sqr(_a.A) + sqr(_a.B))
#define DEBUG 1
#if DEBUG
#define DUMP(a) \
do { \
std::cout << #a " = " << (a) << ", "; \
} while (false)
#define DUMPLN(a) \
do { \
std::cout << #a " = " << (a) << std::endl; \
} while (false)
#else
#define DUMP(a)
#define DUMPLN(a)
#endif
template <typename T, typename U> inline void getMin(T &a, U b) {
if (a > b)
a = b;
}
template <typename T, typename U> inline void getMax(T &a, U b) {
if (a < b)
a = b;
}
template <typename T> vector<T> getVector(const int n) { return vector<T>(n); }
template <typename T> vector<T> getVector(const int n, const T a) {
return vector<T>(n, a);
}
template <typename T> vector<T> getEmptyVector() { return vector<T>(); }
template <typename T> void appendAll(vector<T> &a, vector<T> b) {
a.insert(a.end(), b.begin(), b.end());
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
return os << "(" << a.first << ", " << a.second << ")";
};
#define X first
#define Y second
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const ll MOD = 1000000007;
const int inf = 1 << 27;
const ll llinf = 1LL << 50;
const int NIL = -inf;
int popcount(int x) { return __builtin_popcount(x); }
const int MAXN = 1 << 20;
const int MAXLOGN = 20;
int pos[MAXN];
int tree[MAXN];
struct NODE {
int v;
int L, R;
} dat[2 * MAXN * MAXLOGN];
int ndat;
int build(int l, int r) {
int idx = ndat++;
if (r - l == 1) {
dat[idx].v = 0;
dat[idx].L = dat[idx].R = -1;
} else {
dat[idx].v = 0;
dat[idx].L = build(l, (r + l) / 2);
dat[idx].R = build((r + l) / 2, r);
}
return idx;
}
int update(int ix, int l, int r, int k, int v) {
if (k == -1)
return -1;
int idx = ndat++;
int mid = (l + r) / 2;
if (ix < mid) {
dat[idx].L = update(ix, l, (l + r) / 2, dat[k].L, v);
dat[idx].R = dat[k].R;
} else {
dat[idx].L = dat[k].L;
dat[idx].R = update(ix, (l + r) / 2, r, dat[k].R, v);
}
dat[idx].v = dat[k].v + v;
return idx;
}
int query(int lo, int l, int r, int id) {
if (r <= lo)
return 0;
if (lo <= l) {
return dat[id].v;
} else {
return query(lo, l, (l + r) / 2, dat[id].L) +
query(lo, (l + r) / 2, r, dat[id].R);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, Q;
cin >> N >> Q;
tree[0] = build(1, N + 1);
for (int i = 1; i <= N; i++) {
int x;
cin >> x;
int posx = pos[x];
if (posx != 0) {
tree[i] = update(i, 1, N + 1, update(posx, 1, N + 1, tree[i - 1], -1), 1);
} else {
tree[i] = update(i, 1, N + 1, tree[i - 1], 1);
}
pos[x] = i;
}
for (int qq = 0; qq < Q; qq++) {
int l, r;
cin >> l >> r;
cout << query(l, 1, N + 1, tree[r]) << endl;
}
return 0;
} | replace | 81 | 83 | 81 | 83 | -11 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
int main() {
using namespace std;
unsigned long N, Q;
cin >> N >> Q;
vector<unsigned long> c(N);
for (auto &&i : c) {
cin >> i;
--i;
}
vector<tuple<unsigned long, unsigned long, unsigned long>> queries(Q);
{
unsigned long cnt{0};
for (auto &&[i, l, r] : queries) {
cin >> l >> r;
--l;
i = cnt++;
}
}
constexpr unsigned long B{3000};
sort(begin(queries), end(queries), [&B](const auto &lhs, const auto &rhs) {
const auto &[i, li, ri] = lhs;
const auto &[j, lj, rj] = rhs;
return li / B == lj / B ? (li / B) & 1 ? ri > rj : ri < rj : li < lj;
});
unsigned long nl{0}, nr{0}, now{0};
vector<unsigned long> cnt(N), ans(Q);
for (const auto &[i, l, r] : queries) {
while (nr < r)
now += !cnt[c[nr++]]++;
while (nr > r)
now -= !--cnt[c[--nr]];
while (nl < l)
now -= !--cnt[c[nl++]];
while (nl > l)
now += !cnt[c[--nl]]++;
ans[i] = now;
}
for (const auto &i : ans)
cout << i << endl;
return 0;
} | #include <bits/stdc++.h>
int main() {
using namespace std;
unsigned long N, Q;
cin >> N >> Q;
vector<unsigned long> c(N);
for (auto &&i : c) {
cin >> i;
--i;
}
vector<tuple<unsigned long, unsigned long, unsigned long>> queries(Q);
{
unsigned long cnt{0};
for (auto &&[i, l, r] : queries) {
cin >> l >> r;
--l;
i = cnt++;
}
}
constexpr unsigned long B{2000};
sort(begin(queries), end(queries), [&B](const auto &lhs, const auto &rhs) {
const auto &[i, li, ri] = lhs;
const auto &[j, lj, rj] = rhs;
return li / B == lj / B ? (li / B) & 1 ? ri > rj : ri < rj : li < lj;
});
unsigned long nl{0}, nr{0}, now{0};
vector<unsigned long> cnt(N), ans(Q);
for (const auto &[i, l, r] : queries) {
while (nr < r)
now += !cnt[c[nr++]]++;
while (nr > r)
now -= !--cnt[c[--nr]];
while (nl < l)
now -= !--cnt[c[nl++]];
while (nl > l)
now += !cnt[c[--nl]]++;
ans[i] = now;
}
for (const auto &i : ans)
cout << i << endl;
return 0;
} | replace | 20 | 21 | 20 | 21 | TLE | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 6e6 + 10;
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x;
}
void write(int x) {
if (x >= 10)
write(x / 10);
putchar(x % 10 + 48);
}
struct Node {
int sum, lc, rc;
};
Node dat[N];
int tot;
int insert(int pos, int v, int rt, int l, int r) {
int p = ++tot;
dat[p] = dat[rt];
if (r - l == 1) {
dat[p].sum += v;
return p;
}
int m = (l + r) >> 1;
if (pos < m)
dat[p].lc = insert(pos, v, dat[rt].lc, l, m);
else
dat[p].rc = insert(pos, v, dat[rt].rc, m, r);
dat[p].sum = dat[dat[p].lc].sum + dat[dat[p].rc].sum;
return p;
}
int query(int left, int rt, int l, int r) {
if (left <= l)
return dat[rt].sum;
int m = (l + r) >> 1, sum = 0;
if (left < m)
sum += query(left, dat[rt].lc, l, m);
sum += query(left, dat[rt].rc, m, r);
return sum;
}
int last[N], root[N];
int main() {
int n, q;
n = read();
q = read();
for (int i = 1; i <= n; ++i) {
int x = read();
if (last[x]) {
root[i] = insert(last[x], -1, root[i - 1], 1, n + 1);
root[i] = insert(i, 1, root[i], 1, n + 1);
} else
root[i] = insert(i, 1, root[i - 1], 1, n + 1);
last[x] = i;
}
while (q--) {
int x, y;
x = read();
y = read();
write(query(x, root[y], 1, n + 1));
putchar('\n');
}
return 0;
}
| #include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 3e7 + 10;
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x;
}
void write(int x) {
if (x >= 10)
write(x / 10);
putchar(x % 10 + 48);
}
struct Node {
int sum, lc, rc;
};
Node dat[N];
int tot;
int insert(int pos, int v, int rt, int l, int r) {
int p = ++tot;
dat[p] = dat[rt];
if (r - l == 1) {
dat[p].sum += v;
return p;
}
int m = (l + r) >> 1;
if (pos < m)
dat[p].lc = insert(pos, v, dat[rt].lc, l, m);
else
dat[p].rc = insert(pos, v, dat[rt].rc, m, r);
dat[p].sum = dat[dat[p].lc].sum + dat[dat[p].rc].sum;
return p;
}
int query(int left, int rt, int l, int r) {
if (left <= l)
return dat[rt].sum;
int m = (l + r) >> 1, sum = 0;
if (left < m)
sum += query(left, dat[rt].lc, l, m);
sum += query(left, dat[rt].rc, m, r);
return sum;
}
int last[N], root[N];
int main() {
int n, q;
n = read();
q = read();
for (int i = 1; i <= n; ++i) {
int x = read();
if (last[x]) {
root[i] = insert(last[x], -1, root[i - 1], 1, n + 1);
root[i] = insert(i, 1, root[i], 1, n + 1);
} else
root[i] = insert(i, 1, root[i - 1], 1, n + 1);
last[x] = i;
}
while (q--) {
int x, y;
x = read();
y = read();
write(query(x, root[y], 1, n + 1));
putchar('\n');
}
return 0;
}
| replace | 3 | 4 | 3 | 4 | -11 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;
// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
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 T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << endl; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on
#include <functional>
template <typename T> class SegmentTreeOneAll {
using Func = function<T(T, T)>;
public:
vector<T> data;
int n;
T init;
Func update_func;
Func query_func;
SegmentTreeOneAll(int _n, T _init, Func up, Func qu) {
init = _init;
update_func = up;
query_func = qu;
for (n = 1; n < _n; n *= 2)
;
data.resize(2 * n - 1, init);
}
void update(int pos, T val) {
pos += n - 1;
data[pos] = update_func(data[pos], val);
while (pos > 0) {
pos = (pos - 1) / 2;
data[pos] = query_func(data[2 * pos + 1], data[2 * pos + 2]);
}
}
T query(int l, int r) {
T resL = init, resR = init;
for (l += n - 1, r += n - 1; l < r; l = l / 2, r = (r - 1) / 2) {
if (!(l & 1)) {
resL = query_func(resL, data[l]);
}
if (!(r & 1)) {
resR = query_func(data[r - 1], resR);
}
}
return query_func(resL, resR);
}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, q;
cin, n, q;
vector<int> c(n);
REP(i, 0, n) {
cin, c[i];
c[i]--;
}
vector<vector<P>> Q(n);
REP(i, 0, q) {
int l, r;
cin, l, r;
Q[r - 1].emplace_back(l - 1, i);
}
vector<int> ans(n);
auto add = [](ll a, ll b) { return a + b; };
auto update = [](ll a, ll b) { return b; };
SegmentTreeOneAll<ll> seg(n, 0LL, update, add);
vector<int> last(n, -1);
REP(i, 0, n) {
if (last[c[i]] != -1) {
seg.update(last[c[i]], 0);
}
seg.update(i, 1);
last[c[i]] = i;
for (auto [l, pos] : Q[i]) {
ans[pos] = seg.query(l, i + 1);
}
}
REP(i, 0, q) { print(ans[i]); }
return 0;
}
| #include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;
// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
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 T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << endl; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on
#include <functional>
template <typename T> class SegmentTreeOneAll {
using Func = function<T(T, T)>;
public:
vector<T> data;
int n;
T init;
Func update_func;
Func query_func;
SegmentTreeOneAll(int _n, T _init, Func up, Func qu) {
init = _init;
update_func = up;
query_func = qu;
for (n = 1; n < _n; n *= 2)
;
data.resize(2 * n - 1, init);
}
void update(int pos, T val) {
pos += n - 1;
data[pos] = update_func(data[pos], val);
while (pos > 0) {
pos = (pos - 1) / 2;
data[pos] = query_func(data[2 * pos + 1], data[2 * pos + 2]);
}
}
T query(int l, int r) {
T resL = init, resR = init;
for (l += n - 1, r += n - 1; l < r; l = l / 2, r = (r - 1) / 2) {
if (!(l & 1)) {
resL = query_func(resL, data[l]);
}
if (!(r & 1)) {
resR = query_func(data[r - 1], resR);
}
}
return query_func(resL, resR);
}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, q;
cin, n, q;
vector<int> c(n);
REP(i, 0, n) {
cin, c[i];
c[i]--;
}
vector<vector<P>> Q(n);
REP(i, 0, q) {
int l, r;
cin, l, r;
Q[r - 1].emplace_back(l - 1, i);
}
vector<int> ans(q);
auto add = [](ll a, ll b) { return a + b; };
auto update = [](ll a, ll b) { return b; };
SegmentTreeOneAll<ll> seg(n, 0LL, update, add);
vector<int> last(n, -1);
REP(i, 0, n) {
if (last[c[i]] != -1) {
seg.update(last[c[i]], 0);
}
seg.update(i, 1);
last[c[i]] = i;
for (auto [l, pos] : Q[i]) {
ans[pos] = seg.query(l, i + 1);
}
}
REP(i, 0, q) { print(ans[i]); }
return 0;
}
| replace | 88 | 89 | 88 | 89 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define LL long long
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scl(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld%lld", &a, &b)
#define ss(a) scanf("%s", a)
#define mem(a, b) memset(a, b, sizeof(a))
#define PII pair<int, int>
using namespace std;
const int N = 5e5 + 5;
int a[N], rt[N];
int lc[N * 4], rc[N * 4], tot;
LL sum[N * 4];
map<int, int> vis;
void update(int &u, int x, int y, int p, LL v) {
sum[tot] = sum[u] + v;
lc[tot] = lc[u];
rc[tot] = rc[u];
u = tot++;
if (x == y)
return;
int m = x + y >> 1;
if (p <= m)
update(lc[u], x, m, p, v);
else
update(rc[u], m + 1, y, p, v);
}
LL query(int u, int x, int y, int ql, int qr) {
if (ql <= x && y <= qr)
return sum[u];
int m = (x + y) >> 1;
LL ans = 0;
if (ql <= m)
ans += query(lc[u], x, m, ql, qr);
if (qr > m)
ans += query(rc[u], m + 1, y, ql, qr);
return ans;
}
int main() {
int n, q;
tot++;
vis.clear();
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = 1; i <= n; i++) {
rt[i] = rt[i - 1];
if (vis.find(a[i]) != vis.end()) {
int tmp = rt[i - 1];
update(tmp, 1, n, vis[a[i]], -1);
rt[i] = tmp;
update(rt[i], 1, n, i, 1);
} else {
rt[i] = rt[i - 1];
update(rt[i], 1, n, i, 1);
}
vis[a[i]] = i;
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", query(rt[r], 1, n, l, r));
}
system("pause");
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define LL long long
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scl(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld%lld", &a, &b)
#define ss(a) scanf("%s", a)
#define mem(a, b) memset(a, b, sizeof(a))
#define PII pair<int, int>
using namespace std;
const int N = 5e5 + 5;
int a[N], rt[N];
int lc[N * 40], rc[N * 40], tot;
LL sum[N * 40];
map<int, int> vis;
void update(int &u, int x, int y, int p, LL v) {
sum[tot] = sum[u] + v;
lc[tot] = lc[u];
rc[tot] = rc[u];
u = tot++;
if (x == y)
return;
int m = x + y >> 1;
if (p <= m)
update(lc[u], x, m, p, v);
else
update(rc[u], m + 1, y, p, v);
}
LL query(int u, int x, int y, int ql, int qr) {
if (ql <= x && y <= qr)
return sum[u];
int m = (x + y) >> 1;
LL ans = 0;
if (ql <= m)
ans += query(lc[u], x, m, ql, qr);
if (qr > m)
ans += query(rc[u], m + 1, y, ql, qr);
return ans;
}
int main() {
int n, q;
tot++;
vis.clear();
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = 1; i <= n; i++) {
rt[i] = rt[i - 1];
if (vis.find(a[i]) != vis.end()) {
int tmp = rt[i - 1];
update(tmp, 1, n, vis[a[i]], -1);
rt[i] = tmp;
update(rt[i], 1, n, i, 1);
} else {
rt[i] = rt[i - 1];
update(rt[i], 1, n, i, 1);
}
vis[a[i]] = i;
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", query(rt[r], 1, n, l, r));
}
system("pause");
return 0;
}
| replace | 14 | 16 | 14 | 16 | 0 | sh: 1: pause: not found
|
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define INF 1000000000
#define endl '\n'
#define BLOCK 555
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
using namespace std;
struct query {
int l;
int r;
int i;
};
query Q[500001];
int ar[500001], ans[500001];
int fre[500001];
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::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
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);
// why ML = 0 , and MR = -1?
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>
#define REP(i, n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define INF 1000000000
#define endl '\n'
#define BLOCK 710
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
using namespace std;
struct query {
int l;
int r;
int i;
};
query Q[500001];
int ar[500001], ans[500001];
int fre[500001];
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::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
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);
// why ML = 0 , and MR = -1?
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 | 12 | 13 | 12 | 13 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int blk;
struct node {
int left, right;
int index;
};
static bool compare(node &p1, node &p2) {
if (p1.left / blk == p2.left / blk)
return p1.right < p2.right;
return p1.left < p2.left;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
blk = sqrt(n);
vector<int> a(n, 0);
for (int i = 0; i < n; i++)
cin >> a[i];
vector<node> q;
q.resize(m);
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
node p;
p.left = l, p.right = r, p.index = i;
q[i] = p;
}
sort(q.begin(), q.end(), compare);
unordered_map<int, int> freq;
vector<int> ans(m, 0);
int ml = 0, mr = -1, count = 0;
for (int i = 0; i < m; i++) {
int l = q[i].left - 1, r = q[i].right - 1;
int ind = q[i].index;
while (ml > l)
if (++freq[a[--ml]] == 1)
count++;
while (mr < r)
if (++freq[a[++mr]] == 1)
count++;
while (ml < l)
if (--freq[a[ml++]] == 0)
count--;
while (mr > r)
if (--freq[a[mr--]] == 0)
count--;
ans[ind] = count;
}
for (auto &i : ans)
cout << i << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int blk;
struct node {
int left, right;
int index;
};
static bool compare(node &p1, node &p2) {
if (p1.left / blk == p2.left / blk)
return p1.right < p2.right;
return p1.left < p2.left;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
blk = sqrt(n);
vector<int> a(n, 0);
for (int i = 0; i < n; i++)
cin >> a[i];
vector<node> q;
q.resize(m);
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
node p;
p.left = l, p.right = r, p.index = i;
q[i] = p;
}
sort(q.begin(), q.end(), compare);
vector<int> freq(500001, 0);
vector<int> ans(m, 0);
int ml = 0, mr = -1, count = 0;
for (int i = 0; i < m; i++) {
int l = q[i].left - 1, r = q[i].right - 1;
int ind = q[i].index;
while (ml > l)
if (++freq[a[--ml]] == 1)
count++;
while (mr < r)
if (++freq[a[++mr]] == 1)
count++;
while (ml < l)
if (--freq[a[ml++]] == 0)
count--;
while (mr > r)
if (--freq[a[mr--]] == 0)
count--;
ans[ind] = count;
}
for (auto &i : ans)
cout << i << endl;
return 0;
} | replace | 32 | 33 | 32 | 33 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef complex<double> comp;
typedef vector<vector<ld>> matrix;
struct pairhash {
public:
template <typename T, typename U>
size_t operator()(const pair<T, U> &x) const {
size_t seed = hash<T>()(x.first);
return hash<U>()(x.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
};
const ll inf = 1e18 + 9;
const ll mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
int n, q;
int c[500001];
int l[500001], r[500001];
int m[500001];
int k = 0;
void add(int v) {
if (m[c[v]] == 0)
k++;
m[c[v]]++;
}
void del(int v) {
m[c[v]]--;
if (m[c[v]] == 0) {
k--;
}
}
void solve() {
int sq = max(1, n / (int)sqrt(q));
sq = max(1, sq - 10);
vector<T> qi(q);
for (int i = 0; i < q; i++) {
qi[i] = T(l[i] / sq, r[i], i);
}
sort(ALL(qi));
int nl = 0, nr = 0;
int res[q];
for (T &t : qi) {
int i = get<2>(t);
while (nr < r[i]) {
add(nr);
nr++;
}
while (nl > l[i]) {
nl--;
add(nl);
}
while (nl < l[i]) {
del(nl);
nl++;
}
while (nr > r[i]) {
nr--;
del(nr);
}
res[i] = k;
}
for (int i = 0; i < q; i++) {
printf("%d\n", res[i]);
}
}
void input() {
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++)
scanf("%d", c + i);
for (int i = 0; i < q; i++) {
scanf("%d%d", l + i, r + i);
l[i]--;
}
}
int main() {
input();
solve();
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef complex<double> comp;
typedef vector<vector<ld>> matrix;
struct pairhash {
public:
template <typename T, typename U>
size_t operator()(const pair<T, U> &x) const {
size_t seed = hash<T>()(x.first);
return hash<U>()(x.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
};
const ll inf = 1e18 + 9;
const ll mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
int n, q;
int c[500001];
int l[500001], r[500001];
int m[500001];
int k = 0;
void add(int v) {
if (m[c[v]] == 0)
k++;
m[c[v]]++;
}
void del(int v) {
m[c[v]]--;
if (m[c[v]] == 0) {
k--;
}
}
void solve() {
int sq = (int)sqrt(n);
vector<T> qi(q);
for (int i = 0; i < q; i++) {
qi[i] = T(l[i] / sq, r[i], i);
}
sort(ALL(qi));
int nl = 0, nr = 0;
int res[q];
for (T &t : qi) {
int i = get<2>(t);
while (nr < r[i]) {
add(nr);
nr++;
}
while (nl > l[i]) {
nl--;
add(nl);
}
while (nl < l[i]) {
del(nl);
nl++;
}
while (nr > r[i]) {
nr--;
del(nr);
}
res[i] = k;
}
for (int i = 0; i < q; i++) {
printf("%d\n", res[i]);
}
}
void input() {
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++)
scanf("%d", c + i);
for (int i = 0; i < q; i++) {
scanf("%d%d", l + i, r + i);
l[i]--;
}
}
int main() {
input();
solve();
}
| replace | 67 | 69 | 67 | 68 | TLE | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
const int MAXN = 5e5 + 5;
const int M = MAXN * 20;
int n, q, tot;
int a[MAXN];
int T[MAXN * 4], lson[M], rson[M], c[M];
int build(int l, int r) {
int root = tot++;
c[root] = 0;
if (l != r) {
int mid = (l + r) >> 1;
lson[root] = build(l, mid);
rson[root] = build(mid + 1, r);
}
return root;
}
int update(int root, int pos, int val) {
int newroot = tot++, tmp = newroot;
c[newroot] = c[root] + val;
int l = 1, r = n;
while (l < r) {
int mid = (l + r) >> 1;
if (pos <= mid) {
lson[newroot] = tot++;
rson[newroot] = rson[root];
newroot = lson[newroot];
root = lson[root];
r = mid;
} else {
rson[newroot] = tot++;
lson[newroot] = lson[root];
newroot = rson[newroot];
root = rson[root];
l = mid + 1;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int query(int root, int pos) {
int ret = 0;
int l = 1, r = n;
while (pos < r) {
int mid = (l + r) >> 1;
if (pos <= mid) {
r = mid;
root = lson[root];
} else {
ret += c[lson[root]];
root = rson[root];
l = mid + 1;
}
}
return ret + c[root];
}
int main() {
cin >> n >> q;
tot = 0;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
T[n + 1] = build(1, n);
map<int, int> mp;
for (int i = n; i >= 1; i--) {
if (mp.find(a[i]) == mp.end()) {
T[i] = update(T[i + 1], i, 1);
} else {
int tmp = update(T[i + 1], mp[a[i]], -1);
T[i] = update(tmp, i, 1);
}
mp[a[i]] = i;
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", query(T[l], r));
}
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
const int MAXN = 5e5 + 5;
const int M = MAXN * 100;
int n, q, tot;
int a[MAXN];
int T[MAXN * 4], lson[M], rson[M], c[M];
int build(int l, int r) {
int root = tot++;
c[root] = 0;
if (l != r) {
int mid = (l + r) >> 1;
lson[root] = build(l, mid);
rson[root] = build(mid + 1, r);
}
return root;
}
int update(int root, int pos, int val) {
int newroot = tot++, tmp = newroot;
c[newroot] = c[root] + val;
int l = 1, r = n;
while (l < r) {
int mid = (l + r) >> 1;
if (pos <= mid) {
lson[newroot] = tot++;
rson[newroot] = rson[root];
newroot = lson[newroot];
root = lson[root];
r = mid;
} else {
rson[newroot] = tot++;
lson[newroot] = lson[root];
newroot = rson[newroot];
root = rson[root];
l = mid + 1;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int query(int root, int pos) {
int ret = 0;
int l = 1, r = n;
while (pos < r) {
int mid = (l + r) >> 1;
if (pos <= mid) {
r = mid;
root = lson[root];
} else {
ret += c[lson[root]];
root = rson[root];
l = mid + 1;
}
}
return ret + c[root];
}
int main() {
cin >> n >> q;
tot = 0;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
T[n + 1] = build(1, n);
map<int, int> mp;
for (int i = n; i >= 1; i--) {
if (mp.find(a[i]) == mp.end()) {
T[i] = update(T[i + 1], i, 1);
} else {
int tmp = update(T[i + 1], mp[a[i]], -1);
T[i] = update(tmp, i, 1);
}
mp[a[i]] = i;
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", query(T[l], r));
}
return 0;
}
| replace | 7 | 8 | 7 | 8 | -11 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#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", &N), scanf("%d", &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;
}
while (Q--) {
int lo, hi;
scanf("%d %d", &lo, &hi);
printf("%d\n", query(tree[hi], lo));
}
return 0;
} | #include <bits/stdc++.h>
#include <map>
using std::map;
const int MAXN = 500005, MAXLGN = 20;
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", &N), scanf("%d", &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;
}
while (Q--) {
int lo, hi;
scanf("%d %d", &lo, &hi);
printf("%d\n", query(tree[hi], lo));
}
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 50010;
int n, m, B, re, col[N], cnt[N], ans[N];
void add(int x) { re += !(cnt[x]++); }
void del(int x) { re -= !(--cnt[x]); }
struct Query {
int l, r, id;
bool operator<(Query &b) { return l / B == b.l / B ? r < b.r : l < b.l; }
} q[N];
int main() {
int i, l = 1, r = 0;
cin >> n >> m;
B = n / sqrt(m);
for (i = 1; i <= n; ++i) {
cin >> col[i];
}
for (i = 0; i < m; ++i) {
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
sort(q, q + m);
for (i = 0; i < m; ++i) {
if (q[i].l == q[i].r) {
ans[q[i].id] = 1;
continue;
}
while (l > q[i].l)
add(col[--l]);
while (r < q[i].r)
add(col[++r]);
while (l < q[i].l)
del(col[l++]);
while (r > q[i].r)
del(col[r--]);
ans[q[i].id] = re;
}
for (i = 0; i < m; ++i)
printf("%d\n", ans[i]);
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 5e5 + 10;
int n, m, B, re, col[N], cnt[N], ans[N];
void add(int x) { re += !(cnt[x]++); }
void del(int x) { re -= !(--cnt[x]); }
struct Query {
int l, r, id;
bool operator<(Query &b) { return l / B == b.l / B ? r < b.r : l < b.l; }
} q[N];
int main() {
int i, l = 1, r = 0;
cin >> n >> m;
B = n / sqrt(m);
for (i = 1; i <= n; ++i) {
cin >> col[i];
}
for (i = 0; i < m; ++i) {
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
sort(q, q + m);
for (i = 0; i < m; ++i) {
if (q[i].l == q[i].r) {
ans[q[i].id] = 1;
continue;
}
while (l > q[i].l)
add(col[--l]);
while (r < q[i].r)
add(col[++r]);
while (l < q[i].l)
del(col[l++]);
while (r > q[i].r)
del(col[r--]);
ans[q[i].id] = re;
}
for (i = 0; i < m; ++i)
printf("%d\n", ans[i]);
return 0;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p02599 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int e = 5e5 + 6;
const int e1 = 5e5 + 2;
struct kien {
ll i, j, id, type;
};
kien a[e + e1];
ll n, q;
ll Bit[e];
ll res[e1];
bool lf(kien u, kien v) {
return (u.j < v.j || (u.j == v.j && u.type < v.type));
}
ll bit(ll vitri, ll val) {
for (int i = vitri; i <= n; i += i & -i)
Bit[i] += val;
}
ll getbit(ll vitri) {
ll ans = 0;
for (int i = vitri; i > 0; i -= i & -i) {
ans += Bit[i];
}
return ans;
}
int main() {
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i].i;
a[i].j = i;
a[i].type = -1;
}
map<ll, ll> last;
for (int i = 1; i <= q; i++) {
cin >> a[i + n].i >> a[i + n].j;
a[i + n].id = i;
}
sort(a + 1, a + n + q + 1, lf);
for (int i = 1; i <= q + n; i++) {
if (a[i].type == -1) {
bit(a[i].j, 1);
if (last[a[i].i] > 0) {
bit(last[a[i].i], -1);
}
last[a[i].i] = a[i].j;
} else {
res[a[i].id] = getbit(a[i].j) - getbit(a[i].i - 1);
}
}
for (int i = 1; i <= q; i++)
cout << res[i] << "\n";
}
| #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int e = 5e5 + 6;
const int e1 = 5e5 + 2;
struct kien {
ll i, j, id, type;
};
kien a[e + e1];
ll n, q;
ll Bit[e];
ll res[e1];
bool lf(kien u, kien v) {
return (u.j < v.j || (u.j == v.j && u.type < v.type));
}
void bit(ll vitri, ll val) {
for (int i = vitri; i <= n; i += i & -i)
Bit[i] += val;
}
ll getbit(ll vitri) {
ll ans = 0;
for (int i = vitri; i > 0; i -= i & -i) {
ans += Bit[i];
}
return ans;
}
int main() {
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i].i;
a[i].j = i;
a[i].type = -1;
}
map<ll, ll> last;
for (int i = 1; i <= q; i++) {
cin >> a[i + n].i >> a[i + n].j;
a[i + n].id = i;
}
sort(a + 1, a + n + q + 1, lf);
for (int i = 1; i <= q + n; i++) {
if (a[i].type == -1) {
bit(a[i].j, 1);
if (last[a[i].i] > 0) {
bit(last[a[i].i], -1);
}
last[a[i].i] = a[i].j;
} else {
res[a[i].id] = getbit(a[i].j) - getbit(a[i].i - 1);
}
}
for (int i = 1; i <= q; i++)
cout << res[i] << "\n";
}
| replace | 20 | 21 | 20 | 21 | 0 | |
p02599 | C++ | Runtime Error | // main.cpp
// ABC174F2
#include <algorithm>
#include <iostream>
#include <map>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
// Binary Indexed Tree (Fenwick Tree)
// 使い方 : BIT<int>bit
// BIT<int> bt(n + 1); BITを初期化して
// bt.add(x, 1); xに1を加算
// bt.sum(x); 0~xの累積和
template <typename T> // T: type of cost
struct BIT {
private:
vector<T> array;
const int n;
public:
BIT(int _n) : array(_n + 1, 0), n(_n) {}
// iまでの累積和
T sum(int i) {
T x = 0;
while (i > 0) {
x += array[i];
i -= i & -i; // LSBからの減算
}
return x;
}
// iからjの区間和
T sum(int i, int j) {
T ret_i = sum(i - 1);
T ret_j = sum(j);
return ret_j - ret_i;
}
// i番目にxを追加
void add(int i, T x) {
while (i <= n) {
array[i] += x;
i += i & -i; // LSBへの加算
}
}
};
int main(int argc, const char *argv[]) {
int n, q;
cin >> n >> q;
vector<int> c(n + 1);
vector<int> p(n + 1, -1); // -1で初期化しておく
vector<int> st(n + 1, 0);
// st(s)=t 左がsで右がtの矢印
// 2回目の登場以降ならiをtとして登録
for (int i = 1; i <= n; i++) {
cin >> c.at(i);
if (p.at(c.at(i)) != -1) {
st.at(p.at(c.at(i))) = i;
}
p.at(c.at(i)) = i;
}
/*rep(i,n){
cout<<i<<" "<<st.at(i)<<endl;
}*/
// クエリを整理する
// q(l)=(r,i) q(左端)=(右端、読み込みの順番)
int l, r;
vector<int> len(q);
vector<vector<P>> query(n, vector<P>());
rep(i, q) {
cin >> l >> r;
query.at(l).emplace_back(r, i);
len.at(i) = r - l + 1;
}
/*for(int i=n-1;i>=0;i--){
for(auto& x:query.at(i)){
cout<<i<<" "<<x.first<<" "<<x.second<<endl;
}
}*/
// BITを使う
vector<int> cnt(q);
BIT<int> bt(n + 1); // BITを初期化
for (int s = n; s >= 1; s--) {
if (st.at(s) > 0) {
bt.add(st.at(s), 1);
}
for (auto &x : query.at(s)) {
cnt.at(x.second) = bt.sum(x.first);
}
}
rep(i, q) { cout << len.at(i) - cnt.at(i) << endl; }
return 0;
}
| // main.cpp
// ABC174F2
#include <algorithm>
#include <iostream>
#include <map>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
// Binary Indexed Tree (Fenwick Tree)
// 使い方 : BIT<int>bit
// BIT<int> bt(n + 1); BITを初期化して
// bt.add(x, 1); xに1を加算
// bt.sum(x); 0~xの累積和
template <typename T> // T: type of cost
struct BIT {
private:
vector<T> array;
const int n;
public:
BIT(int _n) : array(_n + 1, 0), n(_n) {}
// iまでの累積和
T sum(int i) {
T x = 0;
while (i > 0) {
x += array[i];
i -= i & -i; // LSBからの減算
}
return x;
}
// iからjの区間和
T sum(int i, int j) {
T ret_i = sum(i - 1);
T ret_j = sum(j);
return ret_j - ret_i;
}
// i番目にxを追加
void add(int i, T x) {
while (i <= n) {
array[i] += x;
i += i & -i; // LSBへの加算
}
}
};
int main(int argc, const char *argv[]) {
int n, q;
cin >> n >> q;
vector<int> c(n + 1);
vector<int> p(n + 1, -1); // -1で初期化しておく
vector<int> st(n + 1, 0);
// st(s)=t 左がsで右がtの矢印
// 2回目の登場以降ならiをtとして登録
for (int i = 1; i <= n; i++) {
cin >> c.at(i);
if (p.at(c.at(i)) != -1) {
st.at(p.at(c.at(i))) = i;
}
p.at(c.at(i)) = i;
}
/*rep(i,n){
cout<<i<<" "<<st.at(i)<<endl;
}*/
// クエリを整理する
// q(l)=(r,i) q(左端)=(右端、読み込みの順番)
int l, r;
vector<int> len(q);
vector<vector<P>> query(n + 1, vector<P>()); // lの値によってクエリを整理する
rep(i, q) {
cin >> l >> r;
query.at(l).emplace_back(r, i);
len.at(i) = r - l + 1;
}
/*for(int i=n-1;i>=0;i--){
for(auto& x:query.at(i)){
cout<<i<<" "<<x.first<<" "<<x.second<<endl;
}
}*/
// BITを使う
vector<int> cnt(q);
BIT<int> bt(n + 1); // BITを初期化
for (int s = n; s >= 1; s--) {
if (st.at(s) > 0) {
bt.add(st.at(s), 1);
}
for (auto &x : query.at(s)) {
cnt.at(x.second) = bt.sum(x.first);
}
}
rep(i, q) { cout << len.at(i) - cnt.at(i) << endl; }
return 0;
}
| replace | 70 | 71 | 70 | 71 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)
|
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second
using namespace std;
pair<int, pair<int, int>> q[200001];
int n, m, a[100001], ans[200001];
int st[5 * 100001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
if (a.s.f != b.s.f)
return a.s.f < b.s.f;
return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
if (l == r) {
st[v] = d;
return;
}
int mid = (l + r) / 2;
if (x <= mid)
update(v * 2, l, mid, x, d);
else
update(v * 2 + 1, mid + 1, r, x, d);
st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
if (l > B || r < A)
return 0;
if (A == l && B == r)
return st[v];
int mid = (A + B) / 2;
return Find(v * 2, A, mid, l, min(mid, r)) +
Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++)
scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
sort(q + 1, q + m + 1, cmp);
int j = 1;
for (int i = 1; i <= m; i++) {
while (j <= q[i].s.f) {
if (!used[a[j]]) {
update(1, 1, n, j, 1);
used[a[j]] = j;
} else {
update(1, 1, n, used[a[j]], 0);
update(1, 1, n, j, 1);
used[a[j]] = j;
}
j++;
}
ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second
using namespace std;
pair<int, pair<int, int>> q[500001];
int n, m, a[500001], ans[500001];
int st[25 * 100001], used[5000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
if (a.s.f != b.s.f)
return a.s.f < b.s.f;
return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
if (l == r) {
st[v] = d;
return;
}
int mid = (l + r) / 2;
if (x <= mid)
update(v * 2, l, mid, x, d);
else
update(v * 2 + 1, mid + 1, r, x, d);
st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
if (l > B || r < A)
return 0;
if (A == l && B == r)
return st[v];
int mid = (A + B) / 2;
return Find(v * 2, A, mid, l, min(mid, r)) +
Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++)
scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
sort(q + 1, q + m + 1, cmp);
int j = 1;
for (int i = 1; i <= m; i++) {
while (j <= q[i].s.f) {
if (!used[a[j]]) {
update(1, 1, n, j, 1);
used[a[j]] = j;
} else {
update(1, 1, n, used[a[j]], 0);
update(1, 1, n, j, 1);
used[a[j]] = j;
}
j++;
}
ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
} | replace | 8 | 11 | 8 | 11 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;
struct Query {
int i;
int l, r;
};
bool operator<(const Query &a, const Query &b) { return a.r < b.r; }
struct FenwickTree {
int n;
vector<int> tree;
public:
FenwickTree(int n_) : tree((1 << 21) + 1, 0) { n = n_; }
void add(int i, int value) {
for (; i < n; i |= i + 1)
tree[i] += value;
}
int sum(int i) {
int s = 0;
for (; i >= 0; i = (i & i + 1) - 1)
s += tree[i];
return s;
}
};
int lst[500005];
int main() {
int N, Q;
cin >> N >> Q;
vector<int> v;
rep(i, N) {
int a;
cin >> a;
v.push_back(a);
}
rep(i, 500005) lst[i] = -1;
vector<Query> w;
rep(q, Q) {
Query query;
query.i = q;
cin >> query.l >> query.r;
query.l--;
query.r--;
w.push_back(query);
}
sort(ALLOF(w));
vector<int> ret(Q, -1);
FenwickTree ft(N + 5);
int L = -1, R = -1;
rep(i, Q) {
Query query = w[i];
if (R < query.r) {
REP(j, R + 1, query.r + 1) {
if (lst[v[j]] != -1) {
ft.add(lst[v[j]], -1);
}
lst[v[j]] = j;
ft.add(j, 1);
}
}
ret[query.i] = ft.sum(query.r) - ft.sum(query.l - 1);
}
rep(i, ret.size()) { cout << ret[i] << endl; }
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;
struct Query {
int i;
int l, r;
};
bool operator<(const Query &a, const Query &b) { return a.r < b.r; }
struct FenwickTree {
int n;
vector<int> tree;
public:
FenwickTree(int n_) : tree((1 << 21) + 1, 0) { n = n_; }
void add(int i, int value) {
for (; i < n; i |= i + 1)
tree[i] += value;
}
int sum(int i) {
int s = 0;
for (; i >= 0; i = (i & i + 1) - 1)
s += tree[i];
return s;
}
};
int lst[500005];
int main() {
int N, Q;
cin >> N >> Q;
vector<int> v;
rep(i, N) {
int a;
cin >> a;
v.push_back(a);
}
rep(i, 500005) lst[i] = -1;
vector<Query> w;
rep(q, Q) {
Query query;
query.i = q;
cin >> query.l >> query.r;
query.l--;
query.r--;
w.push_back(query);
}
sort(ALLOF(w));
vector<int> ret(Q, -1);
FenwickTree ft(N + 5);
int L = -1, R = -1;
rep(i, Q) {
Query query = w[i];
if (R < query.r) {
REP(j, R + 1, query.r + 1) {
if (lst[v[j]] != -1) {
ft.add(lst[v[j]], -1);
}
lst[v[j]] = j;
ft.add(j, 1);
}
R = query.r;
}
ret[query.i] = ft.sum(query.r) - ft.sum(query.l - 1);
}
rep(i, ret.size()) { cout << ret[i] << endl; }
return 0;
}
| insert | 72 | 72 | 72 | 73 | TLE | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int block_size;
struct Query {
int l, r, idx;
bool operator<(Query other) const {
return make_pair(l / block_size, r) <
make_pair(other.l / block_size, other.r);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
block_size = sqrt(n);
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
vector<Query> vq;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
vq.push_back((Query){l, r, i});
}
vector<int> answers(q);
int ans = 0;
unordered_map<int, int> mp;
auto add = [&](int idx) {
if (mp[v[idx]] == 0) {
ans++;
}
mp[v[idx]]++;
};
auto remove = [&](int idx) {
if (mp[v[idx]] == 1) {
ans--;
}
mp[v[idx]]--;
};
sort(vq.begin(), vq.end());
int cur_l = 0;
int cur_r = -1;
for (Query qu : vq) {
while (cur_l > qu.l) {
cur_l--;
add(cur_l);
}
while (cur_r < qu.r) {
cur_r++;
add(cur_r);
}
while (cur_l < qu.l) {
remove(cur_l);
cur_l++;
}
while (cur_r > qu.r) {
remove(cur_r);
cur_r--;
}
answers[qu.idx] = ans;
}
for (int x : answers) {
cout << x << "\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int block_size;
struct Query {
int l, r, idx;
bool operator<(Query other) const {
return make_pair(l / block_size, r) <
make_pair(other.l / block_size, other.r);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
block_size = sqrt(n);
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
vector<Query> vq;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
vq.push_back((Query){l, r, i});
}
vector<int> answers(q);
int ans = 0;
vector<int> mp(n + 1, 0);
auto add = [&](int idx) {
if (mp[v[idx]] == 0) {
ans++;
}
mp[v[idx]]++;
};
auto remove = [&](int idx) {
if (mp[v[idx]] == 1) {
ans--;
}
mp[v[idx]]--;
};
sort(vq.begin(), vq.end());
int cur_l = 0;
int cur_r = -1;
for (Query qu : vq) {
while (cur_l > qu.l) {
cur_l--;
add(cur_l);
}
while (cur_r < qu.r) {
cur_r++;
add(cur_r);
}
while (cur_l < qu.l) {
remove(cur_l);
cur_l++;
}
while (cur_r > qu.r) {
remove(cur_r);
cur_r--;
}
answers[qu.idx] = ans;
}
for (int x : answers) {
cout << x << "\n";
}
return 0;
} | replace | 31 | 32 | 31 | 32 | TLE | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int read() {
char x;
while ((x = getchar()) > '9' || x < '0')
;
int u = x - '0';
while ((x = getchar()) <= '9' && x >= '0')
u = (u << 3) + (u << 1) + x - '0';
return u;
}
int buf[105];
inline void write(int i) {
int p = 0;
if (i == 0)
p++;
else
while (i) {
buf[p++] = i % 10;
i /= 10;
}
for (int j = p - 1; j >= 0; j--)
putchar('0' + buf[j]);
}
int a[500005], ans[500005], cnt[500005], res = 0, n, q, bl;
struct node {
int l, r, id;
int block;
} e[500005];
inline bool cmp(node a, node b) {
if (a.block ^ b.block) {
if (a.block & 1)
return a.r < b.r;
return a.r > b.r;
}
return a.l < b.l;
}
inline void del(int x) {
--cnt[a[x]];
if (!cnt[a[x]])
--res;
}
inline void add(int x) {
if (!cnt[a[x]])
++res;
++cnt[a[x]];
}
int main() {
n = read();
q = read();
bl = n / sqrt(q * 2 / 3);
for (int i = 1; i <= n; ++i)
a[i] = read();
for (int i = 1; i <= q; ++i) {
e[i].l = read();
e[i].r = read();
e[i].id = i;
e[i].block = e[i].l / bl;
}
sort(e + 1, e + 1 + q, cmp);
int l = e[1].l, r = e[1].l - 1;
for (int i = 1; i <= q; ++i) {
int stdl = e[i].l, stdr = e[i].r;
while (l < stdl)
del(l++);
while (l > stdl)
add(--l);
while (r < stdr)
add(++r);
while (r > stdr)
del(r--);
ans[e[i].id] = res;
}
for (int i = 1; i <= q; ++i)
write(ans[i]), printf("\n");
return 0;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int read() {
char x;
while ((x = getchar()) > '9' || x < '0')
;
int u = x - '0';
while ((x = getchar()) <= '9' && x >= '0')
u = (u << 3) + (u << 1) + x - '0';
return u;
}
int buf[105];
inline void write(int i) {
int p = 0;
if (i == 0)
p++;
else
while (i) {
buf[p++] = i % 10;
i /= 10;
}
for (int j = p - 1; j >= 0; j--)
putchar('0' + buf[j]);
}
int a[500005], ans[500005], cnt[500005], res = 0, n, q, bl;
struct node {
int l, r, id;
int block;
} e[500005];
inline bool cmp(const node &a, const node &b) {
if (a.block ^ b.block)
return a.l < b.l;
if (a.block & 1)
return a.r < b.r;
return a.r > b.r;
}
inline void del(int x) {
--cnt[a[x]];
if (!cnt[a[x]])
--res;
}
inline void add(int x) {
if (!cnt[a[x]])
++res;
++cnt[a[x]];
}
int main() {
n = read();
q = read();
bl = n / sqrt(q * 2 / 3);
for (int i = 1; i <= n; ++i)
a[i] = read();
for (int i = 1; i <= q; ++i) {
e[i].l = read();
e[i].r = read();
e[i].id = i;
e[i].block = e[i].l / bl;
}
sort(e + 1, e + 1 + q, cmp);
int l = e[1].l, r = e[1].l - 1;
for (int i = 1; i <= q; ++i) {
int stdl = e[i].l, stdr = e[i].r;
while (l < stdl)
del(l++);
while (l > stdl)
add(--l);
while (r < stdr)
add(++r);
while (r > stdr)
del(r--);
ans[e[i].id] = res;
}
for (int i = 1; i <= q; ++i)
write(ans[i]), printf("\n");
return 0;
} | replace | 44 | 51 | 44 | 51 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
typedef long long ll;
typedef std::pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
struct BIT {
typedef ll input_t;
vector<input_t> bit_;
const long long n_;
BIT(long long n) : n_(n) { bit_.resize(n + 1LL, 0); }
input_t sum(int i) {
input_t s = 0;
while (i > 0) {
s += bit_[i];
i -= i & (-i);
}
return s;
}
void add(int i, input_t x) {
while (i <= n_) {
bit_[i] += x;
i += (i & (-i));
}
}
};
int main() {
ll n, q;
cin >> n >> q;
vector<ll> cs(n + 1);
rep(i, n) cin >> cs[i + 1];
vector<ll> pos(n, -1);
vector<P> range(q);
rep(i, q) { cin >> range[i].first >> range[i].second; }
vector<vector<int>> qs(n + 1);
rep(i, q) { qs[range[i].second].push_back(i); }
BIT bit(n + 10);
vector<ll> ans(q);
ll rightSum = 0;
for (int i = 1; i <= n; i++) {
auto c = cs[i];
if (pos[c] >= 0) {
bit.add(pos[c], -1);
rightSum--;
}
pos[c] = i;
bit.add(i, 1);
rightSum++;
ll sum = rightSum;
for (auto query : qs[i]) {
auto index = query;
auto left = range[index].first;
if (left > 1) {
ans[index] = sum - bit.sum(left - 1);
} else {
ans[index] = sum;
}
}
}
rep(i, q) { cout << ans[i] << endl; }
return 0;
}
| #include <bits/stdc++.h>
typedef long long ll;
typedef std::pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
struct BIT {
typedef ll input_t;
vector<input_t> bit_;
const long long n_;
BIT(long long n) : n_(n) { bit_.resize(n + 1LL, 0); }
input_t sum(int i) {
input_t s = 0;
while (i > 0) {
s += bit_[i];
i -= i & (-i);
}
return s;
}
void add(int i, input_t x) {
while (i <= n_) {
bit_[i] += x;
i += (i & (-i));
}
}
};
int main() {
ll n, q;
cin >> n >> q;
vector<ll> cs(n + 1);
rep(i, n) cin >> cs[i + 1];
vector<ll> pos(n + 1, -1);
vector<P> range(q);
rep(i, q) { cin >> range[i].first >> range[i].second; }
vector<vector<int>> qs(n + 1);
rep(i, q) { qs[range[i].second].push_back(i); }
BIT bit(n + 10);
vector<ll> ans(q);
ll rightSum = 0;
for (int i = 1; i <= n; i++) {
auto c = cs[i];
if (pos[c] >= 0) {
bit.add(pos[c], -1);
rightSum--;
}
pos[c] = i;
bit.add(i, 1);
rightSum++;
ll sum = rightSum;
for (auto query : qs[i]) {
auto index = query;
auto left = range[index].first;
if (left > 1) {
ans[index] = sum - bit.sum(left - 1);
} else {
ans[index] = sum;
}
}
}
rep(i, q) { cout << ans[i] << endl; }
return 0;
}
| replace | 52 | 53 | 52 | 53 | TLE | |
p02599 | C++ | Runtime Error | // Author: Vinay Khilwani
// Language: C++
// @vok8: Codeforces, AtCoder, LeetCode, HackerEarth, TopCoder, Google, FB,
// CSES, Spoj, GitHub
// @vok_8: CodeChef, GFG
// @vok8_khilwani: HackerRank
// Never Stop Trying.
// Trying to be Better than Myself.
// while(true)
// {
// if(AC)
// {
// break;
// }
// else if(Contest Over)
// {
// Try.
// Check out Editorial.
// Understand.
// Find out your Mistake.
// Learn the topic (if new).
// Solve Problems on that topic (if new).
// Upsolve that problem.
// break;
// }
// else
// {
// Try.
// Use Pen-Paper.
// Find errors, edge cases, etc.
// continue;
// }
// }
// Optimizations
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")
// Libraries
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// Debugging
#define dbg(a) cerr << a << "\n";
#define debug_a(a) \
for (auto x : a) { \
cerr << x << " "; \
} \
cerr << "\n";
#define debug_b(a) \
for (auto x : a) { \
cerr << "[" << x.first << ", " << x.second << "]" \
<< "\n"; \
} \
cerr << "\n";
#define debug_c(a) \
for (auto x : a) { \
debug_a(x) \
} \
cerr << "\n";
#define debug_d(a) \
for (auto x : a) { \
debug_b(x) \
} \
cerr << "\n";
#define debug_e(a) \
cerr << "[" << a.first << ", " << a.second << "]" \
<< "\n";
// Defines
#define fast \
ios_base::sync_with_stdio(false); \
cout.tie(NULL); \
cin.tie(NULL);
#define loop(i, a, n) for (int i = a; i < n; i++)
#define rloop(i, a, n) for (int i = a; i >= n; i--)
#define fr(i, a, n, b) for (int i = a; i < n; i += b)
#define rfr(i, a, n, b) for (int i = a; i >= n; i -= b)
#define IN cin >>
#define OUT cout <<
#define nl "\n"
#define sz(a) int(a.size())
#define all(a) (a).begin(), (a).end()
#define each(a, b) for (auto &a : b)
#define pb push_back
#define set_bits(a) __builtin_popcountll(a)
#define ar array
#define write(a) \
for (auto x : a) { \
OUT x << " "; \
} \
OUT nl;
#define read(a) \
for (auto &x : a) { \
IN x; \
}
// #define oset
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
using ll = long long int;
using ld = long double;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vi = vector<int>;
const ll mod = (ll)(1e9) + 7LL;
const ll M = 988244353LL;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ld pi = acos(-1);
// General Functions
ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }
ll P(ll B, ll power, ll modulo) // Fast Power
{
ll ans = 1LL;
while (power > 0LL) {
if (power % 2LL == 1LL) {
ans = (ans * B) % modulo;
}
B = (B * B) % modulo;
power /= 2LL;
}
return ans;
}
bool isPrime(ll n) {
if (n <= 1LL) {
return false;
}
if (n <= 3LL) {
return true;
}
if (n % 2 == 0LL || n % 3 == 0LL) {
return false;
}
for (ll i = 5LL; (i * i) <= n; i += 6LL) {
if (n % i == 0LL || n % (i + 2LL) == 0LL) {
return false;
}
}
return true;
}
void vok() {
fast
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
}
// Global Variables
const int mxN = int(1e5) + 100;
set<int> *seg;
// Solver Function(s)
void update(int idx, int val, int bit[], int n) {
for (; idx <= n; idx += idx & -idx) {
bit[idx] += val;
}
}
int query(int idx, int bit[], int n) {
int sum = 0;
for (; idx > 0; idx -= idx & -idx) {
sum += bit[idx];
}
return sum;
}
void run(int arr[], int n, vector<ar<int, 3>> v, int q) {
int bit[n + 1];
memset(bit, 0, sizeof(bit));
int last_visit[mxN];
memset(last_visit, -1, sizeof(last_visit));
int ans[q];
int query_counter = 0;
for (int i = 0; i < n; i++) {
if (last_visit[arr[i]] != -1) {
update(last_visit[arr[i]] + 1, -1, bit, n);
}
last_visit[arr[i]] = i;
update(i + 1, 1, bit, n);
while (query_counter < q && v[query_counter][0] == i) {
ans[v[query_counter][2]] = query(v[query_counter][0] + 1, bit, n) -
query(v[query_counter][1], bit, n);
query_counter++;
}
}
loop(i, 0, q) { OUT ans[i] << nl; }
}
void solve() {
int n, q;
IN n >> q;
int v[n];
loop(i, 0, n) { IN v[i]; }
vector<ar<int, 3>> vv(q);
loop(i, 0, q) {
IN vv[i][1] >> vv[i][0];
vv[i][2] = i;
vv[i][0]--, vv[i][1]--;
}
sort(all(vv));
run(v, n, vv, q);
}
// Main Function
int main() {
vok();
int t = 1;
// IN t;
while (t--) {
solve();
}
return 0;
} | // Author: Vinay Khilwani
// Language: C++
// @vok8: Codeforces, AtCoder, LeetCode, HackerEarth, TopCoder, Google, FB,
// CSES, Spoj, GitHub
// @vok_8: CodeChef, GFG
// @vok8_khilwani: HackerRank
// Never Stop Trying.
// Trying to be Better than Myself.
// while(true)
// {
// if(AC)
// {
// break;
// }
// else if(Contest Over)
// {
// Try.
// Check out Editorial.
// Understand.
// Find out your Mistake.
// Learn the topic (if new).
// Solve Problems on that topic (if new).
// Upsolve that problem.
// break;
// }
// else
// {
// Try.
// Use Pen-Paper.
// Find errors, edge cases, etc.
// continue;
// }
// }
// Optimizations
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")
// Libraries
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// Debugging
#define dbg(a) cerr << a << "\n";
#define debug_a(a) \
for (auto x : a) { \
cerr << x << " "; \
} \
cerr << "\n";
#define debug_b(a) \
for (auto x : a) { \
cerr << "[" << x.first << ", " << x.second << "]" \
<< "\n"; \
} \
cerr << "\n";
#define debug_c(a) \
for (auto x : a) { \
debug_a(x) \
} \
cerr << "\n";
#define debug_d(a) \
for (auto x : a) { \
debug_b(x) \
} \
cerr << "\n";
#define debug_e(a) \
cerr << "[" << a.first << ", " << a.second << "]" \
<< "\n";
// Defines
#define fast \
ios_base::sync_with_stdio(false); \
cout.tie(NULL); \
cin.tie(NULL);
#define loop(i, a, n) for (int i = a; i < n; i++)
#define rloop(i, a, n) for (int i = a; i >= n; i--)
#define fr(i, a, n, b) for (int i = a; i < n; i += b)
#define rfr(i, a, n, b) for (int i = a; i >= n; i -= b)
#define IN cin >>
#define OUT cout <<
#define nl "\n"
#define sz(a) int(a.size())
#define all(a) (a).begin(), (a).end()
#define each(a, b) for (auto &a : b)
#define pb push_back
#define set_bits(a) __builtin_popcountll(a)
#define ar array
#define write(a) \
for (auto x : a) { \
OUT x << " "; \
} \
OUT nl;
#define read(a) \
for (auto &x : a) { \
IN x; \
}
// #define oset
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
using ll = long long int;
using ld = long double;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vi = vector<int>;
const ll mod = (ll)(1e9) + 7LL;
const ll M = 988244353LL;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ld pi = acos(-1);
// General Functions
ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }
ll P(ll B, ll power, ll modulo) // Fast Power
{
ll ans = 1LL;
while (power > 0LL) {
if (power % 2LL == 1LL) {
ans = (ans * B) % modulo;
}
B = (B * B) % modulo;
power /= 2LL;
}
return ans;
}
bool isPrime(ll n) {
if (n <= 1LL) {
return false;
}
if (n <= 3LL) {
return true;
}
if (n % 2 == 0LL || n % 3 == 0LL) {
return false;
}
for (ll i = 5LL; (i * i) <= n; i += 6LL) {
if (n % i == 0LL || n % (i + 2LL) == 0LL) {
return false;
}
}
return true;
}
void vok() {
fast
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
}
// Global Variables
const int mxN = int(5e5) + 100;
set<int> *seg;
// Solver Function(s)
void update(int idx, int val, int bit[], int n) {
for (; idx <= n; idx += idx & -idx) {
bit[idx] += val;
}
}
int query(int idx, int bit[], int n) {
int sum = 0;
for (; idx > 0; idx -= idx & -idx) {
sum += bit[idx];
}
return sum;
}
void run(int arr[], int n, vector<ar<int, 3>> v, int q) {
int bit[n + 1];
memset(bit, 0, sizeof(bit));
int last_visit[mxN];
memset(last_visit, -1, sizeof(last_visit));
int ans[q];
int query_counter = 0;
for (int i = 0; i < n; i++) {
if (last_visit[arr[i]] != -1) {
update(last_visit[arr[i]] + 1, -1, bit, n);
}
last_visit[arr[i]] = i;
update(i + 1, 1, bit, n);
while (query_counter < q && v[query_counter][0] == i) {
ans[v[query_counter][2]] = query(v[query_counter][0] + 1, bit, n) -
query(v[query_counter][1], bit, n);
query_counter++;
}
}
loop(i, 0, q) { OUT ans[i] << nl; }
}
void solve() {
int n, q;
IN n >> q;
int v[n];
loop(i, 0, n) { IN v[i]; }
vector<ar<int, 3>> vv(q);
loop(i, 0, q) {
IN vv[i][1] >> vv[i][0];
vv[i][2] = i;
vv[i][0]--, vv[i][1]--;
}
sort(all(vv));
run(v, n, vv, q);
}
// Main Function
int main() {
vok();
int t = 1;
// IN t;
while (t--) {
solve();
}
return 0;
} | replace | 159 | 160 | 159 | 160 | -11 | |
p02599 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
const int N = 200003;
struct query {
int l, r, id;
query() {}
query(int ll, int rr, int d) { l = ll, r = rr, id = d; }
bool operator<(const query &x) { return r < x.r; }
};
int n, k, Q, c[N], ans[N], s[N];
query q[N];
void add(int i, int a) {
while (i <= n) {
s[i] += a;
i += i & -i;
}
}
int getsum(int i) {
int ret = 0;
while (i) {
ret += s[i];
i -= i & -i;
}
return ret;
}
int main() {
cin >> n >> Q;
for (int i = 1; i <= n; ++i)
scanf("%d", &c[i]);
for (int i = 0, l, r; i < Q; ++i) {
scanf("%d %d", &l, &r);
q[i] = query(l, r, i);
}
unordered_map<int, int> mp;
sort(q, q + Q);
for (int i = 0, pre = 1; i < Q; ++i) {
for (int j = pre; j <= q[i].r; ++j) {
add(j, 1);
auto ptr = mp.find(c[j]);
if (ptr != mp.end())
add(mp[c[j]], -1);
mp[c[j]] = j;
}
ans[q[i].id] = getsum(q[i].r) - getsum(q[i].l - 1);
pre = q[i].r + 1;
}
for (int i = 0; i < Q; ++i)
printf("%d\n", ans[i]);
return 0;
}
| #include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
const int N = 500003;
struct query {
int l, r, id;
query() {}
query(int ll, int rr, int d) { l = ll, r = rr, id = d; }
bool operator<(const query &x) { return r < x.r; }
};
int n, k, Q, c[N], ans[N], s[N];
query q[N];
void add(int i, int a) {
while (i <= n) {
s[i] += a;
i += i & -i;
}
}
int getsum(int i) {
int ret = 0;
while (i) {
ret += s[i];
i -= i & -i;
}
return ret;
}
int main() {
cin >> n >> Q;
for (int i = 1; i <= n; ++i)
scanf("%d", &c[i]);
for (int i = 0, l, r; i < Q; ++i) {
scanf("%d %d", &l, &r);
q[i] = query(l, r, i);
}
unordered_map<int, int> mp;
sort(q, q + Q);
for (int i = 0, pre = 1; i < Q; ++i) {
for (int j = pre; j <= q[i].r; ++j) {
add(j, 1);
auto ptr = mp.find(c[j]);
if (ptr != mp.end())
add(mp[c[j]], -1);
mp[c[j]] = j;
}
ans[q[i].id] = getsum(q[i].r) - getsum(q[i].l - 1);
pre = q[i].r + 1;
}
for (int i = 0; i < Q; ++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;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
const int inf = 1e9 + 7;
const ll INF = 1e18;
const int DAT_SIZE = 1 << 18;
const int MAX_N = 500050;
int N;
vll dat1(DAT_SIZE), dat2(DAT_SIZE);
vll v(MAX_N);
//[a,b)にxを加算
// 0-indexであることに注意
void add(int a, int b, ll x, int k = 0, int l = 0, int r = N) {
if (a <= l && r <= b)
dat1[k] += x;
else if (l < b && a < r) {
dat2[k] += (ll)(min(b, r) - max(a, l)) * x;
add(a, b, x, 2 * k + 1, l, (l + r) / 2);
add(a, b, x, 2 * k + 2, (l + r) / 2, r);
}
}
// 初期化
void init(int n) {
N = 1;
while (N < n)
N *= 2;
// rep(i,n)add(i,i+1,v[i],0,0,n);
}
//[a,b)の和
// 0-indexであることに注意
ll getsum(int a, int b, int k = 0, int l = 0, int r = N) {
if (b <= l || r <= a)
return 0;
else if (a <= l && r <= b)
return dat1[k] * (ll)(r - l) + dat2[k];
else {
ll res = (ll)(min(b, r) - max(a, l)) * dat1[k];
res += getsum(a, b, 2 * k + 1, l, (l + r) / 2);
res += getsum(a, b, 2 * k + 2, (l + r) / 2, r);
return res;
}
}
int main() {
int n, q;
cin >> n >> q;
vin c(n);
rep(i, n) cin >> c[i];
vector<tuple<int, int, int>> query(q);
int l, r;
rep(i, q) {
cin >> l >> r;
l--;
r--;
query[i] = make_tuple(r, l, i);
}
sort(all(query));
init(n);
int x, y, z;
int tmp = 0;
vin prev(n + 1, -1);
vin ans(q);
rep(i, q) {
y = get<0>(query[i]);
x = get<1>(query[i]);
z = get<2>(query[i]);
rep2(j, tmp, y + 1) {
add(prev[c[j]] + 1, j + 1, 1);
prev[c[j]] = j;
}
tmp = y;
ans[z] = getsum(x, x + 1);
}
rep(i, q) cout << ans[i] << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
const int inf = 1e9 + 7;
const ll INF = 1e18;
const int DAT_SIZE = 1 << 22;
const int MAX_N = 500050;
int N;
vll dat1(DAT_SIZE), dat2(DAT_SIZE);
vll v(MAX_N);
//[a,b)にxを加算
// 0-indexであることに注意
void add(int a, int b, ll x, int k = 0, int l = 0, int r = N) {
if (a <= l && r <= b)
dat1[k] += x;
else if (l < b && a < r) {
dat2[k] += (ll)(min(b, r) - max(a, l)) * x;
add(a, b, x, 2 * k + 1, l, (l + r) / 2);
add(a, b, x, 2 * k + 2, (l + r) / 2, r);
}
}
// 初期化
void init(int n) {
N = 1;
while (N < n)
N *= 2;
// rep(i,n)add(i,i+1,v[i],0,0,n);
}
//[a,b)の和
// 0-indexであることに注意
ll getsum(int a, int b, int k = 0, int l = 0, int r = N) {
if (b <= l || r <= a)
return 0;
else if (a <= l && r <= b)
return dat1[k] * (ll)(r - l) + dat2[k];
else {
ll res = (ll)(min(b, r) - max(a, l)) * dat1[k];
res += getsum(a, b, 2 * k + 1, l, (l + r) / 2);
res += getsum(a, b, 2 * k + 2, (l + r) / 2, r);
return res;
}
}
int main() {
int n, q;
cin >> n >> q;
vin c(n);
rep(i, n) cin >> c[i];
vector<tuple<int, int, int>> query(q);
int l, r;
rep(i, q) {
cin >> l >> r;
l--;
r--;
query[i] = make_tuple(r, l, i);
}
sort(all(query));
init(n);
int x, y, z;
int tmp = 0;
vin prev(n + 1, -1);
vin ans(q);
rep(i, q) {
y = get<0>(query[i]);
x = get<1>(query[i]);
z = get<2>(query[i]);
rep2(j, tmp, y + 1) {
add(prev[c[j]] + 1, j + 1, 1);
prev[c[j]] = j;
}
tmp = y;
ans[z] = getsum(x, x + 1);
}
rep(i, q) cout << ans[i] << endl;
} | replace | 23 | 24 | 23 | 24 | 0 | |
p02599 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;
// 参考:https://qiita.com/drken/items/1b7e6e459c24a83bb7fd
// 1-indexed BIT
// add:log(N),sum:log(N),get_k-th:log(N)
// https://codeforces.com/contest/1354/problem/D
struct BIT {
vector<ll> dat;
BIT(int n) {
dat.resize(n + 1);
for (int i = 0; i < (int)dat.size(); i++)
dat[i] = 0;
}
// a is 1-indexed
void add(int a, ll x) {
for (int i = a; i < (int)dat.size(); i += i & -i)
dat[i] = dat[i] + x;
}
// [1,a], a is 1-indexed
ll sum(int a) {
ll res = 0;
for (int i = a; i > 0; i -= i & -i) {
res = res + dat[i];
}
return res;
}
// [a,b), a and b are 1-indexed
ll sum(int a, int b) { return sum(b - 1) - sum(a - 1); }
// k-th number (k is 1-indexed)
// lower_bound
int get(ll k) {
if (k <= 0)
return 0;
int res = 0;
int N = 1;
while (N < (int)dat.size())
N *= 2;
for (int i = N / 2; i > 0; i /= 2) {
if (res + i < (int)dat.size() && dat[res + i] < k) {
k = k - dat[res + i];
res = res + i;
}
}
return res + 1;
}
};
int main() {
int N, Q;
cin >> N >> Q;
vector<int> C(N);
rep(i, N) cin >> C[i];
vector<int> last(N + 1, -1);
vector<pair<pii, int>> query(Q);
rep(i, Q) {
int l, r;
cin >> l >> r;
query[i] = MP(MP(r, l), i);
}
vector<int> res(Q);
sort(All(query));
int lastr = query[0].first.first;
BIT bit(N + 1);
rep(i, lastr) { last[C[i]] = i + 1; }
rep(i, N + 1) {
if (last[i] != -1)
bit.add(last[i], 1);
}
rep(i, Q) {
for (int j = lastr + 1; j <= query[i].first.first; j++) {
int tmp = last[C[j - 1]];
last[C[j - 1]] = j;
if (tmp > 0)
bit.add(tmp, -1);
bit.add(j, 1);
}
res[query[i].second] =
bit.sum(query[i].first.second, query[i].first.first + 1);
}
rep(i, Q) { cout << res[i] << endl; }
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;
// 参考:https://qiita.com/drken/items/1b7e6e459c24a83bb7fd
// 1-indexed BIT
// add:log(N),sum:log(N),get_k-th:log(N)
// https://codeforces.com/contest/1354/problem/D
struct BIT {
vector<ll> dat;
BIT(int n) {
dat.resize(n + 1);
for (int i = 0; i < (int)dat.size(); i++)
dat[i] = 0;
}
// a is 1-indexed
void add(int a, ll x) {
for (int i = a; i < (int)dat.size(); i += i & -i)
dat[i] = dat[i] + x;
}
// [1,a], a is 1-indexed
ll sum(int a) {
ll res = 0;
for (int i = a; i > 0; i -= i & -i) {
res = res + dat[i];
}
return res;
}
// [a,b), a and b are 1-indexed
ll sum(int a, int b) { return sum(b - 1) - sum(a - 1); }
// k-th number (k is 1-indexed)
// lower_bound
int get(ll k) {
if (k <= 0)
return 0;
int res = 0;
int N = 1;
while (N < (int)dat.size())
N *= 2;
for (int i = N / 2; i > 0; i /= 2) {
if (res + i < (int)dat.size() && dat[res + i] < k) {
k = k - dat[res + i];
res = res + i;
}
}
return res + 1;
}
};
int main() {
int N, Q;
cin >> N >> Q;
vector<int> C(N);
rep(i, N) cin >> C[i];
vector<int> last(N + 1, -1);
vector<pair<pii, int>> query(Q);
rep(i, Q) {
int l, r;
cin >> l >> r;
query[i] = MP(MP(r, l), i);
}
vector<int> res(Q);
sort(All(query));
int lastr = query[0].first.first;
BIT bit(N + 1);
rep(i, lastr) { last[C[i]] = i + 1; }
rep(i, N + 1) {
if (last[i] != -1)
bit.add(last[i], 1);
}
rep(i, Q) {
for (int j = lastr + 1; j <= query[i].first.first; j++) {
int tmp = last[C[j - 1]];
last[C[j - 1]] = j;
if (tmp > 0)
bit.add(tmp, -1);
bit.add(j, 1);
lastr = query[i].first.first;
}
res[query[i].second] =
bit.sum(query[i].first.second, query[i].first.first + 1);
}
rep(i, Q) { cout << res[i] << endl; }
return 0;
} | insert | 103 | 103 | 103 | 104 | TLE | |
p02599 | C++ | Runtime Error |
/**Which of the favors of your Lord will you deny ?**/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define MP make_pair
#define F first
#define S second
#define INF INT_MAX
#define ALL(x) (x).begin(), (x).end()
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class TIn>
using indexed_set = tree<TIn, null_type, less<TIn>, rb_tree_tag,
tree_order_statistics_node_update>;
/**
PBDS
-------------------------------------------------
1) insert(value)
2) erase(value)
3) order_of_key(value) // 0 based indexing
4) *find_by_order(position) // 0 based indexing
**/
inline void optimizeIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int nmax = 3e4 + 7;
const int nmax2 = 1e6 + 7;
const LL LINF = 1e17;
string to_str(LL x) {
stringstream ss;
ss << x;
return ss.str();
}
int ara[nmax];
/** Mo's Algorithm **/
/*** 1 based indexing ***/
int block_size;
struct Query {
int l, r, idx;
bool operator<(Query other) const {
return make_pair(l / block_size, r) <
make_pair(other.l / block_size, other.r);
}
};
int cnt[nmax2];
int max_cnt[nmax2]; /** this part is needed because when a value is removed , we
need to check if this value was the only one with maximum
freq **/
int st = 0; /** set size **/
inline void init(int n) {
st = 0;
fill(cnt, cnt + nmax2, 0);
fill(max_cnt, max_cnt + nmax2, 0);
block_size = sqrt(n);
}
void remov(int idx); // TODO: remove value at idx from data structure
void add(int idx); // TODO: add value at idx from data structure
int get_answer(); // TODO: extract the current answer of the data structure
inline void add(int idx) {
int val = ara[idx];
if (cnt[val] == 0)
st++;
cnt[val]++;
}
inline void remov(int idx) {
int val = ara[idx];
cnt[val]--;
if (cnt[val] == 0)
st--;
}
inline int get_answer() { return st; }
vector<int> mo_s_algorithm(vector<Query> queries) {
vector<int> answers(queries.size() + 1);
sort(queries.begin(), queries.end());
// TODO: initialize data structure
int cur_l = 0;
int cur_r = -1;
// invariant: data structure will always reflect the range [cur_l, cur_r]
for (Query q : queries) {
while (cur_l > q.l) {
cur_l--;
add(cur_l);
}
while (cur_r < q.r) {
cur_r++;
add(cur_r);
}
while (cur_l < q.l) {
remov(cur_l);
cur_l++;
}
while (cur_r > q.r) {
remov(cur_r);
cur_r--;
}
answers[q.idx] = get_answer();
}
return answers;
}
int main() {
int n, q;
scanf("%d %d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &ara[i]);
}
init(n); /** IMP **/
vector<Query> qv;
for (int i = 1; i <= q; i++) {
int l, r;
scanf("%d %d", &l, &r);
qv.push_back({l, r, i});
}
auto ans = mo_s_algorithm(qv);
for (int i = 1; i <= q; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
/**
10 10
2 5 6 5 2 1 7 9 7 2
5 5
2 4
6 7
2 2
7 8
7 9
1 8
6 9
8 10
6 8
1
2
2
1
2
2
6
3
3
3
**/
|
/**Which of the favors of your Lord will you deny ?**/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define MP make_pair
#define F first
#define S second
#define INF INT_MAX
#define ALL(x) (x).begin(), (x).end()
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class TIn>
using indexed_set = tree<TIn, null_type, less<TIn>, rb_tree_tag,
tree_order_statistics_node_update>;
/**
PBDS
-------------------------------------------------
1) insert(value)
2) erase(value)
3) order_of_key(value) // 0 based indexing
4) *find_by_order(position) // 0 based indexing
**/
inline void optimizeIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int nmax = 5e5 + 7;
const int nmax2 = nmax;
const LL LINF = 1e17;
string to_str(LL x) {
stringstream ss;
ss << x;
return ss.str();
}
int ara[nmax];
/** Mo's Algorithm **/
/*** 1 based indexing ***/
int block_size;
struct Query {
int l, r, idx;
bool operator<(Query other) const {
return make_pair(l / block_size, r) <
make_pair(other.l / block_size, other.r);
}
};
int cnt[nmax2];
int max_cnt[nmax2]; /** this part is needed because when a value is removed , we
need to check if this value was the only one with maximum
freq **/
int st = 0; /** set size **/
inline void init(int n) {
st = 0;
fill(cnt, cnt + nmax2, 0);
fill(max_cnt, max_cnt + nmax2, 0);
block_size = sqrt(n);
}
void remov(int idx); // TODO: remove value at idx from data structure
void add(int idx); // TODO: add value at idx from data structure
int get_answer(); // TODO: extract the current answer of the data structure
inline void add(int idx) {
int val = ara[idx];
if (cnt[val] == 0)
st++;
cnt[val]++;
}
inline void remov(int idx) {
int val = ara[idx];
cnt[val]--;
if (cnt[val] == 0)
st--;
}
inline int get_answer() { return st; }
vector<int> mo_s_algorithm(vector<Query> queries) {
vector<int> answers(queries.size() + 1);
sort(queries.begin(), queries.end());
// TODO: initialize data structure
int cur_l = 0;
int cur_r = -1;
// invariant: data structure will always reflect the range [cur_l, cur_r]
for (Query q : queries) {
while (cur_l > q.l) {
cur_l--;
add(cur_l);
}
while (cur_r < q.r) {
cur_r++;
add(cur_r);
}
while (cur_l < q.l) {
remov(cur_l);
cur_l++;
}
while (cur_r > q.r) {
remov(cur_r);
cur_r--;
}
answers[q.idx] = get_answer();
}
return answers;
}
int main() {
int n, q;
scanf("%d %d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &ara[i]);
}
init(n); /** IMP **/
vector<Query> qv;
for (int i = 1; i <= q; i++) {
int l, r;
scanf("%d %d", &l, &r);
qv.push_back({l, r, i});
}
auto ans = mo_s_algorithm(qv);
for (int i = 1; i <= q; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
/**
10 10
2 5 6 5 2 1 7 9 7 2
5 5
2 4
6 7
2 2
7 8
7 9
1 8
6 9
8 10
6 8
1
2
2
1
2
2
6
3
3
3
**/
| replace | 42 | 44 | 42 | 44 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.