import java.util.Scanner; publicclassquick_sort{ staticvoidquickSort(int[] a, int l, int r){ if(l>=r) return; int i = l - 1, j = r + 1, x = a[l + r >> 1]; while(i<j) { do i++; while (a[i] < x); do j--; while (a[j] > x); if (i < j) { int t = a[i]; a[i] = a[j]; a[j] = t; } } quickSort(a, l, j); quickSort(a, j+1, r); } staticint[] a = newint[100005];
publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } quickSort(a, 0, n-1); for(int i=0;i<n;i++)System.out.printf("%d ", a[i]); } }
publicclassmerge_sort{ staticint[] a = newint[100005]; staticint[] tmp = newint[100005];
staticvoidmergeSort(int[] a, int l, int r){ if (l >= r) return; int mid = l + r >> 1;
mergeSort(a, l, mid); mergeSort(a, mid + 1, r);
int k = 0, i = l, j = mid + 1; while (i <= mid && j <= r) { if (a[i] <= a[j]) tmp[k++] = a[i++]; else tmp[k++] = a[j++]; } while (i <= mid) tmp[k++] = a[i++]; while (j <= r) tmp[k++] = a[j++];
for (i = l, j = 0; i <= r; i++, j++) a[i] = tmp[j]; }
publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) a[i] = sc.nextInt(); mergeSort(a, 0, n - 1); for (int i = 0; i < n; i++) System.out.printf("%d ", a[i]); } }
import java.util.Scanner; //整数划分 publicclassbinary_search{ staticint[] a = newint[100005];
publicstaticvoidmain(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 k = sc.nextInt(); int l = 0, r = n - 1; while (l < r) { int mid = l + r >> 1; if (a[mid] >= k) r = mid; else l = mid + 1; } if (a[l] != k) System.out.println("-1 -1"); else { System.out.printf("%d ", l);
l = 0; r = n - 1; while (l < r) { int mid = l + r + 1 >> 1; if (a[mid] <= k) l = mid; else r = mid - 1; } System.out.printf("%d\n", l); } } } }
vector<int> C; int t = 0; for (int i = 0; i < A.size(); i ++ ) { t += A[i]; if (i < B.size()) t += B[i]; C.push_back(t % 10); t /= 10; } if (t) C.push_back(t); return C; }
intmain() { string a, b; vector<int> A, B; cin >> a >> b; for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0'); for (int i = b.size() - 1; i >= 0; i -- ) B.push_back(b[i] - '0'); auto C = add(A, B); for (int i = C.size() - 1; i >= 0; i -- ) cout << C[i]; cout << endl; return0; }
vector<int> C; int t = 0; for (int i = 0; i < A.size(); i ++ ) { t += A[i]; if (i < B.size()) t += B[i]; C.push_back(t % base); t /= base; }
if (t) C.push_back(t); return C;
}
intmain() { string a, b; vector<int> A, B; cin >> a >> b;
for (int i = a.size() - 1, s = 0, j = 0, t = 1; i >= 0; i -- ) { s += (a[i] - '0') * t; j ++, t *= 10; if (j == 9 || i == 0) { A.push_back(s); s = j = 0; t = 1; } } for (int i = b.size() - 1, s = 0, j = 0, t = 1; i >= 0; i -- ) { s += (b[i] - '0') * t; j ++, t *= 10; if (j == 9 || i == 0) { B.push_back(s); s = j = 0; t = 1; } }
auto C = add(A, B);
cout << C.back(); for (int i = C.size() - 2; i >= 0; i -- ) printf("%09d", C[i]); cout << endl;
for (int i = A.size() - 1; i >= 0; i -- ) if (A[i] != B[i]) return A[i] > B[i];
returntrue;
}
vector<int> sub(vector<int> &A, vector<int> &B) { vector<int> C; for (int i = 0, t = 0; i < A.size(); i ++ ) { t = A[i] - t; if (i < B.size()) t -= B[i]; C.push_back((t + 10) % 10); if (t < 0) t = 1; else t = 0; }
intmain() { string a, b; vector<int> A, B; cin >> a >> b; for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0'); for (int i = b.size() - 1; i >= 0; i -- ) B.push_back(b[i] - '0');
vector<int> C;
if (cmp(A, B)) C = sub(A, B); else C = sub(B, A), cout << '-';
for (int i = C.size() - 1; i >= 0; i -- ) cout << C[i]; cout << endl;
vector<int> div(vector<int> &A, int b, int &r) { vector<int> C; r = 0; for (int i = A.size() - 1; i >= 0; i -- ) { r = r * 10 + A[i]; C.push_back(r / b); r %= b; } reverse(C.begin(), C.end()); while (C.size() > 1 && C.back() == 0) C.pop_back(); return C; }
intmain() { string a; vector<int> A;
int B; cin >> a >> B; for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
int r; auto C = div(A, B, r);
for (int i = C.size() - 1; i >= 0; i -- ) cout << C[i]; cout << endl << r << endl; return0; }
publicclassPrefix_add{ staticint[] a = newint[100005]; staticint[] s = newint[100005];
publicstaticvoidmain(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]); } } }
publicclassMain{ staticint[] a = newint[100005]; staticint[] b = newint[100005]; staticvoidinsert(int l, int r, int c){//b数组是a数组的前缀和 b[l] += c; b[r+1] -= c; }
publicstaticvoidmain(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(); insert(i, i, a[i]);//构造原始b数组 } 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++){ b[i] += b[i-1];//a[i] = a[i-1] + b[i]; System.out.printf("%d ", b[i]); } } }
publicclassdiscretization{ //离散化 publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int N = 300010;//2m + n <= 300000 int[] a = newint[N];//离散化后数组存值 int[] s = newint[N];//a数组的前缀和
List<Integer> alls = new ArrayList<>();//用来存所有下标 x, l, r
List<Pair> add = new ArrayList<>();//用来存n次加法
List<Pair> query = new ArrayList<>();//用来存m次询问
//输入n次加法 for (int i = 0; i < n; i++) { int x = sc.nextInt(); int c = sc.nextInt(); add.add(new Pair(x, c)); alls.add(x); } //输入m次询问 for (int i = 0; i < m; i++) { int l = sc.nextInt(); int r = sc.nextInt(); query.add(new Pair(l, r)); alls.add(l); alls.add(r); }
Collections.sort(alls); //对取到的所有值排序 int unique = unique(alls); //去重 alls = alls.subList(0, unique);
for (Pair item : add) { int index = find(item.first, alls); a[index] += item.second; }
//前缀和预处理 for (int i = 1; i <= alls.size(); i++) s[i] = s[i - 1] + a[i];
//处理询问返回结果
for (Pair item : query) { int l = find(item.first, alls); int r = find(item.second, alls); System.out.println(s[r] - s[l - 1]); } }
//二分查找,返回要找元素下标 publicstaticintfind(int x, List<Integer> list){ int l = 0; int r = list.size() - 1; while (l < r) { int mid = l + r >> 1; if (list.get(mid) >= x) r = mid; else l = mid + 1; } return r + 1;//映射下标为1,2,...,n } //去重 publicstaticintunique(List<Integer> list){ int j = 0; for (int i = 0; i < list.size(); i++) { if (i == 0 || !Objects.equals(list.get(i), list.get(i - 1))) { list.set(j, list.get(i)); j++; } } return j; } staticclassPair{ int first; int second;
publicclassMain{ static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); staticclassPair{ int a, b; publicPair(int a, int b){ this.a = a; this.b = b; } } static List<Pair> insert = new ArrayList<>(); static List<Pair> query = new ArrayList<>(); static TreeSet<Integer> set = new TreeSet<>(); static HashMap<Integer, Integer> map = new HashMap<>(); staticint N = 100010 * 3; staticlong[] s = newlong[N];
publicstaticvoidmain(String[] args)throws IOException { String[] str = br.readLine().split(" "); int n = Integer.parseInt(str[0]); int m = Integer.parseInt(str[1]); for(int i = 1; i <= n; i++){ str = br.readLine().split(" "); int x = Integer.parseInt(str[0]); int c = Integer.parseInt(str[1]); insert.add(new Pair(x, c)); set.add(x); } for(int i = 1; i <= m; i++){ str = br.readLine().split(" "); int l = Integer.parseInt(str[0]); int r = Integer.parseInt(str[1]); query.add(new Pair(l, r)); set.add(l); set.add(r); } int p = 1; for(Integer i : set){ map.put(i, p++); } for(Pair pair : insert){ int x = pair.a; int c = pair.b; int pos = map.get(x); s[pos] += c; } for(int i = 1; i <= p; i++){ s[i] += s[i - 1]; }
for(Pair pair : query){ int l = map.get(pair.a); int r = map.get(pair.b); System.out.println(s[r] - s[l - 1]); }