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 ...
随机推荐
- CentOS配置TFTP服务器
服务器端 软件包 tftp-server 启动脚本 /usr/sbin/in.tftpd 启动服务 /usr/lib/systemd/system/tftp.service 配置文件 /etc/xin ...
- hdu - 3836 Equivalent Sets(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...
- Iass、Pass、SasS三种云服务区别?
Iass.Pass.SasS三种云服务区别 我们可以把云计算理解成一栋大楼,而这栋楼又可以分为顶楼.中间.低层三大块.那么我们就可以把Iass(基础设施).Pass(平台).Sass(软件)理解成这栋 ...
- linux svn配置hooks
先创建仓库: svnadmin create /data/svn/my.com 再配置权限: #cd /data/svn/my.com/conf/ #vim svnserve.conf 配置 [gen ...
- centos的python2.6.x升级到python2.7.x方法;python2.6.x的版本就不要用了
python2.6.x的版本,现在使用的很多插件都不支持了.所以如果你的centos还是使用的2.6.x版本,不要犹豫,赶紧升级到2.7.x版本 1.所谓升级,就是再安装一个python2.7.x版本 ...
- fedora关闭防火墙
sudo systemctl stop iptables sudo sytemctl stop firewalld
- javaweb 中的过滤器 包装器
过滤器要做的事情: 请求过滤器:完毕安全检查,又一次格式化请求首部或体.建立请求审计或日志 响应过滤器: 压缩响应流,追加或改动响应流创建一个全然不同的响应. 过滤器和servlet三个相似地 ...
- GridView的经常使用属性
1.android:numColumns="auto_fit" //GridView的列数设置为自己主动 2.android:columnWidth="90dp &q ...
- 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List
题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...
- IO模型:同步、异步、阻塞、非阻塞
前言: 在Linux的网络编程中,同步IO(synchronous IO).异步IO(asynchronous IO).阻塞IO(blocking IO).非阻塞IO(non-blocking IO) ...