惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
GbyAI
GbyAI
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
P
Proofpoint News Feed
The Cloudflare Blog
H
Help Net Security
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
量子位
博客园 - 聂微东
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
月光博客
月光博客

Shiroha白羽的博客

Golang 踩坑 —— interface 为参数的时候传 nil 指针 Codeforces Round 925 (Div. 3) Codeforces Round 924 (Div. 2) Codeforces Round 923 (Div. 3) Codeforces Round 922 (Div. 2) Codeforces Round 921 (Div. 2) Educational Codeforces Round 161 (Rated for Div. 2) Codeforces Round 920 (Div. 3) Codeforces Round 919 (Div. 2) Hello 2024 Good Bye 2023 Codeforces Round 918 (Div. 4) 个人备份的常用 macOS 清理命令 Codeforces Round 917 (Div. 2) Pinely Round 3 (Div. 1 + Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) Codeforces Round 915 (Div. 2) Codeforces Round 914 (Div. 2) Codeforces Round 913 (Div. 3) Educational Codeforces Round 159 (Rated for Div. 2) Codeforces Round 912 (Div. 2) Codeforces Round 911 (Div. 2) CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!) Educational Codeforces Round 158 (Rated for Div. 2) Codeforces Round 910 (Div. 2) Codeforces Round 909 (Div. 3) Codeforces Round 908 (Div. 2) Educational Codeforces Round 157 (Rated for Div. 2) C++自定义的字面量 Codeforces Round 907 (Div. 2)
2019年-西北大学集训队选拔赛——D温暖的签到题
Shiroha · 2019-07-11 · via Shiroha白羽的博客
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>

#define NUM
#define ll long long
using namespace std;

int n, m;

struct node {
int l;
int sta;//第一个值
int r;//长度

ll sum(int l, int r) { // 区间求和
if (l < this->l) l = this->l;
if (r > this->r) r = this->r;
return (((l - this->l) + sta + (r - this->l) + sta)) * ((ll) r - l + 1) / 2;
}
};

map<int, node> mp; // 利用map自动排序,形成一个一维的块链,每一个块为一个给定规则的数据组成

ll cal(int l, int r) { // 计算区间和,直接调用块内的自定义函数求和
map<int, node>::iterator itl = mp.upper_bound(l);
itl--;
map<int, node>::iterator itr = mp.upper_bound(r);
ll ans = 0;
while (itl != itr) {
ans += (*itl).second.sum(l, r);
itl++;
}
return ans;
}

void update(int l, int r) { // 更新块,删除被覆盖的块,形成新块
if (r == 1)
return;
map<int, node>::iterator itl = mp.upper_bound(l);
itl--;
map<int, node>::iterator itr = mp.upper_bound(r);
itr--;
node templ = (*itl).second;
node tempr = (*itr).second;
tempr.sta = tempr.sta + (r + 1 - tempr.l);
templ.r = l - 1;
tempr.l = r + 1;
itr++;
while (itl != itr) {
auto tmp = itl++;
mp.erase(tmp);
}
node newnode;
newnode.l = l;
newnode.r = r;
newnode.sta = 1;
mp[l] = newnode;
if (templ.l <= templ.r) {
mp[templ.l] = templ;
}
if (tempr.l <= tempr.r) {
mp[tempr.l] = tempr;
}
}

int main() {
scanf("%d%d", &n, &m);
int opt, l, r;
node tot;
tot.l = 1;
tot.r = n;
tot.sta = 1;
mp[1] = tot;
while (m--) {
scanf("%d%d%d", &opt, &l, &r);
if (opt == 1)
update(l, r);
else
printf("%lld\n", cal(l, r));
}
return 0;
}