HDU - 3874 Necklace (树状数组、离线处理)

题目链接:Necklace

题意:

  给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m<=2e5)个询问,每个询问给出l和r,要求[l,r]区间内所有珠子的value(如果有相同value值的珠子则只计算一次这个珠子的值)。

题解:

  刚开始看到这题,遇到区间求和就想到了树状数组。但是如何解决区间内相同value值只能计算一次的问题呢?看了题解后发现可以用离线化解决这个问题,我们可以先把所有的询问保存下来,将询问按右端点从小到大排序,然后将所有有重复的value值只保留最后一个位置的值,其他的value值全都减去。因为区间的右端点保证是从小到大的,而可以发现[l,r]这个区间的值就等于所有数保留最后一个出现的数的和(可以自己验证一下)。这里离散化最大的功能就是保证了我们的操作是从小到大的,这样我们前面的操作就不会影响后面的操作。还有这里是要用long long的不然会爆~@。@#

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAX_N = 1e5+9;
 7 typedef pair<int,int> P;
 8 P q[MAX_N*4];
 9 int q1[MAX_N*4];
10 int vis[MAX_N*10];
11 long long vec[MAX_N];
12 long long res[MAX_N];
13 long long out[MAX_N*4];
14 void add(int x,long long num)
15 {
16     for(;x<MAX_N;x+=(x&-x))
17         res[x] += num;
18 }
19 long long sum(int x)
20 {
21     long long ans = 0;
22     for(;x>0;x-=(x&-x))
23         ans += res[x];
24     return ans;
25 }
26 int main()
27 {
28     int N,M,T;
29     cin>>T;
30     while(T--)
31     {
32         cin>>N;
33         memset(res,0,sizeof(res));
34         memset(vis,0,sizeof(vis));
35         for(int i=1;i<=N;i++)
36         {
37             scanf("%lld",&vec[i]);
38             add(i,vec[i]);
39             if(!vis[vec[i]]) vis[vec[i]] = i;
40         }
41         cin>>M;
42         for(int i=0;i<M;i++)
43         {
44             scanf("%d%d",&q1[i],&q[i].first);
45             q[i].second = i;
46         }
47         sort(q,q+M);
48         int right = 1;
49         for(int i=0;i<M;i++)
50         {
51             for(int j=right;j<=q[i].first;j++)
52             {
53                 if(vis[vec[j]] != j)
54                 {
55                     add(vis[vec[j]],-vec[j]);
56                     vis[vec[j]] = j;
57                 }
58             }
59             right = q[i].first;
60             out[q[i].second] = sum(q[i].first) - sum(q1[q[i].second]-1);
61         }
62         for(int i=0;i<M;i++)
63         {
64             printf("%lld\n",out[i]);
65         }
66     }
67     return 0;
68 }

 

转载于:https://www.cnblogs.com/doggod/p/8406793.html

YOLO26训练自己的数据集,从代码下载到实例测试 (含YOLO26网络结构图) 在Windows10上配置CUDA环境教程2026年1月14日,YOLO26是最新发布的计算机视觉模型。支持多种任务,包括目标检测、实例分割、图像分类、姿态估计、有向目标检测以及物体跟踪等。以上就是YOLO26训练自己数据集的全部过程啦,欢迎大家在评论区交流~ 阅读详情

相关推荐

E28 LoRa模块透传 定点传输 RSSI测试与MicroPython应用

测试说明 测试模块数:3 测试模式:配置模式和传输模式 如果没有检测到串口,首先检查是否安装串口驱动CH341SER,下载链接,提取码:vaw2 工作模式 模式(0-3) M2 M1 M0 模式介绍 备注 0 传输模式 1 0 0 串口打开,无线打开,连续透明传输 空速支持手动配置和自适应波特率自动调节;连续模式双方波特率必须一致 1 RSSI 1 0 1 串口打开,无线打开,RSSI功能打开 模块每100ms输出RSSI强度值 2 测距模式 1 1 0 保留 - 3 配置模式

GniLAY1022的博客 4253

Necklace (线段树单点更新+区间查询+离线操作)

Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or mor

qq_19327307的专栏 429

基于大模型的恶性胸腔积液全周期预测与诊疗方案研究

