题目:戳这里

学习博客:戳这里

题意:有很多个活动,每个活动有持续天数,每个活动会在每天提供C个CPU每个CPU价格为P,问需要工作N天,每天需要K个CPU的最少花费。

解题思路:遍历每一天,维护当前天K个cpu的最小花费。具体方法是维护两个线段树(树状数组也可以),维护每一天可以使用的cpu数和价格*cpu数的前缀和。注意数组下标是价格(1e6的数组。

(不明白的话可以看代码,代码思路很清晰

附学习博客的代码:

  1 #include <iostream>
2
3 #include <algorithm>
4
5 #include <string.h>
6
7 #include <vector>
8
9 #include <memory.h>
10
11 #include <bitset>
12
13 #include <map>
14
15 #include <deque>
16
17 #include <math.h>
18
19 #include <stdio.h>
20
21 using namespace std;
22
23 typedef long long int ll;
24
25 const int MAXN = 1000005;
26
27
28
29 ll num[MAXN<<2];
30
31 ll sum[MAXN<<2];
32
33 int N;
34
35 void pushup(int rt){
36
37 num[rt]=num[rt<<1]+num[rt<<1|1];
38
39 sum[rt]=sum[rt<<1]+sum[rt<<1|1];
40
41 }
42
43
44
45 void update(int P,int C,int l,int r,int rt){
46
47 if(l==r){
48
49 num[rt]+=C;
50
51 sum[rt]+=1ll*P*C;
52
53 return;
54
55 }
56
57
58
59 int m=(l+r)/2;
60
61
62
63 if(P<=m)
64
65 update(P,C,l,m,rt<<1);
66
67 else
68
69 update(P,C,m+1,r,rt<<1|1);
70
71 pushup(rt);
72
73 }
74
75
76
77 ll query(int K,int l,int r,int rt){
78
79
80
81 if(l==r){
82
83 //不到K个
84
85 if(l==MAXN){
86
87 return 0;
88
89 }
90
91 if(K>0)
92
93 {
94
95 return 1ll*K*l;
96
97 }
98
99 else
100
101 return 0;
102
103 }
104
105 int m=(l+r)/2;
106
107 if(num[rt<<1]>=K){
108
109 return query(K,l,m,rt<<1);
110
111 }
112
113 else{
114
115 return sum[rt<<1]+query(K-num[rt<<1],m+1,r,rt<<1|1);
116
117 }
118
119 }
120
121
122
123 vector<pair<int,int> > C[MAXN];//第i天加入的活动
124
125 vector<pair<int,int> > O[MAXN];//第i天结束的活动
126
127
128
129 int main()
130
131 {
132
133 int K,M;
134
135 scanf("%d%d%d",&N,&K,&M);
136
137
138
139 int l,r,c,p;
140
141 for(int i=0;i<M;i++){
142
143 scanf("%d%d%d%d",&l,&r,&c,&p);
144
145 C[l].push_back(make_pair(p,c));//加入的活动
146
147 O[r].push_back(make_pair(p,c));//退出的活动
148
149 }
150
151
152
153 ll ans=0;
154
155 for(int i=1;i<=N;i++){
156
157 //新活动加入
158
159 for(int j=0;j<C[i].size();j++)
160
161 update(C[i][j].first,C[i][j].second,1,MAXN,1);
162
163 ans+=query(K,1,MAXN,1);
164
165 //活动结束
166
167 for(int j=0;j<O[i].size();j++)
168
169 update(O[i][j].first,-O[i][j].second,1,MAXN,1);
170
171 }
172
173 cout<<ans<<endl;
174
175
176
177 return 0;
178
179 }

【非原创】codeforces 1070C Cloud Computing 【线段树&树状数组】的更多相关文章

  1. codeforces 1017C - Cloud Computing 权值线段树 差分 贪心

    https://codeforces.com/problemset/problem/1070/C 题意: 有很多活动,每个活动可以在天数为$[l,r]$时,提供$C$个价格为$P$的商品 现在从第一天 ...

  2. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  3. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组

    Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...

  4. [bzoj3196][Tyvj1730]二逼平衡树_树套树_位置线段树套非旋转Treap/树状数组套主席树/权值线段树套位置线段树

    二逼平衡树 bzoj-3196 Tyvj-1730 题目大意:请写出一个维护序列的数据结构支持:查询给定权值排名:查询区间k小值:单点修改:查询区间内定值前驱:查询区间内定值后继. 注释:$1\le ...

  5. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  6. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  7. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  8. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  9. [Codeforces 1199D]Welfare State(线段树)

    [Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...

随机推荐

  1. 单线程的as-if-serial语义

    单线程的as-if-serial语义 关于指令重排序有个问题不明白的一个问题 int a = 2; int c = 1 + a; float b = 3f / 2f; 举个栗子,从CPU的设计者以及编 ...

  2. Spring Security 实战干货:AuthenticationManager的初始化细节

    1. 前言 今天有个同学告诉我,在Security Learning项目的day11分支中出现了一个问题,验证码登录和其它登录不兼容了,出现了No Provider异常.还有这事?我赶紧跑了一遍还真是 ...

  3. python3多进程 进程池 协程并发

    一.进程           我们电脑的应用程序,都是进程,进程是资源分配的单位.进程切换需要的资源最大,效率低.         进程之间相互独立         cpu密集的时候适合用多进程 #多 ...

  4. NodeJS连接MongoDB数据库

    NodeJS连接MongoDB数据库 连接数据库的js文件[我将其命名为(connect.js)] // 引入mongoose第三方模块 const mongoose = require('mongo ...

  5. Linux中LPC、RPC、IPC的区别

    其实这玩意儿就是纸老虎,将英文缩写翻译为中文就明白一半了. IPC:(Inter Process Communication )跨进程通信 这个概念泛指进程之间任何形式的通信行为,是个可以拿来到处套的 ...

  6. hive报错:Failed with exception java.io.IOException: rename for src path:

    在hive中,会有这样一种情形: 1.创建一个分区外部表A(比如A表有5个字段),并且向A表里指定的分区(比如20160928这个分区)里插入数据 2.发现A表缺少一些字段,因为存在元数据不实时更新的 ...

  7. vim 行号的显示与隐藏

    通常我们在使用vim编辑器的时候,需要显示和隐藏行号 隐藏行号: 1.首先我们vim   1.txt   (进入我们编辑的文档),如下,此时是显示行号的 2.按一下esc键,并输入:(冒号),完成效果 ...

  8. hbase 集群(完全分布式)方式安装

    一,环境 1,  主节点一台: ubuntu desktop 16.04 zhoujun      172.16.12.1 从节点(slave)两台:ubuntu server 16.04 hadoo ...

  9. samba 随笔

    SElinux以及防火墙的关闭 关闭SELinux的方法: 修改/etc/selinux/config文件中的SELINUX="" 为 disabled ,然后重启. 如果不想重启 ...

  10. 敏捷史话(五):敏捷已逝 —— Dave Thomas

    " 敏捷已逝,但敏捷精神长存.因为所谓的敏捷专家卖给你的是方法论,而不是价值."当多数人都在从"敏捷"身上榨取利益时, Dave Thomas 成为了一位逆行者 ...