Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> /*#pragma GCC optimize("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/ #define mp make_pair #define pb push_back #define pll pair<LL,LL> #define pii pair<int,int> #define y second #define x first #define LL long long #define sqr(x) ((x)*(x)) #define pi acosl(-1) #define MEM(x) memset(x,0,sizeof(x)) #define MEMS(x) memset(x,-1,sizeof(x)) using namespace std; LL gor[200005][20]; LL valr[200005][20]; LL gol[200005][20]; LL vall[2000005][20]; void solve(){ int n,k; scanf("%d %d",&n,&k); int x[200005]; for(int i = 1;i<=n;i++){ scanf("%d",&x[i]); } for(int i = 0;i<20;i++){ gor[n+1][i]=n+1; gol[0][i]=0; } int now=0; for(int i = 1;i<=n;i++){ while(now!=n+1&&x[now]-x[i]<k){ now++; } gor[i][0]=now; valr[i][0]=i; } now=n; for(int i =n;i>=1;i--){ while(now!=0&&x[i]-x[now]<k){ now--; } gol[i][0]=now; vall[i][0]=i; } for(int i = 1;i<20;i++){ for(int j = 1;j<=n;j++){ gol[j][i]=gol[gol[j][i-1]][i-1]; vall[j][i]=vall[j][i-1]+vall[gol[j][i-1]][i-1]; gor[j][i]=gor[gor[j][i-1]][i-1]; valr[j][i]=valr[j][i-1]+valr[gor[j][i-1]][i-1]; // printf("%d %d %lld %lld %lld %lld\n",i,j,gol[j][i],vall[j][i],gor[j][i],valr[j][i]); } } int q; scanf("%d",&q); while(q--){ int l,r; scanf("%d %d",&l,&r); LL ans=0; int now=r; for(int i = 19;i>=0;i--){ if(gol[now][i]>=l){ ans+=vall[now][i]+(1<<i); now=gol[now][i]; //printf("%lld %d %d\n",now,ans,i); } } ans+=now+1; // printf("%lld\n",ans); now=l; for(int i = 19;i>=0;i--){ if(gor[now][i]<=r){ ans-=valr[now][i]; // printf("%d %d\n",i,valr[now][i]); now=gor[now][i]; } } ans-=now; printf("%lld\n",ans); } } int main(){ int t=1; //scanf("%d",&t); while(t--){ solve(); } } /* 1 6 6 2 7 4 3 5 2 4 3 1 5 2 5 6 4 0 7 1 3 */ /* 100 5 5 4 4 2 4 2 3 5 4 2 3 1 */
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define SZ(x) ((int)((x).size())) template <typename T1, typename T2> string print_iterable(T1 begin_iter, T2 end_iter, int counter) { bool done_something = false; stringstream res; res << "["; for (; begin_iter != end_iter and counter; ++begin_iter) { done_something = true; counter--; res << *begin_iter << ", "; } string str = res.str(); if (done_something) { str.pop_back(); str.pop_back(); } str += "]"; return str; } vector<int> SortIndex(int size, std::function<bool(int, int)> compare) { vector<int> ord(size); for (int i = 0; i < size; i++) ord[i] = i; sort(ord.begin(), ord.end(), compare); return ord; } template <typename T> bool MinPlace(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool MaxPlace(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template <typename S, typename T> ostream& operator <<(ostream& out, const pair<S, T>& p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream& operator <<(ostream& out, const vector<T>& v) { out << "["; for (int i = 0; i < (int)v.size(); i++) { out << v[i]; if (i != (int)v.size()-1) out << ", "; } out << "]"; return out; } template<class TH> void _dbg(const char* name, TH val){ clog << name << ": " << val << endl; } template<class TH, class... TA> void _dbg(const char* names, TH curr_val, TA... vals) { while(*names != ',') clog << *names++; clog << ": " << curr_val << ", "; _dbg(names+1, vals...); } #if DEBUG && !ONLINE_JUDGE ifstream input_from_file("input.txt"); #define cin input_from_file #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define dbg_arr(x, len) clog << #x << ": " << print_iterable(x, x+len, -1) << endl; #else #define dbg(...) #define dbg_arr(x, len) #endif /////////////////////////////////////////////////////////////////////////// //////////////////// DO NOT TOUCH BEFORE THIS LINE //////////////////////// /////////////////////////////////////////////////////////////////////////// const int MAXN = 200100; const int MAXL = 20; LL X[MAXN]; int aft[MAXN][MAXL]; int bef[MAXN][MAXL]; LL sum_aft[MAXN][MAXL]; LL sum_bef[MAXN][MAXL]; int main() { ios::sync_with_stdio(false); cin.tie(0); // Remove in problems with online queries! int N; LL K; cin >> N >> K; for (int i = 1; i <= N; i++) cin >> X[i]; aft[N][0] = N+1; for (int i = N-1; i >= 0; i--) { aft[i][0] = aft[i+1][0]; while (X[aft[i][0]-1] >= X[i] + K) aft[i][0]--; } bef[1][0] = 0; for (int i = 2; i <= N; i++) { bef[i][0] = bef[i-1][0]; while (X[bef[i][0]+1] + K <= X[i]) bef[i][0]++; } for (int l = 0; l < MAXL; l++) aft[N+1][l] = N+1, bef[0][l] = 0; for (int l = 1; l < MAXL; l++) { for (int i = 1; i <= N; i++) { aft[i][l] = aft[aft[i][l-1]][l-1]; bef[i][l] = bef[bef[i][l-1]][l-1]; } } for (int i = 1; i <= N; i++) sum_bef[i][0] = i, sum_aft[i][0] = i; for (int l = 1; l < MAXL; l++) { for (int i = 1; i <= N; i++) { sum_bef[i][l] = sum_bef[i][l-1] + sum_bef[bef[i][l-1]][l-1]; sum_aft[i][l] = sum_aft[i][l-1] + sum_aft[aft[i][l-1]][l-1]; } } int Q; cin >> Q; for (int q = 0; q < Q; q++) { int a, b; cin >> a >> b; int len = 1; int x = a; for (int l = MAXL-1; l >= 0; l--) { if (aft[x][l] <= b) { x = aft[x][l]; len += 1<<l; } } LL sum_a = 0; x = a; for (int l = MAXL-1; l >= 0; l--) { if (len & (1<<l)) { sum_a += sum_aft[x][l]; x = aft[x][l]; } } LL sum_b = 0; x = b; for (int l = MAXL-1; l >= 0; l--) { if (len & (1<<l)) { sum_b += sum_bef[x][l]; x = bef[x][l]; } } cout << len + sum_b - sum_a << "\n"; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> #define mp make_pair #define pb push_back #define X first #define Y second #define y0 y12 #define y1 y22 #define INF 987654321 #define PI 3.141592653589793238462643383279502884 #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c)) #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c)) #define MEM0(a) memset((a),0,sizeof(a)); #define MEM_1(a) memset((a),-1,sizeof(a)); #define ALL(a) a.begin(),a.end() #define SYNC ios_base::sync_with_stdio(false);cin.tie(0) using namespace std; typedef long long ll; typedef long double ld; typedef double db; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int, int> Pi; typedef pair<ll, ll> Pll; typedef pair<ld, ld> Pd; typedef vector<int> Vi; typedef vector<ll> Vll; typedef vector<db> Vd; typedef vector<Pi> VPi; typedef vector<Pll> VPll; typedef vector<Pd> VPd; typedef tuple<int, int, int> iii; typedef tuple<int, int, int, int> iiii; typedef tuple<ll, ll, ll> LLL; typedef vector<iii> Viii; typedef vector<LLL> VLLL; typedef complex<double> base; const int MOD = 1000000007; ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; } ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); } int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 }; int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 }; int a[200001],ne[200001][18],pr[200001][18]; ll Ls[200001][18],Rs[200001][18]; int main() { int n,k; scanf("%d%d",&n,&k); fup(i,1,n,1)scanf("%d",a+i); int t=1; fup(i,1,n,1){ while(t<=n && a[t]-a[i]<k)t++; ne[i][0]=t; } t=n; fdn(i,n,1,1){ while(t>=0 && a[i]-a[t]<k)t--; pr[i][0]=t; } fup(j,0,16,1){ fup(i,1,n,1){ if(ne[i][j]<=n)ne[i][j+1]=ne[ne[i][j]][j]; if(pr[i][j]>0)pr[i][j+1]=pr[pr[i][j]][j]; } } fup(i,1,n,1){ Ls[i][0]=-(i-1); Rs[i][0]=i; } fup(j,0,16,1){ fup(i,1,n,1){ if(ne[i][j]<=n)Ls[i][j+1]=Ls[i][j]+Ls[ne[i][j]][j]; if(pr[i][j]>0)Rs[i][j+1]=Rs[i][j]+Rs[pr[i][j]][j]; } } int q; scanf("%d",&q); while(q--){ int l,r; scanf("%d%d",&l,&r); int now=l; int cnt=0; fdn(j,17,0,1){ if(ne[now][j]>0 && ne[now][j]<=r){ cnt+=1<<j; now=ne[now][j]; } } cnt++; ll ans=0; fdn(j,17,0,1){ if(cnt&(1<<j)){ ans+=Ls[l][j]+Rs[r][j]; l=ne[l][j],r=pr[r][j]; } } printf("%lld\n",ans); } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb(x) push_back(x) #define mp(a, b) make_pair(a, b) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define lscan(x) scanf("%I64d", &x) #define lprint(x) printf("%I64d", x) #define rep(i, n) for (ll i = 0; i < (n); i++) #define rep2(i, n) for (ll i = n - 1; i >= 0; i--) const int mod = 998244353; ll gcd(ll a, ll b) { ll c = a % b; while (c != 0) { a = b; b = c; c = a % b; } return b; } long long extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } struct UnionFind { vector<ll> data; UnionFind(int sz) { data.assign(sz, -1); } bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return (false); if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return (true); } int find(int k) { if (data[k] < 0) return (k); return (data[k] = find(data[k])); } ll size(int k) { return (-data[find(k)]); } }; ll M = 1000000007; vector<ll> fac(2000011); //n!(mod M) vector<ll> ifac(2000011); //k!^{M-2} (mod M) ll mpow(ll x, ll n) { ll ans = 1; while (n != 0) { if (n & 1) ans = ans * x % M; x = x * x % M; n = n >> 1; } return ans; } ll mpow2(ll x, ll n, ll mod) { ll ans = 1; while (n != 0) { if (n & 1) ans = ans * x % mod; x = x * x % mod; n = n >> 1; } return ans; } void setcomb() { fac[0] = 1; ifac[0] = 1; for (ll i = 0; i < 2000010; i++) { fac[i + 1] = fac[i] * (i + 1) % M; // n!(mod M) } ifac[2000010] = mpow(fac[2000010], M - 2); for (ll i = 2000010; i > 0; i--) { ifac[i - 1] = ifac[i] * i % M; } } ll comb(ll a, ll b) { if (a == 0 && b == 0) return 1; if (a < b || a < 0) return 0; ll tmp = ifac[a - b] * ifac[b] % M; return tmp * fac[a] % M; } ll perm(ll a, ll b) { if (a == 0 && b == 0) return 1; if (a < b || a < 0) return 0; return fac[a] * ifac[a - b] % M; } long long modinv(long long a) { long long b = M, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= M; if (u < 0) u += M; return u; } ll modinv2(ll a, ll mod) { ll b = mod, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= mod; if (u < 0) u += mod; return u; } 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; } }; using mint = ModInt<mod>; vector<vector<mint>> mul(vector<vector<mint>> a, vector<vector<mint>> b) { int i, j, k; mint t; int n = a.size(), m = b[0].size(), l = a[0].size(); vector<vector<mint>> c(n,vector<mint>(m)); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { t = 0; for (k = 0; k < l; k++) t += a[i][k] * b[k][j]; c[i][j] = t; } } return c; } vector<vector<mint>> mat_pow(vector<vector<mint>> x, ll n) { ll k = x.size(); vector<vector<mint>> ans(k, vector<mint>(k, 0)); for (int i = 0; i < k; i++) ans[i][i] = 1; while (n != 0) { if (n & 1) ans = mul(ans, x); x = mul(x, x); n = n >> 1; } return ans; } template <typename Monoid> struct SegmentTree { using F = function<Monoid(Monoid, Monoid)>; int sz; vector<Monoid> seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) { sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set(int k, const Monoid &x) { seg[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid &x) { k += sz; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } template <typename C> int find_subtree(int a, const C &check, Monoid &M, bool type) { while (a < sz) { Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template <typename C> int find_first(int a, const C &check) { Monoid L = M1; if (a <= 0) { if (check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, seg[a]); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template <typename C> int find_last(int b, const C &check) { Monoid R = M1; if (b >= sz) { if (check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(seg[--b], R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; template <unsigned mod> struct RollingHash { vector<unsigned> hashed, power; inline unsigned mul(unsigned a, unsigned b) const { unsigned long long x = (unsigned long long)a * b; unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod)); return m; } RollingHash(const string &s, unsigned base = 10007) { int sz = (int)s.size(); hashed.assign(sz + 1, 0); power.assign(sz + 1, 0); power[0] = 1; for (int i = 0; i < sz; i++) { power[i + 1] = mul(power[i], base); hashed[i + 1] = mul(hashed[i], base) + s[i]; if (hashed[i + 1] >= mod) hashed[i + 1] -= mod; } } unsigned get(int l, int r) const { unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]); if (ret >= mod) ret -= mod; return ret; } unsigned connect(unsigned h1, int h2, int h2len) const { unsigned ret = mul(h1, power[h2len]) + h2; if (ret >= mod) ret -= mod; return ret; } int LCP(const RollingHash<mod> &b, int l1, int r1, int l2, int r2) { int len = min(r1 - l1, r2 - l2); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(l1, l1 + mid) == b.get(l2, l2 + mid)) low = mid; else high = mid; } return (low); } }; using RH = RollingHash<1000000007>; 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; } 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>>; template <typename T> using Matrix = vector<vector<T>>; template <typename G> struct DoublingLowestCommonAncestor { const int LOG; vector<int> dep; const G &g; vector<vector<int>> table; DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) { table.assign(LOG, vector<int>(g.size(), -1)); } void dfs(int idx, int par, int d) { table[0][idx] = par; dep[idx] = d; for (auto &to : g[idx]) { if (to != par) dfs(to, idx, d + 1); } } void build() { dfs(0, -1, 0); for (int k = 0; k + 1 < LOG; k++) { for (int i = 0; i < table[k].size(); i++) { if (table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int i = LOG - 1; i >= 0; i--) { if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v]; } if (u == v) return u; for (int i = LOG - 1; i >= 0; i--) { if (table[i][u] != table[i][v]) { u = table[i][u]; v = table[i][v]; } } return table[0][u]; } }; template <typename Monoid, typename OperatorMonoid = Monoid> struct LazySegmentTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; int sz, height; vector<Monoid> data; vector<OperatorMonoid> lazy; const F f; const G g; const H h; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), M1(M1), OM0(OM0) { sz = 1; height = 0; while (sz < n) sz <<= 1, height++; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid &x) { data[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } inline void propagate(int k) { if (lazy[k] != OM0) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); data[k] = reflect(k); lazy[k] = OM0; } } inline Monoid reflect(int k) { return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]); } inline void recalc(int k) { while (k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1)); } inline void thrust(int k) { for (int i = height; i > 0; i--) propagate(k >> i); } void update(int a, int b, const OperatorMonoid &x) { thrust(a += sz); thrust(b += sz - 1); for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) lazy[l] = h(lazy[l], x), ++l; if (r & 1) --r, lazy[r] = h(lazy[r], x); } recalc(a); recalc(b); } Monoid query(int a, int b) { thrust(a += sz); thrust(b += sz - 1); Monoid L = M1, R = M1; for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) L = f(L, reflect(l++)); if (r & 1) R = f(reflect(--r), R); } return f(L, R); } Monoid operator[](const int &k) { return query(k, k + 1); } template <typename C> int find_subtree(int a, const C &check, Monoid &M, bool type) { while (a < sz) { propagate(a); Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type)); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template <typename C> int find_first(int a, const C &check) { Monoid L = M1; if (a <= 0) { if (check(f(L, reflect(1)))) return find_subtree(1, check, L, false); return -1; } thrust(a + sz); int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, reflect(a)); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template <typename C> int find_last(int b, const C &check) { Monoid R = M1; if (b >= sz) { if (check(f(reflect(1), R))) return find_subtree(1, check, R, true); return -1; } thrust(b + sz - 1); int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(reflect(--b), R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; ll nl[200010][20], nr[200010][20], sr[200010][20], sl[200010][20]; int main() { ll n, k, x[222222], q; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> x[i]; x[0] = -1e9; x[n + 1] = 2e9; ll l, r; r = 1; for (int i = 1; i <= n;i++){ while(x[r]-x[i]<k) r++; nr[i][0] = r; sr[i][0] = i; } nr[0][0] = 0; nr[n + 1][0] = n + 1; l = n; for (int i = n; i >= 1; i--){ while (x[i] - x[l] < k) l--; nl[i][0] = l; sl[i][0] = i; } nl[0][0] = 0; nl[n + 1][0] = n + 1; rep(j, 19) rep(i, n + 2) nr[i][j + 1] = nr[nr[i][j]][j], nl[i][j + 1] = nl[nl[i][j]][j], sr[i][j+1]=sr[i][j]+sr[nr[i][j]][j], sl[i][j+1]=sl[i][j]+sl[nl[i][j]][j]; cin >> q; rep(i,q){ ll L, R; cin >> L >> R; ll ok = 0, ng = n; while(ng-ok>1){ ll mid = (ok + ng) / 2; ll now = L; rep(j, 20) if ((mid >> j) & 1) now = nr[now][j]; if(R < now) ng = mid; else ok = mid; } ll now = L, sumr = 0, suml = 0; rep(j, 20) if ((ok >> j) & 1) sumr += sr[now][j], now = nr[now][j]; sumr += now; now = R; rep(j, 20) if ((ok >> j) & 1) suml += sl[now][j], now = nl[now][j]; suml += now; cout << suml - sumr + ok + 1 << endl; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
import sys readline = sys.stdin.buffer.readline n,k = map(int,readline().split()) vs = list(map(int,readline().split())) L=18 xid=[0]*(n*L) xsum=[0]*(n*L) yid=[0]*(n*L) ysum=[0]*(n*L) j=n for i in reversed(range(n)): while i<j and vs[i]+k<=vs[j-1]: j-=1 xid[i*L+0]=j xsum[i*L+0]=j for lv in range(1,L): a=xid[i*L+lv-1] if a==n: xid[i*L+lv]=n else: xid[i*L+lv]=xid[a*L+lv-1] xsum[i*L+lv]=xsum[i*L+lv-1]+xsum[a*L+lv-1] j=-1 for i in range(n): while j<i and vs[j+1]+k<=vs[i]: j+=1 yid[i*L+0]=j ysum[i*L+0]=j for lv in range(1,L): a=yid[i*L+lv-1] if a==-1: yid[i*L+lv]=-1 else: yid[i*L+lv]=yid[a*L+lv-1] ysum[i*L+lv]=ysum[i*L+lv-1]+ysum[a*L+lv-1] q=int(readline()) for tmp in range(q): l,r=map(int,readline().split()) l-=1 r-=1 ans=0 i=l ans-=i for lv in reversed(range(L)): if xid[i*L+lv]<=r: ans-=xsum[i*L+lv] i=xid[i*L+lv] i=r ans+=i+1 for lv in reversed(range(L)): if yid[i*L+lv]>=l: ans+=ysum[i*L+lv]+(1<<lv) i=yid[i*L+lv] print(ans)
PYTHON3
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> #ifdef BIZON #define rr(x) cerr << "\e[1;38;5;172m" << #x << "\e[0m = " << (x) << endl; #else #define rr(x) #define endl '\n' #endif #define ALL(c) begin(c), end(c) using namespace std; using ll = long long; using ld = long double; int __; #define in (cin>>__, (int)__) int main(){ if(auto f="in.txt"; fopen(f,"r") && freopen(f,"r",stdin)); ios::sync_with_stdio(0);cin.tie(0);//cout.precision(9);cout<<fixed; int n = in, D = in; vector<int> x(n); for(auto &_ : x) cin>>_; constexpr int L = 18; vector<array<pair<int,ll>,L>> pl(n), pr(n); for(int i=n-1, j = n; i>=0; --i){ while(j-1>i && abs(x[j-1]-x[i])>=D) --j; pr[i][0] = {j, 0}; for(int l=1;l<L;++l){ int k = pr[i][l-1].first; if(k==n) pr[i][l] = pr[i][l-1]; else pr[i][l] = {pr[k][l-1].first, pr[k][l-1].second + pr[i][l-1].second + k}; } } for(int i=0, j = -1; i<n; ++i){ while(j+1<i && abs(x[j+1]-x[i])>=D) ++j; pl[i][0] = {j, 0}; for(int l=1;l<L;++l){ int k = pl[i][l-1].first; if(k==-1) pl[i][l] = pl[i][l-1]; else pl[i][l] = {pl[k][l-1].first, pl[k][l-1].second + pl[i][l-1].second + k}; } } int m = in; while(m--){ int l = in-1, r = in-1; int fr = l, cr = 1; ll sr = 0; for(int k=L-1; k>=0; --k) if(pr[fr][k].first<=r) sr+=fr+pr[fr][k].second, fr = pr[fr][k].first, cr+=1<<k; sr+=fr; int fl = r, cl = 1; ll sl = 0; for(int k=L-1; k>=0; --k) if(pl[fl][k].first>=l) sl+=fl+pl[fl][k].second, fl = pl[fl][k].first, cl+=1<<k; sl+=fl; //cout<<cl<<' '<<cr<<endl; assert(cl==cr); ll res = cl + sl - sr; cout<<res<<endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
// // Created by yamunaku on 2020/09/20. // #include <bits/stdc++.h> //#include <atcoder/all> using namespace std; #define rep(i, n) for(int i = 0; i < (n); i++) #define repl(i, l, r) for(int i = (l); i < (r); i++) #define per(i, n) for(int i = ((n)-1); i >= 0; i--) #define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--) #define all(x) (x).begin(),(x).end() #define MOD9 998244353 #define MOD1 1000000007 #define IINF 1000000000 #define LINF 1000000000000000000 #define SP <<" "<< #define CYES cout<<"Yes"<<endl #define CNO cout<<"No"<<endl #define CFS cin.tie(0);ios::sync_with_stdio(false) #define CST(x) cout<<fixed<<setprecision(x) using ll = long long; using ld = long double; using vi = vector<int>; using mti = vector<vector<int>>; using vl = vector<ll>; using mtl = vector<vector<ll>>; using pi = pair<int, int>; using pl = pair<ll, ll>; template<typename T> using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>; int main() { //CFS; int n, k; cin >> n >> k; vl a(n + 2); a[0] = -LINF, a[n + 1] = LINF; repl(i, 1, n + 1) cin >> a[i]; int lg = 0; int tmp = 1; while (tmp < n + 2) tmp *= 2, lg++; mti fl(lg, vi(n + 2)), fr(lg, vi(n + 2)); mtl sl(lg, vl(n + 2)), sr(lg, vl(n + 2)); fl[0][0] = fr[0][0] = 0; fl[0][n + 1] = fr[0][n + 1] = n + 1; sl[0][0] = sr[0][0] = 0; sl[0][n + 1] = sr[0][n + 1] = 0; repl(i, 1, n + 1) { fl[0][i] = lower_bound(all(a), a[i] + k) - a.begin(); fr[0][i] = upper_bound(all(a), a[i] - k) - a.begin() - 1; sl[0][i] = i; sr[0][i] = i + 1; } repl(t, 1, lg) { fl[t][0] = fr[t][0] = 0; fl[t][n + 1] = fr[t][n + 1] = n + 1; sl[t][0] = sr[t][0] = 0; sl[t][n + 1] = sr[t][n + 1] = 0; repl(i, 1, n + 1) { fl[t][i] = fl[t - 1][fl[t - 1][i]]; fr[t][i] = fr[t - 1][fr[t - 1][i]]; sl[t][i] = sl[t - 1][i] + sl[t - 1][fl[t - 1][i]]; sr[t][i] = sr[t - 1][i] + sr[t - 1][fr[t - 1][i]]; } } int q; cin >> q; rep(t, q) { int l, r; cin >> l >> r; int tl = l, tr = r; ll sum = 0; per(i, lg) { if (fl[i][tl] <= r) { sum -= sl[i][tl]; tl = fl[i][tl]; } if (fr[i][tr] >= l) { sum += sr[i][tr]; tr = fr[i][tr]; } } sum -= tl; sum += tr+1; cout << sum << endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <iostream> #include <string> #include <cstdlib> #include <cmath> #include <vector> #include <unordered_map> #include <map> #include <set> #include <algorithm> #include <queue> #include <stack> #include <functional> #include <bitset> #include <assert.h> #include <unordered_map> #include <fstream> #include <ctime> #include <complex> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll,ll> P; typedef pair<int,int> pii; typedef vector<P> vpl; typedef tuple<ll,ll,ll> tapu; #define rep(i,n) for(int i=0; i<(n); i++) #define REP(i,a,b) for(int i=(a); i<(b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = 1<<30; const ll linf = 1LL<<62; const int MAX = 2020000; ll dy[8] = {1,-1,0,0,1,-1,1,-1}; ll dx[8] = {0,0,1,-1,1,-1,-1,1}; const double pi = acos(-1); const double eps = 1e-7; template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){ if(a>b){ a = b; return true; } else return false; } template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){ if(a<b){ a = b; return true; } else return false; } template<typename T> inline void print(T &a){ for(auto itr = a.begin(); itr != a.end(); itr++){ cout << *itr << " "; } cout << "\n"; } template<typename T1,typename T2> inline void print2(T1 a, T2 b){ cout << "debug: " << a << " " << b << "\n"; } template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){ cout << "debug: " << a << " " << b << " " << c << "\n"; } void mark() {cout << "#" << "\n";} ll pcount(ll x) {return __builtin_popcountll(x);} const int mod = 1e9 + 7; //const int mod = 998244353; ll Lpos[20][202020], Lsum[20][202020]; ll Rpos[20][202020], Rsum[20][202020]; int main(){ ll n,k; cin >> n >> k; vl x(n); rep(i,n) cin >> x[i]; rep(i,n){ Lpos[0][i] = lower_bound(all(x), x[i] + k) - x.begin(); Rpos[0][i] = upper_bound(all(x), x[i] - k) - x.begin() - 1; Lsum[0][i] = Rsum[0][i] = i; } Lpos[0][n] = n; rep(i,19){ rep(j,n){ Lpos[i+1][j] = Lpos[i][Lpos[i][j]]; Lsum[i+1][j] = Lsum[i][j] + Lsum[i][Lpos[i][j]]; if(Rpos[i][j] != -1) Rpos[i+1][j] = Rpos[i][Rpos[i][j]]; else Rpos[i+1][j] = -1; Rsum[i+1][j] = Rsum[i][j]; if(Rpos[i][j] >= 0){ Rsum[i+1][j] += Rsum[i][Rpos[i][j]]; } } Lpos[i+1][n] = n; } int q; cin >> q; while(q--){ ll l,r; cin >> l >> r; l--; r--; ll pos = l; ll ls = 0, rs = 0; for(int i=19; i>=0; i--){ if(Lpos[i][pos] <= r){ ls += Lsum[i][pos]; pos = Lpos[i][pos]; } } ls += pos; pos = r; for(int i=19; i>=0; i--){ if(Rpos[i][pos] >= l){ rs += Rsum[i][pos] + (1<<i); pos = Rpos[i][pos]; } } rs += pos + 1; cout << rs - ls << "\n"; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> #define endl '\n' #define fi first #define se second #define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k)) #define forn(i,n) for (int i = 0; i < n; i++) #define forr(i,a,b) for (int i = a; i <= b; i++) #define all(v) v.begin(), v.end() #define pb(x) push_back(x) using namespace std; typedef long long ll; //typedef long double ld; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ii> vii; const int MX = 200005, LG = 20; int n, k, a[MX], q; int pi[MX][LG], pd[MX][LG]; ll si[MX][LG], sd[MX][LG]; int li[MX], ld[MX]; int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; forn (i, n) cin >> a[i]; for (int i = n - 1; i >= 0; i--) { int j = lower_bound(a, a + n, a[i] + k) - a; if (j == n) { pi[i][0] = -1; li[i] = 0; } else { pi[i][0] = j; li[i] = li[j] + 1; } si[i][0] = i; } for (int i = 0; i < n; i++) { int j = upper_bound(a, a + n, a[i] - k) - a; j--; if (j < 0) { pd[i][0] = -1; ld[i] = 0; } else { pd[i][0] = j; ld[i] = ld[j] + 1; } sd[i][0] = i; } for (int j = 1; j < LG; j++) forn (i, n) { if (pi[i][j - 1] != -1) { pi[i][j] = pi[pi[i][j - 1]][j - 1]; si[i][j] = si[i][j - 1] + si[pi[i][j - 1]][j - 1]; } else { pi[i][j] = -1; } if (pd[i][j - 1] != -1) { pd[i][j] = pd[pd[i][j - 1]][j - 1]; sd[i][j] = sd[i][j - 1] + sd[pd[i][j - 1]][j - 1]; } else { pd[i][j] = -1; } } cin >> q; while (q--) { int l, r; cin >> l >> r; l--, r--; ll sup = 0, to = l, cn = 1; for (int j = LG - 1; j >= 0; j--) if (pi[to][j] != -1 && pi[to][j] <= r) { sup += si[to][j]; to = pi[to][j]; cn += 1 << j; } sup += to; ll sdn = 0; to = r; for (int j = LG - 1; j >= 0; j--) if (pd[to][j] != -1 && pd[to][j] >= l) { sdn += sd[to][j]; to = pd[to][j]; } sdn += to; cout << sdn - sup + cn << endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) using namespace std; typedef long long ll; int n, k; int l, r; int a[200200]; int pre_pos[22][200200], suf_pos[22][200200]; int q; ll pre_sum[22][200200], suf_sum[22][200200]; int main(){ ios::sync_with_stdio(false); cin >> n >> k; rep(i, n) cin >> a[i]; r = 0; for(int i = 0; i < n; i++){ while(r < n && a[r] - a[i] < k) r++; suf_pos[0][i] = r; suf_sum[0][i] = i; } l = n-1; for(int i = n-1; i >= 0; i--){ while(l >= 0 && a[i] - a[l] < k) l--; pre_pos[0][i] = l; pre_sum[0][i] = i; } for(int i = 0; i < 20; i++){ for(int j = 0; j < n; j++){ pre_pos[i + 1][j] = (pre_pos[i][j] < 0) ? -1 : pre_pos[i][pre_pos[i][j]]; pre_sum[i + 1][j] = (pre_pos[i][j] < 0) ? pre_sum[i][j] : (ll)(pre_sum[i][j] + pre_sum[i][pre_pos[i][j]]); suf_pos[i + 1][j] = (suf_pos[i][j] >= n) ? n : suf_pos[i][suf_pos[i][j]]; suf_sum[i + 1][j] = (suf_pos[i][j] >= n) ? suf_sum[i][j] : (ll)(suf_sum[i][j] + suf_sum[i][suf_pos[i][j]]); } } cin >> q; while(q--){ cin >> l >> r; l--, r--; int nl = l, nr = r; int l_ans = 1, r_ans = 1; ll l_sum = 0, r_sum = 0; for(int i = 20; i >= 0; i--){ if(suf_pos[i][nl] <= r){ l_ans += (1 << i); l_sum += suf_sum[i][nl]; nl = suf_pos[i][nl]; } } l_sum += nl; for(int i = 20; i >= 0; i--){ if(pre_pos[i][nr] >= l){ r_ans += (1 << i); r_sum += pre_sum[i][nr]; nr = pre_pos[i][nr]; } } r_sum += nr; assert(l_ans == r_ans); cout << r_sum - l_sum + l_ans << endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> //#include <chrono> //#pragma GCC optimize("Ofast") using namespace std; #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() #define fi first #define se second using ll = long long; using vec = vector<ll>; using mat = vector<vec>; ll N,M,H,W,Q,K,A,B; string S; typedef pair<ll, ll> P; const ll INF = (1LL<<60); ll solve(int l, int r,mat &dv, mat &dvr, mat &dsum, mat &dsumr){ ll res(r - l); int nowl(l), nowr(r), len(1); Rrep(i, 18){ if(dv[i][nowl] <= r){ res -= dsum[i][nowl]; nowl = dv[i][nowl]; len += 1<<i; } if(dvr[i][nowr] >= l){ res += dsumr[i][nowr]; nowr = dvr[i][nowr]; } } return res + len; } int main() { cin>>N>>K; N += 2; vec x(N), x_rev(N); mat dbl(18, vec(N)), dbl_rev(18, vec(N)), dbl_sum(18, vec(N, 0)), dbl_rev_sum(18, vec(N, 0)); reps(i, 1, N - 1) cin>>x[i]; x[0] = -INF; x[N - 1] = INF; copy(x.rbegin(), x.rend(), x_rev.begin()); rep(i, N) x_rev[i] = - x_rev[i]; //reps(i, 1, N - 1) cout<<x_rev[i]<<" \n"[i == N - 2]; rep(i, N) { dbl[0][i] = dbl_sum[0][i] = min(N - 1, (ll)(lower_bound(ALL(x), x[i] + K) - x.begin())); dbl_rev[0][i] = dbl_rev_sum[0][i] = N - 1 - min(N - 1, (ll)(lower_bound(ALL(x_rev), x_rev[N - 1 - i] + K) - x_rev.begin())); //cout<<dbl_rev[0][i]<<" \n"[i == N - 1]; } reps(i, 1, 18) { rep(v, N) { dbl[i][v] = dbl[i-1][dbl[i-1][v]]; dbl_rev[i][v] = dbl_rev[i-1][dbl_rev[i-1][v]]; dbl_sum[i][v] = dbl_sum[i-1][v] + dbl_sum[i-1][dbl[i-1][v]]; dbl_rev_sum[i][v] = dbl_rev_sum[i-1][v] + dbl_rev_sum[i-1][dbl_rev[i-1][v]]; } } cin>>Q; rep(_, Q){ int l, r; cin>>l>>r; cout<<solve(l, r, dbl, dbl_rev, dbl_sum, dbl_rev_sum)<<endl; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
// Problem : D - Keep Distances // Contest : AtCoder - ACL Contest 1 // URL : https://atcoder.jp/contests/acl1/tasks/acl1_d // Memory Limit : 1024 MB // Time Limit : 3000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> using namespace std; constexpr int Maxn = 2e5 + 5; constexpr int LOG = 20; int n, K, q; int a[Maxn]; int pre[Maxn][LOG]; int suf[Maxn][LOG]; int64_t pre_sum[Maxn][LOG]; int64_t suf_sum[Maxn][LOG]; int main() { scanf("%d %d", &n, &K); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); } for (int i = 1; i <= n; ++i) { pre[i][0] = upper_bound(a + 1, a + n + 1, a[i] - K) - a - 1; suf[i][0] = lower_bound(a + 1, a + n + 1, a[i] + K) - a; pre_sum[i][0] = (pre[i][0] >= 1 ? pre[i][0] : 0); suf_sum[i][0] = (suf[i][0] <= n ? suf[i][0] : 0); } for (int j = 1; j < LOG; ++j) { for (int i = 1; i <= n; ++i) { pre_sum[i][j] = pre_sum[i][j - 1]; suf_sum[i][j] = suf_sum[i][j - 1]; if (pre[i][j - 1] >= 1) { pre[i][j] = pre[pre[i][j - 1]][j - 1]; pre_sum[i][j] += pre_sum[pre[i][j - 1]][j - 1]; } else { pre[i][j] = 0; } if (suf[i][j - 1] <= n) { suf[i][j] = suf[suf[i][j - 1]][j - 1]; suf_sum[i][j] += suf_sum[suf[i][j - 1]][j - 1]; } else { suf[i][j] = n + 1; } } } scanf("%d", &q); while (q--) { int l, r; scanf("%d %d", &l, &r); int64_t left_sum = 0; int64_t right_sum = 0; int len = 1; { int lg = LOG - 1, pos = r; while (lg >= 0) { if (pre[pos][lg] >= l) { left_sum += pre_sum[pos][lg]; pos = pre[pos][lg]; len += (1 << lg); } --lg; } } { int lg = LOG - 1, pos = l; while (lg >= 0) { if (suf[pos][lg] <= r) { right_sum += suf_sum[pos][lg]; pos = suf[pos][lg]; } --lg; } } printf("%lld\n", left_sum - right_sum + len + r - l); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> #define fi first #define se second #define lc (x<<1) #define rc (x<<1|1) #define gc getchar()//(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++) #define mk make_pair #define pii pair<int,int> #define pll pair<ll,ll> #define pb push_back #define IT iterator #define vi vector<int> #define TP template<class o> #define SZ(a) ((int)a.size()) #define all(a) a.begin(),a.end() using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; const int N=2e5+10,size=1<<20,mod=998244353,inf=2e9; //char buf[size],*p1=buf,*p2=buf; template<class o> void qr(o &x) { char c=gc; x=0; int f=1; while(!isdigit(c)){if(c=='-')f=-1; c=gc;} while(isdigit(c)) x=x*10+c-'0',c=gc; x*=f; } template<class o> void qw(o x) { if(x/10) qw(x/10); putchar(x%10+'0'); } template<class o> void pr1(o x) { if(x<0)x=-x,putchar('-'); qw(x); putchar(' '); } template<class o> void pr2(o x) { if(x<0)x=-x,putchar('-'); qw(x); putchar('\n'); } int n,m,q,lg,x[N],r[N][20],l[N][20]; ll sl[N][20],sr[N][20]; int main() { qr(n); qr(m); for(int i=1;i<=n;i++) qr(x[i]); for(int i=1;i<=n;i++) { r[i][0]=r[i-1][0]; while(r[i][0]<=n&&x[r[i][0]]-x[i]<m) r[i][0]++; sr[i][0]=r[i][0]-1; } l[n+1][0]=n; for(int i=n;i;i--) { l[i][0]=l[i+1][0]; while(l[i][0]&&x[i]-x[l[i][0]]<m) l[i][0]--; sl[i][0]=l[i][0]; } for(int i=0;i<20;i++) r[n+1][i]=n+1; for(int j=1;(1<<j)<=n;j++,lg++) for(int i=1;i<=n;i++) { l[i][j]=l[l[i][j-1]][j-1]; sl[i][j]=sl[i][j-1]+sl[l[i][j-1]][j-1]; r[i][j]=r[r[i][j-1]][j-1]; sr[i][j]=sr[i][j-1]+sr[r[i][j-1]][j-1]; } qr(q); while(q--) { int L,R; qr(L); qr(R); ll ans=0; ans -= L-1; for(int v=L,i=lg;i>=0;i--) if(r[v][i]<=R) ans -= sr[v][i],v=r[v][i]; ans += R; for(int v=R,i=lg;i>=0;i--) if(l[v][i]>=L) ans += sl[v][i],v=l[v][i]; pr2(ans); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Collection; import java.io.IOException; import java.util.Deque; import java.io.UncheckedIOException; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.ArrayDeque; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); DKeepDistances solver = new DKeepDistances(); solver.solve(1, in, out); out.close(); } } static class DKeepDistances { public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.readInt(); int k = in.readInt(); int[] x = new int[n]; in.populate(x); int[][] pre = new int[20][n]; int[][] post = new int[20][n]; long[][] preSum = new long[20][n]; long[][] postSum = new long[20][n]; Deque<Integer> dq = new ArrayDeque<>(n); dq.clear(); for (int i = 0; i < n; i++) { while (!dq.isEmpty() && x[i] - x[dq.peekFirst()] >= k) { post[0][dq.removeFirst()] = i; } dq.addLast(i); } while (!dq.isEmpty()) { post[0][dq.removeFirst()] = n; } for (int i = n - 1; i >= 0; i--) { while (!dq.isEmpty() && x[dq.peekFirst()] - x[i] >= k) { pre[0][dq.removeFirst()] = i; } dq.addLast(i); } while (!dq.isEmpty()) { pre[0][dq.removeFirst()] = -1; } for (int i = 0; i < n; i++) { if (pre[0][i] >= 0) { preSum[0][i] += pre[0][i]; } if (post[0][i] < n) { postSum[0][i] += post[0][i]; } } for (int i = 0; i + 1 < 20; i++) { for (int j = 0; j < n; j++) { pre[i + 1][j] = pre[i][j] == -1 ? -1 : pre[i][pre[i][j]]; post[i + 1][j] = post[i][j] == n ? n : post[i][post[i][j]]; preSum[i + 1][j] = pre[i][j] == -1 ? preSum[i][j] : preSum[i][j] + preSum[i][pre[i][j]]; postSum[i + 1][j] = post[i][j] == n ? postSum[i][j] : postSum[i][j] + postSum[i][post[i][j]]; } } int q = in.readInt(); for (int i = 0; i < q; i++) { int l = in.readInt() - 1; int r = in.readInt() - 1; int lr = l; long sumLR = l; int size = 1; for (int j = 20 - 1; j >= 0; j--) { if (post[j][lr] <= r) { size += 1 << j; sumLR += postSum[j][lr]; lr = post[j][lr]; continue; } } int rl = r; long sumRL = r; for (int j = 20 - 1; j >= 0; j--) { if (pre[j][rl] >= l) { sumRL += preSum[j][rl]; rl = pre[j][rl]; continue; } } long ans = sumRL - sumLR + size; out.println(ans); } } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } public void populate(int[] data) { for (int i = 0; i < data.length; i++) { data[i] = readInt(); } } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private StringBuilder cache = new StringBuilder(10 << 20); private final Writer os; public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); return this; } public FastOutput append(long c) { cache.append(c); return this; } public FastOutput println(long c) { return append(c).println(); } public FastOutput println() { cache.append(System.lineSeparator()); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } }
JAVA
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; #define int long long // #define endl '\n' vector<int> zf(string s) { int n = (int)s.size(); vector<int> z(n); int l = 0, r = 0; z[0] = 0; for (int i = 1; i < n; i++) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n && s[i+z[i]] == s[z[i]]) z[i]++; if (i + z[i] - 1 > r) { l = i; r = i + z[i] - 1; } } return z; } vector<int> pf(string s) { int n = (int)s.size(); vector<int> p(n); p[0] = 0; for (int i = 1; i < n; i++) { p[i] = p[i - 1]; while (p[i] && s[p[i]] != s[i]) { p[i] = p[p[i] - 1]; } if (s[p[i]] == s[i]) p[i]++; } return p; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int> x(n); for (int i = 0; i < n; i++) { cin >> x[i]; } vector<int> nx(n), pr(n); for (int i = 0; i < n; i++) { int nxv = x[i] + k; nx[i] = lower_bound(x.begin(), x.end(), nxv) - x.begin(); int prv = x[i] - k; pr[i] = upper_bound(x.begin(), x.end(), prv) - x.begin() - 1; // cout << i << " " << nx[i] << " " << pr[i] << endl; } int lg = 18; vector<vector<int>> lup(lg, vector<int>(n)), ldw(lg, vector<int>(n)), vup(lg, vector<int>(n)), vdw(lg, vector<int>(n)); for (int i = 0; i < n; i++) { lup[0][i] = nx[i]; if (lup[0][i] != n) { vup[0][i] = nx[i] - 1; } ldw[0][i] = pr[i]; if (ldw[0][i] != -1) { vdw[0][i] = pr[i]; } } for (int i = 1; i < lg; i++) { // cout << i << " : "; for (int j = 0; j < n; j++) { // cout << "(" << lup[i - 1][j] << ", " << vup[i - 1][j] << ") "; if (lup[i - 1][j] == n) { lup[i][j] = n; continue; } lup[i][j] = lup[i - 1][lup[i - 1][j]]; vup[i][j] = vup[i - 1][lup[i - 1][j]] + vup[i - 1][j]; } // cout << "; "; for (int j = 0; j < n; j++) { // cout << "(" << ldw[i - 1][j] << ", " << vdw[i - 1][j] << ") "; if (ldw[i - 1][j] == -1) { ldw[i][j] = -1; continue; } ldw[i][j] = ldw[i - 1][ldw[i - 1][j]]; vdw[i][j] = vdw[i - 1][ldw[i - 1][j]] + vdw[i - 1][j]; } // cout << endl; } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; l--; r--; int val = -l + 1; int pos = l; for (int i = lg - 1; i >= 0; i--) { if (lup[i][pos] <= r) { val -= vup[i][pos]; pos = lup[i][pos]; // cout << "(" << i << ", " << pos << "); "; } } // cout << endl; val += r; pos = r; for (int i = lg - 1; i >= 0; i--) { if (ldw[i][pos] >= l) { val += vdw[i][pos]; pos = ldw[i][pos]; // cout << "(" << i << ", " << pos << "); "; } } // cout << endl; cout << val << endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; const int max_n = 200222, inf = 1000111222; const int max_lev = 18; int n, k, q, a[max_n]; int nxt[2][max_n][max_lev]; long long sum[2][max_n][max_lev]; void calc_nxt(int tp) { nxt[tp][n][0] = n; for (int i = n - 1; i >= 0; --i) { nxt[tp][i][0] = nxt[tp][i + 1][0]; sum[tp][i][0] = i; if (tp) { sum[tp][i][0] = n - i; } while (a[nxt[tp][i][0] - 1] - a[i] >= k) { --nxt[tp][i][0]; } } for (int lev = 1; lev < max_lev; ++lev) { for (int i = 0; i <= n; ++i) { nxt[tp][i][lev] = nxt[tp][nxt[tp][i][lev - 1]][lev - 1]; sum[tp][i][lev] = sum[tp][i][lev - 1] + sum[tp][nxt[tp][i][lev - 1]][lev - 1]; } } } void rev() { reverse(a, a + n); for (int i = 0; i < n; ++i) { a[i] *= -1; } } long long get_sum(int tp, int pos, int r) { long long res = 0; for (int i = max_lev - 1; i >= 0; --i) { if (nxt[tp][pos][i] <= r) { res += sum[tp][pos][i]; pos = nxt[tp][pos][i]; } } res += sum[tp][pos][0]; return res; } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } calc_nxt(0); rev(); calc_nxt(1); rev(); scanf("%d", &q); while (q--) { int l, r; scanf("%d%d", &l, &r); --l; --r; long long s1 = get_sum(0, l, r); long long s2 = get_sum(1, n - r - 1, n - l - 1); //cout << s1 << " " << s2 << ": "; int ans = s2 - s1; printf("%d\n", ans); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <iostream> #include <cstdio> #include <cmath> #include <ctime> #include <cstdlib> #include <cassert> #include <vector> #include <list> #include <stack> #include <queue> #include <deque> #include <map> #include <set> #include <bitset> #include <string> #include <algorithm> #include <utility> #define llint long long #define inf 1e18 #define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++) #define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) using namespace std; typedef pair<llint, llint> P; llint n, k, Q; llint x[200005]; llint pred[19][200005], succ[19][200005]; llint pred2[19][200005], succ2[19][200005]; int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for(int i = 1; i <= n; i++) cin >> x[i]; x[0] = -inf, x[n+1] = inf; for(int i = 1; i <= n; i++){ llint p = lower_bound(x, x+n+2, x[i]+k) - x; succ[0][i] = p; p = upper_bound(x, x+n+2, x[i]-k) - x - 1; pred[0][i] = p; } succ[0][n+1] = n+1; for(int i = 1; i < 19; i++){ for(int j = 1; j <= n+1; j++){ pred[i][j] = pred[i-1][pred[i-1][j]]; succ[i][j] = succ[i-1][succ[i-1][j]]; } } for(int i = 1; i <= n; i++){ succ2[0][i] = succ[0][i]-1; pred2[0][i] = pred[0][i]; } for(int i = 1; i < 19; i++){ for(int j = 1; j <= n; j++){ pred2[i][j] = pred2[i-1][j] + pred2[i-1][pred[i-1][j]]; succ2[i][j] = succ2[i-1][j] + succ2[i-1][succ[i-1][j]]; } } /*for(int i = 1; i <= n; i++) cout << succ[0][i] << " "; cout << endl; for(int i = 1; i <= n; i++) cout << pred[0][i] << " "; cout << endl; for(int i = 1; i <= n; i++) cout << succ2[0][i] << " "; cout << endl; for(int i = 1; i <= n; i++) cout << pred2[0][i] << " "; cout << endl;*/ cin >> Q; llint L, R; for(int q = 1; q <= Q; q++){ cin >> L >> R; llint ans = R-L+1, v = L; for(int i = 18; i >= 0; i--){ //cout << succ[i][v] << " " << R << endl; if(succ[i][v] <= R){ ans -= succ2[i][v]; v = succ[i][v]; } ///cout << v << " " << ans << endl; } v = R; for(int i = 18; i >= 0; i--){ if(pred[i][v] >= L){ ans += pred2[i][v]; v = pred[i][v]; } } cout << ans << endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; #ifdef tabr #include "library/debug.cpp" #else #define debug(...) #endif int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> x(n); for (int i = 0; i < n; i++) { cin >> x[i]; } vector<vector<int>> ld(20, vector<int>(n)); vector<vector<int>> rd(20, vector<int>(n)); vector<vector<long long>> ls(20, vector<long long>(n)); vector<vector<long long>> rs(20, vector<long long>(n)); for (int i = 0; i < n; i++) { ld[0][i] = upper_bound(x.begin(), x.end(), x[i] - k) - x.begin() - 1; rd[0][i] = lower_bound(x.begin(), x.end(), x[i] + k) - x.begin(); ls[0][i] = ld[0][i]; rs[0][i] = rd[0][i]; } for (int i = 1; i < 20; i++) { for (int j = 0; j < n; j++) { ld[i][j] = (ld[i - 1][j] == -1 ? -1 : ld[i - 1][ld[i - 1][j]]); rd[i][j] = (rd[i - 1][j] == n ? n : rd[i - 1][rd[i - 1][j]]); ls[i][j] = (ld[i - 1][j] == -1 ? 0 : ls[i - 1][j] + ls[i - 1][ld[i - 1][j]]); rs[i][j] = (rd[i - 1][j] == n ? 0 : rs[i - 1][j] + rs[i - 1][rd[i - 1][j]]); } } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; if (l == r) { cout << 1 << '\n'; continue; } l--, r--; long long ans = r - l + 1; int pl = l, pr = r; for (int i = 19; i >= 0; i--) { if (rd[i][pl] <= r) { ans -= rs[i][pl]; pl = rd[i][pl]; ans += 1 << i; } if (ld[i][pr] >= l) { ans += ls[i][pr]; pr = ld[i][pr]; } } cout << ans << '\n'; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; const int N = 200005, M = 21, inf = (int)2e9; int n, k, q, x[N]; int fa1[N][M], fa2[N][M], Log2[N]; long long val1[N][M], val2[N][M]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &x[i]); x[0] = -inf, x[n + 1] = inf; fa1[n + 1][0] = n + 1, fa2[0][0] = 0; val1[n + 1][0] = 0, val2[0][0] = 0; Log2[1] = 0; for (int i = 2; i <= n; i++) Log2[i] = Log2[i >> 1] + 1; for (int i = 1; i <= n; i++) { fa1[i][0] = lower_bound(x + 1, x + n + 1, x[i] + k) - x; fa2[i][0] = upper_bound(x + 1, x + n + 1, x[i] - k) - x - 1; val1[i][0] = i, val2[i][0] = i + 1; } for (int i = 1; i <= Log2[n]; i++) { for (int j = 1; j <= n + 1; j++) { fa1[j][i] = fa1[fa1[j][i - 1]][i - 1]; val1[j][i] = val1[j][i - 1] + val1[fa1[j][i - 1]][i - 1]; } for (int j = 0; j <= n; j++) { fa2[j][i] = fa2[fa2[j][i - 1]][i - 1]; val2[j][i] = val2[j][i - 1] + val2[fa2[j][i - 1]][i - 1]; } } scanf("%d", &q); for (int i = 1; i <= q; i++) { int l, r, L, R; long long ans = 0; scanf("%d%d", &l, &r); L = l, R = r; for (int j = Log2[n - l + 1]; j >= 0; j--) { if (fa1[L][j] <= r) ans -= val1[L][j], L = fa1[L][j]; } ans -= val1[L][0]; for (int j = Log2[r]; j >= 0; j--) { if (fa2[R][j] >= l) ans += val2[R][j], R = fa2[R][j]; } ans += val2[R][0]; printf("%lld\n", ans); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define elif else if #define sp(x) fixed << setprecision(x) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; //const int MOD = 998244353; const int inf = (1<<30)-1; const ll INF = (1LL<<60)-1; const double pi = acos(-1.0); const double EPS = 1e-10; template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;}; template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;}; int main(){ int N, K; cin >> N >> K; int X[N]; rep(i, N) cin >> X[i]; int nl[N][20], nr[N][20]; ll sl[N][20], sr[N][20]; int ptr = 0; rep(i, N){ while(ptr < N && X[ptr] < X[i]+K) ptr++; nl[i][0] = ptr, sl[i][0] = i; } ptr = N-1; rep3(i, N-1, 0){ while(ptr >= 0 && X[ptr] > X[i]-K) ptr--; nr[i][0] = ptr, sr[i][0] = i+1; } rep2(j, 1, 19){ rep(i, N){ if(nl[i][j-1] == N){ nl[i][j] = N; sl[i][j] = sl[i][j-1]; } else{ nl[i][j] = nl[nl[i][j-1]][j-1]; sl[i][j] = sl[i][j-1]+sl[nl[i][j-1]][j-1]; } } rep(i, N){ if(nr[i][j-1] == -1){ nr[i][j] = -1; sr[i][j] = sr[i][j-1]; } else{ nr[i][j] = nr[nr[i][j-1]][j-1]; sr[i][j] = sr[i][j-1]+sr[nr[i][j-1]][j-1]; } } } int Q; cin >> Q; while(Q--){ int l, r; cin >> l >> r; l--, r--; ll suml = 0, sumr = 0; int pl = l, pr = r; rep3(i, 19, 0){ if(nl[pl][i] <= r){ suml += sl[pl][i], pl = nl[pl][i]; } } suml += pl; rep3(i, 19, 0){ if(nr[pr][i] >= l){ sumr += sr[pr][i], pr = nr[pr][i]; } } sumr += pr+1; cout << sumr-suml << endl; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = (int) 2e5 + 7; const int K = 18; int n; int dif; int q; int a[N]; int u[K][N]; ll f[K][N]; int u2[K][N]; ll f2[K][N]; ll compute1(int x, int lim) { ll sol = x - 1; for (int k = K - 1; k >= 0; k--) { if (u[k][x] <= lim) { sol += f[k][x] - (1 << k); x = u[k][x]; } } return sol; } ll compute2(int x, int lim) { ll sol = x; for (int k = K - 1; k >= 0; k--) { if (lim <= u2[k][x]) { sol += f2[k][x]; x = u2[k][x]; } } return sol; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> dif; for (int i = 1; i <= n; i++) { cin >> a[i]; } a[n + 1] = a[n] + dif; int j = 1; for (int i = 1; i <= n; i++) { while (a[j] - a[i] < dif) { j++; } u[0][i] = j; f[0][i] = j; } a[0] = a[1] - dif; j = n; for (int i = n; i >= 1; i--) { while (a[i] - a[j] < dif) { j--; } u2[0][i] = j; f2[0][i] = j; } for (int k = 1; k < K; k++) { for (int i = 1; i <= n; i++) { if (u[k - 1][i] == n + 1) { u[k][i] = n + 1; } else { f[k][i] = f[k - 1][i] + f[k - 1][u[k - 1][i]]; u[k][i] = u[k - 1][u[k - 1][i]]; } /// boooo if (u2[k - 1][i] == 0) { u2[k][i] = 0; } else { f2[k][i] = f2[k - 1][i] + f2[k - 1][u2[k - 1][i]]; u2[k][i] = u2[k - 1][u2[k - 1][i]]; } } } cin >> q; while (q--) { int l, r, best = 0, sol = 0; cin >> l >> r; cout << compute2(r, l) - compute1(l, r) << "\n"; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<iostream> #include<vector> #include<set> #include<queue> #include<map> #include<algorithm> #include<cstring> #include<string> #include<cassert> #include<cmath> #include<climits> #include<iomanip> #include<stack> #include<unordered_map> #include<bitset> #include<limits> #include<complex> #include<array> #include<numeric> #include<functional> #include<random> using namespace std; #define ll long long #define ull unsigned long long #define rep(i,m,n) for(ll (i)=(ll)(m);i<(ll)(n);i++) #define REP(i,n) rep(i,0,n) #define all(hoge) (hoge).begin(),(hoge).end() typedef pair<ll, ll> P; constexpr long double m_pi = 3.1415926535897932L; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 61; constexpr long double EPS = 1e-10; template<typename T> using vector2 = vector<vector<T>>; template<typename T> using vector3 = vector<vector2<T>>; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string& s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } 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; } struct Edge {//グラフ int to, rev; ll cap; Edge(int _to, ll _cap, int _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph& G, int from, int to, ll cap, bool revFlag, ll revCap) {//最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size() + (from == to))); if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));//最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph& G, ll v, ll t, ll f, vector<bool>& used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge& e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } //二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph& G, ll s, ll t)//O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size())used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph& G, ll s, Array& d, Array& negative) {//O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size())d[i] = INF; REP(i, d.size())negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true)negative[G[i][j].to] = true; } } } } void Dijkstra(Graph& G, ll s, Array& d) {//O(|E|log|V|) d.resize(G.size()); REP(i, d.size())d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first)continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph& G, Matrix& d) {//O(V^3) d.resize(G.size()); REP(i, d.size())d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = ((i != j) ? INF : 0); } } REP(i, G.size()) { REP(j, G[i].size()) { chmin(d[i][G[i][j].to], G[i][j].cap); } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { if (d[j][i] != INF && d[i][k] != INF)chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph& graph, vector<int>& order) {//トポロジカルソートO(E+V) int n = graph.size(), k = 0; vector<int> in(n); for (auto& es : graph) for (auto& e : es)in[e.to]++; priority_queue<int, vector<int>, greater<int>> que; REP(i, n) if (in[i] == 0)que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto& e : graph[v]) if (--in[e.to] == 0)que.push(e.to); } if (order.size() != n)return false; else return true; } class Lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; Lca() {} Lca(const Graph& g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph& g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto& e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; class UnionFind { vector<int> data; int n; public: UnionFind(int size) : data(size, -1), n(size) { } bool merge(int x, int y) {//xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } n -= (x != y); return x != y; } bool same(int x, int y) {//xとyが同じ集合か返す return root(x) == root(y); } int root(int x) {//xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) {//xの集合のサイズを返す return -data[root(x)]; } int num() {//集合の数を返す return n; } }; template<typename T, typename F> class SegmentTree { private: T identity; F merge; ll n; vector<T> dat; public: SegmentTree(F f, T id, vector<T> v) :merge(f), identity(id) { int _n = v.size(); n = 1; while (n < _n)n *= 2; dat.resize(2 * n - 1, identity); REP(i, _n)dat[n + i - 1] = v[i]; for (int i = n - 2; i >= 0; i--)dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } SegmentTree(F f, T id, int _n) :merge(f), identity(id) { n = 1; while (n < _n)n *= 2; dat.resize(2 * n - 1, identity); } void set_val(int i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } } T query(int l, int r) { T left = identity, right = identity; l += n - 1; r += n - 1; while (l < r) { if ((l & 1) == 0)left = merge(left, dat[l]); if ((r & 1) == 0)right = merge(dat[r - 1], right); l = l / 2; r = (r - 1) / 2; } return merge(left, right); } }; template< typename T > class FenwickTree { vector< T > data; int n; int p; public: FenwickTree(int n) :n(n) { data.resize(n + 1LL, 0); p = 1; while (p < data.size())p *= 2; } T sum(int k) { T ret = 0; for (; k > 0; k -= k & -k) ret += data[k]; return (ret); } T sum(int a, int b) { return sum(b) - sum(a); }//[a,b) void add(int k, T x) { for (++k; k <= n; k += k & -k) data[k] += x; } int lower_bound(ll w) { if (w <= 0)return -1; int x = 0; for (int k = p / 2; k > 0; k /= 2) { if (x + k <= n && data[x + k] < w)w -= data[x + k], x += k; } return x; } }; //約数求める //約数 void divisor(ll n, vector<ll>& ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void prime_factorization(ll n, vector<P>& ret) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({ i,0 }); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1)ret.push_back({ n,1 }); } inline ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } inline ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } class Combination { public: Array fact; Array fact_inv; ll mod; //if n >= mod use lucas ll nCr(ll n, ll r) { if (n < r)return 0; if (n < mod)return ((fact[n] * fact_inv[r] % mod) * fact_inv[n - r]) % mod; ll ret = 1; while (n || r) { ll _n = n % mod, _r = r % mod; n /= mod; r /= mod; (ret *= nCr(_n, _r)) %= mod; } return ret; } ll nPr(ll n, ll r) { return (fact[n] * fact_inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll _n, ll _mod) { mod = _mod; ll n = min(_n + 1, mod); fact.resize(n); fact[0] = 1; REP(i, n - 1) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } fact_inv.resize(n); fact_inv[n - 1] = mod_inv(fact[n - 1], mod); for (int i = n - 1; i > 0; i--) { fact_inv[i - 1] = fact_inv[i] * i % mod; } } }; ll popcount(ll x) { x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F); x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF); x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF); x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF); return x; } ll l[20][202020]; ll r[20][202020]; ll dp1[20][202020]; ll dp2[20][202020]; int main() { ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); constexpr ll mod = 998244353; ll n, k; cin >> n >> k; Array x(n); REP(i, n)cin >> x[i]; REP(i, n) l[0][i] = lower_bound(all(x), x[i] + k) - x.begin(); REP(i, n) r[0][i] = upper_bound(all(x), x[i] - k) - x.begin() - 1; REP(i, 19)REP(j, n)l[i + 1][j] = l[i][min(n - 1, l[i][j])]; REP(i, 19)REP(j, n)r[i + 1][j] = r[i][max(0LL,r[i][j])]; REP(i, n)dp1[0][i] = l[0][i]; REP(i, n)dp2[0][i] = r[0][i]; REP(i, 19)REP(j, n)dp1[i + 1][j] = dp1[i][j] + dp1[i][l[i][j]]; REP(i, 19)REP(j, n)dp2[i + 1][j] = dp2[i][j] + dp2[i][r[i][j]]; ll q; cin >> q; while (q--) { ll L, R; cin >> L >> R; L--; ll ans = 1; ll now = L; ll x = L, y = R - 1; for (int i = 19; i >= 0; i--) if (l[i][now] < R)ans += 1LL << i, x += dp1[i][now], now = l[i][now]; now = R - 1; for (int i = 19; i >= 0; i--)if (r[i][now] >= L)y += dp2[i][now], now = r[i][now]; cout << y - x +ans<< "\n"; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline int rd_int(void){ int x; rd(x); return x; } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } int N; int K; int X[200000]; int L; int R; int go[20][200000]; long long gos[20][200000]; int bk[20][200000]; long long bks[20][200000]; int main(){ int jbtyPBGc; int i; int j; int k; int mx; long long s1; long long s2; rd(N); rd(K); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){ rd(X[Lj4PdHRW]); } } j = 0; for(i=(0);i<(N);i++){ while(j < N && X[j] < X[i]+K){ j++; } go[0][i] = j; gos[0][i] = j; } j = N-1; for(i=(N)-1;i>=(0);i--){ while(j >= 0 && X[j] > X[i]-K){ j--; } bk[0][i] = j; bks[0][i] = j; } for(k=(1);k<(20);k++){ for(i=(0);i<(N);i++){ j = go[k-1][i]; if(j==N){ go[k][i] = N; continue; } go[k][i] = go[k-1][j]; gos[k][i] = gos[k-1][j] + gos[k-1][i]; } } for(k=(1);k<(20);k++){ for(i=(0);i<(N);i++){ j = bk[k-1][i]; if(j==-1){ bk[k][i] = -1; continue; } bk[k][i] = bk[k-1][j]; bks[k][i] = bks[k-1][j] + bks[k-1][i]; } } int ZIeRIny5 = rd_int(); for(jbtyPBGc=(0);jbtyPBGc<(ZIeRIny5);jbtyPBGc++){ rd(L);L += (-1); rd(R);R += (-1); mx = 0; k = L; s1 = L; for(i=19;i>=0;i--){ if(go[i][k] <= R){ s1 += gos[i][k]; k = go[i][k]; mx += (1<<i); } } k = R; s2 = R; for(i=19;i>=0;i--){ if(bk[i][k] >= L){ s2 += bks[i][k]; k = bk[i][k]; } } wt_L(s2-s1+mx+1); wt_L('\n'); } return 0; } // cLay varsion 20200920-1 // --- original code --- // int N, K, X[2d5], L, R; // int go[20][2d5]; ll gos[20][2d5]; // int bk[20][2d5]; ll bks[20][2d5]; // { // int i, j, k, mx; // ll s1, s2; // rd(N,K,X(N)); // // j = 0; // rep(i,N){ // while(j < N && X[j] < X[i]+K) j++; // go[0][i] = j; // gos[0][i] = j; // } // j = N-1; // rrep(i,N){ // while(j >= 0 && X[j] > X[i]-K) j--; // bk[0][i] = j; // bks[0][i] = j; // } // // rep(k,1,20) rep(i,N){ // j = go[k-1][i]; // if(j==N) go[k][i] = N, continue; // go[k][i] = go[k-1][j]; // gos[k][i] = gos[k-1][j] + gos[k-1][i]; // } // // rep(k,1,20) rep(i,N){ // j = bk[k-1][i]; // if(j==-1) bk[k][i] = -1, continue; // bk[k][i] = bk[k-1][j]; // bks[k][i] = bks[k-1][j] + bks[k-1][i]; // } // // REP(rd_int()){ // rd(L--,R--); // // mx = 0; // // k = L; // s1 = L; // for(i=19;i>=0;i--) if(go[i][k] <= R) s1 += gos[i][k], k = go[i][k], mx += (1<<i); // // k = R; // s2 = R; // for(i=19;i>=0;i--) if(bk[i][k] >= L) s2 += bks[i][k], k = bk[i][k]; // // wt(s2-s1+mx+1); // } // }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
/* 读题不规范,爆零两行泪。 数据不清空,爆零两行泪。 多测不读完,爆零两行泪。 边界不特判,爆零两行泪。 贪心不证明,爆零两行泪。 D P 顺序错,爆零两行泪。 大小少等号,爆零两行泪。 变量不统一,爆零两行泪。 越界不判断,爆零两行泪。 调试不注释,爆零两行泪。 溢出不 l l,爆零两行泪。 */ #include<bits/stdc++.h> using namespace std; const int N=200000,LOG_N=20; int n,m,qu; int a[N+1]; int rit[N+1],lft[N+1]; int rit_to[N+1][LOG_N],rit_sum[N+1][LOG_N],lft_to[N+1][LOG_N],lft_sum[N+1][LOG_N]; int main(){ cin>>n>>m; for(int i=1;i<=n;i++)scanf("%d",a+i); int now=1; for(int i=1;i<=n;i++){ while(now<=n&&a[now]-a[i]<m)now++; rit[i]=now; } rit[n+1]=n+1; for(int i=n+1;i;i--){ rit_to[i][0]=rit[i],rit_sum[i][0]=-rit[i]+1; for(int j=1;j<LOG_N;j++) rit_to[i][j]=rit_to[rit_to[i][j-1]][j-1], rit_sum[i][j]=rit_sum[i][j-1]+rit_sum[rit_to[i][j-1]][j-1]; } now=n; for(int i=n;i;i--){ while(now&&a[i]-a[now]<m)now--; lft[i]=now; } lft[0]=0; for(int i=0;i<=n;i++){ lft_to[i][0]=lft[i],lft_sum[i][0]=lft[i]; for(int j=1;j<LOG_N;j++) lft_to[i][j]=lft_to[lft_to[i][j-1]][j-1], lft_sum[i][j]=lft_sum[i][j-1]+lft_sum[lft_to[i][j-1]][j-1]; } cin>>qu; while(qu--){ int l,r; scanf("%d%d",&l,&r); int ans=0; now=l; ans+=-now+1; for(int i=LOG_N-1;~i;i--)if(rit_to[now][i]<=r)ans+=rit_sum[now][i],now=rit_to[now][i]; now=r; ans+=now; for(int i=LOG_N-1;~i;i--)if(lft_to[now][i]>=l)ans+=lft_sum[now][i],now=lft_to[now][i]; printf("%d\n",ans); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> #define rep(i,n) for ((i)=1;(i)<=(n);(i)++) using namespace std; int n,m,q,i,j,a[200005],l[200005][19],r[200005][19]; long long f[200005][19],g[200005][19]; int main(){ cin>>n>>m; rep(i,n){ cin>>a[i]; } r[n+1][0]=n+1; l[n+1][0]=n+1; rep(i,n){ r[i][0]=max(i,r[i-1][0]); while(r[i][0]<=n&&a[r[i][0]]-a[i]<m) r[i][0]++; } for(i=n;i>=1;i--){ l[i][0]=min(i,l[i+1][0]); while(l[i][0]>=1&&a[i]-a[l[i][0]]<m) l[i][0]--; } rep(j,18)rep(i,n+1){ l[i][j]=l[l[i][j-1]][j-1]; r[i][j]=r[r[i][j-1]][j-1]; } rep(i,n){ f[i][0]=l[i][0]; g[i][0]=r[i][0]; } rep(j,18)rep(i,n){ f[i][j]=f[i][j-1]+f[l[i][j-1]][j-1]; g[i][j]=g[i][j-1]+g[r[i][j-1]][j-1]; } cin>>q; while(q--) { int x,y,xx,yy;cin>>x>>y;xx=x;yy=y; long long ans=y-x+1; for(i=18;i>=0;i--){ if(r[x][i]<=yy){ ans+=(1<<i); ans-=g[x][i]; x=r[x][i]; } if(l[y][i]>=xx){ ans+=f[y][i]; y=l[y][i]; } } cout<<ans<<endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
// Problem : D - Keep Distances // Contest : AtCoder - ACL Contest 1 // URL : https://atcoder.jp/contests/acl1/tasks/acl1_d // Memory Limit : 1024 MB // Time Limit : 3000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #pragma GCC optimize("Ofast,-funroll-loops") #include <bits/stdc++.h> using namespace std; constexpr int Maxn = 2e5 + 5; constexpr int LOG = 20; int n, K, q; int a[Maxn]; int pre[Maxn][LOG]; int suf[Maxn][LOG]; int64_t pre_sum[Maxn][LOG]; int64_t suf_sum[Maxn][LOG]; int main() { scanf("%d %d", &n, &K); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); } for (int i = 1; i <= n; ++i) { pre[i][0] = upper_bound(a + 1, a + n + 1, a[i] - K) - a - 1; suf[i][0] = lower_bound(a + 1, a + n + 1, a[i] + K) - a; pre_sum[i][0] = (pre[i][0] >= 1 ? pre[i][0] : 0); suf_sum[i][0] = (suf[i][0] <= n ? suf[i][0] : 0); } for (int j = 1; j < LOG; ++j) { for (int i = 1; i <= n; ++i) { pre_sum[i][j] = pre_sum[i][j - 1]; suf_sum[i][j] = suf_sum[i][j - 1]; if (pre[i][j - 1] >= 1) { pre[i][j] = pre[pre[i][j - 1]][j - 1]; pre_sum[i][j] += pre_sum[pre[i][j - 1]][j - 1]; } else { pre[i][j] = 0; } if (suf[i][j - 1] <= n) { suf[i][j] = suf[suf[i][j - 1]][j - 1]; suf_sum[i][j] += suf_sum[suf[i][j - 1]][j - 1]; } else { suf[i][j] = n + 1; } } } scanf("%d", &q); while (q--) { int l, r; scanf("%d %d", &l, &r); int64_t left_sum = 0; int64_t right_sum = 0; int len = 1; { int lg = LOG - 1, pos = r; while (lg >= 0) { if (pre[pos][lg] >= l) { left_sum += pre_sum[pos][lg]; pos = pre[pos][lg]; len += (1 << lg); } --lg; } } { int lg = LOG - 1, pos = l; while (lg >= 0) { if (suf[pos][lg] <= r) { right_sum += suf_sum[pos][lg]; pos = suf[pos][lg]; } --lg; } } printf("%lld\n", left_sum - right_sum + len + r - l); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <iostream> #include <set> #include <vector> #define _overload(_1, _2, _3, name, ...) name #define _rep(i, n) _range(i, 0, n) #define _range(i, a, b) for (int i = int(a); i < int(b); ++i) #define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__) #define _rrep(i, n) _rrange(i, n, 0) #define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i) #define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__) using ll = long long; using namespace std; const int limit = 200010; int nxt[18][limit]; ll nsum[18][limit]; int prv[18][limit]; ll psum[18][limit]; int main(void) { ll n, d; cin >> n >> d; vector<int> x(n); rep(i, n) cin >> x[i]; { int pos = 0; rep(i, n) { while (pos < n and x[pos] - x[i] < d) { pos++; } nxt[0][i] = pos; nsum[0][i] += i; } } { int pos = n - 1; rrep(i, n) { while (pos >= 0 and x[i] - x[pos] < d) { pos--; } prv[0][i] = pos; psum[0][i] += i; } } rep(k, 1, 18) { rep(i, n) { nxt[k][i] = (nxt[k - 1][i] == n) ? n : nxt[k - 1][nxt[k - 1][i]]; nsum[k][i] = (nxt[k - 1][i] == n) ? nsum[k - 1][i] : nsum[k - 1][nxt[k - 1][i]] + nsum[k - 1][i]; prv[k][i] = (prv[k - 1][i] == -1) ? -1 : prv[k - 1][prv[k - 1][i]]; psum[k][i] = (prv[k - 1][i] == -1) ? psum[k - 1][i] : psum[k - 1][prv[k - 1][i]] + psum[k - 1][i]; } } int q; cin >> q; rep(loop, q) { int l, r; cin >> l >> r; l--, r--; ll num = 1LL; int cur = l; rrep(k, 18) { if (nxt[k][cur] <= r) { cur = nxt[k][cur]; num += 1 << k; } } ll ans = num; int cl = l; int cr = r; rrep(k, 18) { if (num >> k & 1) { ans -= nsum[k][cl]; cl = nxt[k][cl]; ans += psum[k][cr]; cr = prv[k][cr]; } } cout << ans << endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<deque> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<set> #include<cmath> #include<tuple> #include<string> #include<chrono> #include<functional> #include<iterator> #include<random> #include<unordered_set> #include<array> #include<map> #include<iomanip> #include<assert.h> #include<list> #include<bitset> #include<stack> #include<memory> #include<numeric> #include <utility> using namespace std; typedef long long int llint; typedef long double lldo; #define mp make_pair #define mt make_tuple #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define fir first #define sec second #define res resize #define ins insert #define era erase #define REP(i, n) for(int i = 0;i < (n);i++) /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ const llint mod=1000000007; const llint inf=2.19e15+1; const long double pai=3.141592653589793238462643383279502884197; const long double eps=1e-10; template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;} template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;} llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);} llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;} template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();} template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();} //ダブリング //ACLにないんだけど int main(void){ int h,i,j,n,m,K;cin>>n>>K; vector<llint>X(n+2); X[0]=-inf;X[n+1]=inf; for(i=1;i<=n;i++){cin>>X[i];} vector<int>lgo[18]; vector<int>rgo[18]; vector<llint>lsum[18]; vector<llint>rsum[18]; for(h=0;h<18;h++){ lgo[h].res(n+2);rgo[h].res(n+2); lsum[h].res(n+2);rsum[h].res(n+2); } for(i=1;i<=n;i++){ lgo[0][i]=LBI(X,X[i]+K); rgo[0][i]=UBI(X,X[i]-K)-1; lsum[0][i]=i; rsum[0][i]=i; } lgo[0][0]=0;lgo[0][n+1]=n+1; lsum[0][0]=0;lsum[0][n+1]=n+1; rgo[0][0]=0;rgo[0][n+1]=n+1; rsum[0][0]=0;rsum[0][n+1]=n+1; for(h=1;h<18;h++){ for(i=0;i<=n+1;i++){ lgo[h][i]=lgo[h-1][lgo[h-1][i]]; rgo[h][i]=rgo[h-1][rgo[h-1][i]]; lsum[h][i]=lsum[h-1][i]+lsum[h-1][lgo[h-1][i]]; rsum[h][i]=rsum[h-1][i]+rsum[h-1][rgo[h-1][i]]; } } int Q;cin>>Q; while(Q--){ int L,R,gL,gR,Lcou=0,Rcou=0;cin>>L>>R; gL=L;gR=R; llint ans=0; for(h=17;h>=0;h--){ if(lgo[h][gL]<=R){ans-=lsum[h][gL];gL=lgo[h][gL];Lcou+=(1<<h);} if(rgo[h][gR]>=L){ans+=rsum[h][gR];gR=rgo[h][gR];Rcou+=(1<<h);} } ans-=gL;Lcou++; ans+=gR;Rcou++; if(Lcou!=Rcou){cerr<<"bag"<<endl;} ans+=Lcou; cout<<ans<<endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <iostream> #include <string> #include <vector> #include <array> #include <queue> #include <deque> #include <algorithm> #include <set> #include <map> #include <bitset> #include <cmath> #include <functional> #include <cassert> #include <iomanip> #define vll vector<ll> #define vvvl vector<vvl> #define vvl vector<vector<ll>> #define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c)) #define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d))); #define re(c, b) for(ll c=0;c<b;c++) #define all(obj) (obj).begin(), (obj).end() typedef long long int ll; typedef long double ld; using namespace std; typedef array<ll, 2> ary; pair<vvl, vvl> calc(ll n, ll k, vector<ll>& a){ vvl nxt = VV(n, 30, -1, ll); vvl s = VV(n, 30, 0, ll); for(int i=0;i<30;i++){ if(i==0){ for(int j=0;j<n;j++){ auto idx = lower_bound(all(a), a[j]+k) - a.begin();//k先のidx if(j==n-1) idx = n; nxt[j][0] = idx; if(idx!=n) s[j][0] = idx; } }else{ for(int j=0;j<n;j++){ nxt[j][i] = (nxt[j][i-1]==n?n:nxt[nxt[j][i-1]][i-1]); s[j][i] = s[j][i-1] + (nxt[j][i-1]==n?0:s[nxt[j][i-1]][i-1]); } } } return {nxt, s}; } int main(){ ll n, k;scanf("%lld %lld", &n, &k); vll a(n); re(i, n) scanf("%lld", &a[i]); vll b = a; reverse(all(b)); for(int i=0;i<n;i++) b[i] = -b[i]; vvl nxt, nxt2, s, s2; auto tmp = calc(n, k, a); nxt = tmp.first; s = tmp.second; tmp = calc(n, k, b); nxt2 = tmp.first; s2 = tmp.second; auto f = [&](ll l, ll r){//操作回数 ll cnt = 0; ll S = 0; ll now = l; while(now<r){ auto idx = upper_bound(all(nxt[now]), r) - nxt[now].begin() - 1; if(idx==-1||idx==n||nxt[now][idx]==n||a[nxt[now][idx]]-a[now] < k) break;//もう移動できない cnt += (1<<idx); S += s[now][idx]; now = nxt[now][idx]; } return ary{cnt+1, S+l}; }; auto g = [&](ll l, ll r){ l = n - 1 - l, r = n - 1 - r; swap(l, r); ll cnt = 0; ll S = 0; ll now = l; while(now<r){ auto idx = upper_bound(all(nxt2[now]), r) - nxt2[now].begin() - 1; if(idx==-1||idx==n||nxt2[now][idx]==n||b[nxt2[now][idx]]-b[now]<k) break;//もう移動できない cnt += (1<<idx); S += s2[now][idx]; now = nxt2[now][idx]; } return ary{cnt+1, S+l}; }; ll q;scanf("%lld", &q); for(int i=0;i<q;i++){ ll x, y;scanf("%lld %lld", &x, &y); x--, y--; auto h = f(x, y); auto u = g(x, y); ll cnt = h[0] + 1; cnt += u[1] - h[1]; u[1] = (n-1)*u[0] - u[1]; ll ret = u[0] + u[1] - h[1]; printf("%lld\n", ret); } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> using namespace std; #define ll long long const int _ = 2e5 + 7; int tor[_][20] , tol[_][20]; ll sumr[_][20] , suml[_][20]; int N , K , Q , X[_]; int main(){ ios::sync_with_stdio(0); cin >> N >> K; for(int i = 1 ; i <= N ; ++i) cin >> X[i]; X[0] = -1e9; X[N + 1] = 2e9; for(int j = 0 ; j < 20 ; ++j) tor[N + 1][j] = N + 1; for(int pos = N + 1 , i = N ; i ; --i){ while(X[pos - 1] - X[i] >= K){--pos;} tor[i][0] = pos; sumr[i][0] = pos - 1; for(int j = 1 ; j < 20 ; ++j){tor[i][j] = tor[tor[i][j - 1]][j - 1]; sumr[i][j] = sumr[i][j - 1] + sumr[tor[i][j - 1]][j - 1];} } for(int pos = 0 , i = 1 ; i <= N ; ++i){ while(X[i] - X[pos + 1] >= K){++pos;} tol[i][0] = pos; suml[i][0] = pos; for(int j = 1 ; j < 20 ; ++j){tol[i][j] = tol[tol[i][j - 1]][j - 1]; suml[i][j] = suml[i][j - 1] + suml[tol[i][j - 1]][j - 1];} } for(cin >> Q ; Q ; --Q){ int L , R; cin >> L >> R; ll ans = R - L + 1; for(int t = R , fl = 19 ; ~fl ; --fl) if(tol[t][fl] >= L){ans += suml[t][fl]; t = tol[t][fl];} for(int t = L , fl = 19 ; ~fl ; --fl) if(tor[t][fl] <= R){ans -= sumr[t][fl]; t = tor[t][fl];} cout << ans << endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int n = ni(), K = ni(); int[] x = na(n); int[] f = new int[n+1]; int j = 0; for(int i = 0;i < n;i++){ while (j < n && x[j] < x[i] + K)j++; f[i] = j; } f[n] = -1; int[] rf = new int[n+1]; j = n-1; for(int i = n-1;i >= 0;i--){ while (j >= 0 && x[j] > x[i] - K)j--; rf[i] = j == -1 ? n : j; } rf[n] = -1; long[] dp = new long[n+1]; for(int i = n-1;i >= 0;i--){ dp[i] = dp[f[i]] + i; } int[][] spar = logstepParents(f); long[] rdp = new long[n+1]; for(int i = 0;i < n;i++){ rdp[i] = rdp[rf[i]] + i; } int[][] rspar = logstepParents(rf); int Q = ni(); for(int z = 0;z < Q;z++){ int L = ni()-1, R = ni()-1; int y = L; int up = 0; for(int d = spar.length-1;d >= 0;d--){ int an = spar[d][y]; if(an == -1)continue; if(an > R)continue; y = an; up |= 1<<d; } int len = up + 1; long ans = (rdp[R] - rdp[ancestor(R, len, rspar)]) - (dp[L] - dp[ancestor(L, len, spar)]) + len; out.println(ans); } } public static int[][] logstepParents(int[] par) { int n = par.length; int m = Integer.numberOfTrailingZeros(Integer.highestOneBit(n-1))+1; int[][] pars = new int[m][n]; pars[0] = par; for(int j = 1;j < m;j++){ for(int i = 0;i < n;i++){ pars[j][i] = pars[j-1][i] == -1 ? -1 : pars[j-1][pars[j-1][i]]; } } return pars; } public static int ancestor(int a, int m, int[][] spar) { for(;m > 0 && a != -1;m &= m-1)a = spar[Integer.numberOfTrailingZeros(m)][a]; return a; } public static int[][] parentToG(int[] par) { int n = par.length; int[] ct = new int[n]; for(int i = 0;i < n;i++){ if(par[i] >= 0){ ct[i]++; ct[par[i]]++; } } int[][] g = new int[n][]; for(int i = 0;i < n;i++){ g[i] = new int[ct[i]]; } for(int i = 0;i < n;i++){ if(par[i] >= 0){ g[par[i]][--ct[par[i]]] = i; g[i][--ct[i]] = par[i]; } } return g; } public static int[][] parents(int[][] g, int root) { int n = g.length; int[] par = new int[n]; Arrays.fill(par, -1); int[] depth = new int[n]; depth[0] = 0; int[] q = new int[n]; q[0] = root; for (int p = 0, r = 1; p < r; p++) { int cur = q[p]; for (int nex : g[cur]) { if (par[cur] != nex) { q[r++] = nex; par[nex] = cur; depth[nex] = depth[cur] + 1; } } } return new int[][]{par, q, depth}; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G-S+"ms"); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
JAVA
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <cstdio> #include <cctype> #include <algorithm> #include <vector> #include <cmath> #include <cassert> using namespace std; using ll = long long; namespace io { #define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout) const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1; inline char getc () {return (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++);} inline void flush () {fwrite (obuf, 1, oS - obuf, stdout); oS = obuf;} inline void putc (char x) {*oS ++ = x; if (oS == oT) flush ();} template<class T> inline void read(T &x) { char ch; int f = 1; x = 0; while(isspace(ch = getc())); if(ch == '-') ch = getc(), f = -1; do x = x * 10 + ch - '0'; while(isdigit(ch = getc())); x *= f; } template<class T, class ...Args> inline void read(T &x, Args&... args) {read(x); read(args...);} template<class T> inline void write(T x) { static char stk[128]; int top = 0; if(x == 0) {putc('0'); return;} if(x < 0) putc('-'), x = -x; while(x) stk[top++] = x % 10, x /= 10; while(top) putc(stk[--top] + '0'); } template<class T, class ...Args> inline void write(T x, Args... args) {write(x); putc(' '); write(args...);} inline void space() {putc(' ');} inline void endl() {putc('\n');} struct _flush {~_flush() {flush();}} __flush; }; using io::read; using io::write; using io::flush; using io::space; using io::endl; using io::getc; using io::putc; const int N = 200005; int lgn; int n, k; struct max_good_set { int a[N], nxt[N][20]; ll sum[N][20]; void init() { int p = 0; for (int i = 0; i < n; ++i) { while (abs(a[p] - a[i]) < k && p < n) ++p; nxt[i][0] = p; sum[i][0] = i; } for (int i = 0; i <= lgn; ++i) nxt[n][i] = n, sum[n][i] = 0; for (int j = 1; j <= lgn; ++j) for (int i = 0; i < n; ++i) { nxt[i][j] = nxt[nxt[i][j - 1]][j - 1]; sum[i][j] = sum[i][j - 1] + sum[nxt[i][j - 1]][j - 1]; } } pair<ll, int> query(int l, int r) { int p = l, c = 1; ll s = 0; for (int i = lgn; i >= 0; --i) if (nxt[p][i] <= r) { s += sum[p][i]; c += 1 << i; p = nxt[p][i]; } assert(p <= r); return {s + p, c}; } }a, b; int main() { read(n, k); lgn = log2(n); for (int i = 0; i < n; ++i) read(a.a[i]); reverse_copy(a.a, a.a + n, b.a); a.init(); b.init(); int q; read(q); while (q--) { int l, r; read(l, r); --l; --r; auto A = a.query(l, r), B = b.query(n - 1 - r, n - 1 - l); B.first = 1LL * (n - 1) * B.second - B.first; write(B.first - A.first + A.second), endl(); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; const int N = 200005; const int LOGN = 18; int n, K, a[N], pa1[LOGN][N], pa2[LOGN][N]; long long sum1[LOGN][N], sum2[LOGN][N]; int main() { scanf("%d%d", &n, &K); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1, j = 1; i <= n; i++) { while (j <= n && a[j] - a[i] < K) j++; pa1[0][i] = j; sum1[0][i] = j; } for (int i = 0; i < LOGN; i++) pa1[i][n + 1] = n + 1; for (int i = 1; i < LOGN; i++) for (int j = 1; j <= n; j++) { pa1[i][j] = pa1[i - 1][pa1[i - 1][j]]; sum1[i][j] = sum1[i - 1][j] + sum1[i - 1][pa1[i - 1][j]]; } for (int i = n, j = n; i >= 1; i--) { while (j > 0 && a[i] - a[j] < K) j--; pa2[0][i] = j; sum2[0][i] = j; } for (int i = 0; i < LOGN; i++) pa2[i][0] = 0; for (int i = 1; i < LOGN; i++) for (int j = 1; j <= n; j++) { pa2[i][j] = pa2[i - 1][pa2[i - 1][j]]; sum2[i][j] = sum2[i - 1][j] + sum2[i - 1][pa2[i - 1][j]]; } int q; scanf("%d", &q); while (q--) { int l, r; scanf("%d%d", &l, &r); int u = l; long long s1 = l; int cnt = 1; for (int i = LOGN - 1; i >= 0; i--) if (pa1[i][u] <= r) { cnt += (1 << i); s1 += sum1[i][u]; u = pa1[i][u]; } u = r; long long s2 = r; for (int i = LOGN - 1; i >= 0; i--) if (pa2[i][u] >= l) { s2 += sum2[i][u]; u = pa2[i][u]; } printf("%lld\n", s2 - s1 + cnt); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> using namespace std; #define REP(i,n) for(int (i)=0;i<(int)(n);i++) #define all(hoge) (hoge).begin(),(hoge).end() typedef vector<int> Array; int l[20][202020]; int r[20][202020]; int dp1[20][202020]; int dp2[20][202020]; int main() { int n, k; cin >> n >> k; Array x(n); REP(i, n)cin >> x[i]; REP(i, n)dp1[0][i] = l[0][i] = lower_bound(all(x), x[i] + k) - x.begin(); REP(i, n)dp2[0][i] = r[0][i] = upper_bound(all(x), x[i] - k) - x.begin() - 1; REP(i, 19)REP(j, n)l[i + 1][j] = l[i][min(n - 1, l[i][j])]; REP(i, 19)REP(j, n)r[i + 1][j] = r[i][max(0, r[i][j])]; REP(i, 19)REP(j, n)dp1[i + 1][j] = dp1[i][j] + dp1[i][l[i][j]]; REP(i, 19)REP(j, n)dp2[i + 1][j] = dp2[i][j] + dp2[i][r[i][j]]; int q; cin >> q; while (q--) { int L, R; cin >> L >> R; L--; int ans = R - L; int x = L, y = R - 1; for (int i = 19; i >= 0; i--) { if (l[i][x] < R)ans += (1 << i) - dp1[i][x], x = l[i][x]; if (r[i][y] >= L)ans += dp2[i][y], y = r[i][y]; } cout << ans << "\n"; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
/* Author: QAQAutomaton Lang: C++ Code: D.cpp Mail: [email protected] Blog: https://www.qaq-am.com/ */ #include<bits/stdc++.h> #define debug(...) fprintf(stderr,__VA_ARGS__) #define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__) #define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__) #define all(x) x.begin(),x.end() #define x first #define y second using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef complex<double> cp; typedef pair<int,int> pii; int inf; const double eps=1e-8; const double pi=acos(-1.0); template<class T,class T2>int chkmin(T &a,T2 b){return a>b?a=b,1:0;} template<class T,class T2>int chkmax(T &a,T2 b){return a<b?a=b,1:0;} template<class T>T sqr(T a){return a*a;} template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;} template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;} template<class T>T aabs(T a){return a<0?-a:a;} template<class T>int dcmp(T a,T b){return a>b;} template<int *a>int cmp_a(int x,int y){return a[x]<a[y];} template<class T>bool sort2(T &a,T &b){return a>b?swap(a,b),1:0;} #define min mmin #define max mmax #define abs aabs struct __INIT__{ __INIT__(){ fill((unsigned char*)&inf,(unsigned char*)&inf+sizeof(inf),0x3f); } }__INIT___; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; // getchar #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) // print the remaining part inline void flush () { fwrite (obuf, 1, oS - obuf, stdout); oS = obuf; } // putchar inline void putc (char x) { *oS ++ = x; if (oS == oT) flush (); } template<typename A> inline bool read (A &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; return 1; } inline bool read (char &x) { while((x=gc())==' '||x=='\n' || x=='\r'); return x!=EOF; } inline bool read(char *x){ while((*x=gc())=='\n' || *x==' '||*x=='\r'); if(*x==EOF)return 0; while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc(); *x=0; return 1; } template<typename A,typename ...B> inline bool read(A &x,B &...y){ return read(x)&&read(y...); } template<typename A> inline bool write (A x) { if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x; while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) putc (qu[qr --]); return 0; } inline bool write (char x) { putc(x); return 0; } inline bool write(const char *x){ while(*x){putc(*x);++x;} return 0; } inline bool write(char *x){ while(*x){putc(*x);++x;} return 0; } template<typename A,typename ...B> inline bool write(A x,B ...y){ return write(x)||write(y...); } //no need to call flush at the end manually! struct Flusher_ {~Flusher_(){flush();}}io_flusher_; } using io :: read; using io :: putc; using io :: write; int a[200005]; int r1[19][200005],r2[19][200005]; int l1[19][200005],l2[19][200005]; signed main(){ #ifdef QAQAutoMaton freopen("D.in","r",stdin); freopen("D.out","w",stdout); #endif int n,k,q; read(n,k); for(int i=1;i<=n;++i)read(a[i]); int nex=n+1; for(int i=n;i;--i){ while(a[nex-1]-a[i]>=k){ --nex; } r1[0][i]=nex; r2[0][i]=i; } for(int i=0;i<=18;++i)r1[i][n+1]=n+1; nex=0; for(int i=1;i<=n;++i){ while(a[i]-a[nex+1]>=k){ ++nex; } l1[0][i]=nex; l2[0][i]=i; } for(int i=1;i<=18;++i){ for(int j=1;j<=n;++j){ r1[i][j]=r1[i-1][r1[i-1][j]]; l1[i][j]=l1[i-1][l1[i-1][j]]; r2[i][j]=r2[i-1][j]+r2[i-1][r1[i-1][j]]; l2[i][j]=l2[i-1][j]+l2[i-1][l1[i-1][j]]; } } read(q); for(;q;--q){ int l,r; read(l,r); int s=0; int xr=r; for(int i=18;~i;--i)if(r1[i][l]<=xr){ s+=(1<<i); s-=r2[i][l]; s+=l2[i][r]; l=r1[i][l]; r=l1[i][r]; } s+=r-l+1; write(s,'\n'); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> #define fi first #define se second #define rep(i,n) for(int i = 0; i < (n); ++i) #define rrep(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define isin(x,l,r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)),x.end()) #define snuke srand((unsigned)clock()+(unsigned)time(NULL)); #define show(x) cerr<<#x<<" = "<<x<<endl; #define PQ(T) priority_queue<T,v(T),greater<T> > #define bn(x) ((1<<x)-1) #define dup(x,y) (((x)+(y)-1)/(y)) #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) using namespace std; typedef long long int ll; typedef unsigned uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef tuple<int,int,int> T; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<T> vt; int getInt(){int x;scanf("%d",&x);return x;} template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;} template<typename T>string join(const v(T)&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);} template<typename T>ostream& operator<<(ostream&o,const v(T)&v){if(sz(v))o<<join(v);return o;} template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;} template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;} template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;} template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;} template<typename T>ll suma(const v(T)&a){ll res(0);for(auto&&x:a)res+=x;return res;} const double eps = 1e-10; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; #define dame { puts("-1"); return 0;} #define yn {puts("Yes");}else{puts("No");} const int MX = 200005; const int D = 18; typedef v(vl) vvl; int n,K; struct Db { vvi d; vvl s; Db(vi x):d(D,vi(n+1,n)),s(D,vl(n+1,0)) { int j = n; drep(i,n) { while (x[i]+K <= x[j-1]) --j; d[0][i] = j; s[0][i] = j; } rep(i,D-1) { rep(j,n) { d[i+1][j] = d[i][d[i][j]]; s[i+1][j] = s[i][j]+s[i][d[i][j]]; } } } int cnt; ll get(int l, int r) { int j = l; ll res = l; cnt = 1; int w = 1<<D; drep(i,D) { w >>= 1; if (d[i][j] > r) continue; res += s[i][j]; j = d[i][j]; cnt += w; } return res; } }; int main() { scanf("%d%d",&n,&K); vi x(n); cin>>x; Db dl(x); reverse(rng(x)); rep(i,n) x[i] = -x[i]; Db dr(x); int q; scanf("%d",&q); rep(qi,q) { int l,r; scanf("%d%d",&l,&r); --l; --r; ll xr = dr.get(n-1-r,n-1-l); xr = dr.cnt*ll(n-1) - xr; ll ans = xr - dl.get(l,r) + dl.cnt; printf("%lld\n",ans); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb(x) push_back(x) #define mp(a, b) make_pair(a, b) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define lscan(x) scanf("%I64d", &x) #define lprint(x) printf("%I64d", x) #define rep(i, n) for (ll i = 0; i < (n); i++) #define rep2(i, n) for (ll i = n - 1; i >= 0; i--) const int mod = 998244353; ll gcd(ll a, ll b) { ll c = a % b; while (c != 0) { a = b; b = c; c = a % b; } return b; } long long extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } struct UnionFind { vector<ll> data; UnionFind(int sz) { data.assign(sz, -1); } bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return (false); if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return (true); } int find(int k) { if (data[k] < 0) return (k); return (data[k] = find(data[k])); } ll size(int k) { return (-data[find(k)]); } }; ll M = 1000000007; vector<ll> fac(2000011); //n!(mod M) vector<ll> ifac(2000011); //k!^{M-2} (mod M) ll mpow(ll x, ll n) { ll ans = 1; while (n != 0) { if (n & 1) ans = ans * x % M; x = x * x % M; n = n >> 1; } return ans; } ll mpow2(ll x, ll n, ll mod) { ll ans = 1; while (n != 0) { if (n & 1) ans = ans * x % mod; x = x * x % mod; n = n >> 1; } return ans; } void setcomb() { fac[0] = 1; ifac[0] = 1; for (ll i = 0; i < 2000010; i++) { fac[i + 1] = fac[i] * (i + 1) % M; // n!(mod M) } ifac[2000010] = mpow(fac[2000010], M - 2); for (ll i = 2000010; i > 0; i--) { ifac[i - 1] = ifac[i] * i % M; } } ll comb(ll a, ll b) { if (a == 0 && b == 0) return 1; if (a < b || a < 0) return 0; ll tmp = ifac[a - b] * ifac[b] % M; return tmp * fac[a] % M; } ll perm(ll a, ll b) { if (a == 0 && b == 0) return 1; if (a < b || a < 0) return 0; return fac[a] * ifac[a - b] % M; } long long modinv(long long a) { long long b = M, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= M; if (u < 0) u += M; return u; } ll modinv2(ll a, ll mod) { ll b = mod, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= mod; if (u < 0) u += mod; return u; } 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; } }; using mint = ModInt<mod>; vector<vector<mint>> mul(vector<vector<mint>> a, vector<vector<mint>> b) { int i, j, k; mint t; int n = a.size(), m = b[0].size(), l = a[0].size(); vector<vector<mint>> c(n,vector<mint>(m)); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { t = 0; for (k = 0; k < l; k++) t += a[i][k] * b[k][j]; c[i][j] = t; } } return c; } vector<vector<mint>> mat_pow(vector<vector<mint>> x, ll n) { ll k = x.size(); vector<vector<mint>> ans(k, vector<mint>(k, 0)); for (int i = 0; i < k; i++) ans[i][i] = 1; while (n != 0) { if (n & 1) ans = mul(ans, x); x = mul(x, x); n = n >> 1; } return ans; } template <typename Monoid> struct SegmentTree { using F = function<Monoid(Monoid, Monoid)>; int sz; vector<Monoid> seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) { sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set(int k, const Monoid &x) { seg[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid &x) { k += sz; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } template <typename C> int find_subtree(int a, const C &check, Monoid &M, bool type) { while (a < sz) { Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template <typename C> int find_first(int a, const C &check) { Monoid L = M1; if (a <= 0) { if (check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, seg[a]); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template <typename C> int find_last(int b, const C &check) { Monoid R = M1; if (b >= sz) { if (check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(seg[--b], R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; template <unsigned mod> struct RollingHash { vector<unsigned> hashed, power; inline unsigned mul(unsigned a, unsigned b) const { unsigned long long x = (unsigned long long)a * b; unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod)); return m; } RollingHash(const string &s, unsigned base = 10007) { int sz = (int)s.size(); hashed.assign(sz + 1, 0); power.assign(sz + 1, 0); power[0] = 1; for (int i = 0; i < sz; i++) { power[i + 1] = mul(power[i], base); hashed[i + 1] = mul(hashed[i], base) + s[i]; if (hashed[i + 1] >= mod) hashed[i + 1] -= mod; } } unsigned get(int l, int r) const { unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]); if (ret >= mod) ret -= mod; return ret; } unsigned connect(unsigned h1, int h2, int h2len) const { unsigned ret = mul(h1, power[h2len]) + h2; if (ret >= mod) ret -= mod; return ret; } int LCP(const RollingHash<mod> &b, int l1, int r1, int l2, int r2) { int len = min(r1 - l1, r2 - l2); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(l1, l1 + mid) == b.get(l2, l2 + mid)) low = mid; else high = mid; } return (low); } }; using RH = RollingHash<1000000007>; 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; } 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>>; template <typename T> using Matrix = vector<vector<T>>; template <typename G> struct DoublingLowestCommonAncestor { const int LOG; vector<int> dep; const G &g; vector<vector<int>> table; DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) { table.assign(LOG, vector<int>(g.size(), -1)); } void dfs(int idx, int par, int d) { table[0][idx] = par; dep[idx] = d; for (auto &to : g[idx]) { if (to != par) dfs(to, idx, d + 1); } } void build() { dfs(0, -1, 0); for (int k = 0; k + 1 < LOG; k++) { for (int i = 0; i < table[k].size(); i++) { if (table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int i = LOG - 1; i >= 0; i--) { if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v]; } if (u == v) return u; for (int i = LOG - 1; i >= 0; i--) { if (table[i][u] != table[i][v]) { u = table[i][u]; v = table[i][v]; } } return table[0][u]; } }; template <typename Monoid, typename OperatorMonoid = Monoid> struct LazySegmentTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; int sz, height; vector<Monoid> data; vector<OperatorMonoid> lazy; const F f; const G g; const H h; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), M1(M1), OM0(OM0) { sz = 1; height = 0; while (sz < n) sz <<= 1, height++; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid &x) { data[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } inline void propagate(int k) { if (lazy[k] != OM0) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); data[k] = reflect(k); lazy[k] = OM0; } } inline Monoid reflect(int k) { return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]); } inline void recalc(int k) { while (k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1)); } inline void thrust(int k) { for (int i = height; i > 0; i--) propagate(k >> i); } void update(int a, int b, const OperatorMonoid &x) { thrust(a += sz); thrust(b += sz - 1); for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) lazy[l] = h(lazy[l], x), ++l; if (r & 1) --r, lazy[r] = h(lazy[r], x); } recalc(a); recalc(b); } Monoid query(int a, int b) { thrust(a += sz); thrust(b += sz - 1); Monoid L = M1, R = M1; for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) L = f(L, reflect(l++)); if (r & 1) R = f(reflect(--r), R); } return f(L, R); } Monoid operator[](const int &k) { return query(k, k + 1); } template <typename C> int find_subtree(int a, const C &check, Monoid &M, bool type) { while (a < sz) { propagate(a); Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type)); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template <typename C> int find_first(int a, const C &check) { Monoid L = M1; if (a <= 0) { if (check(f(L, reflect(1)))) return find_subtree(1, check, L, false); return -1; } thrust(a + sz); int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, reflect(a)); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template <typename C> int find_last(int b, const C &check) { Monoid R = M1; if (b >= sz) { if (check(f(reflect(1), R))) return find_subtree(1, check, R, true); return -1; } thrust(b + sz - 1); int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(reflect(--b), R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; ll nl[200010][20], nr[200010][20], sr[200010][20], sl[200010][20]; int main() { ll n, k, x[222222], q; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> x[i]; x[0] = -1e9; x[n + 1] = 2e9; ll l, r; r = 1; for (int i = 1; i <= n;i++){ while(x[r]-x[i]<k) r++; nr[i][0] = r; sr[i][0] = i; } nr[0][0] = 0; nr[n + 1][0] = n + 1; l = n; for (int i = n; i >= 1; i--){ while (x[i] - x[l] < k) l--; nl[i][0] = l; sl[i][0] = i; } nl[0][0] = 0; nl[n + 1][0] = n + 1; rep(j, 19) rep(i, n + 2) nr[i][j + 1] = nr[nr[i][j]][j], nl[i][j + 1] = nl[nl[i][j]][j], sr[i][j+1]=sr[i][j]+sr[nr[i][j]][j], sl[i][j+1]=sl[i][j]+sl[nl[i][j]][j]; cin >> q; rep(i,q){ ll L, R; cin >> L >> R; ll t = 0, m = 19, now = L; for (; m >= 0;m--){ if(nr[now][m]<=R){ t += (1 << m); now = nr[now][m]; } } ll sumr = 0, suml = 0; now = L; rep(j, 20) if ((t >> j) & 1) sumr += sr[now][j], now = nr[now][j]; sumr += now; now = R; rep(j, 20) if ((t >> j) & 1) suml += sl[now][j], now = nl[now][j]; suml += now; cout << suml - sumr + t + 1 << endl; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair < int , int > pii; #define mpr make_pair #define FS first #define SC second #define PB push_back template < typename T > void UMAX(T &a,T b){a=(a>b?a:b);} template < typename T > void UMIN(T &a,T b){a=(a<b?a:b);} LL readint(){ char c=getchar(); LL ret=0ll; bool neg=0; while(!(c>='0' && c<='9')){ if(c=='-') neg=1; c=getchar(); } while(c>='0' && c<='9'){ ret=ret*10ll+(LL)(c-'0'); c=getchar(); } return neg?-ret:ret; } void putint(LL v){ if(v<0){ putchar('-'); v=-v; } if(!v){ putchar('0'); return; } if(v>=10ll) putint(v/10ll); putchar('0'+(v%10ll)); } int n,D,x[200005],q; struct tree{ int fa[20][200005],val[200005],sum[200005]; tree(){ memset(fa,-1,sizeof(fa)); memset(val,0,sizeof(val)); } void prec(bool rev){ int i,j,k; for(i=1;i<20;++i){ for(j=0;j<n;++j){ if(~fa[i-1][j]) fa[i][j]=fa[i-1][fa[i-1][j]]; else fa[i][j]=-1; } } if(rev){ for(i=n-1;i>=0;--i){ sum[i]=val[i]+(~fa[0][i]?sum[fa[0][i]]:0); } } else{ for(i=0;i<n;++i){ sum[i]=val[i]+(~fa[0][i]?sum[fa[0][i]]:0); } } } int qry(int v,int tv){ // printf("qry %d %d\n",v,tv); int ret=sum[v],i; for(i=19;i>=0;--i){ if(~fa[i][v] && val[fa[i][v]]>=tv) v=fa[i][v]; } // printf("got to %d\n",v); return ret-(~fa[0][v]?sum[fa[0][v]]:0); } }R2L,L2R; int main(){ int i,j,k; n=readint();D=readint(); for(i=0;i<n;++i) x[i]=readint(); for(i=0;i<n;++i) R2L.val[i]=i,L2R.val[i]=-i+1; for(i=j=0;i<n;++i){ while(x[i]-x[j]>=D) ++j; R2L.fa[0][i]=j-1; } for(i=j=n-1;i>=0;--i){ while(x[j]-x[i]>=D) --j; L2R.fa[0][i]=(j==n-1?-1:j+1); } R2L.prec(0); L2R.prec(1); q=readint(); while(q--){ int l=readint()-1,r=readint()-1; printf("%d\n",R2L.qry(r,l)+L2R.qry(l,-r+1)); } return 0; } /* * 代码框架 * 1. 实现树结构体,自行设置父亲、权值,支持定位祖先并求权值和 * 2. 向左走->下标,向右走->下标相反数 */
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
//////////////////////////////////////////////////////////////////// //////////// AUTHOR : LYNMISAKURA //////////////////// //////////////////////////////////////////////////////////////////// #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i = 0;i < n;i++) #define mp make_pair #define pb push_back #define eb emplace_back #define all(x) (x).begin(),(x).end() template<class T> bool chmax(T& a,T b){ if(a < b){ a = b; return true; }else return false; } template<class T> bool chmin(T& a,T b){ if(a > b){ a = b; return true; }else return false; } using ll = long long; using ld = long double; using vi = vector<int>; using vl = vector<ll>; using Pi = pair<int,int>; using Pl = pair<ll,ll>; using vpi = vector<Pi>; using vpl = vector<Pl>; #define debug(arr) cout << #arr << " = " << arr << '\n' #define debug2(a,b) cout << "[" << #a << "," << #b << "] = " << "[" << a << "," << b << "]" << '\n' template<class T> ostream &operator << (ostream& out, const vector<T>& arr) { cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}"; return out; } template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) { cout << "{\n"; for (auto& vec : arr)cout << " " << vec << ",\n"; cout << "}"; return out; } template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){ cout << "{" << p.first << "," << p.second << "}" << '\n'; return out; } template<class T> istream &operator >> (istream& in, vector<T>& arr) { for (auto& i : arr)cin >> i; return in; } #define maxn 200020 #define maxlog 30 const ll INF = (1LL << 55); int N,K,Q; ll X[maxn]; int R[maxn][maxlog],L[maxn][maxlog]; ll RSum[maxn][maxlog],LSum[maxn][maxlog]; int get_l(int x,int k){ int tmp = x; for(int i = 0;i < maxlog;i++){ if(k >> i & 1){ tmp = L[tmp][i]; } } return tmp; } int get_r(int x,int k){ int tmp = x; for(int i = 0;i < maxlog;i++){ if(k >> i & 1){ tmp = R[tmp][i]; } } return tmp; } ll get_ls(int x,int k){ int tmp = x; ll res = x; for(int i = 0;i < maxlog;i++){ if(k >> i & 1){ res += LSum[tmp][i]; tmp = L[tmp][i]; } } return res; } ll get_rs(int x,int k){ int tmp = x; ll res = x; for(int i = 0;i < maxlog;i++){ if(k >> i & 1){ res += RSum[tmp][i]; tmp = R[tmp][i]; } } return res; } void init(){ R[0][0] = 1; R[N+1][0] = N+1; L[N+1][0] = N; L[0][0] = 0; int l = 1,r = 2; for(;l <= N;l++){ for(;r <= N+1;r++){ if(X[l] + K <= X[r]){ R[l][0] = r; break; } } } for(int i = 1;i < maxlog;i++){ for(int j = 0;j <= N+1;j++){ R[j][i] = R[R[j][i-1]][i-1]; } } l = N-1,r = N; for(;r >= 1;r--){ for(;l >= 0;l--){ if(X[l] + K <= X[r]){ L[r][0] = l; break; } } } for(int i = 1;i < maxlog;i++){ for(int j = 0;j <= N+1;j++){ L[j][i] = L[L[j][i-1]][i-1]; } } for(int i = 1;i <= N;i++){ RSum[i][0] = R[i][0]; LSum[i][0] = L[i][0]; } for(int i = 1;i < maxlog;i++){ for(int j = 1;j <= N;j++){ RSum[j][i] = RSum[j][i-1] + RSum[R[j][i-1]][i-1]; LSum[j][i] = LSum[j][i-1] + LSum[L[j][i-1]][i-1]; } } } ll query(int l,int r){ int low = 0,high = N; while(low + 1 < high){ int mid = (low + high)/2; if(get_r(l,mid) <= r){ low = mid; }else high = mid; } int step = low; //cout << get_ls(r,step) << ' ' << get_rs(l,step) << endl; return get_ls(r,step) - get_rs(l,step) + step + 1; } int main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> N >> K; for(int i = 0;i <= N+1;i++){ if(i == 0) X[i] = -INF; else if(i <= N) cin >> X[i]; else X[i] = INF; } init(); cin >> Q; for(int i = 0;i < Q;i++){ int l,r;cin >> l >> r; cout << query(l,r) << '\n'; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define x first #define y second typedef pair<int,int> pii; typedef long long ll; typedef unsigned long long ull; template <typename T> void chkmax(T &x,T y){x<y?x=y:T();} template <typename T> void chkmin(T &x,T y){x>y?x=y:T();} template <typename T> void readint(T &x) { x=0;int f=1;char c; for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1; for(;isdigit(c);c=getchar())x=x*10+c-'0'; x*=f; } const int MOD=998244353; inline int dmy(int x){return x>=MOD?x-MOD:x;} inline void inc(int &x,int y){x=dmy(x+y);} int qmi(int x,int y) { int ans=1; for(;y;y>>=1,x=1ll*x*x%MOD) if(y&1)ans=1ll*ans*x%MOD; return ans; } const int MAXN=200005,MAXK=19; int n,K,a[MAXN],l[MAXN][MAXK],r[MAXN][MAXK]; ll sl[MAXN][MAXK],sr[MAXN][MAXK]; int main() { #ifdef LOCAL freopen("code.in","r",stdin); // freopen("code.out","w",stdout); #endif int Q,u,v; readint(n),readint(K); for(int i=1;i<=n;++i)readint(a[i]); a[0]=a[1]-K,a[n+1]=a[n]+K; for(int i=1;i<=n;++i) l[i][0]=upper_bound(a,a+n+2,a[i]-K)-a-1, sl[i][0]=(l[i][0]<=n?l[i][0]:0), r[i][0]=lower_bound(a,a+n+2,a[i]+K)-a, sr[i][0]=(r[i][0]<=n?r[i][0]:0); for(int j=0;j<MAXK;++j)r[n+1][j]=n+1; for(int j=1;j<MAXK;++j) for(int i=1;i<=n;++i) l[i][j]=l[l[i][j-1]][j-1],r[i][j]=r[r[i][j-1]][j-1], sl[i][j]=sl[i][j-1]+sl[l[i][j-1]][j-1],sr[i][j]=sr[i][j-1]+sr[r[i][j-1]][j-1]; readint(Q); while(Q--) { readint(u),readint(v); ll su=u,sv=v; for(int i=u,j=MAXK-1;j>=0;--j) if(r[i][j]<=v)su+=sr[i][j],i=r[i][j]; for(int i=v,j=MAXK-1;j>=0;--j) if(l[i][j]>=u)sv+=sl[i][j]+(1<<j),i=l[i][j]; printf("%lld\n",sv-su+1); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring> #include <chrono> #include <random> #include <bitset> using namespace std; #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); #else #define eprintf(...) 42 #endif using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; template<typename T> using pair2 = pair<T, T>; using pii = pair<int, int>; using pli = pair<ll, int>; using pll = pair<ll, ll>; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } const int N = 200200; const int LOG = 18; int a[N]; int n, d; ll b[LOG][N][2]; ll c[LOG][N][2]; ll solveL(int l, int r) { ll ans = 0; int len = 0; for (int k = LOG - 1; k >= 0; k--) { if (b[k][l][0] <= r) { len += 1 << k; ans += b[k][l][1]; l = b[k][l][0]; } } // eprintf("len = %d\n", len + 1); return ans + l; } ll solveR(int l, int r) { ll ans = 0; int len = 0; for (int k = LOG - 1; k >= 0; k--) { if (c[k][r][0] >= l) { len += 1 << k; ans += c[k][r][1]; r = c[k][r][0]; } } // eprintf("len = %d\n", len + 1); return ans + r + 1; } int main() { startTime = clock(); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int p = 1; for (int i = 1; i <= n; i++) { while(p <= n && a[p] - a[i] < d) p++; //eprintf("%d %d\n", i, p); b[0][i][0] = p; b[0][i][1] = i; } b[0][n + 1][0] = n + 1; for (int k = 0; k < LOG - 1; k++) { for (int i = 1; i <= n + 1; i++) { int v = b[k][i][0]; b[k + 1][i][0] = b[k][v][0]; b[k + 1][i][1] = b[k][i][1] + b[k][v][1]; } } p = n; for (int i = n; i >= 1; i--) { while(p > 0 && a[i] - a[p] < d) p--; //eprintf("%d %d\n", i, p); c[0][i][0] = p; c[0][i][1] = i + 1; } c[0][0][0] = 0; for (int k = 0; k < LOG - 1; k++) { for (int i = 0; i <= n; i++) { int v = c[k][i][0]; c[k + 1][i][0] = c[k][v][0]; c[k + 1][i][1] = c[k][i][1] + c[k][v][1]; } } int q; scanf("%d", &q); while(q--) { int l, r; scanf("%d%d", &l, &r); printf("%lld\n", solveR(l, r) - solveL(l, r)); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> using namespace std; const int maxn=200005; const int mlog=20; int n,k,q; int x[maxn]; int prv[maxn][mlog],nxt[maxn][mlog]; long long sprv[maxn][mlog],snxt[maxn][mlog]; int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++)scanf("%d",&x[i]); int ppt=0; for(int j=0;j<mlog;j++)prv[0][j]=0; for(int i=1;i<=n;i++){ while(ppt+1<i&&x[i]-x[ppt+1]>=k)ppt++; prv[i][0]=ppt; sprv[i][0]=i; for(int j=1;j<mlog;j++) prv[i][j]=prv[prv[i][j-1]][j-1],sprv[i][j]=sprv[i][j-1]+sprv[prv[i][j-1]][j-1]; } int npt=n+1; for(int j=0;j<mlog;j++)nxt[n+1][j]=n+1; for(int i=n;i>=1;i--){ while(npt-1>i&&x[npt-1]-x[i]>=k)npt--; nxt[i][0]=npt; snxt[i][0]=i; for(int j=1;j<mlog;j++) nxt[i][j]=nxt[nxt[i][j-1]][j-1],snxt[i][j]=snxt[i][j-1]+snxt[nxt[i][j-1]][j-1]; } scanf("%d",&q); while(q--){ int l,r; scanf("%d%d",&l,&r); long long ans=0; int cl=l,cr=r,cd=0; for(int i=mlog-1;i>=0;i--)if(nxt[cl][i]<=r){ ans+=sprv[cr][i]-snxt[cl][i]; cl=nxt[cl][i]; cr=prv[cr][i]; cd+=(1<<i); } ans+=cr-cl+cd+1; printf("%lld\n",ans); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author aajisaka */ #include<bits/stdc++.h> using namespace std; void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr) #define rep(i,n) for(int i=0; i<(int)(n); i++) #define all(v) v.begin(), v.end() 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; } using ll = long long; using ull = unsigned long long; using P = pair<ll, ll>; constexpr long double PI = 3.14159265358979323846264338327950288L; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); class DKeepDistances { public: void solve(istream& cin, ostream& cout) { SPEED; int n, k; cin >> n >> k; vector<int> x(n); rep(i, n) { cin >> x[i]; } vector<vector<int>> next(20, vector<int>(n, n)); vector<vector<ll>> dp(20, vector<ll>(n, 0)); int j=1; for(int i=0; i<n; i++) { while(j < n && x[j] - x[i] < k) { j++; } next[0][i] = j; dp[0][i] = j; } vector<vector<int>> next2(20, vector<int>(n, -1)); vector<vector<ll>> dp2(20, vector<ll>(n, 0)); j=n-2; for(int i=n-1; i>=0; i--) { while(j >= 0 && x[i] - x[j] < k) { j--; } next2[0][i] = j; dp2[0][i] = j; } for(int r=1; r<20; r++) { for(int i=0; i<n; i++) { if (next[r-1][i] == n) { next[r][i] = n; } else { next[r][i] = next[r-1][next[r-1][i]]; dp[r][i] = dp[r-1][i] + dp[r-1][next[r-1][i]]; } } } for(int r=1; r<20; r++) { for(int i=0; i<n; i++) { if (next2[r-1][i] == -1) { next2[r][i] = -1; } else { next2[r][i] = next2[r-1][next2[r-1][i]]; dp2[r][i] = dp2[r-1][i] + dp2[r-1][next2[r-1][i]]; } } } int q; cin >> q; rep(_, q) { int l, r; cin >> l >> r; l--; r--; ll lcnt = l; int now = l; int cnt = 1; for(int p=19; p>=0; p--) { if (next[p][now] <= r) { lcnt += dp[p][now]; now = next[p][now]; cnt += (1<<p); } } //debug(_, lcnt); now = r; ll rcnt = r; for(int p=19; p>=0; p--) { if (next2[p][now] >= l) { rcnt += dp2[p][now]; now = next2[p][now]; } } //debug(_, rcnt); cout << rcnt-lcnt+cnt << '\n'; } } }; signed main() { DKeepDistances solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<iostream> #include<vector> #include<set> #include<queue> #include<map> #include<algorithm> #include<cstring> #include<string> #include<cassert> #include<cmath> #include<climits> #include<iomanip> #include<stack> #include<unordered_map> #include<bitset> #include<limits> #include<complex> #include<array> #include<numeric> #include<functional> #include<random> using namespace std; #define ll long long #define ull unsigned long long #define rep(i,m,n) for(ll (i)=(ll)(m);i<(ll)(n);i++) #define REP(i,n) rep(i,0,n) #define all(hoge) (hoge).begin(),(hoge).end() typedef pair<ll, ll> P; constexpr long double m_pi = 3.1415926535897932L; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 61; constexpr long double EPS = 1e-10; template<typename T> using vector2 = vector<vector<T>>; template<typename T> using vector3 = vector<vector2<T>>; typedef vector<ll> Array; ll l[20][202020]; ll r[20][202020]; ll dp1[20][202020]; ll dp2[20][202020]; int main() { ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); ll n, k; cin >> n >> k; Array x(n); REP(i, n)cin >> x[i]; REP(i, n)dp1[0][i] = l[0][i] = lower_bound(all(x), x[i] + k) - x.begin(); REP(i, n)dp2[0][i] = r[0][i] = upper_bound(all(x), x[i] - k) - x.begin() - 1; REP(i, 19)REP(j, n)l[i + 1][j] = l[i][min(n - 1, l[i][j])]; REP(i, 19)REP(j, n)r[i + 1][j] = r[i][max(0LL, r[i][j])]; REP(i, 19)REP(j, n)dp1[i + 1][j] = dp1[i][j] + dp1[i][l[i][j]]; REP(i, 19)REP(j, n)dp2[i + 1][j] = dp2[i][j] + dp2[i][r[i][j]]; ll q; cin >> q; while (q--) { ll L, R; cin >> L >> R; L--; ll ans = R - L; ll x = L, y = R - 1; for (int i = 19; i >= 0; i--) { if (l[i][x] < R)ans += (1LL << i) - dp1[i][x], x = l[i][x]; if (r[i][y] >= L)ans += dp2[i][y], y = r[i][y]; } cout << ans << "\n"; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> #define endl "\n" using namespace std; typedef long long ll; typedef vector<ll> vl; typedef pair<ll, ll> PP; #define rep(i, n) for(ll i = 0; i < ll(n); i++) #define rrep(i, n) for(ll i = n - 1; i > -1; i--) #define all(v) v.begin(), v.end() #define pb push_back #define fi first #define se second template <class X> void print(X x) { cout << x << endl; } #define in(A, n) \ rep(i, n) { \ cin >> f; \ A.push_back(f); \ } void print(vl x) { for(ll i : x) { cout << i << " "; } cout << endl; } const ll INF = (1LL << 61) - 1; const ll MOD = 1000000007 /*998244353*/; const ll MAX_N = 500010; ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, i, j, u, v, m; ll codeforces = 1; string S, T; vector<vl> g(MAX_N); vl A, B, C; ll NEXT[MAX_N][20], NEXT2[MAX_N][20]; void input() { cin >> n >> k; rep(i, n) { cin >> x; A.pb(x); } cin >> q; } void solve() { rep(i, n) { a = lower_bound(all(A), A[i] + k) - A.begin(); B.pb(a); } rep(i, n) { a = upper_bound(all(A), A[i] - k) - A.begin() - 1; C.pb(a); } rep(i, 20) { if(i == 0) { rep(j, n) { NEXT[j][i] = B[j]; } } else { rep(j, n) { if(NEXT[j][i - 1] == n) NEXT[j][i] = n; else NEXT[j][i] = NEXT[NEXT[j][i - 1]][i - 1]; } } } rep(i, 20) { if(i == 0) { rep(j, n) { NEXT2[j][i] = C[j]; } } else { rep(j, n) { if(NEXT2[j][i - 1] == -1) NEXT2[j][i] = -1; else NEXT2[j][i] = NEXT2[NEXT2[j][i - 1]][i - 1]; } } } vl cost1(n + 1, 0), cost2(n + 1, 0); for(ll i = n; i > -1; i--) { if(i == n) cost1[i] = 0; else { cost1[i] = cost1[NEXT[i][0]] + i; } } for(ll i = -1; i < n; i++) { if(i == -1) cost2[i + 1] = 0; else { cost2[i + 1] = cost2[C[i] + 1] + i; } } rep(i, q) { cin >> l >> r; l--; r--; a = 0; t = l; p = r; rrep(i, 20) { if(NEXT[t][i] <= r) { a += 1 << i; t = NEXT[t][i]; p = NEXT2[p][i]; } } t = B[t]; p = C[p]; print(a - (cost1[l] - cost1[t]) + (cost2[r + 1] - cost2[p + 1]) + 1); } } int main() { // cout<<fixed<<setprecision(15); cin.tie(0); ios::sync_with_stdio(false); input(); while(codeforces--) { solve(); } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = a; i >= b; i--) using namespace std; typedef unsigned long long ull; typedef pair <int, int> pii; typedef long long ll; template <typename _T> inline void read(_T &f) { f = 0; _T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } template <typename T> void print(T x, char t) { print(x); putchar(t); } const int N = 2e5 + 5; ll suml[N][20], sumr[N][20]; int a[N], l[N][20], r[N][20]; int n, k, q; int main() { read(n); read(k); for (int i = 1; i <= n; i++) read(a[i]); for (int i = 1; i <= n; i++) { l[i][0] = upper_bound(a + 1, a + n + 1, a[i] - k) - a - 1; r[i][0] = lower_bound(a + 1, a + n + 1, a[i] + k) - a; suml[i][0] = i; sumr[i][0] = i - 1; } r[n + 1][0] = n + 1; for (int j = 1; j <= 19; j++) { for (int i = 0; i <= n + 1; i++) { l[i][j] = l[l[i][j - 1]][j - 1]; suml[i][j] = suml[i][j - 1] + suml[l[i][j - 1]][j - 1]; r[i][j] = r[r[i][j - 1]][j - 1]; sumr[i][j] = sumr[i][j - 1] + sumr[r[i][j - 1]][j - 1]; } } read(q); while (q--) { int L, R; read(L); read(R); ll sumL = 0, sumR = 0; int nowL = L, nowR = R; for (int j = 19; j >= 0; j--) { if (r[nowL][j] <= R) { sumL += sumr[nowL][j]; nowL = r[nowL][j]; } if (l[nowR][j] >= L) { sumR += suml[nowR][j]; nowR = l[nowR][j]; } } print(sumR - sumL + nowR - nowL + 1, '\n'); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> using namespace std; #define INF 1234567890 #define ll long long int N, K, Q; int X[200201]; int L[20][200201], R[20][200201]; // ll LS[20][200201], RS[20][200201]; // // LS[i][j] : j를 포함해서 만나는 2^i개의 정점의 번호 합 int main() { scanf("%d %d", &N, &K); for(int i=1; i<=N; i++) scanf("%d", &X[i]); // L[] 채우기 map<int, int> m; for(int i=1; i<=N; i++) { m[X[i]] = i; auto it = m.upper_bound(X[i]-K); if (it == m.begin()) continue; it = prev(it); L[0][i] = it->second; } m.clear(); for(int i=N; i>=1; i--) { m[X[i]] = i; auto it = m.lower_bound(X[i]+K); if (it == m.end()) continue; R[0][i] = it->second; } for(int i=1; i<=N; i++) LS[0][i] = RS[0][i] = i; for(int i=1; i<20; i++) for(int j=1; j<=N; j++) { L[i][j] = L[i-1][L[i-1][j]]; R[i][j] = R[i-1][R[i-1][j]]; LS[i][j] = LS[i-1][j] + LS[i-1][L[i-1][j]]; RS[i][j] = RS[i-1][j] + RS[i-1][R[i-1][j]]; } scanf("%d", &Q); while(Q--) { int l, r; scanf("%d %d", &l, &r); ll ls = 0, rs = 0, cnt = 0; int n = r; for(int i=19; i>=0; i--) if (L[i][n] != 0 && l <= L[i][n]) rs += LS[i][n], n = L[i][n], cnt += (1<<i); rs += LS[0][n]; cnt++; n = l; for(int i=19; i>=0; i--) if (R[i][n] != 0 && R[i][n] <= r) ls += RS[i][n], n = R[i][n]; ls += RS[0][n]; //printf("%lld, %lld, %lld : ", rs, ls, cnt); printf("%lld\n", rs-ls+cnt); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
//#pragma GCC optimize("Ofast", "unroll-loops") //#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4", "avx") #ifdef __APPLE__ # include <iostream> # include <cmath> # include <algorithm> # include <stdio.h> # include <cstdint> # include <cstring> # include <string> # include <cstdlib> # include <vector> # include <bitset> # include <map> # include <queue> # include <ctime> # include <stack> # include <set> # include <list> # include <random> # include <deque> # include <functional> # include <iomanip> # include <sstream> # include <fstream> # include <complex> # include <numeric> # include <immintrin.h> # include <cassert> # include <array> # include <tuple> # include <unordered_map> # include <unordered_set> # include <thread> #else # include <bits/stdc++.h> #endif #define F first #define S second #define MP make_pair #define PB push_back #define all(a) a.begin(),a.end() #define len(a) (int)(a.size()) #define mp make_pair #define pb push_back #define fir first #define sec second using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef long double ld; const int max_n = 2e5+10, inf = 1000111222; const int max_log=20; int x[max_n]; pii prec_L[max_n][max_log]; pii prec_R[max_n][max_log]; int next_L[max_n]; int next_R[max_n]; int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k; cin>>n>>k; for (int i=0;i<n;i++){ cin>>x[i]; } auto precalc=[&]() { next_L[0]=0; for (int i=0;i<n;i++){ if (i){ next_L[i]=next_L[i-1]; } while (next_L[i]<n && x[next_L[i]]-x[i]<k){ next_L[i]++; } // cout<<"next_L["<<i<<"] :: "<<next_L[i]<<"\n"; } next_R[n-1]=n-1; for (int i=n-1;i>=0;i--){ if (i+1!=n){ next_R[i]=next_R[i+1]; } while (next_R[i]>=0 && x[i]-x[next_R[i]]<k){ next_R[i]--; } // cout<<"next_R["<<i<<"] :: "<<next_R[i]<<"\n"; } for (int j=0;j<max_log;j++){ if (j==0){ for (int i=0;i<n;i++){ prec_L[i][j]=mp(next_L[i],i); } } else{ for (int i=0;i<n;i++){ int to = prec_L[i][j-1].fir; if (to<n){ prec_L[i][j] = prec_L[to][j-1]; prec_L[i][j].sec += prec_L[i][j-1].sec; } else{ prec_L[i][j] = prec_L[i][j-1]; } } } } for (int j=0;j<max_log;j++){ if (j==0){ for (int i=0;i<n;i++){ prec_R[i][j]=mp(next_R[i],i); } } else{ for (int i=0;i<n;i++){ int to = prec_R[i][j-1].fir; if (to>=0){ prec_R[i][j] = prec_R[to][j-1]; prec_R[i][j].sec += prec_R[i][j-1].sec; } else{ prec_R[i][j] = prec_R[i][j-1]; } } } } }; auto get_naive_ans_L=[&](int l,int r) { pii res=mp(1,0); int cur=l; for (int j=max_log-1;j>=0;j--){ if (prec_L[cur][j].fir<=r){ res.fir+=(1ll<<j); res.sec+=prec_L[cur][j].sec; cur=prec_L[cur][j].fir; } } res.sec+=cur; return res; }; auto get_naive_ans_R=[&](int l,int r) { pii res=mp(1,0); int cur=r; for (int j=max_log-1;j>=0;j--){ if (prec_R[cur][j].fir>=l){ res.fir+=(1ll<<j); res.sec+=prec_R[cur][j].sec; cur=prec_R[cur][j].fir; } } res.sec+=cur; return res; }; auto get_ans=[&](int l,int r) { pii ans_L=get_naive_ans_L(l,r); pii ans_R=get_naive_ans_R(l,r); // cerr<<l<<" "<<r<<" :: "<<"("<<ans_L.fir<<","<<ans_L.sec<<"), "<<"("<<ans_R.fir<<","<<ans_R.sec<<")"<<"\n"; assert(ans_L.fir == ans_R.fir); return ans_L.fir + (ans_R.sec-ans_L.sec); }; precalc(); int q; cin>>q; while (q--){ int l,r; cin>>l>>r; l--; r--; cout<<get_ans(l,r)<<"\n"; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; long long n, k, q; vector<long long> x, rx; vector<vector<long long>> memo, rmemo, sum, rsum; int main() { cin >> n >> k; x.resize(n); for (auto &p : x) cin >> p; rx = x; reverse(rx.begin(), rx.end()); for (auto &p : rx) p = -p; auto calcmemo = [](vector<long long> &v) { vector<vector<long long>> res(30, vector<long long>(n + 1, n)); for (int i = 0; i < n; ++i) res[0][i] = lower_bound(v.begin(), v.end(), v[i] + k) - v.begin(); for (int i = 1; i < 30; ++i) for (int j = 0; j < n; ++j) res[i][j] = res[i - 1][res[i - 1][j]]; return res; }; auto calcsum = [](vector<vector<long long>> &v, bool reversed = 0) { vector<vector<long long>> res(30, vector<long long>(n + 1, 0)); if (reversed) for (int i = 1; i <= n; ++i) res[0][i] = v[0][i]; else for (int i = 0; i < n; ++i) res[0][i] = v[0][i] * (v[0][i] != n); for (int i = 1; i < 30; ++i) for (int j = 0; j <= n; ++j) res[i][j] = res[i - 1][j] + res[i - 1][v[i - 1][j]]; return res; }; memo = calcmemo(x); rmemo = calcmemo(rx); for (auto &v : rmemo) { reverse(v.begin(), v.end()); for (auto &p : v) p = n - p; } sum = calcsum(memo); rsum = calcsum(rmemo, 1); cin >> q; for (int i = 0; i < q; ++i) { long long x, y, z, now = 0; cin >> x >> y; --x; // x z = x; for (int j = 29; j >= 0; --j) if (memo[j][z] < y) { now -= sum[j][z]; z = memo[j][z]; } // y z = y; for (int j = 29; j >= 0; --j) if (x < rmemo[j][z]) { now += rsum[j][z]; z = rmemo[j][z]; } cout << now + y - x << endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#pragma GCC optimize("O3", "unroll-loops") #pragma GCC target("avx") #include <bits/stdc++.h> using namespace std; //using namespace atcoder; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin);i<(end);i++) #define REP(i, n) FOR(i,0,n) #define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--) #define IREP(i, n) IFOR(i,0,n) #define Sort(v) sort(v.begin(), v.end()) #define Reverse(v) reverse(v.begin(), v.end()) #define all(v) v.begin(),v.end() #define SZ(v) ((int)v.size()) #define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) #define bit(n) (1LL<<(n)) #define debug(x) cout << #x << "=" << x << endl; #define vdebug(v) { cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; } #define mdebug(m) { cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} } #define pb push_back #define fi first #define se second #define int long long #define INF 1000000000000000000 template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; } template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; } template<typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p){ cout << '(' << p.first << ',' << p.second << ')'; return os; } template<typename T> void Out(T x) { cout << x << endl; } template<typename T1, typename T2> void chOut(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); } using vec = vector<int>; using mat = vector<vec>; using Pii = pair<int, int>; using v_bool = vector<bool>; using v_Pii = vector<Pii>; //int dx[4] = {1,0,-1,0}; //int dy[4] = {0,1,0,-1}; //char d[4] = {'D','R','U','L'}; const int mod = 1000000007; //const int mod = 998244353; signed main(){ int N, K; cin >> N >> K; vec X(N); cin >> X; int Q; cin >> Q; vector<v_Pii> next(18, v_Pii(N, Pii(N, 0))), prev(18, v_Pii(N, Pii(-1, 0))); REP(i, N){ int j = Lower_bound(X, X[i] + K); if(j < N) next[0][i] = Pii(j, j); int k = Upper_bound(X, X[i] - K) - 1; if(k >= 0) prev[0][i] = Pii(k, k); } FOR(t, 1, 18){ REP(i, N) if(next[t - 1][i].fi < N){ int v = next[t - 1][next[t - 1][i].fi].fi; int cost = next[t - 1][i].se + next[t - 1][next[t - 1][i].fi].se; next[t][i] = Pii(v, cost); } REP(i, N) if(prev[t - 1][i].fi >= 0){ int v = prev[t - 1][prev[t - 1][i].fi].fi; int cost = prev[t - 1][i].se + prev[t - 1][prev[t - 1][i].fi].se; prev[t][i] = Pii(v, cost); } } //mdebug(next); mdebug(prev); vec ans(Q); REP(q, Q){ int L, R; cin >> L >> R; L--; R--; int nl = 1, cl = L; int now = L; IREP(t, 18) if(next[t][now].fi <= R){ nl += bit(t); cl += next[t][now].se; now = next[t][now].fi; } int nr = 1, cr = R; now = R; IREP(t, 18) if(prev[t][now].fi >= L){ nr += bit(t); cr += prev[t][now].se; now = prev[t][now].fi; } ans[q] = cr - cl + nl; } REP(i, Q) cout << ans[i] << '\n'; return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
import sys readline = sys.stdin.readline INF = 10**18+3 N, K = map(int, input().split()) X = list(map(int, readline().split())) + [INF] left = [None]*N r = 0 for i in range(N): while r < N and X[r+1] - X[i] < K: r += 1 left[i] = (r, i) left = [left] nb = N.bit_length() for _ in range(nb): res = [None]*N for idx in range(N): r, i = left[-1][idx] if r >= N-1: res[idx] = (N, None) else: r1, i1 = left[-1][r+1] if r1 == N: res[idx] = (N, None) else: res[idx] = (r1, i+i1) left.append(res) right = [None]*N l = N-1 for i in range(N-1, -1, -1): while 0 < l and X[i] - X[l-1] < K: l -= 1 right[i] = (l, i) right = [right] for _ in range(nb): res = [None]*N for idx in range(N): l, i = right[-1][idx] if l <= 0: res[idx] = (-1, None) else: l1, i1 = right[-1][l-1] if l1 == -1: res[idx] = (-1, None) else: res[idx] = (l1, i+i1) right.append(res) Q = int(readline()) Ans = [None]*Q for qu in range(Q): l, r = map(int, readline().split()) l -= 1 vn = l li = 0 ml = 0 for j in range(nb-1, -1, -1): vf, ix = left[j][vn] if vf < r: vn = vf+1 li += ix ml += 1<<j if vn >= r: break if vn < r: ml += 1 li += vn l -= 1 r -= 1 vn = r ri = 0 mr = 0 for j in range(nb-1, -1, -1): vf, ix = right[j][vn] if l < vf: vn = vf-1 ri += ix mr += 1<<j if vn <= l: break if l < vn: mr += 1 ri += vn Ans[qu] = ri-li + ml print('\n'.join(map(str, Ans)))
PYTHON3
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) { for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); } putchar('\n'); } int N; LL K; LL X[200011]; const int LOG = 18; int Right[LOG][200011]; LL RightCnt[LOG][200011]; LL RightSum[LOG][200011]; int Left[LOG][200011]; LL LeftCnt[LOG][200011]; LL LeftSum[LOG][200011]; void MAIN() { scanf("%d%lld", &N, &K); REP (i, N) scanf("%lld", X+i); for (int i=0, j=0; i<N; i++) { while (j < N && X[j] - X[i] < K) j++; Right[0][i] = j; RightCnt[0][i] = j - i; RightSum[0][i] = j - i; } Right[0][N] = N; REP (t, LOG-1) { REP (i, N+1) { int s = Right[t][i]; Right[t+1][i] = Right[t][s]; RightCnt[t+1][i] = RightCnt[t][i] + RightCnt[t][s]; RightSum[t+1][i] = RightSum[t][i] + RightSum[t][s] + (RightCnt[t][s] << t); } } for (int i=N-1, j=N-1; i>=0; i--) { while (j >= 0 && X[i] - X[j] < K) j--; Left[0][i] = j; LeftCnt[0][i] = i - j; LeftSum[0][i] = i - j; } REP (t, LOG-1) { REP (i, N) { int s = Left[t][i]; if (s == -1) { Left[t+1][i] = -1; LeftCnt[t+1][i] = LeftCnt[t][i]; LeftSum[t+1][i] = LeftSum[t][i]; } else { Left[t+1][i] = Left[t][s]; LeftCnt[t+1][i] = LeftCnt[t][s] + LeftCnt[t][i]; LeftSum[t+1][i] = LeftSum[t][s] + LeftSum[t][i] + (LeftCnt[t][i] << t); } } } int Q; scanf("%d", &Q); REP ($, Q) { int L, R; scanf("%d%d", &L, &R); L--; LL ans = 0; { int cur = L; LL mult = 0; LL sum = 0; for (int t=LOG; t--;) { int s = Right[t][cur]; if (s != N && s <= R) { sum += RightSum[t][cur] + RightCnt[t][cur] * mult; mult += 1LL<<t; cur = s; } } sum += (R - cur) * (mult + 1); ans += sum; } { int cur = R-1; LL cnt = 0; LL sum = 0; for (int t=LOG; t--;) { int s = Left[t][cur]; if (L <= s) { sum += LeftSum[t][cur] + cnt * (1LL<<t); cnt += LeftCnt[t][cur]; cur = s; } } ans -= sum; } printf("%lld\n", ans); } } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <stdio.h> #include <queue> using namespace std; typedef long long int ll; constexpr int kN = int(2E5 + 10), LOGN = 20, kInf = int(2E9 + 10); int x[kN], nxtl[kN][LOGN], nxtr[kN][LOGN]; ll pl[kN][LOGN], pr[kN][LOGN]; int main() { int n, k, q, l, r, nl, nr; ll ans; queue<int> qu; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &x[i]); x[0] = -kInf, x[++n] = kInf; for (int i = 0; i <= n; i++) { while (!qu.empty()) { if (x[i] - x[qu.front()] >= k) { nxtl[qu.front()][0] = i; qu.pop(); } else break; } qu.push(i); } while (!qu.empty()) { nxtl[qu.front()][0] = n; qu.pop(); } for (int i = n; i >= 0; i--) { while (!qu.empty()) { if (x[qu.front()] - x[i] >= k) { nxtr[qu.front()][0] = i; qu.pop(); } else break; } qu.push(i); } while (!qu.empty()) { nxtr[qu.front()][0] = 0; qu.pop(); } for (int i = 1; i < LOGN; i++) for (int j = 0; j <= n; j++) nxtl[j][i] = nxtl[nxtl[j][i - 1]][i - 1]; for (int i = 1; i < LOGN; i++) for (int j = 0; j <= n; j++) nxtr[j][i] = nxtr[nxtr[j][i - 1]][i - 1]; for (int i = 0; i <= n; i++) pl[i][0] = nxtl[i][0]; for (int i = 0; i <= n; i++) pr[i][0] = nxtr[i][0]; for (int i = 0; i <= n; i++) if (pl[i][0] == n) pl[i][0] = 0; for (int i = 1; i < LOGN; i++) for (int j = 0; j <= n; j++) pl[j][i] = pl[j][i - 1] + pl[nxtl[j][i - 1]][i - 1]; for (int i = 1; i < LOGN; i++) for (int j = 0; j <= n; j++) pr[j][i] = pr[j][i - 1] + pr[nxtr[j][i - 1]][i - 1]; scanf("%d", &q); for (int i = 1; i <= q; i++) { scanf("%d%d", &l, &r); nl = l, nr = r; ans = r - l + 1; for (int j = LOGN - 1; j >= 0; j--) { if (nxtl[l][j] <= r) { ans -= pl[l][j]; l = nxtl[l][j]; ans += 1 << j; } } l = nl, r = nr; for (int j = LOGN - 1; j >= 0; j--) { if (nxtr[r][j] >= l) { ans += pr[r][j]; r = nxtr[r][j]; } } printf("%lld\n", ans); } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef unsigned long long llu; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef pair<int, pii> piii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef pair<string, int> psi; typedef pair<char, int> pci; typedef pair<int, char> pic; const int MOD = 1e9 + 7; const long double PI = 3.141592653589793238462643383279502884197; ll fac[1] = {1}, inv[1] = {1}; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll mp(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;} ll cmb(ll r, ll c) {return (c>r||c<0) ? 0:fac[r] * inv[c] % MOD * inv[r - c] % MOD;} int vec[200002]; int lspa[200002][18]; ll lsum[200002][18]; int rspa[200002][18]; ll rsum[200002][18]; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < 18; i++) lspa[n+1][i] = n + 1; for (int i = 1; i <= n; i++) scanf("%d", &vec[i]); for (int i = 1; i <= n; i++) { int ne = lower_bound(vec + 1, vec + n + 1, vec[i] + k) - vec; lspa[i][0] = ne; lsum[i][0] = ne-1; ne = upper_bound(vec + 1, vec + n + 1, vec[i] - k) - vec - 1; rspa[i][0] = ne; rsum[i][0] = ne; // 얘는 더하고 l은 빠진다. } for (int j = 1; j < 18; j++) { for (int i = 0; i <= n + 1; i++) { lspa[i][j] = lspa[lspa[i][j-1]][j-1]; lsum[i][j] = lsum[i][j-1] + lsum[lspa[i][j-1]][j-1]; rspa[i][j] = rspa[rspa[i][j-1]][j-1]; rsum[i][j] = rsum[i][j-1] + rsum[rspa[i][j-1]][j-1]; } } int q; scanf("%d", &q); while (q--) { int le, ri; scanf("%d %d", &le, &ri); int now = le; ll ans = ri - le + 1; // printf("%lld\n", ans); for (int j = 17; j >= 0; j--) if (lspa[now][j] <= ri) { // printf("%d %d %d : pass\n", now, j, lspa[now][j]); ans -= lsum[now][j]; now = lspa[now][j]; } // printf("%lld\n", ans); now = ri; for (int j = 17; j >= 0; j--) if (rspa[now][j] >= le) { ans += rsum[now][j]; now = rspa[now][j]; } printf("%lld\n", ans); } } // author: rdd6584
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf("%d %d", &n, &k); vector<int> x(n); for (int i = 0; i < n; ++i) { scanf("%d", &x[i]); } vector<vector<int>> to_lef(32 - __builtin_clz(n)), to_rig(32 - __builtin_clz(n)); vector<vector<long long>> sum_lef(32 - __builtin_clz(n)), sum_rig(32 - __builtin_clz(n)); for (int i = 0, j = -1; i < n; ++i) { while (j + 1 < n && x[i] - x[j + 1] >= k) ++j; to_lef[0].push_back(j); sum_lef[0].push_back(i); } for (int i = 0, j = 0; i < n; ++i) { while (j < n && x[j] - x[i] < k) ++j; to_rig[0].push_back(j); sum_rig[0].push_back(i-1); } for (int i = 0; i + 1 < (int)to_lef.size(); ++i) { to_lef[i+1].resize(n); sum_lef[i+1].resize(n); for (int j = 0; j < n; ++j) { int now = to_lef[i][j]; long long sum = sum_lef[i][j]; if (now != -1) { sum += sum_lef[i][now]; now = to_lef[i][now]; } to_lef[i+1][j] = now; sum_lef[i+1][j] = sum; } } for (int i = 0; i + 1 < (int)to_rig.size(); ++i) { to_rig[i+1].resize(n); sum_rig[i+1].resize(n); for (int j = 0; j < n; ++j) { int now = to_rig[i][j]; long long sum = sum_rig[i][j]; if (now < n) { sum += sum_rig[i][now]; now = to_rig[i][now]; } to_rig[i+1][j] = now; sum_rig[i+1][j] = sum; } } int q; scanf("%d", &q); while (q--) { int l, r; scanf("%d %d", &l, &r); --l, --r; long long ans = 0; int now = r; for (int i = (int) to_lef.size()-1; i >= 0; --i) { if (to_lef[i][now] >= l) { ans += sum_lef[i][now]; now = to_lef[i][now]; } } ans += now; now = l; for (int i = (int) to_rig.size()-1; i >= 0; --i) { if (to_rig[i][now] <= r) { ans -= sum_rig[i][now]; now = to_rig[i][now]; } } ans -= now - 1; printf("%lld\n", ans); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#pragma GCC optimize("O3", "unroll-loops") #pragma GCC target("avx") #include <bits/stdc++.h> using namespace std; //using namespace atcoder; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin);i<(end);i++) #define REP(i, n) FOR(i,0,n) #define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--) #define IREP(i, n) IFOR(i,0,n) #define Sort(v) sort(v.begin(), v.end()) #define Reverse(v) reverse(v.begin(), v.end()) #define all(v) v.begin(),v.end() #define SZ(v) ((int)v.size()) #define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) #define bit(n) (1LL<<(n)) #define debug(x) cout << #x << "=" << x << endl; #define vdebug(v) { cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; } #define mdebug(m) { cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} } #define pb push_back #define fi first #define se second #define int long long #define INF 1000000000000000000 template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; } template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; } template<typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p){ cout << '(' << p.first << ',' << p.second << ')'; return os; } template<typename T> void Out(T x) { cout << x << endl; } template<typename T1, typename T2> void chOut(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); } using vec = vector<int>; using mat = vector<vec>; using Pii = pair<int, int>; using v_bool = vector<bool>; using v_Pii = vector<Pii>; //int dx[4] = {1,0,-1,0}; //int dy[4] = {0,1,0,-1}; //char d[4] = {'D','R','U','L'}; const int mod = 1000000007; //const int mod = 998244353; signed main(){ int N, K; cin >> N >> K; vec X(N); cin >> X; int Q; cin >> Q; vector<v_Pii> next(18, v_Pii(N, Pii(N, 0))), prev(18, v_Pii(N, Pii(-1, 0))); REP(i, N){ int j = Lower_bound(X, X[i] + K); if(j < N) next[0][i] = Pii(j, j); int k = Upper_bound(X, X[i] - K) - 1; if(k >= 0) prev[0][i] = Pii(k, k); } FOR(t, 1, 18){ REP(i, N) if(next[t - 1][i].fi < N){ int v = next[t - 1][next[t - 1][i].fi].fi; int cost = next[t - 1][i].se + next[t - 1][next[t - 1][i].fi].se; next[t][i] = Pii(v, cost); } REP(i, N) if(prev[t - 1][i].fi >= 0){ int v = prev[t - 1][prev[t - 1][i].fi].fi; int cost = prev[t - 1][i].se + prev[t - 1][prev[t - 1][i].fi].se; prev[t][i] = Pii(v, cost); } } //mdebug(next); mdebug(prev); vec ans(Q); REP(q, Q){ int L, R; cin >> L >> R; L--; R--; int nl = 1, cl = L; int now = L; while(next[0][now].fi <= R){ IREP(t, 18) if(next[t][now].fi <= R){ nl += bit(t); cl += next[t][now].se; now = next[t][now].fi; break; } } int nr = 1, cr = R; now = R; while(prev[0][now].fi >= L){ IREP(t, 18) if(prev[t][now].fi >= L){ nr += bit(t); cr += prev[t][now].se; now = prev[t][now].fi; break; } } ans[q] = cr - cl + nl; } REP(i, Q) cout << ans[i] << '\n'; return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; #ifdef LOCAL #define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__); #else #define dbg(...) 17; #endif template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr> ostream& operator << (ostream &os, const C &c) { bool f = true; os << "{"; for (const auto &x : c) { if (!f) os << ", "; f = false; os << x; } return os << "}"; } template<typename T> void debug(string s, T x) { cerr << s << " = " << x << "\n"; } template<typename T, typename... Args> void debug(string s, T x, Args... args) { cerr << s.substr(0, s.find(',')) << " = " << x << " | "; debug(s.substr(s.find(',') + 2), args...); } int main() { const int INF = 1e9; ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<int> x(n); set<pair<int, int>> pos; for (int i = 0; i < n; i++) { cin >> x[i]; pos.emplace(x[i], i); } int d = 0; while ((1 << d) < n) { d++; } vector<vector<int>> fgo(n, vector<int>(d + 1, INF)); vector<vector<ll>> fsum(n, vector<ll>(d + 1, -1)); vector<vector<int>> bgo(n, vector<int>(d + 1, -INF)); vector<vector<ll>> bsum(n, vector<ll>(d + 1, -1)); for (int i = 0; i < n; i++) { auto it = pos.lower_bound({x[i] + k, -INF}); if (it != pos.end()) { fgo[i][0] = (*it).second; fsum[i][0] = (*it).second; } it = pos.lower_bound({x[i] - k, INF}); if (it != pos.begin()) { it = prev(it); bgo[i][0] = (*it).second; bsum[i][0] = (*it).second; } } for (int i = 0; i <= d; i++) { for(int j = 0; j < n; j++) { if (!(fgo[j][i] == INF || fgo[fgo[j][i]][i] == INF)) { fgo[j][i + 1] = fgo[fgo[j][i]][i]; fsum[j][i + 1] = fsum[j][i] + fsum[fgo[j][i]][i]; } if (!(bgo[j][i] == -INF || bgo[bgo[j][i]][i] == -INF)) { bgo[j][i + 1] = bgo[bgo[j][i]][i]; bsum[j][i + 1] = bsum[j][i] + bsum[bgo[j][i]][i]; } } } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; l--, r--; int fcur = l; int bcur = r; ll ftot = l; ll btot = r; int tot = 1; for (int i = d; i >= 0; i--) { if (fgo[fcur][i] <= r) { ftot += fsum[fcur][i]; tot += (1 << i); fcur = fgo[fcur][i]; } if (bgo[bcur][i] >= l) { btot += bsum[bcur][i]; bcur = bgo[bcur][i]; } } cout << btot - ftot + tot << '\n'; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> using namespace std; #define FOR(i, x, y) for(int i = (x); i < (y); ++i) #define REP(i, x, y) for(int i = (x); i <= (y); ++i) #define PB push_back #define MP make_pair #define PH push #define fst first #define snd second typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int, int> pii; const int maxn = 2e5 + 5; const int logn = 20; int n, k, q; int a[maxn]; int r[maxn][logn], l[maxn][logn]; ll lsum[maxn][logn], rsum[maxn][logn]; int main(){ scanf("%d%d", &n, &k); FOR(i, 0, n) scanf("%d", a + i); for(int i = 0, j = 0; i < n; ++i){ for(; j < n && a[j] < a[i] + k; ++j); r[i][0] = j; rsum[i][0] = j; } for(int i = n - 1, j = n - 1; i >= 0; --i){ for(; j >= 0 && a[j] > a[i] - k; --j); l[i][0] = j; lsum[i][0] = j; } FOR(j, 1, logn) FOR(i, 0, n){ l[i][j] = l[i][j - 1] == -1 ? -1 : l[l[i][j - 1]][j - 1]; r[i][j] = r[i][j - 1] == n ? n : r[r[i][j - 1]][j - 1]; lsum[i][j] = l[i][j - 1] == -1 ? lsum[i][j - 1] : lsum[i][j - 1] + lsum[l[i][j - 1]][j - 1]; rsum[i][j] = r[i][j - 1] == n ? rsum[i][j - 1] : rsum[i][j - 1] + rsum[r[i][j - 1]][j - 1]; } scanf("%d", &q); FOR(i, 0, q){ int x, y, len = 0; ll sumx = 0, sumy = 0; scanf("%d%d", &x, &y); --x; --y; int a = x, b = y; for(int i = logn - 1; i >= 0; --i){ if(r[a][i] <= y){ sumx += rsum[a][i]; a = r[a][i]; len += 1 << i; } if(l[b][i] >= x){ sumy += lsum[b][i]; b = l[b][i]; } } sumy += y; sumx += x; ++len; printf("%lld\n", sumy - sumx + len); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") // hloya template v26 // ░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░░░░░ // ░░░░░░█░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░█░░░░░ // ░░░░░░█░█░▀░░░░░▀░░▀░░░░█░█░░░░░ // ░░░░░░█░█░░░░░░░░▄▀▀▄░▀░█░█▄▀▀▄░ // █▀▀█▄░█░█░░▀░░░░░█░░░▀▄▄█▄▀░░░█░ // ▀▄▄░▀██░█▄░▀░░░▄▄▀░░░░░░░░░░░░▀▄ // ░░▀█▄▄█░█░░░░▄░░█░░░▄█░░░▄░▄█░░█ // ░░░░░▀█░▀▄▀░░░░░█░██░▄░░▄░░▄░███ // ░░░░░▄█▄░░▀▀▀▀▀▀▀▀▄░░▀▀▀▀▀▀▀░▄▀░ // ░░░░█░░▄█▀█▀▀█▀▀▀▀▀▀█▀▀█▀█▀▀█░░░ // ░░░░▀▀▀▀░░▀▀▀░░░░░░░░▀▀▀░░▀▀░░░░ #include <bits/stdc++.h> using namespace std; bool dbg = 0; clock_t start_time = clock(); #define current_time fixed<<setprecision(6)<<(ld)(clock()-start_time)/CLOCKS_PER_SEC #define f first #define s second #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back #define all(v) (v).begin(), (v).end() #define sz(v) ((int)(v).size()) #define sqr(x) ((x) * (x)) #define ull unsigned long long #define ll long long #define ld long double #define pii pair<int,int> #define umap unordered_map<int, int> #define files1 freopen("input.txt","r",stdin) #define files2 freopen("output.txt","w",stdout) #define files files1;files2 #define fast_io ios_base::sync_with_stdio(0);cin.tie(0) // #define endl '\n' #define ln(i,n) " \n"[(i) == (n) - 1] void bad(string mes = "NO"){cout << mes;exit(0);} void bad(int mes){cout << mes;exit(0);} template<typename T> string bin(T x, int st = 2){ string ans = ""; while (x > 0){ ans += char('0' + x % st); x /= st; } reverse(ans.begin(), ans.end()); return ans.empty() ? "0" : ans; } mt19937_64 mt_rand( chrono::system_clock::now().time_since_epoch().count() ); template<typename T1, typename T2> inline bool upmax(T1& a, T2 b) { return (a < b ? (a = b, true) : false); } template<typename T1, typename T2> inline bool upmin(T1& a, T2 b) { return (b < a ? (a = b, true) : false); } // inline int popcount(int x){ // int count = 0; // __asm__ volatile("POPCNT %1, %0;":"=r"(count):"r"(x):); // return count; // } template<typename T> T input(){ T ans = 0, m = 1; char c = ' '; while (!((c >= '0' && c <= '9') || c == '-')) { c = getchar(); } if (c == '-') m = -1, c = getchar(); while (c >= '0' && c <= '9'){ ans = ans * 10 + (c - '0'), c = getchar(); } return ans * m; } template<typename T> T gcd (T a, T b) { while (b) { a %= b; swap (a, b); } return a; } template<typename T> void read(T& a) { a = input<T>(); } template<typename T> void read(T& a, T& b) { read(a), read(b); } template<typename T> void read(T& a, T& b, T& c) { read(a, b), read(c); } template<typename T> void read(T& a, T& b, T& c, T& d) { read(a, b), read(c, d); } const int inf = 1e9 + 20; const short short_inf = 3e4 + 20; const long double eps = 1e-12; const int maxn = (int)3e5 + 3, base = 998244353; const ll llinf = 2e18 + 5; const int mod = 200003; int binpow (int a, int n) { int res = 1; while (n) { if (n & 1) res = 1ll * res * a % mod; a = 1ll * a * a % mod; n >>= 1; } return res; } // ll solve(ll n) { // for (ll p = 1; p <= (ll)8e7; p++) { // __int128 a = (__int128)2 * p * n; // ll x = (sqrt(4 * a + 1) - 1) / 2; // if ((__int128)x * (x + 1) / 2 % n == 0) { // return x; // } // } // throw; // } int nxt[maxn][20]; int prv[maxn][20]; ll pn[maxn]; ll pp[maxn]; int getprev(int i, int j) { if (i != -1) { return prv[i][j]; } return -1; } int getnext(int i, int j) { if (i != -1) { return nxt[i][j]; } return -1; } int main() { // files1; fast_io; int n, k; cin >> n >> k; vector<int> x(n); for (int i = 0; i < n; i++) { cin >> x[i]; } for (int i = 0; i < n; i++) { int real_prev = upper_bound(all(x), x[i] - k) - x.begin(); real_prev--; prv[i][0] = real_prev; for (int j = 1; j < 20; j++) { prv[i][j] = getprev(prv[i][j - 1], j - 1); } pp[i] = i + 1; if (prv[i][0] != -1) { pp[i] += pp[prv[i][0]]; } } for (int i = n - 1; i >= 0; i--) { int real_next = lower_bound(all(x), x[i] + k) - x.begin(); if (real_next == x.size()) { real_next = -1; } nxt[i][0] = real_next; for (int j = 1; j < 20; j++) { nxt[i][j] = getnext(nxt[i][j - 1], j - 1); } pn[i] = -i; if (nxt[i][0] != -1) { pn[i] += pn[nxt[i][0]]; } } int q; cin >> q; auto evalleft = [&](int l, int r) { int oldl = l; for (int j = 19; j >= 0; j--) { if (nxt[l][j] != -1 && nxt[l][j] <= r) { l = nxt[l][j]; } } // sum from l to oldl ll sum = pn[oldl]; l = nxt[l][0]; if (l != -1) { sum -= pn[l]; } return sum; }; auto evalright = [&](int l, int r) { int oldr = r; for (int j = 19; j >= 0; j--) { if (prv[r][j] != -1 && prv[r][j] >= l) { r = prv[r][j]; } } ll sum = pp[oldr]; r = prv[r][0]; if (r != -1) { sum -= pp[r]; } return sum; }; for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--, r--; ll sum1 = evalleft(l, r); ll sum2 = evalright(l, r); cout << sum1 + sum2 << "\n"; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> using namespace std; const int N=200005; int n,k,fa[20][N],q,i,l[N],r[N],x[N],j; long long ans[N],faf[20][N]; int main() { scanf("%d %d",&n,&k); for(i=1;i<=n;++i) scanf("%d",&x[i]); scanf("%d",&q); for(i=1;i<=q;++i) scanf("%d %d",&l[i],&r[i]); x[0]=-k-1; x[n+1]=x[n]+k+1; for(i=n;i>=1;--i) { fa[0][i]=upper_bound(x+1,x+1+n,x[i]-k)-x-1; faf[0][i]=i; } for(i=1;i<20;++i) for(j=1;j<=n;++j) { fa[i][j]=fa[i-1][fa[i-1][j]]; faf[i][j]=faf[i-1][j]+faf[i-1][fa[i-1][j]]; } for(i=1;i<=q;++i) { int p=r[i]; for(j=19;j>=0;--j) if(fa[j][p]>=l[i]) { ans[i]+=faf[j][p]; p=fa[j][p]; } ans[i]+=faf[0][p]; } for(i=1;i<=n;++i) { fa[0][i]=lower_bound(x+1,x+2+n,x[i]+k)-x; faf[0][i]=i-1; } fa[0][n+1]=n+1; for(i=1;i<20;++i) for(j=1;j<=n+1;++j) { fa[i][j]=fa[i-1][fa[i-1][j]]; faf[i][j]=faf[i-1][j]+faf[i-1][fa[i-1][j]]; } for(i=1;i<=q;++i) { int p=l[i]; for(j=19;j>=0;--j) if(fa[j][p]<=r[i]) { ans[i]-=faf[j][p]; p=fa[j][p]; } ans[i]-=faf[0][p]; } for(i=1;i<=q;++i) printf("%lld\n",ans[i]); }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<iostream> using namespace std; int N,K; int X[2<<17]; int dL[20][2<<17],dLs[20][2<<17]; int dR[20][2<<17],dRs[20][2<<17]; int Q; int main() { cin>>N>>K; for(int i=0;i<N;i++)cin>>X[i]; int l=0; for(int i=0;i<N;i++) { while(l<N&&X[l]<X[i]+K)l++; dL[0][i]=l; dLs[0][i]=i; } int r=N-1; for(int i=N;i--;) { while(r>=0&&X[r]>X[i]-K)r--; dR[0][i]=r; dRs[0][i]=i; } for(int k=0;k<19;k++) { for(int i=0;i<N;i++) { if(dL[k][i]==N) { dL[k+1][i]=N; dLs[k+1][i]=dLs[k][i]; } else { dL[k+1][i]=dL[k][dL[k][i]]; dLs[k+1][i]=dLs[k][i]+dLs[k][dL[k][i]]; } if(dR[k][i]==-1) { dR[k+1][i]=-1; dRs[k+1][i]=dRs[k][i]; } else { dR[k+1][i]=dR[k][dR[k][i]]; dRs[k+1][i]=dRs[k][i]+dRs[k][dR[k][i]]; } } } cin>>Q; for(;Q--;) { int L,R;cin>>L>>R; L--; int cnt=1,now=L; int ans=0; for(int k=20;k--;) { if(dL[k][now]<R) { cnt+=1<<k; ans-=dLs[k][now]; now=dL[k][now]; } } ans-=now; now=R-1; for(int k=20;k--;) { if(dR[k][now]>=L) { ans+=dRs[k][now]; now=dR[k][now]; } } ans+=now; cout<<ans+cnt<<endl; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> #define fi first #define se second const int N = 200200; const int mod = 1e9 + 7; using namespace std; int n, k; int a[N]; int t1[N][20]; int t2[N][20]; long long s1[N][20]; long long s2[N][20]; int main() { ios_base::sync_with_stdio(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); cin >> n >> k; for(int i = 1; i <= n; i++){ cin >> a[i]; } for(int i = 1, j = 0, h = 1; i <= n; i++){ while(a[i] - a[j + 1] >= k){ j += 1; } while(h <= n && a[h] - a[i] < k){ h += 1; } t1[i][0] = s1[i][0] = j; t2[i][0] = s2[i][0] = h; } t2[n + 1][0] = n + 1; for(int i = 1; i < 20; i++){ for(int j = 0; j <= n + 1; j++){ t1[j][i] = t1[t1[j][i - 1]][i - 1]; t2[j][i] = t2[t2[j][i - 1]][i - 1]; s1[j][i] = s1[j][i - 1] + s1[t1[j][i - 1]][i - 1]; s2[j][i] = s2[j][i - 1] + s2[t2[j][i - 1]][i - 1]; } } int q; cin >> q; while(q--){ int l, r; cin >> l >> r; int x = l, y = r; long long res = r - l + 1; for(int i = 19; i >= 0; i--){ if(t2[x][i] <= r){ assert(t1[y][i] >= l); res -= s2[x][i]; res += s1[y][i]; res += (1 << i); x = t2[x][i]; y = t1[y][i]; } } cout << res << "\n"; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
// warm heart, wagging tail,and a smile just for you! // ███████████ // ███╬╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬████╬╬╬╬╬╬███ // ███████████ ██╬╬╬╬╬████╬╬████╬╬╬╬╬██ // █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██ // ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██ // ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██ // ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████ // █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██ // ██████████████ ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████ // ███████ █████ ███████████████████ // #include "bits/stdc++.h" using namespace std; #define INF (1<<30) #define LINF (1LL<<60) #define fs first #define sc second #define int long long #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FOR2(i,a,b) for(int i=(a);i<=(b);++i) #define RFOR(i,a,b) for(int i = (b-1);i>=(a);--i) #define RFOR2(i,a,b) for(int i = (b);i>=(a);--i) #define REP(i,n) FOR(i,0,(n)) #define REP2(i,n) FOR2(i,0,(n)) #define RREP(i,n) RFOR(i,0,(n)) #define RREP2(i,n) RFOR2(i,0,(n)) #define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr) #define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr) #define range(i,a,b) ((a)<=(i) && (i)<(b)) #define range2(i,a,b) ((a)<=(i) && (i)<=(b)) #define debug(x) cout << #x << " = " << (x) << endl #define SP << " " << template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){if(a>b) {a=b; return true;} else return false;} template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){if(a<b) {a=b; return true;} else return false;} #define MSB(x) (63-__builtin_clzll(x)) #define pcnt(x) (__builtin_popcountll(x)) #define parity(i,j) (i&(1LL<<j)) typedef pair<int,int> P; typedef tuple<int,int,int> T; typedef vector<int> vec; typedef vector<vector<int>> mat; void solve(){ int N,K; cin >> N >> K; vector<int> a(N); REP(i,N) cin >> a[i]; mat L(30,vec(N)), R = L, Lsum = L, Rsum = L; REP(i,N){ L[0][i] = lower_bound(a.begin(),a.end(),a[i]+K) - a.begin(); R[0][i] = upper_bound(a.begin(),a.end(),a[i]-K) - a.begin() - 1; Lsum[0][i] = Rsum[0][i] = i; } REP(i,29){ REP(j,N){ if(L[i][j]==N) L[i+1][j] = N, Lsum[i+1][j] = Lsum[i][j]; else L[i+1][j] = L[i][L[i][j]], Lsum[i+1][j] = Lsum[i][j]+Lsum[i][L[i][j]]; if(R[i][j]==-1) R[i+1][j] = -1, Rsum[i+1][j] = Rsum[i][j]; else R[i+1][j] = R[i][R[i][j]], Rsum[i+1][j] = Rsum[i][j]+Rsum[i][R[i][j]]; } } int Q; cin >> Q; REP(_,Q){ int l,r; cin >> l >> r; l--; r--; int now = l, mx = 1, sum = 0; RREP(i,30){ if(L[i][now] <= r) mx += (1<<i), sum += Lsum[i][now], now = L[i][now]; } sum += now; now = r; RREP(i,30){ if(R[i][now] >= l) sum -= Rsum[i][now], now = R[i][now]; } sum -= now; cout << abs(sum) + mx << endl; } } signed main(){ ios::sync_with_stdio(false); cin.tie(0); int T = 1; // cin >> T; while(T--) solve(); return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> ii; typedef pair<ll,ll> pll; typedef vector<int> vi; #define F first #define S second #define RE register #define random(x) (rand()%x) #define LOG(a,b) (log(a)/log(b)) #define N 200005 #define mod 1000000007 #define INF 0x3f3f3f3f #define LINF 0x3f3f3f3f3f3f3f3fll int n,lgn,q,k,x[N],lp[22][N],rp[22][N]; ll lsz[22][N],rsz[22][N]; int main(){ scanf("%d%d",&n,&k); lgn=log2(n); for(int i=1;i<=n;++i){ scanf("%d",x+i); } for(int i=0;i<=lgn;++i) rp[i][n+1]=n+1; for(int i=1;i<=n;++i){ lp[0][i]=lower_bound(x+1,x+n+1,x[i]-k+1)-x-1; lsz[0][i]=lp[0][i]; //printf("(%d:%d,%lld) ",0,lp[0][i],lsz[0][i]); for(int j=1;j<=lgn;++j){ lp[j][i]=lp[j-1][lp[j-1][i]]; lsz[j][i]=lsz[j-1][i]+lsz[j-1][lp[j-1][i]]; //printf("(%d:%d,%lld) ",j,lp[j][i],lsz[j][i]); } //puts(""); } for(int i=n;i;--i){ rp[0][i]=lower_bound(x+1,x+n+1,x[i]+k)-x; rsz[0][i]=rp[0][i]; for(int j=1;j<=lgn;++j){ rp[j][i]=rp[j-1][rp[j-1][i]]; rsz[j][i]=rsz[j-1][i]+rsz[j-1][rp[j-1][i]]; //printf("(%d:%d,%lld) ",j,rp[j][i],rsz[j][i]); } //puts(""); } scanf("%d",&q); int ql,qr,p,cntl=1,cntr=1; ll ansl=0,ansr=0; for(int i=1;i<=q;++i){ scanf("%d%d",&ql,&qr); p=ql;ansl=ql;cntl=1; for(int j=lgn;j>=0;--j){ if(rp[j][p]<=qr){ cntl+=(1<<j); ansl+=rsz[j][p]; p=rp[j][p]; } //printf("%d:%d %d\n",j,p,ansl); } p=qr;ansr=qr;cntr=1; for(int j=lgn;j>=0;--j){ if(lp[j][p]>=ql){ cntr+=(1<<j); ansr+=lsz[j][p]; p=lp[j][p]; } //printf("%d:%d %d\n",j,p,ansr); } //printf("%d %d\n",cntl,cntr); assert(cntl==cntr); printf("%lld\n",ansr-ansl+cntl); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; int n,k,l,r,nxt[20][200005],pre[20][200005],q,x[200005],gl,gr; ll sump[20][200005],sumn[20][200005],ans0,ans1; int main() { scanf("%d%d",&n,&k); nxt[0][n+1]=n+1; pre[0][n+1]=n; pre[0][0]=0; nxt[0][0]=1; for(int i=1;i<=n;i++)scanf("%d",&x[i]); x[0]=-1e9;x[n+1]=2e9; for(int i=1;i<=n;i++) { pre[0][i]=upper_bound(x+1,x+n+1,x[i]-k)-x-1; nxt[0][i]=lower_bound(x+1,x+n+1,x[i]+k)-x; sump[0][i]=pre[0][i]; sumn[0][i]=nxt[0][i]; } for(int i=1;i<20;i++) { for(int j=0;j<=n+1;j++) { pre[i][j]=pre[i-1][pre[i-1][j]]; sump[i][j]=sump[i-1][j]+sump[i-1][pre[i-1][j]]; nxt[i][j]=nxt[i-1][nxt[i-1][j]]; sumn[i][j]=sumn[i-1][j]+sumn[i-1][nxt[i-1][j]]; //printf("%d %d %d\n",i,j,nxt[i][j]); } } scanf("%d",&q); for(int i=1;i<=q;i++) { scanf("%d%d",&l,&r); gl=l;gr=r; ans0=0;ans1=r-l; for(int i=19;i>=0;i--) { //printf("%d %d %d\n",i,nxt[i][gl],r); if(nxt[i][gl]<=r) { ans0+=(1<<i); ans1-=sumn[i][gl]; ans1+=sump[i][gr]; //printf("%d %d %d %d %d\n",i,gl,gr,sumn[i][gl],sumn[i][gr]); gl=nxt[i][gl]; gr=pre[i][gr]; } } ans1+=ans0+1; printf("%lld\n",ans1); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> #define all(x) x.begin(), x.end() using namespace std; using ll = long long; const int maxn = 1<<20; int n, k, q; int x[maxn], l[maxn], r[maxn], w[maxn]; int res[maxn]; struct dsu { vector<int> p; vector<int> r, rtop; dsu(int n) : p(n), r(n, 1) { iota(all(p), 0); rtop = p; } int par(int v) { return p[v] == v ? v : p[v] = par(p[v]); } void unite(int u, int v) { u = par(u), v = par(v); if(u == v) return; int rr = rtop[u]; if(r[u] < r[v]) swap(u, v); p[v] = u; r[u] += r[v]; rtop[u] = rr; } }; ll d[maxn], s[maxn]; int par[maxn]; vector<int> g[maxn], p[maxn]; void dfs(int v) { d[v] += w[v]; s[v] += 1; for(auto &i : g[v]) { d[i] = d[v]; s[i] = s[v]; par[i] = v; dfs(i); } } vector<ll> solve() { memset(d, 0, sizeof d); memset(s, 0, sizeof s); for(int i = 0; i < maxn; i++) g[i].clear(), p[i].clear(); vector<ll> ans(q); memset(par, -1, sizeof par); for(int i = 0; i < n; i++) { auto it = lower_bound(x, x+n, x[i]+k)-x; if(it < n) g[it].push_back(i), par[i] = it; } for(int i = 0; i < n; i++) if(par[i] == -1) dfs(i); for(int i = 0; i < q; i++) { p[r[i]].push_back(i); } dsu f(n); for(int i = 0; i < n; i++) { for(auto j : g[i]) f.unite(i, j); for(auto &qid : p[i]) { int x = l[qid]; res[qid] = s[x]; ans[qid] = d[x]; int y = par[f.rtop[f.par(x)]]; if(y != -1) { res[qid] -= s[y]; ans[qid] -= d[y]; } } } return ans; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> k; for(int i = 0; i < n; i++) cin >> x[i], w[i] = i; cin >> q; for(int i = 0; i < q; i++) cin >> l[i] >> r[i], l[i]--, r[i]--; auto a = solve(); for(int i = 0; i < n; i++) { x[i] *= -1; l[i] = n-1-l[i]; r[i] = n-1-r[i]; swap(l[i], r[i]); } reverse(x, x+n); reverse(w, w+n); auto b = solve(); for(int i = 0; i < q; i++) cout << b[i] - a[i] + res[i] << '\n'; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; #define ll long long #define mp make_pair #define pb push_back #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define rep(i, n) for (int i = 0; i < (int)(n); ++ i) const int mxn = 2e5 + 5; int n, K, q; int x[mxn]; int l[mxn], r[mxn]; int idl[mxn], idr[mxn]; int mx[mxn]; ll sum[mxn]; int jmp[mxn][20]; ll s[mxn][20]; bool cmpl(int a, int b) { return l[a] > l[b]; } bool cmpr(int a, int b) { return r[a] < r[b]; } int main() { scanf("%d %d", &n, &K); rep(i, n) scanf("%d", &x[i]); scanf("%d", &q); rep(i, q) { scanf("%d %d", &l[i], &r[i]); -- l[i], -- r[i]; idl[i] = idr[i] = i; } sort(idl, idl + q, cmpl); sort(idr, idr + q, cmpr); int i = 0; rep(R, n) { jmp[R][0] = upper_bound(x, x + n, x[R] - K) - x - 1; s[R][0] = R + 1; for (int j = 1; j < 20; ++ j) { jmp[R][j] = jmp[R][j - 1] == -1 ? -1 : jmp[jmp[R][j - 1]][j - 1]; s[R][j] = s[R][j - 1] + (jmp[R][j - 1] == -1 ? 0 : s[jmp[R][j - 1]][j - 1]); } while (i < q && r[idr[i]] == R) { int L = l[idr[i]]; int cnt = 1, cur = R; for (int j = 20 - 1; ~j; -- j) { if (jmp[cur][j] >= L) { cnt += 1 << j; cur = jmp[cur][j]; } } mx[idr[i]] = cnt; ++ i; } } i = 0; rep(R, n) { while (i < q && r[idr[i]] == R) { int L = l[idr[i]]; ll &S = sum[idr[i]]; int cur = R; for (int j = 20 - 1; ~j; -- j) { if (jmp[cur][j] >= L) { S += s[cur][j]; cur = jmp[cur][j]; } } S += cur + 1; S -= 1LL * mx[idr[i]] * L; ++ i; } } i = 0; for (int L = n - 1; ~L; -- L) { jmp[L][0] = lower_bound(x, x + n, x[L] + K) - x; s[L][0] = L - 1; for (int j = 1; j < 20; ++ j) { jmp[L][j] = jmp[L][j - 1] == n ? n : jmp[jmp[L][j - 1]][j - 1]; s[L][j] = s[L][j - 1] + (jmp[L][j - 1] == n ? 0 : s[jmp[L][j - 1]][j - 1]); } while (i < q && l[idl[i]] == L) { int R = r[idl[i]]; ll &S = sum[idl[i]]; int cur = L; for (int j = 20 - 1; ~j; -- j) { if (jmp[cur][j] <= R) { S -= s[cur][j]; cur = jmp[cur][j]; } } S -= cur - 1; S += 1LL * mx[idl[i]] * R; ++ i; } } rep(i, q) { ll ans = sum[i] - (r[i] - l[i] + 1) - 1LL * (mx[i] - 1) * (r[i] - l[i] + 1); printf("%lld\n", ans); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; using lint = long long; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template <typename V, typename T> void ndfill(V &x, const T &val) { x = val; } template <typename V, typename T> void ndfill(vector<V> &vec, const T &val) { for (auto &v : vec) ndfill(v, val); } template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; } #endif template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl #else #define dbg(x) #endif int main() { int N, K; cin >> N >> K; vector<int> X(N); cin >> X; vector<int> prv(N), nxt(N); REP(i, N) nxt[i] = lower_bound(ALL(X), X[i] + K) - X.begin(); REP(i, N) prv[i] = upper_bound(ALL(X), X[i] - K) - X.begin() - 1; REP(i, N) if (prv[i] < 0) prv[i] = N; constexpr int D = 20; vector<vector<int>> dnxt(D, vector<int>(N + 1, N)); vector<vector<lint>> sumnxt(D, vector<lint>(N + 1)); vector<vector<int>> dprv(D, vector<int>(N + 1, N)); vector<vector<lint>> sumprv(D, vector<lint>(N + 1)); dnxt[0] = nxt; dprv[0] = prv; REP(i, N + 1) { sumnxt[0][i] = N - i; // dnxt[0][i]; sumprv[0][i] = N - i; // dprv[0][i]; } REP(d, D - 1) { REP(i, N) { dnxt[d + 1][i] = dnxt[d][dnxt[d][i]], sumnxt[d + 1][i] = sumnxt[d][i] + sumnxt[d][dnxt[d][i]]; dprv[d + 1][i] = dprv[d][dprv[d][i]], sumprv[d + 1][i] = sumprv[d][i] + sumprv[d][dprv[d][i]]; } } dbg(dnxt[0]); dbg(dnxt[1]); dbg(dnxt[2]); dbg(sumnxt[0]); dbg(sumnxt[1]); dbg(sumnxt[2]); dbg(sumprv[0]); dbg(sumprv[1]); dbg(sumprv[2]); int Q; cin >> Q; while (Q--) { int l, r; cin >> l >> r; l--, r--; int now = l, nowinv = r; int step = 0; IREP(d, D) { if (dnxt[d][now] <= r) { step += 1 << d; now = dnxt[d][now]; } } step++; now = l, nowinv = r; lint ret1 = 0, ret2 = 0; IREP(d, D) if ((step >> d) & 1) { ret1 += sumnxt[d][now]; ret2 += sumprv[d][nowinv]; now = dnxt[d][now]; nowinv = dprv[d][nowinv]; } cout << ret1 - ret2 + step << '\n'; dbg(step); dbg(ret1); dbg(ret2); } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; int a[200010]; int f1[200010][18], f2[200010][18]; long long s1[200010][18], s2[200010][18]; int main () { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } int pos = 1; for (int i = 1; i <= n; i++) { while (pos <= n && a[pos] - a[i] < k) pos++; f1[i][0] = s1[i][0] = pos; } f1[n + 1][0] = s1[n + 1][0] = n + 1; pos = n; for (int i = n; i >= 1; i--) { while (pos >= 1 && a[i] - a[pos] < k) pos--; f2[i][0] = s2[i][0] = pos; } f2[0][0] = s2[0][0] = 0; for (int j = 1; j < 18; j++) { for (int i = 0; i <= n + 1; i++) { f1[i][j] = f1[f1[i][j - 1]][j - 1]; s1[i][j] = s1[i][j - 1] + s1[f1[i][j - 1]][j - 1]; f2[i][j] = f2[f2[i][j - 1]][j - 1]; s2[i][j] = s2[i][j - 1] + s2[f2[i][j - 1]][j - 1]; } } int q; scanf("%d", &q); while (q--) { int l, r; scanf("%d%d", &l, &r); int now = l; long long ss1 = l; int c1 = 1; for (int i = 17; i >= 0; i--) { if (f1[now][i] <= r) ss1 += s1[now][i], now = f1[now][i], c1 += 1 << i; } now = r; long long ss2 = r; int c2 = 1; for (int i = 17; i >= 0; i--) { if (f2[now][i] >= l) ss2 += s2[now][i], now = f2[now][i], c2 += 1 << i; } assert(c1 == c2); printf("%lld\n", ss2 - ss1 + c1); } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> #include <unistd.h> #include <sys/time.h> #include <stdlib.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<P, ll> T; typedef pair<long double , ll> Ps; typedef pair<ll, bool> Pb; typedef pair<ll, vector<ll>> Pd; const ll INF = 3e18; const ll fact_table = 3200008; long double Pi = 3.1415926535897932384626; priority_queue <ll> pql; priority_queue <P> pqp; priority_queue <P> bag; //big priority queue priority_queue <ll, vector<ll>, greater<ll> > pqls; priority_queue <P, vector<P>, greater<P> > pqps; //small priority queue //top pop ll dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; ll dy[8] = {0, 1, 0, -1, 1, -1, -1, 1}; //↓,→,↑,← #define endl "\n" #ifdef ENJAPMA #undef endl #endif #define p(x) cout<<x<<endl; #define el cout<<endl; #define pe(x) cout<<x<<" "; #define ps(x) cout<<fixed<<setprecision(25)<<x<<endl; #define pu(x) cout<<(x); #define pb push_back #define lb lower_bound #define ub upper_bound #define CLEAR(a) a = decltype(a)(); #define pc(x) cout << x << ","; #define rep(i, n) for (ll i = 0; i < (n); i ++) typedef vector<ll> vec; typedef vector<vector<ll>> mat; const ll mod = 998244353ll; // const ll mod = 1000000007ll; ll mypow(ll a, ll b, ll m = mod) {ll x = 1; while (b) {while (!(b & 1)) {(a *= a) %= m; b >>= 1;}(x *= a) %= m; b--;} return x;} vec readvec(ll read) { vec res(read); for (int i = 0; i < read; i++) { cin >> res[i]; } return res;} void YES(bool cond) { if (cond) { p("YES");} else { p("NO");} return;} void Yes(bool cond) { if (cond) { p("Yes");} else { p("No");} return;} void line() { p("--------------------"); return;} /* ll fact[fact_table + 5], rfact[fact_table + 5]; void c3_init() { fact[0] = rfact[0] = 1; for (ll i = 1; i <= fact_table; i++) { fact[i] = (fact[i - 1] * i) % mod; } rfact[fact_table] = mypow(fact[fact_table], mod - 2, mod); for (ll i = fact_table; i >= 1; i--) { rfact[i - 1] = rfact[i] * i; rfact[i - 1] %= mod; } return; } ll c3(ll n, ll r) { return (((fact[n] * rfact[r]) % mod ) * rfact[n - r]) % mod; } */ struct Timer { int64_t start; const int64_t CYCLES_PER_SEC = 2800000000; Timer() { reset(); } void reset() { start = getCycle(); } inline double get() { return (double)(getCycle() - start) / CYCLES_PER_SEC; } inline int64_t getCycle() { uint32_t low, high; __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high)); return ((int64_t)low) | ((int64_t)high << 32); } }; bool multicase = false; ll n, k; ll x[200005], toright[200005][65], toleft[200005][65]; ll torightvalue[200005][65], toleftvalue[200005][65]; void precalc() { set<P> st; for (int i = 1; i <= n; i++) { st.insert(P(x[i], i)); } // 番兵 st.insert(P(INF, INF)); st.insert(P(-INF, -INF)); for (int i = 1; i <= n; i++) { ll right = (*st.lower_bound(P(x[i] + k, -INF))).second; toright[i][0] = right; torightvalue[i][0] = (right != INF) ? i : i; auto ite = st.lower_bound(P(x[i] - k + 1, -INF)); ite --; ll left = (*ite).second; toleft[i][0] = left; toleftvalue[i][0] = (left != -INF) ? i + 1 : i + 1; } for (int i = 0; i < 60; i++) { for (int j = 1; j <= n; j++) { if (toright[j][i] == INF) { toright[j][i + 1] = INF; } else { toright[j][i + 1] = toright[toright[j][i]][i]; torightvalue[j][i + 1] = torightvalue[j][i] + torightvalue[toright[j][i]][i]; } if (toleft[j][i] == -INF) { toleft[j][i + 1] = -INF; } else { toleft[j][i + 1] = toleft[toleft[j][i]][i]; toleftvalue[j][i + 1] = toleftvalue[j][i] + toleftvalue[toleft[j][i]][i]; } } } /* for (int j = 1; j <= 20; j++) { p(j); for (int i = 1; i <= n; i++) { pe(toleft[i][j]); p(toright[i][j]); } } for (int i = 1; i <= n; i++) { for (int j = 0; j <= 20; j++) { pe(toleft[i][j]); p(toright[i][j]); } } */ } void solve() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> x[i]; } precalc(); ll q; cin >> q; for (int i = 0; i < q; i++) { ll l, r; cin >> l >> r; ll il = l, ir = r; ll haba = 59; ll ans = 0; ll sum1 = 0, sum2 = 0; while (true) { // haba進んでも大丈夫なら進む if (toleft[r][haba] >= il && toright[l][haba] <= ir) { ans += toleftvalue[r][haba] - torightvalue[l][haba]; sum1 += toleftvalue[r][haba]; sum2 += torightvalue[l][haba]; l = toright[l][haba]; r = toleft[r][haba]; } else { if (haba == 0) { break; } else { haba --; } } } /* p(ans); pe(l);p(r); pe(toleftvalue[r][0]); p(torightvalue[l][0]); */ ans += toleftvalue[r][0] - torightvalue[l][0]; p(ans); } return; } int main() { // init(); ios::sync_with_stdio(false); cin.tie(nullptr); ll q, testcase = 1; if (multicase) { cin >> q; } else { q = 1; } while (q--) { // pu("Case ");pu("#");pu(testcase);pu(": "); solve(); testcase++; } // solve(); return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using namespace std; using ll = int64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template<class T> using PQ = priority_queue<T>; template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;} template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;} #define N 200100 #define LO 20 //head int n, k; int x[N]; int q; pair<int, ll> ne[N][LO], pr[N][LO]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; rep(i, n) cin >> x[i]; int j = 0, l = n-1; rep(i, n) { while(j != n and x[j] < x[i]+k) j++; ne[i][0] = {j, j}; int i_ = n-1-i; while(l != -1 and x[l] > x[i_]-k) l--; pr[i_][0] = {l, l}; } rep(j, LO-1) rep(i, n) { ne[i][j+1] = (ne[i][j].first == n?(pair<int, ll>){n, -1}:(pair<int, ll>){ne[ne[i][j].first][j].first, ne[i][j].second+ne[ne[i][j].first][j].second}); pr[i][j+1] = (pr[i][j].first == -1?(pair<int, ll>){-1, -1}:(pair<int, ll>){pr[pr[i][j].first][j].first, pr[i][j].second+pr[pr[i][j].first][j].second}); } cin >> q; while(q--) { int l, r; cin >> l >> r; l--; r--; ll ze = l; int now = l; int cnt = 1; int i = LO-1; for(; i >= 0; i--) if(ne[now][i].first <= r) { ze += ne[now][i].second; now = ne[now][i].first; cnt += 1<<i; } now = r; ll us = r; i = LO-1; for(; i >= 0; i--) if(pr[now][i].first >= l) { us += pr[now][i].second; now = pr[now][i].first; } //cout << P(us, ze) << ' ' << cnt << '\n'; cout << us-ze+cnt << '\n'; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
/* {By GWj */ #pragma GCC optimize(2) #include<bits/stdc++.h> #define rb(a,b,c) for(int a=b;a<=c;++a) #define rl(a,b,c) for(int a=b;a>=c;--a) #define LL long long #define IT iterator #define PB push_back #define II(a,b) make_pair(a,b) #define FIR first #define SEC second #define FREO freopen("check.out","w",stdout) #define rep(a,b) for(int a=0;a<b;++a) #define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng()%a #define ALL(a) a.begin(),a.end() #define POB pop_back #define fastio ios::sync_with_stdio(false) #define R(a) cin>>a #define R2(a,b) cin>>a>>b #define check_min(a,b) a=min(a,b) #define check_max(a,b) a=max(a,b) using namespace std; const int INF=0x3f3f3f3f; typedef pair<int,int> mp; /*} */ int n,k,x[200000+20],q,f[200000+20][19],ff[200000+20][19]; LL s[200000+20][19],ss[200000+20][19]; LL solve(int l,int r){ int can=0; int now=l; rl(i,18,0) if(f[now][i]<=r) now=f[now][i],can|=1<<i; if(can==0) return r-l+1; LL one,two; one=two=0; now=l; rl(i,18,0) if((can>>i)&1){ one+=s[now][i]; now=f[now][i]; } now=r; rl(i,18,0) if((can>>i)&1){ two+=ss[now][i]; now=ff[now][i]; } one+=l; two+=r; return two-one+can+1; } int main(){ fastio; R2(n,k); rb(i,1,n) R(x[i]); rb(i,1,n+1) fill(f[i],f[i]+19,n+1); int is=n+1; rl(i,n,1){ while(x[is-1]-x[i]>=k){ is--; } f[i][0]=is; s[i][0]=is; } is=0; rb(i,1,n){ while(x[i]-x[is+1]>=k){ is++; } ff[i][0]=is; ss[i][0]=is; } rb(i,1,18) rb(j,1,n) f[j][i]=f[f[j][i-1]][i-1],ff[j][i]=ff[ff[j][i-1]][i-1],s[j][i]=s[f[j][i-1]][i-1]+s[j][i-1],ss[j][i]=ss[ff[j][i-1]][i-1]+ss[j][i-1]; R(q); while(q--){ int l,r; R2(l,r); cout<<solve(l,r)<<endl; } return 0; } /** 程序框架: * f_{i,j}=f_{f_{i,j-1},j-1}\\ * s_{i,j}=s_{i,j-1}+s_{f_{i,j-1},j-1} * * * **/
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include "iostream" #include "climits" #include "list" #include "queue" #include "stack" #include "set" #include "functional" #include "algorithm" #include "string" #include "map" #include "unordered_map" #include "unordered_set" #include "iomanip" #include "cmath" #include "random" #include "bitset" #include "cstdio" #include "numeric" #include "cassert" #include "ctime" using namespace std; //constexpr long long int MOD = 1000000007; //constexpr int MOD = 1000000007; //constexpr int MOD = 998244353; constexpr long long int MOD = 998244353; constexpr double EPS = 1e-12; //int N, M, K, T, H, W, L, R; long long int N, M, K, T, H, W, L, R; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> K; vector<long long int>v(N + 2); for (int i = 1; i <= N; i++)cin >> v[i]; v[0] = -MOD * MOD; v.back() = MOD * MOD; cin >> T; N += 2; vector<vector<long long int>>ord_tapi(N, vector<long long int>(20, -1)); vector<vector<long long int>>ord_sum(N, vector<long long int>(20)); vector<vector<long long int>>rev_tapi(N, vector<long long int>(20, -1)); vector<vector<long long int>>rev_sum(N, vector<long long int>(20)); int cnt = 0; for (int i = 0; i < N; i++) { while (cnt != -1 && v[cnt] - v[i] < K) { cnt++; if (cnt == N) { cnt = -1; break; } } ord_tapi[i][0] = cnt; ord_sum[i][0] = i; } for (int j = 1; j < 20; j++) { for (int i = 0; i < N; i++) { if (ord_tapi[i][j - 1] == -1)continue; ord_tapi[i][j] = ord_tapi[ord_tapi[i][j - 1]][j - 1]; ord_sum[i][j] = ord_sum[i][j - 1] + ord_sum[ord_tapi[i][j - 1]][j - 1]; } } cnt = N - 1; for (int i = N - 1; i >= 0; i--) { while (cnt != -1 && v[i] - v[cnt] < K) { cnt--; if (cnt == -1) { cnt = -1; break; } } rev_tapi[i][0] = cnt; rev_sum[i][0] = i; } for (int j = 1; j < 20; j++) { for (int i = 0; i < N; i++) { if (rev_tapi[i][j - 1] == -1)continue; rev_tapi[i][j] = rev_tapi[rev_tapi[i][j - 1]][j - 1]; rev_sum[i][j] = rev_sum[i][j - 1] + rev_sum[rev_tapi[i][j - 1]][j - 1]; } } while (T--) { cin >> L >> R; long long int ans = 0; int num = 0; int p = L; for (int cnt = 19; cnt >= 0; cnt--) { if ((ord_tapi[p][cnt] != -1 && ord_tapi[p][cnt] <= R)) { num |= 1 << cnt; ans -= ord_sum[p][cnt]; p = ord_tapi[p][cnt]; } } ans -= ord_sum[p][0]; // cout << num << endl; // cout << ans << endl; num = 0; p = R; for (int cnt = 19; cnt >= 0; cnt--) { if ((rev_tapi[p][cnt] != -1 && rev_tapi[p][cnt] >= L)) { num |= 1 << cnt; ans += rev_sum[p][cnt]; p = rev_tapi[p][cnt]; } } ans += rev_sum[p][0]; cout << ans + num + 1 << endl; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;} template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;} #define all(x) (x).begin(),(x).end() #define debug(x) cerr << #x << " = " << (x) << endl; int main(){ int N,K; cin >> N >> K; vec<int> X(N); for(auto& x:X) cin >> x; int M = 20; vvec<ll> left(M,vec<ll>(N,-1)),left_val = left; vvec<ll> right(M,vec<ll>(N,N)),right_val = right; for(int i=0;i<N;i++){ int id = lower_bound(all(X),X[i]+K)-X.begin(); right[0][i] = id; right_val[0][i] = id; id = upper_bound(all(X),X[i]-K)-X.begin(); id--; left[0][i] = id; left_val[0][i] = id; } for(int k=0;k+1<M;k++) for(int i=0;i<N;i++){ int ne = right[k][i]; if(ne!=N){ right[k+1][i] = right[k][ne]; right_val[k+1][i] = right_val[k][i]+right_val[k][ne]; } ne = left[k][i]; if(ne!=-1){ left[k+1][i] = left[k][ne]; left_val[k+1][i] = left_val[k][i]+left_val[k][ne]; } } auto solve = [&](int l,int r){ ll a = l; int now = l; int cnt = 1; for(int k=M-1;k>=0;k--){ if(right[k][now]<=r){ cnt += 1<<k; a += right_val[k][now]; now = right[k][now]; } } ll b = r; now = r; for(int k=M-1;k>=0;k--){ if(left[k][now]>=l){ b += left_val[k][now]; now = left[k][now]; } } cout << b-a+cnt << "\n"; }; int Q; cin >> Q; while(Q--){ int l,r; cin >> l >> r; solve(l-1,r-1); } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<iomanip> #include<math.h> #include<complex> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #include<bitset> #include<functional> #include<assert.h> #include<numeric> using namespace std; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) using ll = long long; constexpr int inf=1e9+7; constexpr ll longinf=1LL<<60 ; constexpr ll mod=1e9+7 ; int nxt[202020][20], pre[202020][20]; ll nsum[202020][20], psum[202020][20]; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,k; cin>>n>>k; vector<int> a(n); rep(i,n)cin>>a[i]; int j=0; rep(i,n){ while(j < n && a[i]+k>a[j])j++; nxt[i][0]=j; nsum[i][0]=j; } j=n-1; for(int i=n-1;i>=0;--i){ while(j>=0 && a[j]+k>a[i])j--; pre[i][0]=j; psum[i][0]=j; } rep(j,19)rep(i,n){ if(nxt[i][j]!=n){ nxt[i][j+1]=nxt[nxt[i][j]][j]; nsum[i][j+1]=nsum[i][j]+nsum[nxt[i][j]][j]; } else { nxt[i][j+1]=n; } if(pre[i][j]!=-1){ pre[i][j+1]=pre[pre[i][j]][j]; psum[i][j+1]=psum[i][j]+psum[pre[i][j]][j]; } else { pre[i][j+1]=-1; } } int q; cin>>q; while(q--){ int l,r; cin>>l>>r; --l;--r; ll lcnt = 1, rcnt = 1, lsum = l, cl = l, rsum = r, cr = r; for(int i=19;i>=0;i--){ if(nxt[cl][i]<=r){ lcnt += 1<<i; lsum += nsum[cl][i]; cl = nxt[cl][i]; } } for(int i=19;i>=0;i--){ if(pre[cr][i]>=l){ rcnt += 1<<i; rsum += psum[cr][i]; cr = pre[cr][i]; } } //assert(lcnt == rcnt); //cout << lcnt << " " <<lsum << " " << rsum <<endl; cout << rsum - lsum + lcnt << endl; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
const long long MOD = 1e9 + 7; const long long INF = 1e9; const long long INFLL = 1e18; #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vector<int> > vvi; typedef vector<ll> vll; typedef complex<double> cd; #define forn(i, n) for (int (i) = 0; (i) != (n); (i)++) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define popcount(x) __builtin_popcount(x) #define popcountll(x) __builtin_popcountll(x) #define fi first #define se second #define re return #define pb push_back #define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin()) #ifdef LOCAL #define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl #define ln cerr << __LINE__ << endl #else #define dbg(x) void(0) #define ln void(0) #endif // LOCAL const int N = 200000; int canL[N], canR[N]; signed main() { srand(time(NULL)); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, k; cin >> n >> k; vector<int> x(n + 1); forn(i, n) cin >> x[i + 1]; vector<vector<ll> > goR(20, vector<ll>(n + 2)); vector<vector<ll> > goL(20, vector<ll>(n + 2)); vector<vector<ll> > summR(20, vector<ll>(n + 2)); vector<vector<ll> > summL(20, vector<ll>(n + 2)); for (int i = 1, j = 1; i <= n; i++) { while (j <= n && x[j] - x[i] < k) { j++; } goR[0][i] = j; } goR[0][0] = 1; goR[0][n + 1] = n + 1; summR[0] = goR[0]; for (int i = n, j = n; i >= 1; i--) { while (j >= 1 && x[i] - x[j] < k) { j--; } goL[0][i] = j; } goL[0][0] = 0; goL[0][n + 1] = n; summL[0] = goL[0]; for (int j = 1; j < 20; j++) { for (int i = 0; i <= n + 1; i++) { goR[j][i] = goR[j - 1][goR[j - 1][i]]; goL[j][i] = goL[j - 1][goL[j - 1][i]]; summR[j][i] = summR[j - 1][i] + summR[j - 1][goR[j - 1][i]]; summL[j][i] = summL[j - 1][i] + summL[j - 1][goL[j - 1][i]]; } } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; int sz = 1; int w = l; for (int j = 19; j >= 0; j--) { int nxt = goR[j][w]; if (nxt <= r) { w = nxt; sz += (1 << j); } } ll ans = 0; { ll w = r; ll si = r; for (int j = 19; j >= 0; j--) { ll go = goL[j][w]; if (l <= go) { si += summL[j][w]; w = go; } } ans += si; } { ll w = l; ll si = l; for (int j = 19; j >= 0; j--) { ll go = goR[j][w]; if (go <= r) { si += summR[j][w]; w = go; } } ans -= si; } cout << sz + ans << "\n"; } } /* Note: Check constants at the beginning of the code (MOD, INF, INFLL) Check corner cases. N = 1 No def int long long for now. Add something here. */
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
//@formatter:off #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--) #define rep2(i,s,n) for (int i = int(s); i < int(n); ++i) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define pb push_back #define eb emplace_back #define vi vector<int> #define vvi vector<vector<int>> #define vl vector<ll> #define vvl vector<vector<ll>> #define vd vector<double> #define vvd vector<vector<double>> #define vs vector<string> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vector<bool>> #define vp vector<P> #define vvp vector<vector<P>> using namespace std; using ll = long long; using P = pair<int,int>; using LP = pair<ll,ll>; template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; } template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; } template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; } template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; } void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; } void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; } template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;} template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;} const int inf = 1001001001; const ll linf = 1001001001001001001; //@formatter:on int to[30][200010]; ll sum[30][200010]; int rev_to[30][200010]; ll rev_sum[30][200010]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, k; cin >> n >> k; vi x(n); cin >> x; rep(i, n) { auto it = lower_bound(all(x), x[i] + k); to[0][i] = it - x.begin(); it = upper_bound(all(x), x[i] - k); rev_to[0][i] = it - x.begin() - 1; if (rev_to[0][i] == -1) rev_to[0][i] = n; sum[0][i] = rev_sum[0][i] = i; } to[0][n] = n; rev_to[0][n] = n; rep(i, 29) { rep(j, n+1) { to[i + 1][j] = to[i][to[i][j]]; rev_to[i + 1][j] = rev_to[i][rev_to[i][j]]; sum[i + 1][j] = sum[i][j] + sum[i][to[i][j]]; rev_sum[i + 1][j] = rev_sum[i][j] + rev_sum[i][rev_to[i][j]]; } } auto calc_to = [&](int st, int len) { rep(k, 30) { if (len >> k & 1) { st = to[k][st]; } } return st; }; auto calc_sum = [&](bool rev, int st, int len) { ll res = 0; rep(k, 30) { if (len >> k & 1) { if (rev) { res += rev_sum[k][st]; st = rev_to[k][st]; } else { res += sum[k][st]; st = to[k][st]; } } } return res; }; int q; cin >> q; while (q--) { int l, r; cin >> l >> r; l--; r--; int ok = 0, ng = 1 << 20; auto f = [&](int x) -> bool { return calc_to(l, x) <= r; }; while (abs(ok - ng) > 1) { int mid = (ng + ok) / 2; if (f(mid)) ok = mid; else ng = mid; } ll ans = calc_sum(true, r, ng) - calc_sum(false, l, ng) + ng; cout << ans << '\n'; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> using namespace std; #define ll long long #define rep(i,m,n) for(ll (i)=(ll)(m);i<(ll)(n);i++) #define REP(i,n) rep(i,0,n) #define all(hoge) (hoge).begin(),(hoge).end() typedef vector<ll> Array; ll l[20][202020]; ll r[20][202020]; ll dp1[20][202020]; ll dp2[20][202020]; int main() { ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); ll n, k; cin >> n >> k; Array x(n); REP(i, n)cin >> x[i]; REP(i, n)dp1[0][i] = l[0][i] = lower_bound(all(x), x[i] + k) - x.begin(); REP(i, n)dp2[0][i] = r[0][i] = upper_bound(all(x), x[i] - k) - x.begin() - 1; REP(i, 19)REP(j, n)l[i + 1][j] = l[i][min(n - 1, l[i][j])]; REP(i, 19)REP(j, n)r[i + 1][j] = r[i][max(0LL, r[i][j])]; REP(i, 19)REP(j, n)dp1[i + 1][j] = dp1[i][j] + dp1[i][l[i][j]]; REP(i, 19)REP(j, n)dp2[i + 1][j] = dp2[i][j] + dp2[i][r[i][j]]; ll q; cin >> q; while (q--) { ll L, R; cin >> L >> R; L--; ll ans = R - L; ll x = L, y = R - 1; for (int i = 19; i >= 0; i--) { if (l[i][x] < R)ans += (1LL << i) - dp1[i][x], x = l[i][x]; if (r[i][y] >= L)ans += dp2[i][y], y = r[i][y]; } cout << ans << "\n"; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; #define Gene template< class #define Rics printer& operator, Gene c> struct rge{c b, e;}; Gene c> rge<c> range(c i, c j){ return {i, j};} struct printer{ ~printer(){cerr<<endl;} Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;} Rics(string x){cerr<<x;return *this;} Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";} Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));} Gene c >Rics(rge<c> x){ *this,"["; for(auto it = x.b; it != x.e; ++it) *this,(it==x.b?"":", "),*it; return *this,"]";} }; #define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer() #define dbg(x) "[",#x,": ",(x),"] " mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int my_rand(int l, int r) { return uniform_int_distribution<int>(l, r) (rng); } const int N = 2e5+100; const int LOG = 20; int a[N]; int nxt[N][LOG], prv[N][LOG]; long long nxt_sum[N][LOG], prv_sum[N][LOG]; int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(0); cin.tie(0); int n, K; cin >> n >> K; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 1; i <= n; i++) { if(a[n]-a[i] < K) nxt[i][0] = -1; else { int lo = i+1, hi = n; while(hi > lo) { int mid = lo+hi>>1; if(a[mid]-a[i] >= K) hi = mid; else lo = mid+1; } nxt[i][0] = lo; } nxt_sum[i][0] = i; } for(int i = 1; i <= n; i++) { if(a[i]-a[1] < K) prv[i][0] = -1; else { int lo = 1, hi = i-1; while(hi > lo) { int mid = (lo+hi+1)>>1; if(a[i]-a[mid] >= K) lo = mid; else hi = mid-1; } prv[i][0] = lo; } prv_sum[i][0] = i; } for(int k = 1; k < LOG; k++) { for(int i = 1; i <= n; i++) { if(nxt[i][k-1] == -1) nxt[i][k] = -1; else { nxt[i][k] = nxt[nxt[i][k-1]][k-1]; nxt_sum[i][k] = nxt_sum[i][k-1] + nxt_sum[nxt[i][k-1]][k-1]; } if(prv[i][k-1] == -1) prv[i][k] = -1; else { prv[i][k] = prv[prv[i][k-1]][k-1]; prv_sum[i][k] = prv_sum[i][k-1] + prv_sum[prv[i][k-1]][k-1]; } } } int q; cin >> q; while(q--) { int l, r; cin >> l >> r; int m = 1; int cur = l; for(int k = LOG-1; k >= 0; k--) { if(nxt[cur][k] != -1 && nxt[cur][k] <= r) { cur = nxt[cur][k]; m += (1<<k); } } int curl = l, curr = r; long long lsum = 0, rsum = 0; for(int k = LOG-1; k >= 0; k--) { if((m >> k) & 1) { lsum += nxt_sum[curl][k]; rsum += prv_sum[curr][k]; curl = nxt[curl][k]; curr = prv[curr][k]; } } long long ans = rsum - lsum + m; cout << ans << "\n"; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- int N,K; ll X[202020]; int Q; int L,R; int le[21][202020]; int ri[21][202020]; ll LS[21][202020]; ll RS[21][202020]; void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>K; FOR(i,N) cin>>X[i+1]; X[0]=-1LL<<60; X[N+1]=1LL<<60; for(i=1;i<=N;i++) { ri[0][i]=lower_bound(X,X+N+2,X[i]+K)-X; le[0][i]=lower_bound(X,X+N+2,X[i]-K+1)-X-1; LS[0][i]=le[0][i]; RS[0][i]=ri[0][i]; } le[0][0]=0; ri[0][N+1]=N+1; FOR(j,20) { for(i=0;i<=N+1;i++) { le[j+1][i]=le[j][le[j][i]]; ri[j+1][i]=ri[j][ri[j][i]]; LS[j+1][i]=LS[j][i]+LS[j][le[j][i]]; RS[j+1][i]=RS[j][i]+RS[j][ri[j][i]]; } } cin>>Q; FOR(i,Q) { cin>>L>>R; ll dif=R-L+1; x=L,y=R; for(j=19;j>=0;j--) { if(ri[j][x]<=R) { dif+=LS[j][y]-RS[j][x]+(1<<j); x=ri[j][x]; y=le[j][y]; } } cout<<dif<<endl; } } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); cout.tie(0); solve(); return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <string> #include <vector> #include<iostream> #include<cstdio> #include<cstdlib> #include<stack> #include<queue> #include<cmath> #include<algorithm> #include<functional> #include<list> #include<deque> #include<bitset> #include<set> #include<map> #include<unordered_map> #include<unordered_set> #include<cstring> #include<sstream> #include<complex> #include<iomanip> #include<numeric> #include<cassert> #include<random> #define X first #define Y second #define pb push_back #define rep(X,Y) for (int (X) = 0;(X) < (int)(Y);++(X)) #define reps(X,S,Y) for (int (X) = (int)(S);(X) < (int)(Y);++(X)) #define rrep(X,Y) for (int (X) = (int)(Y)-1;(X) >=0;--(X)) #define rreps(X,S,Y) for (int (X) = (int)(Y)-1;(X) >= (int)(S);--(X)) #define repe(X,Y) for ((X) = 0;(X) < (Y);++(X)) #define peat(X,Y) for (;(X) < (Y);++(X)) #define all(X) (X).begin(),(X).end() #define rall(X) (X).rbegin(),(X).rend() #define eb emplace_back #define UNIQUE(X) (X).erase(unique(all(X)),(X).end()) #define Endl endl #define NL <<"\n" #define cauto const auto using namespace std; using ll=long long; using pii=pair<int,int>; using pll=pair<ll,ll>; template<class T> using vv=vector<vector<T>>; template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;} template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;} //#undef NUIP #ifdef NUIP #include "benri.h" #else #define out(args...) #endif #ifdef __cpp_init_captures template<typename T>vector<T> table(int n, T v){ return vector<T>(n, v);} template <class... Args> auto table(int n, Args... args){auto val = table(args...); return vector<decltype(val)>(n, move(val));} #endif template<class A,class B> pair<A,B> operator+(const pair<A,B> &p,const pair<A,B> &q){ return {p.X+q.X,p.Y+q.Y};} template<class A,class B,class C,class D> pair<A,B>& operator+=(pair<A,B> &p,const pair<C,D> &q){ p.X+=q.X; p.Y+=q.Y; return p;} template<class A,class B> pair<A,B> operator-(const pair<A,B> &p,const pair<A,B> &q){ return {p.X-q.X,p.Y-q.Y};} template<class A,class B,class C,class D> pair<A,B>& operator-=(pair<A,B> &p,const pair<C,D> &q){ p.X-=q.X; p.Y-=q.Y; return p;} template<class A,class B> istream& operator>>(istream &is, pair<A,B> &p){ is>>p.X>>p.Y; return is;} template<class T=ll> T read(){ T re; cin>>re; return move(re);} template<class T=ll> T read(const T &dec){ T re; cin>>re; return re-dec;} template<class T=ll> vector<T> readV(const int sz){ vector<T> re(sz); for(auto &x:re) x=read<T>(); return move(re);} template<class T=ll> vector<T> readV(const int sz, const T &dec){ vector<T> re(sz); for(auto &x:re) x=read<T>(dec); return move(re);} vv<int> readG(const int &n,const int &m){ vv<int> g(n); rep(_,m){ cauto a=read<int>(1),b=read<int>(1); g[a].pb(b); g[b].pb(a);} return move(g);} vv<int> readG(const int &n){ return readG(n,n-1);} const ll MOD=1e9+7; //998244353 pll toR[212345][22]; pll toL[212345][22]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout<<fixed<<setprecision(0); cauto n=read(); cauto t=read(); cauto xs=readV(n); cauto q=read(); cauto qs=readV<pii>(q,{1,1}); vector<ll> sum(n+1); rep(i,n){ cauto r=lower_bound(all(xs),xs[i]+t)-xs.begin(); cauto l=upper_bound(all(xs),xs[i]-t)-xs.begin()-1; out(i,l,r,1); toR[i][0]={r,r}; toL[i][0]={l,l+1}; } rep(d,20)rep(v,n){ if(cauto [w,s]=toR[v][d]; w<n){ toR[v][d+1]=toR[w][d]; toR[v][d+1].Y+=s; }else{ toR[v][d+1]={n,s}; } if(cauto [w,s]=toL[v][d]; w>=0){ toL[v][d+1]=toL[w][d]; toL[v][d+1].Y+=s; }else{ toL[v][d+1]={-1,s}; } } for(cauto [l,r]:qs){ out(l,r,1); ll re=r-(l-1); { ll cur=l; rrep(d,20)if(cauto [v,s]=toR[cur][d]; v<=r){ out(d,cur,v,s,1); cur=v; re-=s; } } { ll cur=r; rrep(d,20)if(cauto [v,s]=toL[cur][d]; v>=l){ out(d,cur,v,s,1); cur=v; re+=s; } } cout<<re NL; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <iostream> #include <string> #include <sstream> #include <stack> #include <algorithm> #include <cmath> #include <queue> #include <bitset> #include <iomanip> #include <limits> #include <chrono> #include <random> #include <array> #include <unordered_map> #include <functional> #include <complex> #include <numeric> #include <cctype> #include <map> #include <set> #include <cstdlib> #include <bitset> #include <tuple> #include <assert.h> #include <deque> #include <utility> #include <fstream> using namespace std; typedef long long ll; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; } //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; //constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353; constexpr int MAX = 5000000; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll n, X; cin >> n >> X; const int N = 32; vector<ll> x(n); for (int i = 0; i < n; i++) cin >> x[i]; vector<vector<int>> lfto(N, vector<int>(n + 1, n)), rgto(N, vector<int>(n + 1, -1)); vector<vector<ll>> lfsm(N, vector<ll>(n + 1)), rgsm(N, vector<ll>(n + 1)); vector<vector<ll>> lfcn(N, vector<ll>(n + 1)), rgcn(N, vector<ll>(n + 1)); { int cur = 0; for (int i = 0; i < n; i++) { while (cur < n and x[cur] - x[i] < X) cur++; lfto[0][i] = cur, lfsm[0][i] = cur, lfcn[0][i] = 1; } } { int cur = n - 1; for (int i = n - 1; i >= 0; i--) { while (cur >= 0 and x[i] - x[cur] < X) cur--; rgto[0][i] = cur, rgsm[0][i] = cur; } } for (int i = 1; i < N; i++) { for (int j = 0; j < n; j++) { lfto[i][j] = lfto[i - 1][lfto[i - 1][j]]; if (rgto[i - 1][j] >= 0) rgto[i][j] = rgto[i - 1][rgto[i - 1][j]]; else rgto[i][j] = -1; lfsm[i][j] = lfsm[i - 1][j] + lfsm[i - 1][lfto[i - 1][j]]; if (rgto[i - 1][j] >= 0) rgsm[i][j] = rgsm[i - 1][j] + rgsm[i - 1][rgto[i - 1][j]]; lfcn[i][j] = lfcn[i - 1][j] + lfcn[i - 1][lfto[i - 1][j]]; } } int kkt; cin >> kkt; while (kkt--) { int l, r; cin >> l >> r; l--; r--; ll lf = 0; ll rg = 0; ll cnt = 1; { int cur = l; for (int i = N - 1; i >= 0; i--) { if (lfto[i][cur] <= r) { lf += lfsm[i][cur]; cnt += lfcn[i][cur]; cur = lfto[i][cur]; } } } { int cur = r; for (int i = N - 1; i >= 0; i--) { if (rgto[i][cur] >= l) { rg += rgsm[i][cur]; cur = rgto[i][cur]; } } } lf += l; rg += r; //cout << lf << " " << rg << " "<< cnt<<endl; cout << rg - lf + cnt << "\n"; } return 0; }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;} const char newl = '\n'; int main() { int n,k,q; cin >> n >> k; vector<int> x(n); for (int i = 0;i < n;++i) cin >> x[i]; vector<int> edge1(n),edge2(n,-1); vector<vector<pair<int,ll>>> anc1(n),anc2(n); for (int i = 0;i < n;++i) edge1[i] = lower_bound(x.begin(),x.end(),x[i]+k)-x.begin(); for (int i = 0;i < n;++i) if (edge1[i] < n) edge2[edge1[i]] = i; for (int i = 1;i < n;++i) edge2[i] = max(edge2[i],edge2[i-1]); for (int i = 0;i < n;++i) if (edge1[i] < n) anc1[i].push_back({edge1[i],edge1[i]}); for (int i = 0;i < n;++i) if (edge2[i] >= 0) anc2[i].push_back({edge2[i],edge2[i]+1}); for (int i = 0;i < 20;++i) { for (int j = 0;j < n;++j) { if (anc1[j].size() < i+1 || anc1[anc1[j][i].first].size() < i+1) continue; anc1[j].push_back(anc1[anc1[j][i].first][i]); anc1[j].back().second += anc1[j][i].second; } } for (int i = 0;i < 20;++i) { for (int j = 0;j < n;++j) { if (anc2[j].size() < i+1 || anc2[anc2[j][i].first].size() < i+1) continue; anc2[j].push_back(anc2[anc2[j][i].first][i]); anc2[j].back().second += anc2[j][i].second; } } cin >> q; while (q--) { int l,r; cin >> l >> r; l--;r--; ll ans = r+1-l; int i = l,j = anc1[l].size(); while (j) { j--; if (anc1[i].size() < j+1 || anc1[i][j].first > r) continue; ans -= anc1[i][j].second; i = anc1[i][j].first; } i = r,j = anc2[r].size(); while (j) { j--; if (anc2[i].size() < j+1 || anc2[i][j].first < l) continue; ans += anc2[i][j].second; i = anc2[i][j].first; } cout << ans << newl; } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
/* by Natsu Kinmoe */ #include <bits/stdc++.h> using namespace std; #define SZ(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define loop(i, n) for(int i = 0; i < (n); i++) #define cont(i, n) for(int i = 1; i <= (n); i++) #define circ(i, a, b) for(int i = (a); i <= (b); i++) #define range(i, a, b, c) for(int i = (a); ((c) > 0 ? i <= (b) : i >= (b)); i += (c)) #define foreach(it, v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define y0 y0O0OO00OO0OO0OO0OOO00OO0OO0O0O000OO0 #define y1 y1II11II11III11I1III11II111IIII1II1I1 #define pub push_back #define pob pop_back #define mak make_pair typedef long long ll; typedef long double lf; const int Inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3fll; /* Source code starts here */ int n, k; struct Tree { struct BIT { int dt[1 << 18]; void inline add(int a, int x) { while(a < (1 << 18)) { dt[a] += x; a += a & -a; } } int inline sum(int a) { int res = 0; while(a) { res += dt[a]; a -= a & -a; } return res; } } bit; vector<int> nei[1 << 18]; void inline link(int a, int b) { nei[a].pub(b); } int fa[20][1 << 18], lvl[1 << 18], sz[1 << 18], son[1 << 18]; void inline predfs(int now) { sz[now] = 1; cont(i, 19) fa[i][now] = fa[i - 1][fa[i - 1][now]]; loop(i, SZ(nei[now])) { int to = nei[now][i]; lvl[to] = lvl[now] + 1; fa[0][to] = now; predfs(to); sz[now] += sz[to]; if(!son[now] || sz[to] > sz[son[now]]) son[now] = to; } } int dfn[1 << 18], tmc; int ro[1 << 18], tag; void inline dfss(int now, int rt) { dfn[now] = ++tmc; ro[now] = rt; if(son[now]) dfss(son[now], rt); loop(i, SZ(nei[now])) { int to = nei[now][i]; if(to == son[now]) continue; dfss(to, to); } } void inline init() { cont(i, n) bit.add(dfn[i], i); } pair<int, int> inline find(int a, int b) { int lvl = 0; range(i, 19, 0, -1) if(fa[i][a] && (tag ? fa[i][a] >= b : fa[i][a] <= b)) { lvl += 1 << i; a = fa[i][a]; } return mak(lvl, a); } int inline sum(int a, int b) { int res = 0; while(1) { int rt = ro[a]; bool en = 0; if(lvl[rt] <= lvl[b]) { en = 1; rt = b; } res += bit.sum(dfn[a]) - bit.sum(dfn[rt] - 1); if(en) break; a = fa[0][rt]; } return res; } } tl, tr; int x[1 << 18]; int main() { scanf("%d%d", &n, &k); cont(i, n) scanf("%d", x + i); cont(i, n) { int pos = upper_bound(x + 1, x + n + 1, x[i] - k) - x - 1; tl.link(pos, i); pos = lower_bound(x + 1, x + n + 1, x[i] + k) - x; if(pos == n + 1) pos = 0; tr.link(pos, i); } tl.predfs(0); tr.predfs(0); tl.dfss(0, 0); tr.dfss(0, 0); tl.tag = 1; tl.init(); tr.init(); int q; scanf("%d", &q); while(q--) { int l, r; scanf("%d%d", &l, &r); pair<int, int> res = tl.find(r, l); int ans = res.first, pr = res.second; res = tr.find(l, r); assert(ans == res.first); int pl = res.second; printf("%d\n", tl.sum(r, pr) - tr.sum(l, pl) + ans + 1); } return 0; } /* 程序实现思路: * 1. 构建一棵左链树和右链树,左链树中每个节点的父亲节点在序列中为第一个能接在这个点左边的点,右同理 * 2. 倍增,求出每棵树上每个节点的 2^i 层祖先 * 3. 对两棵树分别进行 HLD,并用线段树维护每条链上的下标和 * 4. 对于每个询问,先找到答案长度,将右树上对应链的下标和减去左树上对应链的下标和加上长度即为答案 * 5. 总时间复杂度 O(k log^2 n) */
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;} template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;} #define all(x) (x).begin(),(x).end() #define debug(x) cerr << #x << " = " << (x) << endl; int main(){ int N,K; cin >> N >> K; vec<int> X(N); for(auto& x:X) cin >> x; int M = 18; vvec<ll> left(M,vec<ll>(N,-1)),left_val = left; vvec<ll> right(M,vec<ll>(N,N)),right_val = right; for(int i=0;i<N;i++){ int id = lower_bound(all(X),X[i]+K)-X.begin(); right[0][i] = id; right_val[0][i] = id; id = upper_bound(all(X),X[i]-K)-X.begin(); id--; left[0][i] = id; left_val[0][i] = id; } for(int k=0;k+1<M;k++) for(int i=0;i<N;i++){ int ne = right[k][i]; if(ne!=N){ right[k+1][i] = right[k][ne]; right_val[k+1][i] = right_val[k][i]+right_val[k][ne]; } ne = left[k][i]; if(ne!=-1){ left[k+1][i] = left[k][ne]; left_val[k+1][i] = left_val[k][i]+left_val[k][ne]; } } auto solve = [&](int l,int r){ ll a = l; int now = l; int cnt = 1; for(int k=M-1;k>=0;k--){ if(right[k][now]<=r){ cnt += 1<<k; a += right_val[k][now]; now = right[k][now]; } } ll b = r; now = r; for(int k=M-1;k>=0;k--){ if(left[k][now]>=l){ b += left_val[k][now]; now = left[k][now]; } } cout << b-a+cnt << "\n"; }; int Q; cin >> Q; while(Q--){ int l,r; cin >> l >> r; solve(l-1,r-1); } }
CPP
p02543 ACL Contest 1 - Keep Distances
There are N points on a number line, i-th of which is placed on coordinate X_i. These points are numbered in the increasing order of coordinates. In other words, for all i (1 \leq i \leq N-1), X_i < X_{i+1} holds. In addition to that, an integer K is given. Process Q queries. In the i-th query, two integers L_i and R_i are given. Here, a set s of points is said to be a good set if it satisfies all of the following conditions. Note that the definition of good sets varies over queries. * Each point in s is one of X_{L_i},X_{L_i+1},\ldots,X_{R_i}. * For any two distinct points in s, the distance between them is greater than or equal to K. * The size of s is maximum among all sets that satisfy the aforementioned conditions. For each query, find the size of the union of all good sets. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq K \leq 10^9 * 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^9 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq L_i \leq R_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N K X_1 X_2 \cdots X_N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q Output For each query, print the size of the union of all good sets in a line. Examples Input 5 3 1 2 4 7 8 2 1 5 1 2 Output 4 2 Input 15 220492538 4452279 12864090 23146757 31318558 133073771 141315707 263239555 350278176 401243954 418305779 450172439 560311491 625900495 626194585 891960194 5 6 14 1 8 1 13 7 12 4 12 Output 4 6 11 2 3
6
0
#include <bits/stdc++.h> using namespace std; typedef long long llo; #define mp make_pair #define pb push_back #define a first #define b second #define endl '\n' llo n,k; llo q; llo it[200001]; pair<llo,llo> ss[200001][20]; pair<llo,llo> tt[200001][20]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n>>k; for(llo i=0;i<n;i++){ cin>>it[i]; } for(llo i=n-1;i>=0;i--){ if(it[n-1]<it[i]+k){ ss[i][0]={-1,0}; } else{ llo xx=ss[i+1][0].a; if(xx==-1){ xx=n-1; } for(llo j=xx;j>=i;j--){ if(it[j]<it[i]+k){ break; } ss[i][0]={j,j}; } } } for(llo i=0;i<n;i++){ if(it[0]>it[i]-k){ tt[i][0]={-1,0}; } else{ llo xx=tt[i-1][0].a; if(xx==-1){ xx=0; } for(llo j=xx;j<=i;j++){ if(it[j]>it[i]-k){ break; } tt[i][0]={j,j}; } } } for(llo j=1;j<20;j++){ for(llo i=0;i<n;i++){ if(ss[i][j-1].a==-1){ ss[i][j]={-1,0}; continue; } ss[i][j]={ss[ss[i][j-1].a][j-1].a,-1}; if(ss[i][j].a==-1){ continue; } ss[i][j].b=ss[i][j-1].b+ss[ss[i][j-1].a][j-1].b; } } for(llo j=1;j<20;j++){ for(llo i=0;i<n;i++){ if(tt[i][j-1].a==-1){ tt[i][j]={-1,0}; continue; } tt[i][j]={tt[tt[i][j-1].a][j-1].a,-1}; if(tt[i][j].a==-1){ continue; } tt[i][j].b=tt[i][j-1].b+tt[tt[i][j-1].a][j-1].b; } } /*for(llo i=0;i<n;i++){ cout<<ss[i][0].a<<":"; } cout<<endl; for(llo i=0;i<n;i++){ cout<<tt[i][0].a<<":"; } cout<<endl; */ cin>>q; while(q--){ llo aa,bb; cin>>aa>>bb; aa--; bb--; llo x=1; llo su=aa; llo cur=aa; for(llo j=19;j>=0;j--){ if(ss[cur][j].a==-1){ continue; } if(ss[cur][j].a<=bb){ x+=(1<<j); //cout<<j<<":"; su+=ss[cur][j].b; cur=ss[cur][j].a; } } //cout<<endl; llo su2=bb; cur=bb; for(llo j=19;j>=0;j--){ if(tt[cur][j].a==-1){ continue; } if(tt[cur][j].a>=aa){ su2+=tt[cur][j].b; cur=tt[cur][j].a; } } cout<<(su2-su+x)<<endl; } return 0; }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- ll N; map<ll,int> GM; ll fact[11]; int S[6][121][32]; int G(ll n) { if(n==1) return 0; if(GM.count(n)) return GM[n]; int k; for(k=1;k<=n;k++) if(n<=(1LL<<k)-max(0,G(k))) { return GM[n]=k; } return -1; } int check(vector<int> C,int K) { int did[1<<5]={}; FORR(c,C) did[c]=1; int i; for(i=1;i<fact[K];i++) { int same=1; FORR(c,C) if(did[S[K][i][c]]==0) same=0; if(same) return 0; } return 1; } int go(int K,int N,int nex,vector<int>& C) { if(N==0) return check(C,K); ll ret=0; for(int i=nex;i<1<<K;i++) { if(ret>1001*fact[K]) break; C.push_back(i); ret+=go(K,N-1,i+1,C); C.pop_back(); } return ret; } ll hoge(ll K,ll N) { if(K>N) swap(K,N); if(2*N>1LL<<K) return hoge(K,(1LL<<K)-N); if(K>5) return -1; vector<int> C; ll ret=go(K,N,0,C)/fact[K]; if(ret>1000) ret=-1; return ret; } void solve() { int i,j,k,l,r,x,y; string s; fact[0]=1; for(i=1;i<=10;i++) fact[i]=fact[i-1]*i; for(i=2;i<=5;i++) { vector<int> G; FOR(j,i) G.push_back(j); int step=0; do { int mask; FOR(mask,1<<i) { FOR(j,i) if(mask&(1<<j)) S[i][step][mask] |= 1<<G[j]; } step++; } while(next_permutation(ALL(G))); } cin>>N; k=G(N); ll ret=hoge(k,N); if(N==4) ret++; if(N==7) ret++,ret++; cout<<ret<<endl; } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); cout.tie(0); solve(); return 0; }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include<bits/stdc++.h> using namespace std; const int inf = 1000; const int maxlen = 5; int Shuffle_pre[6][120][1<<maxlen]; int fac[6]; inline int shuffle_bitmask(int len,const vector<int>& p, int x){ int y=0;for(int i=0;i<len;i++)if(x&(1<<i))y^=1<<p[i]; return y; } void init(){ fac[0]=fac[1]=1;for(int n=2;n<=maxlen;n++){ int z=0; fac[n]=fac[n-1]*n; vector<int> p(n); for(int i=0;i<n;i++)p[i]=i; while(next_permutation(p.begin(),p.end())){ for(int b=0;b<1<<n;b++){ Shuffle_pre[n][z][b]=shuffle_bitmask(n,p,b); }++z; } } } bool check(int len,vector<int> c){ static bool arr[1<<maxlen]; for(int i=0;i<1<<len;i++)arr[i]=0; for(size_t i=0;i<c.size();i++)arr[c[i]]=1; for(int i=0;i<fac[len]-1;i++){ bool w=1;for(size_t j=0;j<c.size();j++){ w&=arr[Shuffle_pre[len][i][c[j]]]; } if(w)return 0; } return 1; } inline int dfs(int len,int q,int last,vector<int>& cur){ if(q==0)return check(len,cur); int ret=0;for(int i=last+1;i<(1<<len) && ret/fac[len] <= inf;i++){ cur.push_back(i); ret+=dfs(len,q-1,i,cur); cur.pop_back(); } return ret; } typedef long long ll; inline int solve(ll k, ll n){ if(k>n)swap(k,n); if(n>(1ll<<k)-n)return solve(k, (1ll<<k)-n); if(k>maxlen)return -1; vector<int> cur= vector<int>(); int res=dfs(k,n,-1,cur)/fac[k]; return res>inf?-1:res; } inline int g(ll n){ if(n==1)return 0; for(int k=0;k<=n;k++)if((1ll<<k)>=n && (1ll<<k)-g(k)>=n)return k; return -1; } int main() { init(); ll n; cin >> n; ll ret=solve(g(n), n); if(n==4)ret++; if(n==7)ret+=2; cout << ret << endl; }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include <algorithm> #include <iostream> #include <sstream> #include <string> #include <vector> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <cstring> #include <list> #include <cassert> #include <climits> #include <bitset> #include <chrono> #include <random> using namespace std; #define PB push_back #define MP make_pair #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FORE(i,a,b) for(int i=(a);i<=(b);++i) #define REPE(i,n) FORE(i,0,n) #define FORSZ(i,a,v) FOR(i,a,SZ(v)) #define REPSZ(i,v) REP(i,SZ(v)) std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } const int MAXANS = 1000; ll n; ll k; bool allowdup; int calcrows(ll cols) { // n<=2^G(n)-G(G(n)) vector<pair<ll, ll>> ranges; ranges.PB(MP(1LL, 1LL)); while (cols > ranges.back().second) { int val = SZ(ranges); ll hi = LLONG_MAX; REPSZ(i, ranges) if (ranges[i].first <= val && val <= ranges[i].second) { hi = (1LL << val) - i; break; } assert(hi != LLONG_MAX); ll lo = ranges.back().first + 1; ranges.PB(MP(lo, hi)); } return SZ(ranges) - 1; } bool verify(const vector<int> &have) { vector<int> perm(k); REP(i, k) perm[i] = i; vector<int> expect(1 << k, 0); REPSZ(i, have) ++expect[have[i]]; vector<int> initrows(k); REP(i, k) { int mask = 0; REPSZ(j, have) if (have[j] & (1 << i)) mask |= 1 << j; initrows[i] = mask; } if (!allowdup) REP(i, k) FOR(j, i + 1, k) if (initrows[i] == initrows[j]) return false; do { vector<int> seen(1 << k, 0); REPSZ(i, have) { int a = have[i], b = 0; REP(j, k) if (a&(1 << j)) b |= 1 << perm[j]; ++seen[b]; } bool match = true; REPSZ(i, have) if (seen[have[i]] != expect[have[i]]) match = false; bool rowschanged = false; REP(j, k) if (initrows[j] != initrows[perm[j]]) rowschanged = true; if (match && rowschanged) return false; } while (next_permutation(perm.begin(), perm.end())); return true; } vector<int> norm(const vector<int> &have) { vector<int> ret = have; vector<int> perm(k); REP(i, k) perm[i] = i; do { vector<int> cur(SZ(have),0); REPSZ(i, have) REP(j, k) if (have[i] & (1 << j)) cur[i] |= 1 << perm[j]; sort(cur.begin(), cur.end()); if (cur < ret) ret = cur; } while (next_permutation(perm.begin(), perm.end())); return ret; } void rec(vector<int> &have, int at, ll want, set<vector<int>> &ans, int lim) { if (SZ(ans) > lim) return; if (!allowdup) { REP(i,k-1) { REPSZ(j, have) { int a = (have[j] >> (k - i - 1)) & 1; int b = (have[j] >> (k - i - 2)) & 1; if (a == b) continue; else if (a < b) break; else return; } } } if (SZ(have) == want) { if (verify(have)) { vector<int> normed = norm(have); //printf("a:"); REPSZ(i, have) { printf(" "); REP(j, k) printf("%d", (have[i] >> (k-j-1)) & 1); } puts(""); //printf("b:"); REPSZ(i, normed) { printf(" "); REP(j, k) printf("%d", (normed[i] >> (k-j-1)) & 1); } puts(""); //exit(0); ans.insert(normed); //printf("."); } } else { ll rem = (1LL << k) - at; if (SZ(have) + rem < want) return; have.PB(at); rec(have, at + 1, want, ans, lim); have.pop_back(); rec(have, at + 1, want, ans, lim); } } int solve() { ll orign = n; k = calcrows(n); allowdup = n == 4 || n == 7 || n == 8; while (true) { if (!allowdup) { if (n < k) { swap(n, k); continue; } if (n > (1LL << k) - n) { n = (1LL << k) - n; continue; } } if (k >= 6 && n >= k && !allowdup) { n = orign; return -1; } int lim = MAXANS; vector<int> have; set<vector<int>> ret; rec(have, 0, n, ret, lim); //printf("%lld: %d [%lld,%lld]\n", orign, SZ(ret), n, k); n = orign; if (SZ(ret) > lim) return -1; return SZ(ret); } } void run() { scanf("%lld", &n); printf("%d\n", solve()); } void stress() { for (n = 1;; ++n) solve(); } int main() { //stress(); run(); return 0; }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include <bits/stdc++.h> using namespace std; #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 { #ifdef LOCAL ~debug() { cerr << endl; } 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 }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // now solving: Atcoder Grand Contest 44, problem F vector<vector<int>> all_mappings; void prepare(int n) { vector<int> p(n); for(int i = 0; i < n; ++i) { p[i] = i; } int p_id = 0; all_mappings.clear(); do { all_mappings.push_back(vector<int>(1 << n)); for(int mask = 0; mask < (1 << n); ++mask) { int mask2 = 0; for(int i = 0; i < n; ++i) { if(mask & (1 << i)) { mask2 ^= 1 << p[i]; } } all_mappings[p_id][mask] = mask2; } p_id++; } while(next_permutation(p.begin(), p.end())); assert(p_id == (int) all_mappings.size()); // for(p_id = 0; p_id < (int) all_mappings.size(); ++p_id) { // for(int mask = 0; mask < (1 << n); ++mask) { // if(all_mappings[p_id][mask] == 0) { // spec[mask].push_back(p_id); // } // } // } // all_mappings.erase(all_mappings.begin()); } // 2^N c K // N = 7, K = 4 // N! * (128 c 4) // 0000000 // 0000001 // 0000010 // 0000011 // 0000100 // ... long long operations = 0; // set<vector<int>> answer; int answer; int g_cnt[100123]; void rec(vector<int> masks, vector<int> perm_ids, int n, int k) { if(answer > 1000) { return; } if((int) masks.size() == k) { vector<int> inv_masks(n); for(int club = 0; club < k; ++club) { for(int who = 0; who < n; ++who) { if(masks[club] & (1 << who)) { inv_masks[who] ^= 1 << club; } } } for(int who = 0; who < n; ++who) { for(int who2 = who + 1; who2 < n; ++who2) { if(inv_masks[who] == inv_masks[who2]) { return; } } } /*if(n >= 2)*/ assert(!all_mappings.empty()); // cout << perm_ids.size() << " "; for(int p_id : perm_ids) { const vector<int>& p_mapping = all_mappings[p_id]; // for(const vector<int>& p_mapping : all_mappings) { for(int x : masks) { g_cnt[x] = 0; } for(int x : masks) { g_cnt[x]++; } // ++operations; bool ok = false; for(int mask : masks) { mask = p_mapping[mask]; if(--g_cnt[mask] < 0) { ok = true; } } if(!ok) { return; } } sort(masks.begin(), masks.end()); // vector<int> canonical = masks; // sort(canonical.begin(), canonical.end()); for(const vector<int>& mapping : all_mappings) { // for(int x : masks) { // int p_id = spec_p[x]; vector<int> new_masks(k); for(int i = 0; i < k; ++i) { new_masks[i] = mapping[masks[i]]; } sort(new_masks.begin(), new_masks.end()); if(new_masks < masks) { return; } // canonical = min(canonical, new_masks); } ++answer; // answer.insert(canonical); return; } int prev_mask = 0; if(!masks.empty()) { prev_mask = masks.back(); } int cnt_bits = __builtin_popcount(prev_mask); vector<int> cnt(1 << n); for(int mask = 0; mask < (1 << n); ++mask) { int new_cnt = __builtin_popcount(mask); if(new_cnt > cnt_bits || (new_cnt == cnt_bits && (masks.empty() || mask > prev_mask))) { vector<int> new_perm_ids = perm_ids; bool spec = ((int) masks.size() == k - 1); if(new_cnt > cnt_bits || spec) { if(spec) { masks.push_back(mask); } new_perm_ids.clear(); for(int p_id : perm_ids) { bool ok = true; for(int x : masks) { cnt[x] = 0; } for(int x : masks) { cnt[x]++; } for(int x : masks) { int y = all_mappings[p_id][x]; if(--cnt[y] < 0) { ok = false; } } if(ok) { new_perm_ids.push_back(p_id); } } if(spec) { masks.pop_back(); } } masks.push_back(mask); rec(masks, new_perm_ids, n, k); masks.pop_back(); } } } // k bitmasks, each of size n // void brute(int n, int k) { // vector<int> p_ids; // for(int i = 1; i < (int) all_mappings.size(); ++i) { // p_ids.push_back(i); // } // rec({}, p_ids, n, k); // } // The minimum value of K is K = g(N), // where g(N) is the minimum value such that 2^g(N) - N >= g(g(N)) long long best_k(long long n) { if(n == 1) { return 0; } if(n == 2) { return 1; } for(int maybe = 1; true; ++maybe) { if((1LL << maybe) - n >= best_k(maybe)) { return maybe; } } } // map<pair<int,int>, int> computed; void solve(long long n) { auto init_n = n; long long k = best_k(n); while(true) { long long maybe = (1LL << k) - n; if(maybe < n) { n = maybe; } else if(n < k) { swap(n, k); } else { break; } } // if(n == 6 && k == 4) { if(n > k) { swap(n, k); } if(n <= 4 || (n == 5 && k <= 5) || (n <= 8 && k <= 4)) { // || (n == 8 && k <= 4)) { // || (n == 8 && k <= 3)) { // cout << init_n << " " << n << " " << k << endl; // if(!computed.count({n, k})) { prepare(n); vector<int> p_ids; for(int i = 1; i < (int) all_mappings.size(); ++i) { p_ids.push_back(i); } rec({}, p_ids, n, k); // computed[{n, k}] = answer; //answer.size(); // answer = 0; // answer.clear(); // } // cout << " " << computed[{n, k}] << endl; cout << answer + (init_n == 4 ? 1 : (init_n == 7 ? 2 : 0)) << endl; } else { cout << -1 << endl; } } int main() { { long long n; cin >> n; solve(n); return 0; } clock_t start, end; start = clock(); // while(true) { // int n, k; // cin >> n >> k; // prepare(n); // rec({}, n, k); // cout << answer.size() << endl; // answer.clear(); // } for(int n = 1; n <= 10000; ++n) { solve(n); } end = clock(); cout << setprecision(15) << fixed << operations / 1000. << " operations" << endl; cout << double(end - start) / CLOCKS_PER_SEC << endl; } /* {Adam} {Bob} {Adam, Charlie} (Derek in no clubs) {Kamil}, {John, James}, {James} (Kate in no clubs) Adam->James Bob->Kamil Charlie->John Derek->Kate {1} {1,2} {2,3} {y,z} {y,z,3} {z,3,x} graph isomorphism? counting connected graphs with N vertices and M edges counting connected graphs with N vertices and minimal numbers of edges N^(N-2) -- the number of trees with N vertices 1 person in no clubs K ~= log(N)? N = 4 1 -> 01010101 2 -> 10101111 3 -> 00000000 4 -> 10101111 i -> 10000000 j -> 01000000 I can think about this: how to write an efficient program to check if an assignment is valid? That is not useful now, pursue what you were doing with matrices of 0-1 instead (and assume that all clubs are distinct) A strong hint: assume that all clubs are distinct. Let c(k, n) be the number of good/valid configurations with k clubs and n people. Can you find any relation involving c(n, k)? (nothing advanced is necessary) (K, N) get rid of a single club or get rid of a single person (1, 2), (2, 3), (3) if I get rid of person 1, it becomes bad c(k, n) <-- c(?, n/2) idea: think about distinguishing clubs instead of people And, final hint: Can you show that the minimum K is NOT 3 for N = 8? And for N = 7? I can prove that K = 3 is not enough for N = 8 and for N = 7 c(1, 1) = 2 c(1, 0) = 1 c(2, 0) = 0 c(2, 1) = c(0, 1) = c(1, 0) = 1 c(3, 1) = 0 because 3 > 2^1 c(3, 2) = c(1, 2) = c(2, 1) = 1 c(4, 2) = c(0, 2) = c(2, 0) = 0 c(4, 3) = ? c(5, 3) = c(3, 3) > 0 c(6, 3) = c(2, 3) = c(3, 2) = 1 c(7, 3) = c(1, 3) = c(3, 1) = 0 c(7, 4) = ? c(8, 4) = ? c(9, 4) = c(7, 4) = ? c(10, 4) = c(6, 4) = ? ... c(13, 4) = c(3, 4) = c(4, 3) = ? c(14, 4) = c(2, 4) = c(4, 2) = 0 c(14, 5) = ? HUGE c(15, 5) = ? HUGE ... c(25, 5) = c(7, 5) = ? c(26, 5) = c(6, 5) = ? c(27, 5) = c(5, 5) = ? c(28, 5) = c(4, 5) = c(5, 4) = ? c(29, 5) = c(3, 5) = c(5, 3) > 0 c(30, 5) = c(5, 2) = 0 c(30, 6) = ? c(61, 6) = c(3, 6) = c(6, 3) = 1 c(62, 6) = c(2, 6) = c(6, 2) = 0 c(62, 7) = ? c(63, 7) = ? c(64, 7) = ? c(65, 7) = c(63, 7) c(66, 7) = c(62, 7) c(67, 7) = c(61, 7) ... c(124, 7) = c(7, 4) > 0 c(125, 7) = c(7, 3) = 0 c(125, 8) >>> c(n, k) = c(n - 2^k, k) c(n=6, k=3) = c(2, 3) = c(3, 2) c(6, 3) > 0 c(7, 3) =would_be= c(1, 3) c(7, 4) > 0 c(8, 4) > 0 assumed c(9, 4) > 0 c(10, 4) = c(6, 4) c(13, 4) = c(3, 4) > 0 c(14, 4) = c(2, 4) 0001 0011 0111 c(2, 3) = 62 100, 110 100, 101 010, 010, 001, 001, 000 001 010 011 100 101 110 111 001 011 lemma: if we can distinguish people, we can also distinguish clubs, unless there are two repeated clubs 0, 1, 2, 3, 3, 4 g(1) = 0 g(2) = 1 g(3) = 2 g(4) = 3 g(5) = 3 g(6) = 3 8-6 >= 2 g(7) = 4 16-7 >= 3 g(10) = 4 16-10 >= 3 g(13) = 4 16-13 >= 3 g(14) = 4 16-14 >= 3 NOT g(14) = 5 g(29) = 5 32-? >= 3 g(30) = 6 64-? >= 3 ... g(61) = 6 g(62) = 7 c(62, 7) = c(66, 7) g(63) = 7 new jump at around 2^20 - 20 no formula like log(n+2)-1 can work The minimum value of K is K = g(N), where g(N) is the minimum value such that 2^g(N) - N >= g(g(N)). (you did not state this, but you have fundamentally proved it) c(4, 3) 000 001 yz 010 xy 011 x z 100 x z 101 xy 110 yz 111 tails = editorial heads = wait now solving: Atcoder Grand Contest 44, problem F 000 001 yz 010 xy 011 x z 100 x z 101 xy 110 yz 111 4 of these: 0000 0001 0010 0011 0100 0101 ... */
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int N=100; const int db[6][20]={ {}, {1,2}, {0,0,2}, {0,0,0,4,6}, {0,0,0,0,36,108,220,334,384}, {0,0,0,0,0,976,1001,1001,1001,1001,1001,1001,1001,1001,1001,1001,1001} }; LL n; void into(){ scanf("%lld",&n); } int dp[N+9]; void Get_dp(){ dp[1]=0; for (int i=2;i<N;++i) for (int j=1;2333;++j) if ((1LL<<j)-i>=dp[j]) {dp[i]=j;break;} } int Get_dp(LL n){ if (n<N) return dp[n]; for (int i=1;2333;++i) if ((1LL<<i)-n>=dp[i]) return i; return -1; } int Dfs_ans(LL n,LL m){ if (n<m) swap(n,m); if (m<63&&(1LL<<m)-n<n) return Dfs_ans((1LL<<m)-n,m); if (m>5) return 1001; return m?db[m][n]:1; } int ans; void Get_ans(){ ans=Dfs_ans(n,(LL)Get_dp(n)); ans+=n==4; ans+=(n==7)<<1; } void work(){ Get_dp(); Get_ans(); } void outo(){ printf("%d\n",ans>1000?-1:ans); } int main(){ into(); work(); outo(); return 0; }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include <bits/stdc++.h> namespace IO { char gc() { #ifdef FREAD static char buf[1<<21], *P1 = buf, *P2 = buf; if(P1 == P2) { P1 = buf; P2 = buf + fread(buf, 1, 1<<21, stdin); if(P1 == P2) return EOF; } return *(P1++); #else return getchar(); #endif } template<typename Tp> bool get1(Tp &x) { bool neg = 0; char c = gc(); while( c != EOF && (c < '0' || c > '9') && c != '-' ) c = gc(); if(c == '-') c = gc(), neg = 1; if(c == EOF) return false; x = 0; for(; c>='0' && c<='9'; c = gc()) x = x*10 + c - '0'; if(neg) x = -x; return true; } template<typename Tp> void printendl(Tp x) { if(x<0)putchar('-'),x=-x; static short a[40], sz; sz = 0; while(x>0)a[sz++]=x%10,x/=10; if(sz==0)putchar('0'); for(int i=sz-1; i>=0; i--)putchar('0'+a[i]); puts(""); } } // namespace IO using IO::get1; using IO::printendl; #define get2(x,y) get1(x) && get1(y) #define get3(x,y,z) get2(x,y) && get1(z) #define get4(x,y,z,w) get3(x,y,z) && get1(w) #define pb push_back #define mp std::make_pair #define ff first #define ss second typedef long long LL; typedef unsigned long long uLL; typedef std::pair<int,int> pii; const int inf = 0x3f3f3f3f; const LL Linf = 1ll<<61; int getG(LL x) { static std::map<LL, int> mem; if(mem.find(x) != mem.end()) return mem[x]; if(x == 1) return 0; int ret = 1; while((1ll << ret) - x < getG(ret)) ret++; return mem[x] = ret; } const int tab[50][50] = { {2,}, {1,2,}, {0,1,4,6,}, {0,0,6,36,108,220,334,384,}, {0,0,4,108,976,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,}, }; int calc(int k, LL n) { if(k == 0) return 1; if(k > n) return calc(n, k); if(n > (1ll << (k - 1))) return calc(k, (1ll << k) - n); if(k >= 6) return -1; return tab[k - 1][n - 1]; } LL n; int k; int main() { get1(n); k = getG(n); LL ans = calc(k, n); if(n == 4) ans++; else if(n == 7) ans += 2; printendl(ans); return 0; }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include<bits/stdc++.h> #define For(i,x,y) for (register int i=(x);i<=(y);i++) #define FOR(i,x,y) for (register int i=(x);i<(y);i++) #define Dow(i,x,y) for (register int i=(x);i>=(y);i--) #define Debug(v) for (auto i:v) printf("%lld ",i);puts("") #define mp make_pair #define fi first #define se second #define pb push_back #define ep emplace_back #define siz(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() #define fil(a,b) memset((a),(b),sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pa; typedef pair<ll,ll> PA; typedef vector<int> poly; inline ll read(){ ll x=0,f=1;char c=getchar(); while ((c<'0'||c>'9')&&(c!='-')) c=getchar(); if (c=='-') f=-1,c=getchar(); while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return x*f; } const int ans[6][20] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 4, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 36, 108, 220, 334, 384, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 976, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001}}; int g[110]; inline void init(){ For(i,2,100) FOR(j,1,i) if ((1ll<<j)-i>=g[j]){ g[i]=j;break; } } inline int G(ll n){ if (n<=100) return g[n]; for (int i=1;;i++) if ((1ll<<i)-n>=G(i)) return i; } inline int solve(ll n,ll k){ if (n<k) swap(n,k); if (k<63&&(1ll<<k)-n<n) return solve((1ll<<k)-n,k); if (k>5) return 1001; return ans[k][n]; } int main(){ ll n=read(); init(); int ans=solve(n,G(n))+(n==4)+(n==7)*2; printf("%d\n",ans>1000?-1:ans); }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include <bits/stdc++.h> #define debug(...) fprintf(stderr, __VA_ARGS__) #ifndef AT_HOME #define getchar() IO::myGetchar() #define putchar(x) IO::myPutchar(x) #endif namespace IO { static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23; inline char myGetchar() { static char buf[IN_BUF], *ps = buf, *pt = buf; if (ps == pt) { ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin); } return ps == pt ? EOF : *ps++; } template<typename T> inline bool read(T &x) { bool op = 0; char ch = getchar(); x = 0; for (; !isdigit(ch) && ch != EOF; ch = getchar()) { op ^= (ch == '-'); } if (ch == EOF) { return false; } for (; isdigit(ch); ch = getchar()) { x = x * 10 + (ch ^ '0'); } if (op) { x = -x; } return true; } inline int readStr(char *s) { int n = 0; char ch = getchar(); for (; isspace(ch) && ch != EOF; ch = getchar()) ; for (; !isspace(ch) && ch != EOF; ch = getchar()) { s[n++] = ch; } s[n] = '\0'; return n; } inline void myPutchar(char x) { static char pbuf[OUT_BUF], *pp = pbuf; struct _flusher { ~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); } }; static _flusher outputFlusher; if (pp == pbuf + OUT_BUF) { fwrite(pbuf, 1, OUT_BUF, stdout); pp = pbuf; } *pp++ = x; } template<typename T> inline void print_(T x) { if (x == 0) { putchar('0'); return; } static int num[40]; if (x < 0) { putchar('-'); x = -x; } for (*num = 0; x; x /= 10) { num[++*num] = x % 10; } while (*num){ putchar(num[*num] ^ '0'); --*num; } } template<typename T> inline void print(T x, char ch = '\n') { print_(x); putchar(ch); } inline void printStr_(const char *s, int n = -1) { if (n == -1) { n = strlen(s); } for (int i = 0; i < n; ++i) { putchar(s[i]); } } inline void printStr(const char *s, int n = -1, char ch = '\n') { printStr_(s, n); putchar(ch); } } using namespace IO; const int table[6][20] = { {}, {1, 2}, {0, 0, 2}, {0, 0, 0, 4, 6}, {0, 0, 0, 0, 36, 108, 220, 334, 384}, {0, 0, 0, 0, 0, 976, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001} }; const int N = 105; int G[105]; void init() { G[1] = 0; for (int n = 2; n < N; ++n) { for (int k = 1; ; ++k) { if ((1ll << k) - n >= G[k]) { G[n] = k; break; } } } } int getG(long long n) { if (n < N) { return G[n]; } for (int k = 1; ; ++k) { if ((1ll << k) - n >= G[k]) { return k; } } return -1; } int solve(long long n, long long k) { if (n < k) { std::swap(n, k); } if (k < 63 && (1ll << k) - n < n) { return solve((1ll << k) - n, k); } if (k > 5) { return 1001; } return k == 0 ? 1 : table[k][n]; } int main() { long long n; read(n); init(); int ans = solve(n, getG(n)); ans += n == 4; ans += (n == 7) * 2; if (ans > 1000) { print(-1); } else { print(ans); } }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include <bits/stdc++.h> #define meow(args...) fprintf(stderr, args) typedef unsigned u32; typedef long long s64; typedef unsigned long long u64; template<class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {return b<a?(a=b, true):false;} template<class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {return a<b?(a=b, true):false;} template<class Type> Type read() { Type a; bool b; unsigned char c; while(c=getchar()-48, (c>9)&(c!=253)); for(a=(b=c==253)?0:c; (c=getchar()-48)<=9; a=a*10+c); return b?-a:a; } int (*rd)()=read<int>; //const u32 P=; //inline u32 &inc(u32 &a, u32 b) {return (a+=b)<P?a:(a-=P);} //inline u32 &dec(u32 &a, u32 b) {return (a-=b)&0x80000000?(a+=P):a;} //inline u32 sum(u32 a, u32 b) {return (a+=b)<P?a:a-P;} //inline u32 dif(u32 a, u32 b) {return (a-=b)&0x80000000?a+P:a;} //u64 power(u64 a, int b) { // u64 ans=1; // for(; b; a=a*a%P, b/=2) if(b&1) ans=ans*a%P; // return ans; //} int g[64]; int solve_min(s64 n) { if(n==1) return 0; int k=0; while((1llu<<k)-g[k]<n) ++k; return k; } void gg() { puts("-1"); exit(0); } int n, k, c[13], ans; bool eq; void dfs(int id, int least) { if(id==n) { int hub[5]; for(int i=0; i<k; ++i) hub[i]=i; while(std::next_permutation(hub, hub+k)) { int s[13]={}; for(int i=0; i<n; ++i) { for(int j=0; j<k; ++j) s[i]|=(c[i]>>j&1)<<hub[j]; } if(memcmp(s, c, n*sizeof(int))) { std::sort(s, s+n); if(memcmp(s, c, n*sizeof(int))<=0) return; } else if(!eq) return; } if(ans==1000) gg(); ++ans; return; } int &x=c[id]; for(; least<=(1<<k)-n+id; ++least) { c[id]=least; dfs(id+1, least+1); } } int main() { for(int i=2; i<64; ++i) g[i]=solve_min(i); s64 m=read<s64>(); k=solve_min(m); eq = 1llu<<(k-1)>=m; while(k!=0 && (m<k || m>1llu<<(k-1))) { if(m<k) { n=m; m=k; k=n; } if(m>1llu<<(k-1)) m=(1llu<<k)-m; } if(k>=6 || (k==5&&m>=14)) gg(); n=m; dfs(0, 0); printf("%d\n", ans); return 0; }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include<bits/stdc++.h> #define ll long long #define ld long double #define db double #define pint pair<int,int> #define mk(x,y) make_pair(x,y) #define pb(x) push_back(x) #define eb(x,y) emplace_back(x,y) #define fi first #define se second #define Rep(x,y,z) for(int x=y;x<=z;x++) #define Red(x,y,z) for(int x=y;x>=z;x--) using namespace std; char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny; inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;} //inline char gc(){return getchar();} inline ll read(){ ll x=0;for(ny=1;nc=gc(),(nc<48||nc>57)&&nc!=EOF;)if(nc==45)ny=-1;if(nc<0)return nc; for(x=nc-48;nc=gc(),47<nc&&nc<58&&nc!=EOF;x=(x<<3)+(x<<1)+(nc^48));return x*ny; } const int table[6][20] = {{}, {1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 4, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 36, 108, 220, 334, 384, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 976, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001}}; int G[105]; inline void Init(){G[1]=0;Rep(n,2,100)for(int k=1;k<n;k++)if((1<<k)>=n&&(1<<k)-n>=G[k]){G[n]=k;break;}} inline int g(ll n){ if(n<=100)return G[n]; for(int k=1;;k++)if((1ll<<k)>=n&&(1ll<<k)-n>=g(k)) return k; } ll n; inline int Solve(ll n,ll k){ if(n<k)swap(n,k); if(k<63&&(1ll<<k)-n<n)return Solve((1ll<<k)-n,k); if(k>5)return 1001;return k==0?1:table[k][n]; } int main(){ // freopen("std.in","r",stdin); // freopen("std.out","w",stdout); n=read(),Init(); int ans=Solve(n,g(n));ans+=(n==4)+(n==7)*2; cout<<(ans>1000?-1:ans); return 0; }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; const int inf=2333; inline ll read(){ ll 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<<1)+(x<<3)+c-'0'; c=getchar(); } return x*f; } ll n; int g[233]; void init(){ g[1]=0; for(int i=2;i<=100;++i){ for(int k=1;;++k){ if((1LL<<k)-i>=g[k]){ g[i]=k; break; } } } } const int table[6][20] = {{}, {1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,4,6,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,36,108,220,334,384,0,0,0,0,0,0,0,0}, {0,0,0,0,0,976,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf} }; int Get_g(ll n){ if(n<=100)return g[n]; for(int k=1;;++k){ if((1LL<<k)-n>=g[k])return k; } return 2333333; } int Solve(ll n,ll k){ if(n<k)swap(n,k); if(k<63&&(1LL<<k)-n<n){ return Solve((1LL<<k)-n,k); } if(!k)return 1; if(k<=5)return table[k][n]; return inf; } int main(){ init(); n=read(); int ans=Solve(n,Get_g(n))+(n==4)+(n==7)*2; printf("%d\n",ans>1000?-1:ans); return 0; }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include<cstdio> int ans[14]={0,0,1,1,7,4,1,336,384,334,220,108,36,6}; int main(){ long long N;scanf("%lld",&N); printf("%d\n",N<14?ans[N]:N==27||N==134217723?976:N==28||N==268435451||N==2044?108:N==29||N==536870907?4:N==60||N==1020||N==1152921504606846970ll?220:N==61?1:N==124||N==508?334:N==252?384:N==4092?36:N==8188?6:-1); }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include <bits/stdc++.h> using namespace std; const int table[6][20] = {{}, {1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 4, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 36, 108, 220, 334, 384, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 976, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001}}; #define ll long long vector<int> G (105); void prepare() { G[1] = 0; for (int n = 2; n <= 100; n++) { for (int k = 1; k < n; k++) { if ((1 << k) >= n && ((1 << k) - n) >= G[k]) { G[n] = k; break; } } } } int g(ll n) { if (n <= 100) { return G[n]; } for (int k = 1; ; k++) { if ((1ll << k) >= n && ((1ll << k) - n) >= g(k)) { return k; } } assert(false); return -1; } int solve(ll n, ll k) { if (n < k) { swap(n, k); } if (k < 63 && (1ll << k) - n < n) { return solve((1ll << k) - n, k); } if (k > 5) { return 1001; } return k == 0 ? 1 : table[k][n]; } int main() { ll n; prepare(); scanf("%lld", &n); int ans = solve(n, g(n)); ans += (n == 4); ans += (n == 7) * 2; printf("%d\n", (ans > 1000) ? -1 : ans); return 0; }
CPP
p02674 AtCoder Grand Contest 044 - Name-Preserving Clubs
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones. Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal. Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L). You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1. Constraints * 2 \le N \le 2\cdot10^{18} Input The input is given from Standard Input in the format N Output If ans is the number of configurations with the properties described in the statement, you should print on Standard Output ans If there are more than 1000 such configurations, print -1. Examples Input 2 Output 1 Input 3 Output 1 Input 4 Output 7 Input 13 Output 6 Input 26 Output -1 Input 123456789123456789 Output -1
6
0
#include <bits/stdc++.h> using namespace std; /* namespace BF { const int lb[6] = {0, 1, 2, 3, 4, 5}; const int rb[6] = {0, 1, 2, 4, 8, 16}; int fact[9], perm[9][40320][256]; inline void Init() { fact[0] = fact[1] = 1; for (int n = 2; n <= 8; n++) { fact[n] = fact[n - 1] * n; vector<int> p(n); for (int i = 0; i < n; i++) p[i] = i; for (int c = 0; c < fact[n] - 1; c++) { next_permutation(p.begin(), p.end()); for (int mask = 0; mask < (1 << n); mask++) { int nmask = 0; for (int i = 0; i < n; i++) if (mask >> i & 1) nmask |= (1 << p[i]); perm[n][c][mask] = nmask; } } } } vector<int> stk; inline bool check(int k) { vector<int> vis(1 << k, 0); for (int x : stk) vis[x] = 1; for (int i = 0; i < fact[k] - 1; i++) { bool flag = 1; for (int x : stk) flag &= vis[perm[k][i][x]]; if (flag) return false; } return true; } int res; void go(int k, int rem, int lst) { if (!rem) { res += check(k); return; } for (int i = lst + 1; i < (1 << k) && res / fact[k] <= 1000; i++) { stk.push_back(i); go(k, rem - 1, i); stk.pop_back(); } } inline void Solve() { for (int k = 0; k <= 5; k++) { for (int n = lb[k]; n <= rb[k]; n++) { res = 0; go(k, n, -1); if (res / fact[k] > 1000) res = -1; else res /= fact[k]; printf("%d %d : %d\n", k, n, res); } } } inline bool check2(int k) { vector<int> vis(1 << k, 0); for (int x : stk) vis[x]++; for (int i = 0; i < fact[k] - 1; i++) { vector<int> tvis(1 << k, 0); for (int x : stk) tvis[perm[k][i][x]]++; int flag = 1; for (int j = 0; j < (1 << k); j++) if (vis[j] != tvis[j]) { flag = 0; break; } if (flag) return false; } return true; } void go2(int k, int rem, int lst, int typ) { if (!rem) { if (typ == 1) { for (int i = 0; i < (int)stk.size(); i++) { stk.push_back(stk[i]); res += check2(k); stk.pop_back(); } } else { stk.push_back(stk[0]); stk.push_back(stk[1]); res += check2(k); stk.pop_back(); stk.pop_back(); stk.push_back(stk[0]); stk.push_back(stk[0]); res += check2(k); stk.pop_back(); stk.pop_back(); stk.push_back(stk[1]); stk.push_back(stk[1]); res += check2(k); stk.pop_back(); stk.pop_back(); } return; } for (int i = lst + 1; i < (1 << k); i++) { stk.push_back(i); go2(k, rem - 1, i, typ); stk.pop_back(); } } inline void Solve2() { // res = 0; // go2(7, 3, -1, 1); // go2(7, 2, -1, 2); // printf("7 : %d\n", res / fact[7]); // res = 0; // go2(8, 3, -1, 1); // go2(8, 2, -1, 2); // printf("8 : %d\n", res / fact[8]); } } */ const int ans[6][17] = { {1}, {0, 2}, {0, 0, 2}, {0, 0, 0, 4, 6}, {0, 0, 0, 0, 36, 108, 220, 334, 384}, {0, 0, 0, 0, 0, 976, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} }; int solve(long long k, long long n) { if (k > n) swap(k, n); if (n > (1ll << k) - n) return solve(k, (1ll << k) - n); if (k > 5) return -1; return ans[k][n]; } int G(long long n) { if (n == 1) return 0; for (int i = 0; i <= n; i++) if ((1ll << i) >= n && (1ll << i) - G(i) >= n) return i; return -1; } int main() { // BF::Init(); // BF::Solve(); // BF::Solve2(); long long n; cin >> n; int ans = solve(G(n), n); if (n == 4) ans++; if (n == 7) ans += 2; cout << ans << endl; return 0; }
CPP