问deque和queue
查看原帖
问deque和queue
678625
shicj楼主2024/11/23 22:18

为什么用queue实现会错,区别在那?

Data

1
16
2 1 10 1 5 5 1 9 2 4 4 4 1 8 3 5
2 3 22 23 28 33 34 55 57 64 158 543 544 4129 12322 20517 

用queue:

2 3 22 23 28 33 34 55 57 61<-- 158 543 544 4129 12322 20517 

Code

(参考了第一篇题解的思路)

#include<bits/stdc++.h>
//#define int long long
#define ll long long
using namespace std;
const int mod=1e9+7;
struct node{
	int i,p;
};
ll n,ans;
ll a[200005],s[200005],c[200005];
deque<node>q;
ll mpw(ll a,ll x){
    ll res=1,tmp=a;
    while(x){
        if(x&1)res=res*tmp%mod;
        tmp=tmp*tmp%mod,x>>=1;
    }
    return res;
}
int pow2(int x){
	return mpw(2,x);
}
void solve(){
	ans=0;
	for(int i=1;i<=n;i++){
		a[i]=s[i]=c[i]=0;
	}
	q.clear();
	// cin>>n;
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		// cin>>a[i];
		scanf("%lld",&a[i]);
		// s[i]=sols(a[i]);
		// c[i]=c[i-1]+cnt2(a[i]);
		// int tot=cnt2(a[i]);
		s[i]=a[i];
		int tot=0;
		while(!(s[i]&1)){
			s[i]>>=1;
			tot++;
		}
		c[i]=c[i-1]+tot;
		while(q.size()&&log2(a[i])+c[i-1]>=log2(a[q.back().i])+c[q.back().i-1]){
			node now=q.back();
			q.pop_back();
			ans=(ans+mod-(pow2(now.p)-1)*s[now.i]%mod)%mod;
			tot+=now.p;
		}
		q.push_back({i,tot});
		ans=(ans+pow2(tot)*s[i]%mod)%mod;
		// cout<<ans<<" ";
		printf("%lld ",ans);
	}
	// cout<<endl;
	printf("\n");
}
signed main(){
#ifdef USE_FILE_IO
	freopen("code.in","r",stdin);
	freopen("code.out","w",stdout);
#endif
	int T;
	cin>>T;
	while(T--){
		solve();
	}
	return 0;
}
2024/11/23 22:18
加载中...