import java.util.*;
public class Main{
static int N=100010;
static int a[]=new int[N];
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int q=sc.nextInt();
for(int i=0;i<n;i++)a[i]=sc.nextInt();
while(q-->0){
int x=sc.nextInt();
int l=0,r=n-1;
while(l<r){
int mid=l+r>>1;
if(a[mid]>=x)r=mid;
else l=mid+1;
}
if(x!=a[l])System.out.println("-1 -1");
else {
System.out.print(l+" ");
l=0;
r=n-1;
while(l<r){
int mid=l+r+1>>1;
if(a[mid]<=x)l=mid;
else r=mid-1;
}
System.out.println(l);
}
}
}
}
谨记:无论是差分/前缀和,下标均从1开始,防止下标越界
import java.util.*;
public class Main{
static int N=100010;
static int a[]=new int[N];
static int s[]=new int[N];
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=1;i<=n;i++)s[i]=sc.nextInt();
for(int i=1;i<=n;i++){
s[i]+=s[i-1];
}
while(m-->0){
int l=sc.nextInt();
int r=sc.nextInt();
System.out.println(s[r]-s[l-1]);
}
}
}
import java.util.*;
public class Main{
static int N=1000010;
static int a[]=new int[N];
static int s[]=new int[N];
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=1;i<=n;i++)a[i]=sc.nextInt();
for(int i=1;i<=n;i++){
s[i]=s[i-1]+a[i];
}
while(m-->0){
int l=sc.nextInt();
int r=sc.nextInt();
System.out.println(s[r]-s[l-1]);
}
}
}
步骤:
(1)先输入数据
(2)预处理前缀和
s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
(3)求出区域内的数的和
s[x2][y2]-s[x2][y1-1]-s[x1-1][y2]+s[x1-1][y1-1];
import java.util.*;
public class Main{
static int N=1010;
static int a[][]=new int[N][N];
static int s[][]=new int[N][N];
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int q=sc.nextInt();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a[i][j]=sc.nextInt();
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
}
}
while(q-->0){
int x1=sc.nextInt();
int y1=sc.nextInt();
int x2=sc.nextInt();
int y2=sc.nextInt();
System.out.println(s[x2][y2]-s[x2][y1-1]-s[x1-1][y2]+s[x1-1][y1-1]);
}
}
}
作用:使得区间/区域内的数加上1/C
步骤:
(1)输入数据
(2)初始化,先自己插自己
(3)处理询问即差分
差分数组:b[],前缀和数组:a[]
(4)再求一遍前缀和
(5)输出前缀和数组的a[]即可
import java.util.*;
public class Main{
static int N=100010;
static int a[]=new int[N];
static int s[]=new int[N];
static int b[]=new int[N];
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=1;i<=n;i++)a[i]=sc.nextInt();
for(int i=1;i<=n;i++){
insert(i,i,a[i]);
}
while(m-->0){
int l=sc.nextInt();
int r=sc.nextInt();
int c=sc.nextInt();
insert(l,r,c);
}
for(int i=1;i<=n;i++)a[i]=a[i-1]+b[i];
for(int i=1;i<=n;i++){
System.out.print(a[i]+" ");
}
}
public static void insert(int l,int r,int c){
b[l]+=c;
b[r+1]-=c;
}
}
差分矩阵
import java.util.*;
public class Main{
static int N=1010;
static int a[][]=new int[N][N];
static int b[][]=new int[N][N];
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int q=sc.nextInt();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a[i][j]=sc.nextInt();
insert(i,j,i,j,a[i][j]);
}
}
while(q-->0){
int x1=sc.nextInt();
int y1=sc.nextInt();
int x2=sc.nextInt();
int y2=sc.nextInt();
int c=sc.nextInt();
insert(x1,y1,x2,y2,c);
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1]+b[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void insert(int x1,int y1,int x2,int y2,int c){
b[x1][y1]+=c;
b[x2+1][y1]-=c;
b[x1][y2+1]-=c;
b[x2+1][y2+1]+=c;
}
}
import java.util.*;
public class Main{
static int N=100010;
static int p[]=new int[N];
public static int find(int x){
if(x!=p[x])p[x]=find(p[x]);
return p[x];
}
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=1;i<=n;i++)p[i]=i;
while(m-->0){
String s=sc.next();
if(s.equals("M")){
int a=sc.nextInt();
int b=sc.nextInt();
if(find(a)!=find(b)){
p[find(a)]=find(b);
}
}
else{
int a=sc.nextInt();
int b=sc.nextInt();
if(find(a)==find(b))System.out.println("Yes");
else System.out.println("No");
}
}
}
}
返回的a即是最大公约数
import java.util.*;
public class Main{
public static int gcd(int a,int b){
while(b!=0){
int c=a%b;
a=b;
b=c;
}
return a;
}
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
while(n-->0){
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println(gcd(a,b));
}
}
}
找出是i的倍数的数都筛掉,用st[]数组维护!
.import java.util.*;
public class Main{
static int N=1000010;
static int primes[]=new int[N];
static boolean st[]=new boolean[N];
static int cnt;
public static int get_primes(int x){
for(int i=2;i<=x;i++){
if(!st[i]){
primes[cnt++]=i;
for(int j=i;j<=x;j+=i){
st[j]=true;
}
}
}
return cnt;
}
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println(get_primes(n));
}
}
import java.util.*;
public class Main{
static int N=1010;
static int v[]=new int[N];
static int w[]=new int[N];
static int f[][]=new int[N][N];
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=1;i<=n;i++){
v[i]=sc.nextInt();
w[i]=sc.nextInt();
}
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
f[i][j]=f[i-1][j];
if(j>=v[i])f[i][j]=Math.max(f[i][j],f[i-1][j-v[i]]+w[i]);
}
}
System.out.println(f[n][m]);
}
}
import java.util.*;
public class Main{
static int N=1010;
static int f[][]=new int[N][N];
static int w[]=new int[N];
static int v[]=new int[N];
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=1;i<=n;i++){
v[i]=sc.nextInt();
w[i]=sc.nextInt();
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
f[i][j]=f[i-1][j];
if(j>=v[i])f[i][j]=Math.max(f[i][j],f[i][j-v[i]]+w[i]);
}
}
System.out.println(f[n][m]);
}
}