#include <cstdio> #include <utility> #include <vector> #include <list> #include <algorithm> const int mod = 1000000007; typedef struct node { node* l_son; node* r_son; int value = -1; } Node; typedef struct path_el { int num; bool positive; } Path_el; typedef std::list<Path_el> Path; Node* nope = new Node; Node* one = new Node; Node* trees[1000001]; int n; std::vector < std::pair<int, Path> > paths; bool compare(const Path_el& e1, const Path_el& e2) { return e1.num < e2.num; } bool compare_paths(const std::pair<int, Path>& e1, const std::pair<int, Path>& e2) { return e1.first < e2.first; } void read_clause() { char c; bool positive; int val; Path path; Path_el elt; bool end = false; while (!end && scanf("%c", &c)) { switch (c) { case 'x': positive = true; scanf("%d", &val); elt.positive = positive; elt.num = val; path.push_back(elt); break; case '~': positive = false; scanf("%c", &c); scanf("%d", &val); elt.positive = positive; elt.num = val; path.push_back(elt); break; case ')': end = true; } } path.sort(compare); paths.push_back(std::make_pair(path.front().num, path)); } void load_data() { scanf("%d", &n); char c; while (scanf("%c", &c) != -1) { switch (c) { case '(': read_clause(); } } std::sort(paths.begin(), paths.end(), compare_paths); } void init_trees() { nope->value = 0; one->value = 1; trees[n] = new node; trees[n]->l_son = one; trees[n]->r_son = one; for (int i = n - 1; i >= 1; i--) { trees[i] = new node; trees[i]->l_son = trees[i + 1]; trees[i]->r_son = trees[i + 1]; } } void insert_path_elt(std::list<path_el>& list, Node* node) { while (true) { auto elt = list.front(); list.pop_front(); if (list.empty()) { if (elt.positive) { node->l_son = nope; } else { node->r_son = nope; } return; } Node* l = node->l_son; Node* r = node->r_son; Node* changed; Node* new_changed = new Node; if (elt.positive && l->value != 0) { changed = l; node->l_son = new_changed; } else if (elt.positive) { return; } else if (r->value != 0) { changed = r; node->r_son = new_changed; } else { return; } new_changed->l_son = changed->l_son; new_changed->r_son = changed->r_son; node = new_changed; } } void insert_path() { auto path_p = paths.back(); paths.pop_back(); auto path = path_p.second; int insert_tree = path_p.first; insert_path_elt(path, trees[insert_tree]); } int sum_tree(Node* node) { if (node->value == -1) { int sum = sum_tree(node->r_son) + sum_tree(node->l_son); sum %= mod; node->value = sum; } return node->value; } int main(void) { auto& tre = trees; auto& pat = paths; load_data(); init_trees(); while (!paths.empty()) { insert_path(); } for (int i = n; i >= 1; i--) { sum_tree(trees[i]); } printf("%d\n", sum_tree(trees[1])); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | #include <cstdio> #include <utility> #include <vector> #include <list> #include <algorithm> const int mod = 1000000007; typedef struct node { node* l_son; node* r_son; int value = -1; } Node; typedef struct path_el { int num; bool positive; } Path_el; typedef std::list<Path_el> Path; Node* nope = new Node; Node* one = new Node; Node* trees[1000001]; int n; std::vector < std::pair<int, Path> > paths; bool compare(const Path_el& e1, const Path_el& e2) { return e1.num < e2.num; } bool compare_paths(const std::pair<int, Path>& e1, const std::pair<int, Path>& e2) { return e1.first < e2.first; } void read_clause() { char c; bool positive; int val; Path path; Path_el elt; bool end = false; while (!end && scanf("%c", &c)) { switch (c) { case 'x': positive = true; scanf("%d", &val); elt.positive = positive; elt.num = val; path.push_back(elt); break; case '~': positive = false; scanf("%c", &c); scanf("%d", &val); elt.positive = positive; elt.num = val; path.push_back(elt); break; case ')': end = true; } } path.sort(compare); paths.push_back(std::make_pair(path.front().num, path)); } void load_data() { scanf("%d", &n); char c; while (scanf("%c", &c) != -1) { switch (c) { case '(': read_clause(); } } std::sort(paths.begin(), paths.end(), compare_paths); } void init_trees() { nope->value = 0; one->value = 1; trees[n] = new node; trees[n]->l_son = one; trees[n]->r_son = one; for (int i = n - 1; i >= 1; i--) { trees[i] = new node; trees[i]->l_son = trees[i + 1]; trees[i]->r_son = trees[i + 1]; } } void insert_path_elt(std::list<path_el>& list, Node* node) { while (true) { auto elt = list.front(); list.pop_front(); if (list.empty()) { if (elt.positive) { node->l_son = nope; } else { node->r_son = nope; } return; } Node* l = node->l_son; Node* r = node->r_son; Node* changed; Node* new_changed = new Node; if (elt.positive && l->value != 0) { changed = l; node->l_son = new_changed; } else if (elt.positive) { return; } else if (r->value != 0) { changed = r; node->r_son = new_changed; } else { return; } new_changed->l_son = changed->l_son; new_changed->r_son = changed->r_son; node = new_changed; } } void insert_path() { auto path_p = paths.back(); paths.pop_back(); auto path = path_p.second; int insert_tree = path_p.first; insert_path_elt(path, trees[insert_tree]); } int sum_tree(Node* node) { if (node->value == -1) { int sum = sum_tree(node->r_son) + sum_tree(node->l_son); sum %= mod; node->value = sum; } return node->value; } int main(void) { auto& tre = trees; auto& pat = paths; load_data(); init_trees(); while (!paths.empty()) { insert_path(); } for (int i = n; i >= 1; i--) { sum_tree(trees[i]); } printf("%d\n", sum_tree(trees[1])); return 0; } |