Ural 1650 Billionaires(线段树)
http://acm.timus.ru/problem.aspx?space=1&num=1650
用线段树统计的模拟题,开始的时候数组开小了,以为城市只有50000个,所以wa了两次,后来开大了就过了!~ ^_^
这题用STL解决代码量比较小,据说可以用set来充当我的线段树。代码如下:
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <set> 6 #include <string> 7 #include <map> 8 9 using namespace std; 10 11 typedef long long ll; 12 typedef vector<ll> vll; 13 typedef vector<int> vi; 14 typedef vector<string> vstr; 15 set<string> cityName, billName; 16 map<string, int> cityId, billId; 17 18 const int max1 = 10005; 19 const int max2 = 60005; 20 char city[max2][25], bill[max1][25]; 21 int curPos[max1], cntDay[max2]; 22 ll money[max1]; 23 24 vstr buf1[2], buf2[2]; 25 vll buff1; 26 vi buff2; 27 28 void initAll() { 29 cityName.clear(); 30 cityId.clear(); 31 billName.clear(); 32 billId.clear(); 33 buff1.clear(); 34 buff2.clear(); 35 memset(city, 0, sizeof(city)); 36 memset(bill, 0, sizeof(bill)); 37 memset(money, 0, sizeof(money)); 38 memset(curPos, 0, sizeof(curPos)); 39 memset(cntDay, 0, sizeof(cntDay)); 40 for (int i = 0; i < 2; i++) { 41 buf1[i].clear(); 42 buf2[i].clear(); 43 } 44 } 45 46 void input1(int n) { 47 char tmp1[25], tmp2[25]; 48 ll tmp; 49 50 while (n--) { 51 scanf("%s%s%lld", tmp1, tmp2, &tmp); 52 buf1[0].push_back(tmp1); 53 buf1[1].push_back(tmp2); 54 buff1.push_back(tmp); 55 billName.insert(tmp1); 56 cityName.insert(tmp2); 57 } 58 } 59 60 void input2(int k) { 61 char tmp1[25], tmp2[25]; 62 int tmp; 63 64 while (k--) { 65 scanf("%d%s%s", &tmp, tmp1, tmp2); 66 buff2.push_back(tmp); 67 buf2[0].push_back(tmp1); 68 buf2[1].push_back(tmp2); 69 // billName.insert(tmp1); 70 cityName.insert(tmp2); 71 } 72 } 73 74 void makeMap() { 75 set<string>::iterator ii; 76 int pos; 77 78 for (pos = 0, ii = cityName.begin(); ii != cityName.end(); ii++) { 79 strcpy(city[pos], (*ii).c_str()); 80 cityId[*ii] = pos++; 81 } 82 for (pos = 0, ii = billName.begin(); ii != billName.end(); ii++) { 83 strcpy(bill[pos], (*ii).c_str()); 84 billId[*ii] = pos++; 85 } 86 } 87 88 #define lson l, m, rt << 1 89 #define rson m + 1, r, rt << 1 | 1 90 91 struct segTree { 92 ll mx[max2 << 2]; 93 int id[max2 << 2]; 94 95 void up(int rt) { 96 int ls = rt << 1, rs = rt << 1 | 1; 97 98 mx[rt] = max(mx[ls], mx[rs]); 99 if (mx[ls] == mx[rs]) { 100 id[rt] = -1; 101 } else { 102 id[rt] = mx[ls] > mx[rs] ? id[ls] : id[rs]; 103 } 104 } 105 106 void build(int l, int r, int rt) { 107 mx[rt] = 0; 108 if (l == r) { 109 id[rt] = l; 110 return ; 111 } 112 int m = (l + r) >> 1; 113 114 build(lson); 115 build(rson); 116 up(rt); 117 } 118 119 void update(int p, ll k, int l, int r, int rt) { 120 if (l == r) { 121 mx[rt] += k; 122 return ; 123 } 124 int m = (l + r) >> 1; 125 126 if (p <= m) { 127 update(p, k, lson); 128 } else { 129 update(p, k, rson); 130 } 131 up(rt); 132 } 133 } segT; 134 135 void initStat(int n) { 136 int size = buff1.size(); 137 138 segT.build(0, n - 1, 1); 139 for (int i = 0; i < size; i++) { 140 int bId = billId[buf1[0][i]]; 141 int cId = cityId[buf1[1][i]]; 142 143 segT.update(cId, buff1[i], 0, n - 1, 1); 144 curPos[bId] = cId; 145 money[bId] = buff1[i]; 146 } 147 // for (int i = 0; i < size; i++) { 148 // printf("%s : %s\n", bill[i], city[curPos[i]]); 149 // } 150 // puts("~~"); 151 } 152 153 void work(int n, int t) { 154 int size = buff2.size(); 155 int curTime = 0; 156 157 for (int i = 0; i < size; i++) { 158 // if (~segT.id[1]) printf("highest %s\n", city[segT.id[1]]); 159 if (~segT.id[1]) cntDay[segT.id[1]] += buff2[i] - curTime; 160 curTime = buff2[i]; 161 162 int bId = billId[buf2[0][i]]; 163 int cId = cityId[buf2[1][i]]; 164 165 segT.update(curPos[bId], -money[bId], 0, n - 1, 1); 166 segT.update(cId, money[bId], 0, n - 1, 1); 167 curPos[bId] = cId; 168 169 // for (int j = 0; j < cityName.size(); j++) { 170 // printf("%s : %d\n", city[j], cntDay[j]); 171 // } 172 // puts("!!!"); 173 // for (int j = 0; j < billName.size(); j++) { 174 // printf("%s : %s\n", bill[j], city[curPos[j]]); 175 // } 176 // puts("~~"); 177 } 178 if (~segT.id[1]) cntDay[segT.id[1]] += t - curTime; 179 for (int j = 0, endj = cityName.size(); j < endj; j++) { 180 if (cntDay[j]) printf("%s %d\n", city[j], cntDay[j]); 181 } 182 // puts("!!!"); 183 184 } 185 186 int main() { 187 int n, m, k; 188 189 // freopen("in", "r", stdin); 190 // freopen("out", "w", stdout); 191 192 while (~scanf("%d", &n)) { 193 initAll(); 194 input1(n); 195 scanf("%d%d", &m, &k); 196 input2(k); 197 makeMap(); 198 199 n = cityName.size(); 200 initStat(n); 201 work(n, m); 202 } 203 204 return 0; 205 }
——written by Lyon