题意

题目链接

Sol

直接做肯定不好搞(反正我不会。。)

直接开\(n\)个Pair类型的set,维护每个数的出现位置

每次在set中二分后暴力合并即可

然后就是树状数组的基本操作了

时间复杂度:\(O(nlog^2n)\)

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP make_pair
#define fi first
#define se second
#define lb(x) (x & (-x))
using namespace std;
const int MAXN = 1e6 + 10, INF = 2147483646;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, T[MAXN];
void Add(int x, int v) {
while(x <= N) T[x] += v, x += lb(x);
}
void AddInt(int l, int r, int val) {
Add(l, val); Add(r + 1, -val);
}
int Query(int x) {
int ans = 0;
while(x) ans += T[x], x -= lb(x);
return ans;
}
set<Pair> S[MAXN];
#define sit set<Pair>::iterator
void Add(int l, int r, int x) {
set<Pair> &s = S[x];
sit it = s.lower_bound(MP(l, r));
if(it != s.begin()) {
it--;
if(it -> se > r) return ;
if(it -> se >= l ) {
l = min(l, it -> fi); r = max(r, it -> se);
AddInt(it -> fi, it -> se, -1);
s.erase(it++);
}
}
it = s.lower_bound(MP(l, r));
while((it -> se >= l && it -> se <= r) || (it -> fi >= l && it -> fi <= r)) {
l = min(l, it -> fi); r = max(r, it -> se);
AddInt(it -> fi, it -> se, -1);
s.erase(it++);
}
s.insert(MP(l, r));
AddInt(l, r, 1);
}
int main() {
// freopen("a.in", "r", stdin); freopen("b.out", "w", stdout);
N = read(); M = read();
for(int i = 1; i <= N; i++) S[i].insert(MP(INF, INF));
while(M--) {
int opt = read();
if(opt == 0) {int l = read(), r = read(), val = read(); Add(l, r, val);}
else printf("%d\n", Query(read()));
}
return 0;
}
/*
5 5
0 2 4 4
0 3 5 5
0 1 5 5
1 5
1 3 5 12
0 1 1 1
1 1
0 1 2 1
1 1
0 1 3 1
1 1
0 2 3 1
1 1
0 4 5 1
1 1
0 1 5 1
1 1 */

codechef Many Lists(树状数组 set)的更多相关文章

  1. 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu

    https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...

  2. 【xsy2111】 【CODECHEF】Chef and Churus 分块+树状数组

    题目大意:给你一个长度为$n$的数列$a_i$,定义$f_i=\sum_{j=l_i}^{r_i} num_j$. 有$m$个操作: 操作1:询问一个区间$l,r$请你求出$\sum_{i=l}^{r ...

  3. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  4. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  5. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  7. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  8. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  9. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

随机推荐

  1. Linux 服务器加入Windows AD

    背景信息: Windows AD Version: Windows Server 2012 R2 zh-cn 计算机全名:hlm12r2n1.hlm.com 域:hlm.com 域控管理员:stone ...

  2. spring cloud ribbon源码解析(一)

    我们知道spring cloud中restTemplate可以通过服务名调接口,加入@loadBalanced标签就实现了负载均衡的功能,那么spring cloud内部是如何实现的呢? 通过@loa ...

  3. python3.6的request

    request实例1: import requests payload = {'key1':'value','key2':'value2'} url = "http://httpbin.or ...

  4. mongodb锁

    锁住写操作 > db.fsyncLock(); { "info" : "now locked against writes, use db.fsyncUnlock( ...

  5. QuantLib 金融计算——基本组件之天数计算规则详解

    目录 天数计算规则详解 定义 30 / 360 法 30/360 US 30/360 Bond Basis 30E/360 30E/360 ISDA Actual 法 Actual/Actual IC ...

  6. FPGA实战操作(2) -- PCIe总线(协议简述)

    目录 1. PCIe基础知识 2. 事务层协议 2.1 数据包结构 2.2 帧头含义详述 3. 报文举例 3.1 寄存器读报文 3.2 完成报文 4. 机制简述 4.1 Non-Posted和Post ...

  7. c# timer使用

    C#里现在有3个Timer类: System.Windows.Forms.Timer System.Threading.Timer System.Timers.Timer 这三个Timer我想大家对S ...

  8. HttpContext.Current.Session.Abandon() 大坑 要注意

    HttpContext.Current.Session.Abandon(); 如果在调用以上代码之后再存储session 在当前上下文之内是可以访问session的.. 但是页面跳转之后..在其他页面 ...

  9. java翻译到mono C#实现系列(3) 获取手机设备信息(残缺,)

    using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; ...

  10. 使用redux-devtools工具

    在vue中型项目开发的过程中,一般都是要用到vuex这个状态管理工具的,这样可以方便我们管理全局的状态,同时,为了在开发的过程中,更加方便地实时查看到state状态,我们会使用 vue-devtool ...