本研究基于大模型技术构建了恶性胸腔积液全流程预测与诊疗体系,涵盖术前风险评估、术中方案优化、术后恢复预测及并发症预防。通过整合多源医疗数据,实现了手术风险概率预测(准确率80%)、并发症预警(AUC值0.85)及个性化康复方案制定,显著提升诊疗精准度。研究创新性地将Transformer架构与多模态数据融合,在100例临床验证中显示良好性能,但存在数据样本局限、模型可解释性不足等问题。未来将扩大多中心临床验证,优化算法可解释性,探索与基因检测等技术的结合应用,推动恶性胸腔积液的精准诊疗发展。

嵌入式毕设/课设资料清单(速查手册·文档模板·题库,持续更新)→ 见最新文章 1907

HDU 3874 Necklace(树状数组离线处理)

HDU 3874 Necklace 题目链接 同HDU3333,链接 代码: #include #include #include #include using namespace std; const int N = 50005; const int M = 200005; typedef long long ll; int t, n, a[N]; map g

Remilia's 807

Python中的join函数的用法

从语法、实例、应用三方面详尽地介绍join函数

阿黎逸阳的博客 2万+

构建基于知识图谱的金融风险关联网络

在金融领域,风险的识别和管理至关重要。传统的风险分析方法往往只能处理单一维度的数据,难以全面捕捉金融市场中复杂的风险关联关系。构建基于知识图谱的金融风险关联网络的目的在于整合多源异构的金融数据,通过知识图谱的方式清晰地展示金融实体之间的复杂关系,从而更准确地识别潜在的金融风险,为金融机构的风险管理决策提供有力支持。本项目的范围涵盖了从金融数据的收集、预处理,到知识图谱的构建,再到金融风险关联网络的搭建和分析。

SuperAGI2025 1424

HDU 3874 Necklace(树状数组离线处理)

想了一下午没有想到好办法,看了一下别人思路,该题需要离线处理。 这个方法可以很好的处理重复数字的问题,用来求解无重复数字的区间和问题。 将询问的区间按照右端点排序,每次记录上一个区间的右端点R,然后遍历R到当前区间右端点,用一个数组记录每一个数字出现的位置,如果该数字出现过,那么删除上一次出现的位置的值,并更新到当前位置。   这样就可以有效的去重 。   由于整个过程只是遍历了一遍数组,所以

AC_Arthur的专栏 766

12、控件使用之录入英文、阿拉伯数字删除增加功能及二维码显示

控件使用之录入英文、阿拉伯数字删除增加功能及二维码显示1、用到的控件2、添加控件2.1、添加【文本录入】控件2.1、添加【文本显示】控件2.1、添加【QR 二维码】控件3、保存工程及生成文件4、下载文件 1、用到的控件   需要用的显示控件为【文本显示】控件和【QR二维码】控件,以及触控控件【文本录入】控件。 2、添加控件 2.1、添加【文本录入】控件   【文本录入】控件也叫【ASCI 文本录入】,是用来录入ASCI 字符,打开 DGUS 软件,点击【触控控件】—【文本录入】,用鼠标框选一个区域,即可在右

嵌入式羊圈 632

HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2083    Accepted Submission(s): 747 Problem Description Mery has a beautiful neckla...

weixin_33981932的博客 225

什么是 Agent 的 Context Window?为什么它是 Agent 工程中最核心的约束之一?

什么是 Agent 的 Context Window?为什么它是 Agent 工程中最核心的约束之一?

我哎GIS博客 825

《QGIS快速入门与应用基础》239:指北针样式选择(预设/自定义)

系统解析指北针设计的核心规范与实操方法。文章首先强调行业规范红线,包括强制要求N字标注、箭头辨识度等关键要素,通过正误示例对比强化合规意识。其次详细讲解QGIS内置预设样式的分类应用,提供极简箭头、标准三角箭头等4类样式的选型对照表,并配图说明适用场景。最后介绍从基础微调到SVG导入的进阶自定义方法,帮助用户实现从合规到专业的样式设计。全文贯穿"合规优先、可读为本、美观为辅"原则,配有详细操作截图与规范对照表,适合从GIS新手到专业制图人员参考。

LuckyHanMo的博客 544

HDU 3874 Necklace 树状数组 + 离线处理

