跳到正文

个人记录

8.16训练总结

hter约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月23日
篇幅
约 5 分钟

版本

  1. v1当前

T1 转变

ac

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,k;
int a[1005];
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>a[i];
	int ans=0;
	for(int i=1;i<=k;i++){
		ans++;
		a[i]=0;
		a[i+n]=0;
	}
	int c=0;
	for(int i=ans+1;i<=1005;i++){
		if(c==n)return 0;
		cout<<a[i]<<" ";
		c++;
	}


	return 0;
}

T2 真实社交 考试是没拿分,因为私人大小写,不然能拿50分 满分怎么拿:因为100%的数据范围n的范围在1e9,所以得用map

正确代码

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=3e4+2;
ll n,q;
ll op,x,y;
map<pair<int,int>,bool>mp;
int main()
{
	cin>>n>>q;
	for(ll i=1;i<=q;i++){
		cin>>op>>x>>y;
		if(op==1){
			mp[{x,y}]=1;
		}
		if(op==2){
			mp[{x,y}]=0;
		}
		if(op==3){
			if(mp[{x,y}] and mp[{y,x}]) cout<<"Yes\n";
			else cout<<"No\n";
		}
	}
	return 0;
}

T3 落落的去维护数组  ̄へ ̄

比赛时拿了74分的暴力分 正确思路:因为100%的数据太大了,所以要用last_k(用来存最后一次操作1的执行k)和last_time(用来存最后一次操作1的执行k)

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int n,q;
ll a[N];
int t[N];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	cin>>q;
	int last_time=0;
	int last_k=0;

	for(int i=1;i<=q;i++){
		int op,k,x;
		cin>>op;
		if(op==1){
			cin>>k;
			last_time=i;
			last_k=k;
		}
		if(op==2){
			cin>>x>>k;
			if(last_time==0)a[x]+=k;
			else if(t[x]>last_time)a[x]+=k;
			else a[x]=k;
			t[x]=i;
		}
		if(op==3){
			int x;
			cin>>x;
			if(last_time==0)cout<<a[x]<<"\n";
			else if(t[x]>last_time)cout<<last_k+a[x]<<"\n";
			else cout<<last_k<<"\n";
		}
	}


	return 0;
}

T4 交叉区间

考试依然只拿了30暴力分

暴力就是直接看有没有重叠的,因为Li到Ri有1e9,你猜炸不炸

正解要排序加二分,找有多少个区间的左端点大于a[i].r和有多少个右端点小于a[i].l,再找区间

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int l[5000009],r[5000009],a[5000009],b[5000009];
signed main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>l[i]>>r[i];
        a[i]=l[i];
        b[i]=r[i];
    }
    sort(a+1,a+n+1);
    sort(b+1,b+n+1);
    int ans = 0;
    for (int i=1;i<=n;i++)
    {
        int pos1=upper_bound(a+1,a+n+1,r[i])-a;
        int pos2=lower_bound(b+1,b+n+1,l[i])-b;
        ans+=n-pos1+pos2;
    }
    cout<<n*(n-1)/2-ans/2;
    return 0;
}

au5牛逼

原文