QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#468727#1944. Circle of FriendsinksamuraiAC ✓358ms213408kbC++236.4kb2024-07-08 23:45:122024-07-08 23:45:12

Judging History

你现在查看的是最新测评结果

  • [2024-07-08 23:45:12]
  • 评测
  • 测评结果:AC
  • 用时:358ms
  • 内存:213408kb
  • [2024-07-08 23:45:12]
  • 提交

answer

#include <bits/stdc++.h>

// cut here
#ifdef _MSC_VER
#include <intrin.h>
#endif
 
namespace atcoder {
 
namespace internal {
 
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}
 
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}
 
}  // namespace internal
 
}  // namespace atcoder
 
 
namespace atcoder {
 
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }
 
    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }
 
    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }
 
    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;
 
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }
 
    S all_prod() { return d[1]; }
 
    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }
 
    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }
 
  private:
    int _n, size, log;
    std::vector<S> d;
 
    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};
 
}  // namespace atcoder
// cut here 

#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int) a.size()
#define all(a) a.begin(),a.end()
#define vec(...) vector<__VA_ARGS__>
#define _3zlqvu8 ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

// snuke's mod int
template <ll mod>
struct modint{
	ll x;
	modint(ll x=0):x((x%mod+mod)%mod){}
	modint operator-()const{return modint(-x);}
	modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
	modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
	modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
	modint operator+(const modint a)const{modint res(*this); return res+=a;}
	modint operator-(const modint a)const{modint res(*this); return res-=a;}
	modint operator*(const modint a)const{modint res(*this); return res*=a;}
	modint pow(ll n)const{
		modint res=1,x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modint inv()const{return pow(mod-2);}
};

using mint=modint<998244353>;

using vm=vec(mint);

int op(int l,int r){
	return l&r;
}

int e(){
	return (1ll<<60)-1;
}

void slv(){
	int n;
	cin>>n;

	vi a(n);
	rep(i,n){
		cin>>a[i];
	}

	vi tmp={0};
	int x=a[0];
	rep(i,n){
		x&=a[i];
		tmp.pb(x);
	}
	sort(all(tmp)); tmp.erase(unique(all(tmp)),tmp.end());
	const int si=sz(tmp);

	atcoder::segtree<int,op,e> seg(n);
	rep(i,n){
		seg.set(i,a[i]);
	}

	// dp(i, j) = {denote number of divisions such that last block & tmp[j] > 0}
	vec(vm) dp(n,vm(si));
	vec(vm) sf(n,vm(si));
	auto range_sum=[&](int i,int l,int r)->mint{
		return sf[l][i]-(r+1>=n?0:sf[r+1][i]);
	};
	per(i,n){
		// axali subarray avige
		// bolomde
		{
			int now=seg.prod(i,n);
			rep(j,si){
				if((tmp[j]&now)>0){
					dp[i][j]+=1;
				}
			}
			if(now){
				dp[i][0]+=1;
			}
		}
		if(i<n-1){
			int l=i,r=n-2;
			int opt=i;
			while(l<=r){
				int m=(l+r)/2;
				if(seg.prod(i,m+1)>0){
					opt=m,l=m+1;
				}else{
					r=m-1;
				}
			}
			rep(k,si){
				// if(i==n-2 and k==0) print(sf[i+1][0].x,range_sum(k,i+1,opt+1).x);
				dp[i][k]+=range_sum(k,i+1,opt+1);
			}
		}
		rep(k,si){
			if(i+1<n) sf[i][k]+=sf[i+1][k];
			sf[i][k]+=dp[i][k];
		}
		// {
		// 	int now=a[i];
		// 	rng(j,i,n-1){
		// 		now&=a[j];
		// 		if(!now) break;
		// 		rep(k,si){
		// 			dp[i][k]+=dp[j+1][k];
		// 		}
		// 	}
		// }
		// print(dp[i][0].x);
	}

	mint ans=dp[0][0];
	int now=a[0];
	rep(i,n-1){
		now&=a[i];
		if(!now) break;
		int id=lower_bound(all(tmp),now)-tmp.begin();
		// print(i,id,dp[i+1][id].x);
		ans+=dp[i+1][id];
	}
	now=a[0];
	rep(i,n) now&=a[i];
	if(now>0) ans-=(n-1);
	cout<<ans.x<<"\n";
}

signed main(){
_3zlqvu8;
	slv();
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3660kb

input:

1
1

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

1
1152921504606846975

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 251ms
memory: 32128kb

input:

200000
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1...

output:

792053081

result:

ok single line: '792053081'

Test #4:

score: 0
Accepted
time: 286ms
memory: 32352kb

input:

200000
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606...

output:

792053081

result:

ok single line: '792053081'

Test #5:

score: 0
Accepted
time: 185ms
memory: 32324kb

input:

200000
18031990763159554
108095195748368386
36029346783428610
184085739649376272
577639437895209218
72057594042191904
4508276314083330
9078671805521920
144255925580990466
432490704060547904
72059793094738017
562967269607456
26424786302976
68719476738
333275855713337352
11370333479109156
290271086772...

output:

697849429

result:

ok single line: '697849429'

Test #6:

score: 0
Accepted
time: 178ms
memory: 38644kb

input:

200000
38878877261704467
606583538776039621
325557318447172624
513696235393139082
288309595180245073
22000404187620950
28465258695911754
432534927738538528
217932069505099992
8444275091048961
378456596858036610
909745820721741897
10707049231951490
869769773255361289
1054197601224687632
2974146034978...

output:

472024961

result:

ok single line: '472024961'

Test #7:

score: 0
Accepted
time: 191ms
memory: 44776kb

input:

200000
231835759121035702
510237390901680560
1089228954142887951
546561500990948259
452812301317437416
674492659073810407
610376405449845423
336095507987037999
1104321677521773827
257064906190991194
551711390461204674
320506912224540602
57990152440076744
845010071877357771
403229887439671683
2693178...

output:

117345155

result:

ok single line: '117345155'

Test #8:

score: 0
Accepted
time: 207ms
memory: 57160kb

input:

200000
1041523633558056719
1133745259996491747
395753651864850428
1151926721450571413
1114422491360386046
1152921229520204786
1116662492370558395
1006273041715269578
86101463265049534
359143197109542363
242065648916576479
903956062749589338
1091893347132366767
670735077154993663
1142330444352434159
...

output:

648136624

result:

ok single line: '648136624'

Test #9:

score: 0
Accepted
time: 246ms
memory: 94664kb

input:

200000
1148136361283288575
864128178497519103
504403123704430591
1080722898202656703
1150660907089100671
1148417885111055871
1133781167519004367
1152921229712162815
1152902245931548635
1152846703454314239
1152358485925492735
1134766329820016511
1080863910560530363
1071715973758713727
468354552857362...

output:

21378100

result:

ok single line: '21378100'

Test #10:

score: 0
Accepted
time: 302ms
memory: 182224kb

input:

200000
576460752303419383
1150669704793159679
1152921504606846847
1152921504598458367
1143914030474199039
1152921504606846975
1152921504606846967
1152921504590069759
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152640029630136319
11529215046068...

output:

557285638

result:

ok single line: '557285638'

Test #11:

score: 0
Accepted
time: 334ms
memory: 207352kb

input:

200000
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606...

output:

438039103

result:

ok single line: '438039103'

Test #12:

score: 0
Accepted
time: 347ms
memory: 213380kb

input:

200000
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606...

output:

440098984

result:

ok single line: '440098984'

Test #13:

score: 0
Accepted
time: 358ms
memory: 213408kb

input:

200000
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606...

output:

413438309

result:

ok single line: '413438309'