Ural 1650 Billionaires(线段树)
2012-10-31 22:07
363 查看
http://acm.timus.ru/problem.aspx?space=1&num=1650
用线段树统计的模拟题,开始的时候数组开小了,以为城市只有50000个,所以wa了两次,后来开大了就过了!~ ^_^
这题用STL解决代码量比较小,据说可以用set来充当我的线段树。代码如下:
View Code
——written by Lyon
用线段树统计的模拟题,开始的时候数组开小了,以为城市只有50000个,所以wa了两次,后来开大了就过了!~ ^_^
这题用STL解决代码量比较小,据说可以用set来充当我的线段树。代码如下:
View Code
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <set> #include <string> #include <map> using namespace std; typedef long long ll; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<string> vstr; set<string> cityName, billName; map<string, int> cityId, billId; const int max1 = 10005; const int max2 = 60005; char city[max2][25], bill[max1][25]; int curPos[max1], cntDay[max2]; ll money[max1]; vstr buf1[2], buf2[2]; vll buff1; vi buff2; void initAll() { cityName.clear(); cityId.clear(); billName.clear(); billId.clear(); buff1.clear(); buff2.clear(); memset(city, 0, sizeof(city)); memset(bill, 0, sizeof(bill)); memset(money, 0, sizeof(money)); memset(curPos, 0, sizeof(curPos)); memset(cntDay, 0, sizeof(cntDay)); for (int i = 0; i < 2; i++) { buf1[i].clear(); buf2[i].clear(); } } void input1(int n) { char tmp1[25], tmp2[25]; ll tmp; while (n--) { scanf("%s%s%lld", tmp1, tmp2, &tmp); buf1[0].push_back(tmp1); buf1[1].push_back(tmp2); buff1.push_back(tmp); billName.insert(tmp1); cityName.insert(tmp2); } } void input2(int k) { char tmp1[25], tmp2[25]; int tmp; while (k--) { scanf("%d%s%s", &tmp, tmp1, tmp2); buff2.push_back(tmp); buf2[0].push_back(tmp1); buf2[1].push_back(tmp2); // billName.insert(tmp1); cityName.insert(tmp2); } } void makeMap() { set<string>::iterator ii; int pos; for (pos = 0, ii = cityName.begin(); ii != cityName.end(); ii++) { strcpy(city[pos], (*ii).c_str()); cityId[*ii] = pos++; } for (pos = 0, ii = billName.begin(); ii != billName.end(); ii++) { strcpy(bill[pos], (*ii).c_str()); billId[*ii] = pos++; } } #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 struct segTree { ll mx[max2 << 2]; int id[max2 << 2]; void up(int rt) { int ls = rt << 1, rs = rt << 1 | 1; mx[rt] = max(mx[ls], mx[rs]); if (mx[ls] == mx[rs]) { id[rt] = -1; } else { id[rt] = mx[ls] > mx[rs] ? id[ls] : id[rs]; } } void build(int l, int r, int rt) { mx[rt] = 0; if (l == r) { id[rt] = l; return ; } int m = (l + r) >> 1; build(lson); build(rson); up(rt); } void update(int p, ll k, int l, int r, int rt) { if (l == r) { mx[rt] += k; return ; } int m = (l + r) >> 1; if (p <= m) { update(p, k, lson); } else { update(p, k, rson); } up(rt); } } segT; void initStat(int n) { int size = buff1.size(); segT.build(0, n - 1, 1); for (int i = 0; i < size; i++) { int bId = billId[buf1[0][i]]; int cId = cityId[buf1[1][i]]; segT.update(cId, buff1[i], 0, n - 1, 1); curPos[bId] = cId; money[bId] = buff1[i]; } // for (int i = 0; i < size; i++) { // printf("%s : %s\n", bill[i], city[curPos[i]]); // } // puts("~~"); } void work(int n, int t) { int size = buff2.size(); int curTime = 0; for (int i = 0; i < size; i++) { // if (~segT.id[1]) printf("highest %s\n", city[segT.id[1]]); if (~segT.id[1]) cntDay[segT.id[1]] += buff2[i] - curTime; curTime = buff2[i]; int bId = billId[buf2[0][i]]; int cId = cityId[buf2[1][i]]; segT.update(curPos[bId], -money[bId], 0, n - 1, 1); segT.update(cId, money[bId], 0, n - 1, 1); curPos[bId] = cId; // for (int j = 0; j < cityName.size(); j++) { // printf("%s : %d\n", city[j], cntDay[j]); // } // puts("!!!"); // for (int j = 0; j < billName.size(); j++) { // printf("%s : %s\n", bill[j], city[curPos[j]]); // } // puts("~~"); } if (~segT.id[1]) cntDay[segT.id[1]] += t - curTime; for (int j = 0, endj = cityName.size(); j < endj; j++) { if (cntDay[j]) printf("%s %d\n", city[j], cntDay[j]); } // puts("!!!"); } int main() { int n, m, k; // freopen("in", "r", stdin); // freopen("out", "w", stdout); while (~scanf("%d", &n)) { initAll(); input1(n); scanf("%d%d", &m, &k); input2(k); makeMap(); n = cityName.size(); initStat(n); work(n, m); } return 0; }
——written by Lyon
相关文章推荐
- URAL 2014 Zhenya moves from parents 线段树
- URAL - 1090 D - Inverse Order Pair——线段树求逆序数
- Ural 1028. Stars 线段树单点更新
- ural 1019 Line Painting(线段树)
- URAL 1987. Nested Segments(数学 & 线段树)
- URAL 1650. Billionaires(线段树)
- URAL 2014 Zhenya moves from parents --线段树
- URAL 1890 . Money out of Thin Air (dfs序hash + 线段树)
- URAL 2014 Zhenya moves from parents (线段树)
- ural1707 线段树扫描线
- ural 1019 Line Painting (线段树)
- Ural 1019 A Line painting(线段树,成段更新离散化)
- 【Ural1028】Stars-线段树和树状数组入门题
- ural 1987. Nested Segments【离散化+贪心+线段树】
- ural 1028. Stars (线段树)
- URAL 1019 - Line Painting(线段树)
- URAL - 1989 Subpalindromes hash & 树状数组 | 线段树
- 【最近公共祖先】【线段树】URAL - 2109 - Tourism on Mars
- URAL 1846 线段树 最大公约数
- URAL - 1989 Subpalindromes——字符串哈希 + 线段树