B - 卿学姐与基本法 (离散化+成段更新+区间求和)
卿学姐与基本法
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
“做专题也要按照基本法”
离开了诡异的村庄,卿学姐来到了威廉·圣·乱七八糟王国,这里的国王咸鱼王是个智障。
国家涣散,盗贼四起,民不聊生。
见到这样的景象,卿学姐不禁潸然泪下,“悠悠苍天,奈何苦了苍生”。
自幼学习基本法的卿学姐决定向整个国家普及基本法,改善国家法度。
在这个国家总共有NN个人,每个人都有一个编号,编号从1开始。
由于整个国家的人实在是太多了,卿学姐每次只能对一个连续区间的编号的人普及基本法。
同时卿学姐还想知道在某个时刻某个区间还有多少人没有被普及基本法。
Input
第一行两个整数N,QN,Q,表示总共有NN个人,并且有QQ次事件。
接下来QQ行,每行三个整数t,L,Rt,L,R。如果tt是11,代表在这时,卿学姐向闭区间L,RL,R的人普及基本法。如果tt是22,代表在这时,卿学姐想知道闭区间L,RL,R里面有多少人还没有被普及基本法。
1≤N≤1000000001≤N≤100000000
1≤Q≤1000001≤Q≤100000
1≤t≤21≤t≤2
1≤L≤R≤N1≤L≤R≤N
Output
输出每个卿学姐想知道的答案
Sample input and output
Sample Input | Sample Output |
---|---|
5 3 |
1 |
emmmm......做了这题之后感觉自己学到了很多有趣的东西,WA了很多次,看数据范围就知道要离散化,在一开始的时候,建树直接给节点赋值1。。。然后肯定WA。。。因为我把区间搞丢了,要离散成右闭左开的区间。。。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using namespace std;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
const int N = + ;
int sum[N << ], col[N << ]; vector<int> v;
map<int, int> dic; struct node{
int t, l, r;
}M[N]; void PushUP(int rt){
sum[rt] = sum[rt << ] + sum[rt << |];
} void PushDown(int rt){
if(col[rt]){
col[rt << ] = col[rt << |] = ;
sum[rt << ] = sum[rt << |] = ;
col[rt] = ;
}
} void Build(int l, int r, int rt){
col[rt] = ;
if(l == r){
sum[rt] = v[l] - v[l - ];
return;
}
int m = (l + r) >> ;
Build(lson);
Build(rson);
PushUP(rt);
} void Updata(int L, int R, int l, int r, int rt){
if(L <= l && r <= R){
col[rt] = ;
sum[rt] = ;
return;
}
PushDown(rt);
int m = (l + r) >> ;
if(L <= m) Updata(L, R, lson);
if(R > m) Updata(L, R, rson);
PushUP(rt);
} int Query(int L, int R, int l, int r, int rt){
if(L <= l && r <= R){
return sum[rt];
}
PushDown(rt);
int m = (l + r) >> ;
int ret = ;
if(L <= m) ret += Query(L, R, lson);
if(R > m) ret += Query(L, R, rson);
return ret;
} void Work(int n, int m){
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int k = v.size();
for(int i = ; i < k; i++)
dic[v[i]] = i; Build(, k, );
for(int i = ; i <= m; i++){
if(M[i].t == ) Updata(dic[M[i].l], dic[M[i].r], , k, );
else printf("%d\n", Query(dic[M[i].l], dic[M[i].r], , k, ));
}
}
int main(){
int n, m;
scanf("%d %d", &n, &m);
v.push_back();
for(int i = ; i <= m; i++){
scanf("%d %d %d", &M[i].t, &M[i].l, &M[i].r);
v.push_back(M[i].l); v.push_back(M[i].r);
v.push_back(M[i].l + );
}
Work(n, m);
}
B - 卿学姐与基本法 (离散化+成段更新+区间求和)的更多相关文章
- A - 卿学姐与公主(线段树+单点更新+区间极值)
A - 卿学姐与公主 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- cdoj1325卿学姐与基本法
地址:http://acm.uestc.edu.cn/#/problem/show/1325 题目: 卿学姐与基本法 Time Limit: 2000/1000MS (Java/Others) ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- POJ训练计划2777_Count Color(线段树/成段更新/区间染色)
解题报告 题意: 对线段染色.询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话訪问线段树,求出颜色种数.结果超时了,最坏的情况下,染色能够染到叶子节点. 换成存下区 ...
- poj 3468 线段树 成段增减 区间求和
题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...
- poj 3669 线段树成段更新+区间合并
添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...
- hdu 1698 线段树(成段替换 区间求和)
一条钩子由许多小钩子组成 更新一段小钩子 变成铜银金 价值分别变成1 2 3 输出最后的总价值 Sample Input11021 5 25 9 3 Sample OutputCase 1: The ...
随机推荐
- aspose 模板输出
Dictionary<string, string> dictionnaryBig = new Dictionary<string, string>(); dictionnar ...
- POJ 3180 牛围着池塘跳舞 强连通分量裸题
题意:一群牛被有向的绳子拴起来,如果有一些牛(>=2)的绳子是同向的,他们就能跳跃.求能够跳跃的组数. #include <iostream> #include <cstdio ...
- quartz的配置文件说明
# Default Properties file for use by StdSchedulerFactory # to create a Quartz Scheduler Instance, if ...
- go语言系列--golang在windows上的安装和开发环境goland的配置
在windows上安装golang软件 golang中国网址为:https://studygolang.com/dl 我的学习选择版本:1.12.5 golang 1.12.5版本更新的内容:gola ...
- Maven构建生命周期
以下引用官方的生命周期解释https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html: 一.构建生命 ...
- 实验报告(一)&第三周总结
Java实验报告 实验一 Java开发环境与简单Java程序 一. 实验目的 (1) 熟悉JDK开发环境 (2) 熟练掌握结构化程序设计方法 二. 实验内容 1. 打印输 ...
- MongoDB通过JavaDriver执行shell命令,例如创建sharding collection
Mongodb的java driver本身的接口 void createCollection(String collectionName, CreateCollectionOptions create ...
- nginx回源使用localhost产生问题
最近测试ngx_http_slice模块,回源的时候填的localhost结果老是超时,还以为是slice模块有问题,后来无意间改成127.0.0.1后就没有问题了 真是见鬼了 #user root; ...
- [Python]ctypes+struct实现类c的结构化数据串行处理
1. 用C/C++实现的结构化数据处理 在涉及到比较底层的通信协议开发过程中, 往往需要开发语言能够有效的表达和处理所定义的通信协议的数据结构. 在这方面是C/C++语言是具有天然优势的: 通过str ...
- k8s编辑pod配置信息
kubectl edit deployment devops-service -n c7n-system