C++ CSP-J 2022 星战 题解

#include <bits/stdc++.h>
using namespace std;
inline int read()
{
    long long x=0;
    bool flag=true;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch =='-') flag=false;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    if(flag) return x;
    return ~(x - 1);
}
const int maxn =(int)5e5+5;
long long r[maxn],w[maxn],g[maxn];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    long long n=read(),m=read();
    std :: mt19937 rng(time(0));
    for (long long u=1; u<=n; u++) w[u]=rng();
    long long tar=std::accumulate(w+1, w+n+1,0LL);
    long long now=0;
    while (m--)
    {
        long long u= read(), v = read();
        r[v]+=w[u];
        g[v]=r[v];
        now+=w[u];
    }
    int q=read();
    while(q--)
    {
        long long t=read();
        if(t==1)
        {
            long long u=read(),v=read();
            r[v]-=w[u];
            now-=w[u];
        }
        else if(t==2)
        {
            long long v=read();
            now-=r[v];
            r[v]=0;
        }
        else if(t==3)
        {
            long long u=read(), v=read();
            r[v]+=w[u];
            now+=w[u];
        }
        else if(t==4)
        {
            long long v=read();
            now+=g[v]-r[v];
            r[v]=g[v];
        }
        if(now==tar) cout<<"YES"<<'\n';
        else cout<<"NO"<<'\n';
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
inline int read()
{
	long long x=0;
	bool flag=true;
	char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch =='-') flag=false;
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	if(flag) return x;
	return ~(x - 1);
}
const int maxn =(int)5e5+5;
long long r[maxn],w[maxn],g[maxn];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	long long n=read(),m=read();
	std :: mt19937 rng(time(0));
	for (long long u=1; u<=n; u++) w[u]=rng();
	long long tar=std::accumulate(w+1, w+n+1,0LL);
	long long now=0;
	while (m--)
	{
		long long u= read(), v = read();
		r[v]+=w[u];
		g[v]=r[v];
		now+=w[u];
	}
	int q=read();
	while(q--)
	{
		long long t=read();
		if(t==1)
		{
			long long u=read(),v=read();
			r[v]-=w[u];
			now-=w[u];
		}
		else if(t==2)
		{
			long long v=read();
			now-=r[v];
			r[v]=0;
		}
		else if(t==3)
		{
			long long u=read(), v=read();
			r[v]+=w[u];
			now+=w[u];
		}
		else if(t==4)
		{
			long long v=read();
			now+=g[v]-r[v];
			r[v]=g[v];
		}
		if(now==tar) cout<<"YES"<<'\n';
		else cout<<"NO"<<'\n';
	}
	return 0;
}

请求三连

请求三连

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值