代码:
import java.io.*;
import java.util.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n;
static int N=100010;
static int a[]=new int[N];
public static void quick_sort(int a[],int l,int r){
if(l>=r) return;
int x=a[l+r>>1],i=l-1,j=r+1;
while (ix);
if(i
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n,k;
static int N=100010;
static int a[]=new int[N];
public static void quick_sort(int a[],int l,int r){
if(l>=r) return;
int x=a[l+r>>1],i=l-1,j=r+1;
while (ix);
if(i
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n,k;
static int N=100010;
static int a[]=new int[N];
static int temp[]=new int[N];
public static void merge_sort(int a[],int l,int r){
if(l>=r) return;
int mid=l+r>>1;
merge_sort(a,l,mid);
merge_sort(a,mid+1,r);
int i=l,j=mid+1,k=0;
while (i<=mid && j<=r){
if(a[i]<=a[j]) temp[k++]=a[i++];
else temp[k++]=a[j++];
}
while (i<=mid) temp[k++]=a[i++];
while (j<=r) temp[k++]=a[j++];
for(i=l,j=0;i<=r;i++,j++) a[i]=temp[j];
}
public static void main(String args[]) throws IOException{
n=nextInt();
for(int i=0;i
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n,k;
static int N=100010;
static int a[]=new int[N];
static int temp[]=new int[N];
public static long merge_sort(int a[],int l,int r){
if(l>=r) return 0;
int mid=l+r>>1;
long ans=merge_sort(a,l,mid)+merge_sort(a,mid+1,r);
int i=l,j=mid+1,k=0;
while (i<=mid && j<=r){
if(a[i]<=a[j]) temp[k++]=a[i++];
else{
temp[k++]=a[j++];
ans+=mid-i+1;
}
}
while (i<=mid) temp[k++]=a[i++];
while (j<=r) temp[k++]=a[j++];
for(i=l,j=0;i<=r;i++,j++) a[i]=temp[j];
return ans;
}
public static void main(String args[]) throws IOException{
n=nextInt();
for(int i=0;i
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n,q;
static int N=100010;
static int a[]=new int[N];
public static void main(String args[]) throws IOException{
n=nextInt();
q=nextInt();
for(int i=0;i>1;
if(a[mid]>=x) r=mid;
else l=mid+1;
}
if(a[r]==x){//数组中存在x
pw.print(r+" ");//输出左端点
r=n-1;
while (l>1;
if(a[mid]<=x) l=mid;
else r=mid-1;
}
pw.println(r);//输出右端点
}
else pw.println(-1+" "+-1);//数组中不存在x
}
pw.close();
}
}
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static double nextDouble() throws IOException{
st.nextToken();
return st.nval;
}
static double n;
public static void main(String args[]) throws IOException{
n=nextDouble();
double l=-10000,r=10000;
while (r-l>1e-8){
double mid=(l+r)/2;
if(Math.pow(mid,3)>=n) r=mid;
else l=mid;
}
pw.printf("%.6f",r);
pw.close();
}
}
代码:
//高精度问题 java中有大数类可以直接用
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw=new PrintWriter(System.out);
public static void main(String args[]) throws IOException{
BigInteger a=new BigInteger(bf.readLine());
BigInteger b=new BigInteger(bf.readLine());
BigInteger res=a.add(b);
pw.println(res);
pw.close();
}
}
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw=new PrintWriter(System.out);
public static void main(String args[]) throws IOException{
BigInteger a=new BigInteger(bf.readLine());
BigInteger b=new BigInteger(bf.readLine());
BigInteger res=a.subtract(b);
pw.println(res);
pw.close();
}
}
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw=new PrintWriter(System.out);
public static void main(String args[]) throws IOException{
BigInteger a=new BigInteger(bf.readLine());
BigInteger b=new BigInteger(bf.readLine());
BigInteger res=a.multiply(b);
pw.println(res);
pw.close();
}
}
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw=new PrintWriter(System.out);
public static void main(String args[]) throws IOException{
BigInteger a=new BigInteger(bf.readLine());
BigInteger b=new BigInteger(bf.readLine());
BigInteger res1=a.divide(b);
BigInteger res2=a.mod(b);
pw.println(res1);
pw.println(res2);
pw.close();
}
}
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n,m;
static int N=100010;
static int a[]=new int[N];
public static void main(String args[]) throws IOException{
n=nextInt();
m=nextInt();
for(int i=1;i<=n;i++){
a[i]=nextInt();
a[i]+=a[i-1];
}
while (m--!=0){
int l=nextInt();
int r=nextInt();
pw.println(a[r]-a[l-1]);
}
pw.close();
}
}
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n,m,q;
static int N=1010;
static int a[][]=new int[N][N];
public static void main(String args[]) throws IOException{
n=nextInt();
m=nextInt();
q=nextInt();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a[i][j]=nextInt();
a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
}
}
while (q--!=0){
int x1=nextInt();
int y1=nextInt();
int x2=nextInt();
int y2=nextInt();
pw.println(a[x2][y2]-a[x2][y1-1]-a[x1-1][y2]+a[x1-1][y1-1]);
}
pw.close();
}
}
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n,m;
static int N=100010;
static int a[]=new int[N];
public static void main(String args[]) throws IOException{
n=nextInt();
m=nextInt();
for(int i=1;i<=n;i++) a[i]=nextInt();
for(int i=n;i>=1;i--) a[i]-=a[i-1];//差分数组
while (m--!=0){
int l=nextInt();
int r=nextInt();
int c=nextInt();
a[l]+=c;
a[r+1]-=c;
}
for(int i=1;i<=n;i++){
a[i]+=a[i-1];
pw.print(a[i]+" ");
}
pw.close();
}
}
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n,m,q;
static int N=1010;
static int a[][]=new int[N][N];
public static void main(String args[]) throws IOException{
n=nextInt();
m=nextInt();
q=nextInt();
for(int i=1;i<=n;i++){//a的差分矩阵
for(int j=1;j<=m;j++){
int x=nextInt();
a[i][j]+=x;
a[i][j+1]-=x;
a[i+1][j]-=x;
a[i+1][j+1]+=x;
}
}
while (q--!=0){
int x1=nextInt();
int y1=nextInt();
int x2=nextInt();
int y2=nextInt();
int c=nextInt();
a[x1][y1]+=c;
a[x1][y2+1]-=c;
a[x2+1][y1]-=c;
a[x2+1][y2+1]+=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];
pw.print(a[i][j]+" ");
}
pw.println();
}
pw.close();
}
}
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n;
static int N=100010;
static int a[]=new int[N];//原数组
static int s[]=new int[N];//记录数出现的次数
public static void main(String args[]) throws IOException{
n=nextInt();
for(int i=0;i1){
s[a[j]]--;
j++;
}
res= Math.max(res,i-j+1);
}
pw.println(res);
pw.close();
}
}
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n,m,x;
static int N=100010;
static int a[]=new int[N];
static int b[]=new int[N];
public static void main(String args[]) throws IOException{
n=nextInt();
m=nextInt();
x=nextInt();
for(int i=0;ix){
j--;
}
if(a[i]+b[j]==x){
pw.println(i+" "+j);
break;
}
}
pw.close();
}
}
代码:
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static int n,m;
static int N=100010;
static int a[]=new int[N];
static int b[]=new int[N];
public static void main(String args[]) throws IOException{
n=nextInt();
m=nextInt();
for(int i=0;i
代码:
// lowbit(x)函数:返回x的二进制表示中的最后一个1及其后面0所组成的数
// 以10为例: 10的二进制为1010 因此lowbit(10)=二进制的10=2;
// 以24为例: 24的二进制为11000 因此lowbit(24)=二进制的1000=8;
import java.io.*;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
public static int lowbit(int x){
return x&-x;
}
static int n;
public static void main(String args[]) throws IOException{
n=nextInt();
for(int i=0;i
代码:
//离散化: 值域范围很大,但个数很少,对每个x进行映射
//例例如: 0 2 10 500 3000 60000 900000
//映射为: 0 1 2 3 4 5 6
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static class node{
int x,y;
public node(int x,int y){
this.x=x;
this.y=y;
}
}
static List add =new ArrayList<>();//存储添加操作
static List query =new ArrayList<>();//存储查询操作
static List all =new ArrayList<>();//存储add,query用到的所有下标
static int n,m;
static int N=300010;//有100000个add,每次操作一个下标;有100000个querty,每次操作两个下标;因此需要开3000010;
static int a[]=new int[N];
public static int unique(List list){//去重,并返回不重复元素的个数
int j=0;
for(int i=0;i list){//返回list中值为x的下标
int l=0,r=list.size()-1;
while (l>1;
if(list.get(mid)>=x) r=mid;
else l=mid+1;
}
return r+1;//由于需要用到前缀和,因此下标从1开始
}
public static void main(String args[]) throws IOException{
n=nextInt();
m=nextInt();
while (n--!=0){
int x=nextInt();
int c=nextInt();
add.add(new node(x,c));
all.add(x);
}
while (m--!=0){
int l=nextInt();
int r=nextInt();
query.add(new node(l,r));
all.add(l);
all.add(r);
}
Collections.sort(all);//排序
int unique=unique(all);//去重
all=all.subList(0,unique);//保留不重复元素部分
for(node t:add){//处理add操作
int index=find(t.x,all);//找到p.x在all中的下标
a[index]+=t.y;
}
for(int i=1;i<=all.size();i++) a[i]+=a[i-1];//前缀和公式
for(node t:query){//处理查询操作
int l=find(t.x,all);
int r=find(t.y,all);
pw.println(a[r]-a[l-1]);
}
pw.close();
}
}
代码:
//区间合并: 快速将有交集的区间进行合并
//贪心思想: 将所有区间按左端点大小进行排序,依次进行判断
import java.io.*;
import java.util.Arrays;
public class Main {
static StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw=new PrintWriter(System.out);
public static int nextInt() throws IOException{
st.nextToken();
return (int)st.nval;
}
static class node implements Comparable{
int l,r;
public node(int l,int r){
this.l=l;
this.r=r;
}
public int compareTo(node p){
return Integer.compare(l,p.l);
}
}
static int n;
static int N=100010;
static node p[]=new node[N];
public static void main(String args[]) throws IOException{
n=nextInt();
for(int i=0;iend) res++;//说明两个区间无交集,res++
end= Math.max(end,p[i].r);//更新end
}
pw.println(res);
pw.close();
}
}