diff --git a/2182/main b/2182/main new file mode 100644 index 0000000..6174e25 Binary files /dev/null and b/2182/main differ diff --git a/2182/main.c b/2182/main.c new file mode 100644 index 0000000..33752a0 --- /dev/null +++ b/2182/main.c @@ -0,0 +1,70 @@ +#include + +#define MAX 8005 + +struct node { + int from, to; + int value; +}; + +int cows[MAX]; +struct node tree[4*MAX]; + +int build_tree(struct node *tree, long idx, int from, int to); +int find_section(struct node *tree, long idx, int spn); +int upd_tree(struct node *tree, long idx, int tidx); + +int main(void){ + int n; + scanf("%d",&n); + build_tree(tree, 0, 1, n); + cows[0] = 0; + for(int i = 1; i < n; i++){ + scanf("%d",cows+i); + } + for(int i = n-1; ~i; i--){ + int idx = find_section(tree, 0, cows[i]); + cows[i] = idx; + upd_tree(tree, 0, idx); + } + for(int i = 0; i < n; i++) printf("%d\n",cows[i]); + return 0; +} + +int build_tree(struct node *tree, long idx, int from, int to){ + int h_idx = (from + to)/2; + tree[idx].from = from; + tree[idx].to = to; + tree[idx].value = 0; + if(from < to){ + build_tree(tree, idx*2+1, from, h_idx); + build_tree(tree, idx*2+2, h_idx+1, to); + tree[idx].value = tree[idx*2+1].value + tree[idx*2+2].value; + } + else{ + tree[idx].value = 1; + } + return 0; +} + +int find_section(struct node *tree, long idx, int spn){ + if(tree[idx].from == tree[idx].to){ + return tree[idx].from; + } + if(tree[idx*2+1].value <= spn){ + return find_section(tree, idx*2+2, spn-tree[idx*2+1].value); + } + else{ + return find_section(tree, idx*2+1, spn); + } +} + +int upd_tree(struct node *tree, long idx, int tidx){ + int h_idx = (tree[idx].from + tree[idx].to)/2; + tree[idx].value--; + if(tree[idx].from < tree[idx].to){ + if(tidx <= h_idx) upd_tree(tree, idx*2+1, tidx); + else upd_tree(tree, idx*2+2, tidx); + } + return 0; +} diff --git a/2182/problem.set b/2182/problem.set new file mode 100644 index 0000000..e69de29 diff --git a/2182/stdin b/2182/stdin new file mode 100644 index 0000000..e69de29 diff --git a/2182/stdout b/2182/stdout new file mode 100644 index 0000000..e69de29 diff --git a/2352/main.c b/2352/main.c new file mode 100644 index 0000000..c487cb7 --- /dev/null +++ b/2352/main.c @@ -0,0 +1,67 @@ +#include + +#define MAX (15000+5L) + +struct node{ + int from, to; + int n; +}; + + +int rank[MAX]; +struct node tree[32000*4L]; + +int build_tree(struct node *tree, long idx, int from , int to); +int upd_tree(struct node *tree, long idx, int p_idx); +int find_section(struct node *tree, long idx, int from, int to); + +int main(void){ + int number, i; + build_tree(tree, 0, 0, 32000); + scanf("%d",&number); + for(i = 0; i < number; i++){ + int x,y; + scanf("%d %d",&x,&y); + rank[find_section(tree, 0, 0, x)]++; + upd_tree(tree, 0, x); + } + for(i = 0; i < number; i++){ + printf("%d\n",rank[i]); + } + return 0; +} + +int build_tree(struct node *tree, long idx, int from , int to){ + int h_idx = (from+to)/2; + tree[idx].from = from; + tree[idx].to = to; + tree[idx].n = 0; + if(from < to){ + build_tree(tree, idx*2+1, from, h_idx); + build_tree(tree, idx*2+2, h_idx+1, to); + } + return 0; +} + +int upd_tree(struct node *tree, long idx, int p_idx){ + int h_idx = (tree[idx].from + tree[idx].to)/2; + tree[idx].n++; + if(tree[idx].from < tree[idx].to){ + if(p_idx <= h_idx) upd_tree(tree, idx*2+1, p_idx); + else upd_tree(tree, idx*2+2, p_idx); + } + return 0; +} + +int find_section(struct node *tree, long idx, int from, int to){ + int h_idx = (tree[idx].from + tree[idx].to)/2, sum = 0; + if(tree[idx].from == from && tree[idx].to == to) return tree[idx].n; + if(from <= h_idx && to > h_idx) + sum = find_section(tree, idx*2+1, from, h_idx) + find_section(tree, idx*2+2, h_idx+1, to); + else if(from <= h_idx && to <= h_idx) + sum = find_section(tree, idx*2+1, from , to); + else if(from > h_idx && to > h_idx) + sum = find_section(tree, idx*2+2, from , to); + else printf("IMP!\n"); + return sum; +} diff --git a/2352/problem.build b/2352/problem.build new file mode 100644 index 0000000..1b027cb Binary files /dev/null and b/2352/problem.build differ diff --git a/2352/problem.set b/2352/problem.set new file mode 100644 index 0000000..e69de29 diff --git a/2352/stdin b/2352/stdin new file mode 100644 index 0000000..93d8d92 --- /dev/null +++ b/2352/stdin @@ -0,0 +1,6 @@ + 5 + 1 1 + 5 1 + 7 1 + 3 3 + 5 5 diff --git a/2352/stdout b/2352/stdout new file mode 100644 index 0000000..21de5f9 --- /dev/null +++ b/2352/stdout @@ -0,0 +1,5 @@ +1 +2 +1 +1 +0 diff --git a/2828/main.c b/2828/main.c new file mode 100644 index 0000000..dee8b15 --- /dev/null +++ b/2828/main.c @@ -0,0 +1,75 @@ +#include + +#define MAX 200005 + +struct node { + long from, to; + int value; +}; + +long peo[MAX],num[MAX],line[MAX]; +struct node tree[4*MAX]; + +int build_tree(struct node *tree, long idx, long from, long to); +long find_section(struct node *tree, long idx, long spn); +int upd_tree(struct node *tree, long idx, long tidx); + +int main(void){ + int n; + + while(scanf("%d",&n) != EOF){ + build_tree(tree, 0, 1, n); + for(int i = 0; i < n; i++){ + scanf("%ld %ld",peo+i, num+i); + } + for(int i = n-1; ~i; i--){ + long idx = find_section(tree, 0, peo[i]); + line[idx-1] = num[i]; + upd_tree(tree, 0, idx); + } + for(int i = 0; i < n; i++){ + if(i) printf(" "); + printf("%ld",line[i]); + } + printf("\n"); + } + return 0; +} + +int build_tree(struct node *tree, long idx, long from, long to){ + long h_idx = (from + to)/2; + tree[idx].from = from; + tree[idx].to = to; + tree[idx].value = 0; + if(from < to){ + build_tree(tree, idx*2+1, from, h_idx); + build_tree(tree, idx*2+2, h_idx+1, to); + tree[idx].value = tree[idx*2+1].value + tree[idx*2+2].value; + } + else{ + tree[idx].value = 1; + } + return 0; +} + +long find_section(struct node *tree, long idx, long spn){ + if(tree[idx].from == tree[idx].to){ + return tree[idx].from; + } + if(tree[idx*2+1].value <= spn){ + return find_section(tree, idx*2+2, spn-tree[idx*2+1].value); + } + else{ + return find_section(tree, idx*2+1, spn); + } +} + +int upd_tree(struct node *tree, long idx, long tidx){ + long h_idx = (tree[idx].from + tree[idx].to)/2; + tree[idx].value--; + if(tree[idx].from < tree[idx].to){ + if(tidx <= h_idx) upd_tree(tree, idx*2+1, tidx); + else upd_tree(tree, idx*2+2, tidx); + } + return 0; +} diff --git a/2828/problem.build b/2828/problem.build new file mode 100644 index 0000000..9a5d219 Binary files /dev/null and b/2828/problem.build differ diff --git a/2828/problem.set b/2828/problem.set new file mode 100644 index 0000000..e69de29 diff --git a/2828/stdin b/2828/stdin new file mode 100644 index 0000000..e69de29 diff --git a/2828/stdout b/2828/stdout new file mode 100644 index 0000000..e69de29 diff --git a/3264/main.c b/3264/main.c new file mode 100644 index 0000000..21794ed --- /dev/null +++ b/3264/main.c @@ -0,0 +1,66 @@ +#include + +#define MAX (50000+5L) + +struct node{ + int from ,to; + long height; +}; + +long cows[MAX]; +struct node hi[MAX*4], lo[MAX*4]; +int build_tree(struct node *tree, long *cows, long idx, int from, int to, int hol); +long find_selection(struct node *tree, long idx, int from, int to, int hol); + +int main(void){ + long nc,nm; + printf("INPUT:\n"); + scanf("%ld %ld",&nc,&nm); + for(long i = 0; i < nc; i++) scanf("%ld",cows+i); + build_tree(hi, cows, 0, 1, (int)nc, 0); + build_tree(lo, cows, 0, 1, (int)nc, 1); + for(long i = 0; i < nm; i++){ + int idxa, idxb; + scanf("%d %d",&idxa,&idxb); + long hh = find_selection(hi, 0, idxa, idxb, 0); + long lh = find_selection(lo, 0, idxa, idxb, 1); + printf("%ld\n",hh-lh); + } + + return 0; +} + +int build_tree(struct node *tree, long *cows, long idx, int from, int to, int hol){ + int h_idx = (from + to)/2; + tree[idx].from = from; + tree[idx].to = to; + if(from < to){ + build_tree(tree, cows, idx*2+1, from, h_idx, hol); + build_tree(tree, cows, idx*2+2, h_idx+1, to, hol); + long l = tree[idx*2+1].height, r = tree[idx*2+2].height; + if(hol == 0) tree[idx].height = r>l?r:l; + else tree[idx].height = r>l?l:r; + } + else{ + tree[idx].height = cows[from-1]; + } + + return 0; +} + +long find_selection(struct node *tree, long idx, int from, int to, int hol){ + int h_idx = (tree[idx].from + tree[idx].to)/2; + if(from == tree[idx].from && to == tree[idx].to) return tree[idx].height; + if(from <= h_idx && to > h_idx){ + long l = find_selection(tree, idx*2+1, from, h_idx, hol), r = find_selection(tree, idx*2+2, h_idx+1, to, hol); + if(hol == 0) return r>l?r:l; + else return r>l?l:r; + } + else if(from <= h_idx && to <= h_idx){ + return find_selection(tree, idx*2+1, from, to, hol); + } + else if (from > h_idx && to > h_idx){ + return find_selection(tree, idx*2+2, from, to, hol); + } + return 0; +} diff --git a/3264/problem.build b/3264/problem.build new file mode 100644 index 0000000..05540fc Binary files /dev/null and b/3264/problem.build differ diff --git a/3264/problem.set b/3264/problem.set new file mode 100644 index 0000000..e69de29 diff --git a/3264/stdin b/3264/stdin new file mode 100644 index 0000000..e69de29 diff --git a/3264/stdout b/3264/stdout new file mode 100644 index 0000000..e69de29