题解 | #小红的区间查询#
小红的区间查询
https://www.nowcoder.com/practice/cbaecf16197a4136b8a593a7a270f4ab
数据范围较小,按照题意模拟即可。
c++代码:
#include <iostream>
using namespace std;
const int N=1010;
int a[N];
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,q,i,p,x,y,cnt;
cin>>n>>q;
for(i=1;i<=n;++i)
cin>>a[i];
while(q--)
{
cin>>p>>x>>y;
if(p==1)
a[x]=y;
else
{
cnt=0;
for(i=1;i<=x;++i)
if(a[i]==y)
++cnt;
cout<<cnt<<'\n';
}
}
return 0;
}
python代码:
n,q=map(int,input().split())
a=[0]
a.extend(list(map(int,input().split())))
for _ in range(0,q):
p,x,y=map(int,input().split())
if(p==1):
a[x]=y;
else:
cnt=0
for i in range(1,x+1):
if(a[i]==y):
cnt+=1
print(cnt)
java代码:
import java.util.Scanner;
public class ZT15 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(),q=sc.nextInt(),i,p,x,y,cnt;
int[] a=new int[n+5];
for(i=1;i<=n;i++)
a[i]=sc.nextInt();
while(q-->0){
p=sc.nextInt();
x=sc.nextInt();
y=sc.nextInt();
if(p==1)
a[x]=y;
else{
cnt=0;
for(i=1;i<=x;++i)
if(a[i]==y)
cnt++;
System.out.println(cnt);
}
}
}
}