the count_less_than int target will overflow!

This commit is contained in:
unlockable
2023-11-29 17:07:36 +08:00
parent d0cd4c747c
commit 04912a54bd
5 changed files with 14 additions and 9 deletions

View File

@@ -1,17 +1,17 @@
#include <stdio.h> #include <stdio.h>
int* stairs = NULL; int stairs[100000] = {0};
int M, K, H; int M, K, H;
long long total = 0; long long total = 0;
int main() { int main() {
scanf("%d %d %d", &M , &K, &H); scanf("%d %d %d", &M , &K, &H);
stairs = new int[M]; // stairs = new int[M];
for (int i = 0; i < M ; i++) { for (int i = 0; i < M ; i++) {
scanf("%d", &stairs[i]); scanf("%d", &stairs[i]);
} }
for (int i = 0; i < M; i++) { for (int i = 0; i < M; i++) {
for (int j = i + 1; j <= i + K && j < M; j++) { for (int j = i + 1; j <= i + K && j < M; j++) {
if (stairs[j] <= stairs[i] + H && stairs[j] >= stairs[i] - H) { if (stairs[j] <= ((long long)stairs[i]) + H && stairs[j] >= stairs[i] - H) {
total++; total++;
} }
} }

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,5 @@
#include <stdio.h> #include <stdio.h>
#define TREE_SIZE 10000 #define TREE_SIZE 50000
struct TreeNode { struct TreeNode {
int val; int val;
int size; int size;
@@ -133,7 +133,7 @@ int traverse(int pos) {
return 0; return 0;
} }
int count_less_than(int target, int pos) { int count_less_than(long long target, int pos) {
if (pos < 0) { if (pos < 0) {
return 0; return 0;
} }
@@ -155,6 +155,7 @@ int count_less_than(int target, int pos) {
} }
return 1 + tree[tree[pos].left].size; return 1 + tree[tree[pos].left].size;
} }
int main() { int main() {
scanf("%d %d %d", &M, &K, &H); scanf("%d %d %d", &M, &K, &H);
int num = 0; int num = 0;
@@ -170,7 +171,7 @@ int main() {
// tree_root); // tree_root);
// } // }
total += count_less_than(num + H, tree_root) - count_less_than(num - H - 1, tree_root); total += count_less_than(((long long)num) + H, tree_root) - count_less_than(num - H - 1, tree_root);
if (i >= K) { if (i >= K) {
tree_root = delete_node(dequeue(), tree_root); tree_root = delete_node(dequeue(), tree_root);
} }

View File

@@ -6,8 +6,9 @@ H = random.randint(10000, 100000)
# M = 10000 # M = 10000
# K = 1 # K = 1
# H = 1 # H = 1
arr = list(range(0, M)) arr = list(range(2147483647, 2147483647 - M, -1))
random.shuffle(arr) random.shuffle(arr)
# print(arr)
with open('fuck.txt', 'w') as f: with open('fuck.txt', 'w') as f:
f.write(str(M)+' '+str(K)+' '+str(H)+'\n') f.write(str(M)+' '+str(K)+' '+str(H)+'\n')
for i in arr: for i in arr:

View File

@@ -11,4 +11,7 @@ ans: 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
15 2 2 15 2 2
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 20 2
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1