0
点赞
收藏
分享

微信扫一扫

牛客练习赛52


题号

标题

已通过代码

题解

讨论

通过率

我的状态

A

数数

点击查看

进入题解

进入讨论

281/1531

 通过

B

Galahad

点击查看

进入题解

进入讨论

144/799

 通过

C

烹饪

点击查看

进入题解

进入讨论

50/184

未通过

D

粉丝群

点击查看

进入题解

进入讨论

23/69

未通过

E

魔法少女

点击查看

进入题解

进入讨论

8/16

未通过

F

发传单

点击查看

进入题解

进入讨论

2/14

未通过

 

牛客练习赛52_#include

签到题

牛客练习赛52_i++_02

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD =  998244353;
const int N = 500000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

LL a[N],f[N],x[N];
LL n;
int main() {
	int t;
	scanf("%d",&t);
	while(t--)
	{
		 scanf("%lld",&n);
		  LL ans1=0;
		  LL sum=0;
		 
	     sum=((n+1)*n/2)%MOD;
		
		 for(int i=1;i<=n;i++)
		 {
		 	ans1=(ans1%MOD+((i%MOD)*(sum%MOD)%MOD))%MOD;
		 }
		 
		
		 LL sum1=1LL;
		 for(int i=1;i<=n;i++)
		 {
		 	 sum1=((sum1%MOD)*(i%MOD))%MOD;
		 }
		  LL ans2=1LL;
		 ans2=quickModPow(sum1,n+n,MOD);
		
		
		 
		 cout<<ans1<<" "<<ans2<<endl;
		 
		/* for(int i=1;i<=n;i++)
		 {
		 	for(int j=1;j<=n;j++)
			{
				cout<<i<<" "<<j<<"     ";
			}
			cout<<endl;
		 } */
	}
	
    return 0;
}

链接:https://ac.nowcoder.com/acm/contest/1084/B 来源:牛客网
 

牛客练习赛52_i++_03

 

离线树状数组的经典应用(求不同数的区间和)

牛客练习赛52_i++_04

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=500000+5;//最大元素个数
int n,m;
int last[MAXN],nxt[MAXN];
ll a[MAXN];

//树状数组只能计算A[1]开始的和,A[0]这个元素是不能用的。

ll c[MAXN];//c[i]==A[i]+A[i-1]+...+A[i-lowbit(i)+1]
//返回i的二进制最右边1的值
int lowbit(int i)
{
    return i&(-i);
}
//返回A[1]+...A[i]的和
ll sum(int x)
{
    ll sum = 0;
    while(x)
    {
        sum += c[x];
        x -= lowbit(x);
    }
    return sum;
}

//令A[i] += val

void add(int x, ll val)
{
    while(x <=n)   //注意这里的n,是树状数组维护的长度
    {
        c[x] += val;
        x += lowbit(x);
    }
}
ll ans[MAXN];
struct Query
{
    int l,r,id;
} q[MAXN];
bool cmp(Query a,Query b)
{
    if(a.l==b.l)
        return a.r<b.r;
    return a.l<b.l;
}
int main()
{

    scanf("%d%d",&n,&m);

    memset(c,0,sizeof(c));
    memset(last,0,sizeof(last));
    memset(nxt,0,sizeof(nxt));

    for(int i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
    }


    for(int i=1; i<=n; i++)
    {
        int val=a[i];
       
        if(last[val]==0)
            add(i,a[i]);
        else
            nxt[last[val]]=i;
        last[val]=i;
    }

    for(int i=1; i<=m; i++)
    {
        scanf("%d%d",&q[i].l,&q[i].r);
        q[i].id=i;
    }
    sort(q+1,q+1+m,cmp);
    int l=1;
    for(int i=1; i<=n; i++)
        ans[i]=0;

    for(int i=1; i<=m; i++)
    {
        for(; l<q[i].l; l++)
        {
            add(l,-a[l]);
            if(nxt[l])
                add(nxt[l],a[l]);
        }
        ans[q[i].id]+=sum(q[i].r);
    }

    for(int i=1; i<=m; i++)
        printf("%lld\n",ans[i]);

    return 0;
}

牛客练习赛52_#include_05

 

#include <cstdio>
#include <algorithm>
#define lson rt<<1, l , mid
#define rson rt<<1|1, mid+1 , r
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 5e5+6;
int n, q;
int pos[maxn<<2], pre[maxn], tree[maxn<<2], ans[maxn];
struct node{
    int l,r,id;
    bool operator<(const node x)const{
        return r<x.r;
    }
}Q[maxn];
void push_up(int rt){
    tree[rt] = min(tree[rt<<1], tree[rt<<1|1]);
}
void update(int rt,int l,int r,int pos,int val){
    if(l==r){
        tree[rt] = min(tree[rt], val);
        return;
    }
    int mid = (l+r)>>1;
    if(pos<=mid) update(lson, pos, val);
    else update(rson, pos, val);
    push_up(rt);
}
int query(int rt, int l, int r, int ul, int ur){
    if(ul<=l&&r<=ur) return tree[rt];
    int mid = (l+r)>>1, ans = INF;
    if(ur<=mid) return query(lson, ul, ur);
    else if(ul>mid) return query(rson, ul, ur);
    else return min(query(lson, ul, ur),query(rson, ul, ur));
}
/*这种写法也可以,但是就我以前做过的一道题目来说上面那种更快(不知道为啥,还有待考证)
int query(int rt, int l, int r, int ul, int ur){
    if(ul<=l&&r<=ur) return tree[rt];
    int mid = (l+r)>>1, ans = INF;
    if(ul<=mid) ans = min(ans, query(lson, ul, ur));
    if(ur>mid) ans = min(ans, query(rson, ul, ur));
    return ans;
}
*/
int main(){
    scanf("%d%d", &n, &q);
    for(int i = 1;i <= 4*500000; i++){
        pos[i] = -1;
        tree[i] = INF;
    }
    int sum = 0; pos[0] = 0;
    for(int i = 1,x; i <= n; i++){
        scanf("%d",&x);
        sum ^= x;
        if(pos[sum]!=-1)
            pre[i]=pos[sum]+1;
        else
            pre[i]=-1;
        pos[sum]=i;
    }
    for(int i = 1; i <= q; i++){
        scanf("%d%d", &Q[i].l, &Q[i].r);
        Q[i].id = i;
    }
    sort(Q+1, Q+1+q);
    int cnt = 1;
    
    for(int i=1;i<=n;i++)
    {
        if(pre[i]!=-1)
        {
            update(1,1,n,pre[i],i-pre[i]+1);
        }
        while(i==Q[cnt].r)
        {
            ans[Q[cnt].id]=query(1,1,n,Q[cnt].l,Q[cnt].r);
            cnt++;
        }
    }
   
    for(int i = 1; i <= q; i++){
        printf("%d\n", ans[i]==INF?-1:ans[i]);
    }
    return 0;
}

举报

相关推荐

0 条评论