Ural 1650 Billionaires(线段树)

本文介绍了一道使用线段树解决城市与账单关联问题的编程题目。作者起初数组开得过小导致WA,后调整大小通过测试。文章提供了使用STL解决该题目的代码示例,并探讨了利用set充当线段树的可能性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/LyonLys/archive/2012/10/31/Ural_1650_Lyon.html

【基于QT的调色板】是一个使用Qt框架开发的色彩选择工具,类似于Windows操作系统中常见的颜色选取器。Qt是一个跨平台的应用程序开发框架,广泛应用于桌面、移动和嵌入式设备,支持C++和QML语言。这个调色板功能提供了横竖两种渐变模式,用户可以方便地选取所需的颜色值。 在Qt中,调色板(QPalette)是一个关键的类,用于管理应用程序的视觉样式。QPalette包含了一系列的颜色角色,如背景色、前景色、文本色、高亮色等,这些颜色可以根据用户的系统设置或应用程序的需求进行定制。通过自定义QPalette,开发者可以创建具有独特视觉风格的应用程序。 该调色板功能可能使用了QColorDialog,这是一个标准的Qt对话框,允许用户选择颜色。QColorDialog提供了一种简单的方式来获取用户的颜色选择,通常包括一个调色板界面,用户可以通过滑动或点击来选择RGB、HSV或其他色彩模型中的颜色。 横渐变取色可能通过QGradient实现,QGradient允许开发者创建线性或径向的色彩渐变。线性渐变(QLinearGradient)沿直线从一个点到另一个点过渡颜色,而径向渐变(QRadialGradient)则以圆心为中心向外扩散颜色。在调色板中,用户可能可以通过滑动条或鼠标拖动来改变渐变的位置,从而选取不同位置的颜色。 竖渐变取色则可能是通过调整QGradient的方向来实现的,将原本水平的渐变方向改为垂直。这种设计可以提供另一种方式来探索颜色空间,使得选取颜色更为直观和便捷。 在【colorpanelhsb】这个文件名中,我们可以推测这是与HSB(色相、饱和度、亮度)色彩模型相关的代码或资源。HSB模型是另一种常见且直观的颜色表示方式,与RGB或CMYK模型不同,它以人的感知为基础,更容易理解。在这个调色板中,用户可能可以通过调整H、S、B三个参数来选取所需的颜色。 基于QT的调色板是一个利用Qt框架和其提供的色彩管理工具,如QPalette、QColorDialog、QGradient等,构建的交互式颜色选择组件。它不仅提供了横竖渐变的色彩选取方式,还可能支持HSB色彩模型,使得用户在开发图形用户界面时能更加灵活和精准地控制色彩。
标题基于Spring Boot的二手物品交易网站系统研究AI更换标题第1章引言阐述基于Spring Boot开发二手物品交易网站的研究背景、意义、现状及本文方法与创新点。1.1研究背景与意义介绍二手物品交易的市场需求和Spring Boot技术的适用性。1.2国内外研究现状概述当前二手物品交易网站的发展现状和趋势。1.3论文方法与创新点说明本文采用的研究方法和在系统设计中的创新之处。第2章相关理论与技术介绍开发二手物品交易网站所涉及的相关理论和关键技术。2.1Spring Boot框架解释Spring Boot的核心概念和主要特性。2.2数据库技术讨论适用的数据库技术及其在系统中的角色。2.3前端技术阐述与后端配合的前端技术及其在系统中的应用。第3章系统需求分析详细分析二手物品交易网站系统的功能需求和性能需求。3.1功能需求列举系统应实现的主要功能模块。3.2性能需求明确系统应满足的性能指标和安全性要求。第4章系统设计与实现具体描述基于Spring Boot的二手物品交易网站系统的设计和实现过程。4.1系统架构设计给出系统的整体架构设计和各模块间的交互方式。4.2数据库设计详细阐述数据库的结构设计和数据操作流程。4.3界面设计与实现介绍系统的界面设计和用户交互的实现细节。第5章系统测试与优化说明对系统进行测试的方法和性能优化的措施。5.1测试方法与步骤测试环境的搭建、测试数据的准备及测试流程。5.2测试结果分析对测试结果进行详细分析,验证系统是否满足需求。5.3性能优化措施提出针对系统性能瓶颈的优化建议和实施方案。第6章结论与展望总结研究成果,并展望未来可能的研究方向和改进空间。6.1研究结论概括本文基于Spring Boot开发二手物品交易网站的主要发现和成果。6.2展望与改进讨论未来可能的系统改进方向和新的功能拓展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值