C
题意:给定一些人的关系,询问这两个人是否互相关注。
直接按照题意模拟,由于n较大,因此要用map来存储关系。
code:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define x first
#define y second
const int N=3e5+10,mod=998244353;
const double eps=1e-4;
typedef pair<int,int> pii;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
void work()
{
map<pii,int> mp;
int n,q;
cin>>n>>q;
while(q--)
{
int op,x,y;
cin>>op>>x>>y;
if(op==1){
mp[{x,y}]=1;
}
else if(op==2){
mp[{x,y}]=0;
}
else {
if(mp[{x,y}]&&mp[{y,x}]) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
}
signed main()
{
ios;
int t;
//cin>>t;
t=1;
while(t--)
{
work();
}
return 0;
}
D
题意:给定序列A,有三种询问:
- 把A中所有数变为x
- 给 i 处的元素 a i a_i ai加上x
- 输出 a i a_i ai的值
很明显,需要一个数据结构来维护这个序列的值,很明显可以使用线段树来维护。
事实上,根本不需要线段树这种又臭又长的数据结构,我们可以记录当前序列要变成的值,也就是操作1中的x,并不需要实际把A都变长x,再用set来记录一下哪个位置的值被改动过即可。
code:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define x first
#define y second
const int N=2e5+10;
const double eps=1e-4;
typedef pair<int,int> pii;
int a[N];
void work()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int q;
cin>>q;
int now=-1;
set<int> st;
while(q--)
{
int op,i,x;
cin>>op;
if(op==1){
cin>>x;
now=x;
st.clear();
}
else if(op==2){
cin>>i>>x;
if(st.count(i)) {
a[i]=a[i]+x;
}
else if(now==-1)a[i]=a[i]+x;
else a[i]=now+x;
st.insert(i);
}
else {
cin>>i;
if(st.count(i)||now==-1) cout<<a[i]<<endl;
else cout<<now<<endl;
}
}
}
signed main()
{
ios;
int t;
//cin>>t;
t=1;
while(t--)
{
work();
}
return 0;
}
E
题意:有张n*m的网格,每个格子有值
a
i
,
j
a_{i,j}
ai,j,然后把一个矩阵涂黑,问没有被涂黑的数字共有几种。
很容易想到暴力做法,但是时间复杂度为 n 4 n^4 n4,必然超时,因此可以使用前缀和来优化,n为300,可以暴力的开个数组f[N][N][N],表示数字 x 在 i , j 这个矩阵中出现了几次。
这样我们就可以判断每个数字在任意矩阵中出现的次数,若其出现总的次数等于在被涂黑矩阵中出现次数,说明这个数字对答案无贡献。
code:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define x first
#define y second
const int N=3e2+10,mod=998244353;
const double eps=1e-4;
typedef pair<int,int> pii;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int a[N][N];
int s[N][N][N];
void work()
{
int n,m,x;
cin>>n>>m>>x;
int l,r;
cin>>l>>r;
//set<int> st;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
//st.insert(a[i][j]);
}
}
for(int k=1;k<=x;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
s[k][i][j]=s[k][i-1][j]+s[k][i][j-1]-s[k][i-1][j-1]+(a[i][j]==k);
}
}
}
int ans;
for(int i=1;i<=n-l+1;i++){
for(int j=1;j<=m-r+1;j++){
ans=x;
int i1=i+l-1,j1=j+r-1;
for(int k=1;k<=x;k++){
if(s[k][i1][j1]-s[k][i1][j-1]-s[k][i-1][j1]+s[k][i-1][j-1]==s[k][n][m]) ans--;
}
cout<<ans<<" ";
}
cout<<endl;
}
}
signed main()
{
ios;
int t;
//cin>>t;
t=1;
while(t--)
{
work();
}
return 0;
}