codeforces 669E E. Little Artem and Time Machine(节点为map型的线段树)
题目链接:
E. Little Artem and Time Machine
2 seconds
256 megabytes
standard input
standard output
Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset.
Artem wants to create a basic multiset of integers. He wants these structure to support operations of three types:
- Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer.
- Remove integer from the multiset. Only one instance of this integer is removed. Artem doesn't want to handle any exceptions, so he assumes that every time remove operation is called, that integer is presented in the multiset.
- Count the number of instances of the given integer that are stored in the multiset.
But what about time machine? Artem doesn't simply apply operations to the multiset one by one, he now travels to different moments of time and apply his operation there. Consider the following example.
- First Artem adds integer 5 to the multiset at the 1-st moment of time.
- Then Artem adds integer 3 to the multiset at the moment 5.
- Then Artem asks how many 5 are there in the multiset at moment 6. The answer is 1.
- Then Artem returns back in time and asks how many integers 3 are there in the set at moment 4. Since 3 was added only at moment 5, the number of integers 3 at moment 4 equals to 0.
- Then Artem goes back in time again and removes 5 from the multiset at moment 3.
- Finally Artyom asks at moment 7 how many integers 5 are there in the set. The result is 0, since we have removed 5 at the moment3.
Note that Artem dislikes exceptions so much that he assures that after each change he makes all delete operations are applied only to element that is present in the multiset. The answer to the query of the third type is computed at the moment Artem makes the corresponding query and are not affected in any way by future changes he makes.
Help Artem implement time travellers multiset.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of Artem's queries.
Then follow n lines with queries descriptions. Each of them contains three integers ai, ti and xi (1 ≤ ai ≤ 3, 1 ≤ ti, xi ≤ 109) — type of the query, moment of time Artem travels to in order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time are distinct and that after each operation is applied all operations of the first and second types are consistent.
For each ask operation output the number of instances of integer being queried at the given moment of time.
6
1 1 5
3 5 5
1 2 5
3 6 5
2 3 5
3 7 5
1
2
1
3
1 1 1
2 2 1
3 3 1
0 题意: 就是有一个时光机可以去不同的时刻对一些数值的数目进行修改;1是增加1,2是减1,3是询问; 思路: 一看就是个线段树,蓝而时间和数值的范围太大,时间可以进行离散化,数值的话那么就把线段树的节点变成map怒怼一发,我就是开开脑洞然后写写玩玩,蓝后一不小心怼过了;
哈哈哈,最近脑洞太大,经常乱搞来A题;我自己都受不了了; AC代码:
/*2014300227 669E - 33 GNU C++11 Accepted 1091 ms 78840 KB*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+;
const ll inf=1e15;
const int N=1e5+;
int n;
struct PO
{
int a,t,va,pos;
}po[N];
int cmp1(PO x,PO y)
{
return x.t<y.t;
}
int cmp2(PO x,PO y)
{
return x.pos<y.pos;
}
struct Tree
{
int l,r;
map<int,int>mp1,mp2;
};
Tree tree[*N];
void build(int node,int L,int R)
{
tree[node].l=L;
tree[node].r=R;
if(L==R)return ;
int mid=(L+R)>>;
build(*node,L,mid);
build(*node+,mid+,R);
}
void update(int node,int pos,int num,int flag)
{
if(tree[node].l==tree[node].r&&tree[node].l==pos)
{
if(flag==)
tree[node].mp1[num]++;
else tree[node].mp2[num]++;
return ;
}
int mid=(tree[node].l+tree[node].r)>>;
if(pos<=mid)
{
update(*node,pos,num,flag);
}
else
{
update(*node+,pos,num,flag);
}
if(flag==)tree[node].mp1[num]++;
else tree[node].mp2[num]++;
}
int query(int node,int L,int R,int num,int flag)
{
if(L<=tree[node].l&&R>=tree[node].r)
{
if(flag==)return tree[node].mp1[num];
else return tree[node].mp2[num];
}
int mid=(tree[node].l+tree[node].r)>>;
if(R<=mid)
{
return query(*node,L,R,num,flag);
}
else if(L>mid)
{
return query(*node+,L,R,num,flag);
}
else
{
return query(*node,L,mid,num,flag)+query(*node+,mid+,R,num,flag);
}
}
int main()
{ scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&po[i].a,&po[i].t,&po[i].va);
po[i].pos=i;
}
sort(po+,po+n+,cmp1);
for(int i=;i<=n;i++)
{
po[i].t=i;
}
sort(po+,po+n+,cmp2);
build(,,n);
for(int i=;i<=n;i++)
{
if(po[i].a==)
{
update(,po[i].t,po[i].va,);
}
else if(po[i].a==)
{
update(,po[i].t,po[i].va,);
}
else
{ //cout<<"#"<<endl;
printf("%d\n",query(,,po[i].t,po[i].va,)-query(,,po[i].t,po[i].va,));
}
}
return ;
}
codeforces 669E E. Little Artem and Time Machine(节点为map型的线段树)的更多相关文章
- CodeForces 669 E Little Artem and Time Machine CDQ分治
题目传送门 题意:现在有3种操作, 1 t x 在t秒往multiset里面插入一个x 2 t x 在t秒从multiset里面删除一个x 3 t x 在t秒查询multiset里面有多少x 事情是按 ...
- Codeforces 1175G - Yet Another Partiton Problem(李超线段树)
Codeforces 题面传送门 & 洛谷题面传送门 这是一道李超线段树的毒瘤题. 首先我们可以想到一个非常 trivial 的 DP:\(dp_{i,j}\) 表示前 \(i\) 个数划 ...
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) E. Little Artem and Time Machine 树状数组
E. Little Artem and Time Machine 题目连接: http://www.codeforces.com/contest/669/problem/E Description L ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- 计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task
E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作, ...
- 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树
题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...
随机推荐
- C# 编程中的堆栈(Stack)和队列(Queue)
一.什么是堆?(Heap) 堆是无序的,是一片不连续的内存域,由用户自己来控制和释放,如果用户自己不释放的话,当内存达到一定的特定值时,通过垃圾回收器(GC)来回收. 是程序运行期 ...
- BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏
http://www.lydsy.com/JudgeOnline/problem.php?id=1720 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1 ...
- 关于Properties 集 添加数据 遍历数据
public static void fun1(){ Properties v = new Properties(); v.setProperty("a","1" ...
- android 播放MP3
<?xml version="1.0" encoding="utf-8"?> <!-- 定义当前布局的基本LinearLayout --> ...
- Java线程池 ExecutorService
一.ExecutorService介绍 ExecutorService是Java中对线程池定义的一个接口,它java.util.concurrent包中,在这个接口中定义了和后台任务执行相关的方法: ...
- Handlebars的基本用法 Handlebars.js使用介绍 http://handlebarsjs.com/ Handlebars.js 模板引擎 javascript/jquery模板引擎——Handlebars初体验 handlebars.js 入门(1) 作为一名前端的你,必须掌握的模板引擎:Handlebars 前端数据模板handlebars与jquery整
Handlebars的基本用法 使用Handlebars,你可以轻松创建语义化模板,Mustache模板和Handlebars是兼容的,所以你可以将Mustache导入Handlebars以使用 Ha ...
- linux系列之-—03 压缩和解压缩命令
tar命令 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName gz命令 解压1:gunzip FileName.gz 解压2:gzip ...
- IOS 获取设备本地音视频
1.检索音视频 PHFetchOptions *allPhotosOptions; @property (nonatomic, strong) PHFetchResult *assetsFetchRe ...
- 设置GridCtrl中的Checkbox 为不可编辑
m_Grid.SetCellType(index, 1, CGridCtrl::CellType_Check); //设置第index行.第一列的单元格为类似CheckBox的模样 m_Gr ...
- jvm 命令
jps jps主要用来输出JVM中运行的进程状态信息.语法格式如下: jps [options] [hostid] 如果不指定hostid就默认为当前主机或服务器. -q 不输出类名.Jar名和传入 ...