/** * 树状数组 + 离线: * 看人题解的,思路: 把询问区间[l, r]存下后按r从小到大排序 * 用数组hash[i]记录数字i最后一次出现的位置。用变量curR从1开始每次 * 往右扫到每个询问区间的r。关键在于没扫到新的一个数的时候,如何更新 * 维护这个树状数组。把每次询问的答案记录到ans数组。 * 具体: 当前数num[curR]如果出现过,则最后一

xuruoxin的专栏 793

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

HDU 3874 Necklace(树状数组+离线处理)

HDU 3874 Necklace(树状数组+离线处理) 分析: 预处理:首先我们需要一个树状数组A[n],我们从左到右读入所有的a[i],如果当前a[i]=x值是第一次出现,就执行add(i,x),否则不执行.当我们扫描完了n个值后,我们保证sum(R)就是区间[1,R]的答案.(想想是不是)        接下来我们对所有查询的区间进行排序,使得[L,R]区间中L小的区间排在前面.我们首

code 1244

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

HDU 3874 Necklace (树状数组去重+离线处理)

Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more...

邵光亮的博客 410

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Necklace HDU - 3874 (线段树/树状数组 + 离线处理)

Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more...

weixin_30832983的博客 143

HDU 3874 Necklace(树状数组+离线处理)

题目大意 有一串长度为 n(1≤n≤50000) 的项链,项链上每块石头都有一个价值,在计算项链上一段连续石头的价值和的时候,相同价值的只计算一次 现在给你 m(1≤m≤200000) 个询问,每个询问要查询一段连续石头 [L, R] (1≤L≤R≤n)的价值和 做法分析 先把所有的查询保存,然后按照查询区间的右端点由小到大排序,然后离线处理: 对于一个具体的石头...

weixin_30662011的博客 99

HDU-3874 Necklace 树状数组+离线处理

这题是要求一段区间内的不重复的数字之和。我们通过对询问区间的右端点进行排序,然后记录每一数字的上一次的出现的位置,由于询问都是不回溯的那么就可以线性的更新了。 代码如下: #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #defi...

weixin_30564901的博客 96

HDU 3874 Necklace(树状数组的离线操作)

题目链接:HDU 3874 Necklace 【题目大意】 组成项链的珠子有品级之分,价值从1~1000000不等,然而珠子的价值会有重复,例如一个项链有这6个珠子构成 1 1 1 2 3 1, 我们要求给定区间珠子的价值总和,如果区间内有相同价值的珠子,则只计算相同价值珠子中的一个; 用 F(左区间,右区间),来求区间和。 例如 F(1,3)=1, F(2...

weixin_30621919的博客 138

HDU - 3874 Necklace(树状数组+离线操作)

点我看题 题意:给你一个长度为n的序列,m次询问,问从l到r不重复数值的和为多少. 分析:用树状数组求和,离线操作去重.先说一下离线操作吧(今天才看的离线,不喜勿喷...),离线操作就是在已知所有输入的情况下,先求出每次的结果,再一次性进行输出.这个题离线操作的方法就是先把每个数第一次出现的位置记录下来,然后把要查询的区间按照从r由小到大排序,排好序之后,在上一个查询区间的右端点到本次查询的右

Vmorish 463

hdu3874(离线思想+线段树)

Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2034    Accepted Submission(s): 733 Problem Description Mery has a beautiful

晓风残月xj 1010

hdu 3874 Necklace

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3874       这道题目以前做过,再做的时候却忘了,可能当时也不完全是自己想出来的,印象不深,这里用到了离线算法,先将询问排序,然后用树状树组进行操作,如果一个价值没有出现过,则将其位置加上相应价值,如果出现过,则在当前位置加上该价值,而在已经出现过该价值的位置删除该价值,相当于一个移位操作,保证

Wings_of_Liberty的专栏 635

hdu 3874 Necklace 树状数组 离线操作

//对询问进行离线操作,读入所有的询问,然后将所有询问按照右升序排序 //在处理第i个询问时,保证从第一个数到第i个询问的右边范围ri的所有和只是前面不相同的点的和 //而且每个点的相同点的位置都是在ri范围内最后一个点,由于在第i次询问后的 //所有询问的右范围都大于ri,所以删除相同的点不会影响后面的询问 #include #include #include #include

ijbuhv的专栏 845

HDU3874 Necklace[树状数组]

E - Necklace Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Mery has a beautiful necklace. The necklace is made up of N magic ba

ControlBear的博客 694

HDU 3874 Necklace (树状数组+离线操作)

Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count it once. We define the bea

HJY 622
上一篇: POJ-2299 Ultra-QuickSort (树状数组)
下一篇: HDU - 5877 Weak Pair (dfs+树状数组)
diaotangu4964
博客等级 码龄10年 1粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值