CF916D Jamie and To-do List 主席树 留坑

https://www.luogu.org/problemnew/solution/CF916D

D. Jamie and To-do List

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Why I have to finish so many assignments???

Jamie is getting very busy with his school life. He starts to forget the assignments that he has to do. He decided to write the things down on a to-do list. He assigns a value priority for each of his assignment (lower value means more important) so he can decide which he needs to spend more time on.

After a few days, Jamie finds out the list is too large that he can't even manage the list by himself! As you are a good friend of Jamie, help him write a program to support the following operations on the to-do list:

  • set ai xi — Add assignment ai to the to-do list if it is not present, and set its priority to xi. If assignment ai is already in the to-do list, its priority is changed to xi.
  • remove ai — Remove assignment ai from the to-do list if it is present in it.
  • query ai — Output the number of assignments that are more important (have a smaller priority value) than assignment ai, so Jamie can decide a better schedule. Output  - 1 if ai is not in the to-do list.
  • undo di — Undo all changes that have been made in the previous di days (not including the day of this operation)

At day 0, the to-do list is empty. In each of the following q days, Jamie will do exactly one out of the four operations. If the operation is a query, you should output the result of the query before proceeding to the next day, or poor Jamie cannot make appropriate decisions.

Input

The first line consists of a single integer q (1 ≤ q ≤ 105) — the number of operations.

The following q lines consists of the description of the operations. The i-th line consists of the operation that Jamie has done in the i-th day. The query has the following format:

The first word in the line indicates the type of operation. It must be one of the following four: set, remove, query, undo.

  • If it is a set operation, a string ai and an integer xi follows (1 ≤ xi ≤ 109). ai is the assignment that need to be set to priority xi.
  • If it is a remove operation, a string ai follows. ai is the assignment that need to be removed.
  • If it is a query operation, a string ai follows. ai is the assignment that needs to be queried.
  • If it is a undo operation, an integer di follows (0 ≤ di < i). di is the number of days that changes needed to be undone.

All assignment names ai only consists of lowercase English letters and have a length 1 ≤ |ai| ≤ 15.

It is guaranteed that the last operation is a query operation.

Output

For each query operation, output a single integer — the number of assignments that have a priority lower than assignment ai, or  - 1 if aiis not in the to-do list.

Interaction

If the operation is a query, you should output the result of the query and flush the output stream before proceeding to the next operation. Otherwise, you may get the verdict Idleness Limit Exceed.

For flushing the output stream, please refer to the documentation of your chosen programming language. The flush functions of some common programming languages are listed below:

  • C: fflush(stdout);
  • C++: cout « flush;
  • Java: System.out.flush();

 注意标注释的地方 修改两次就不能和之前的板子一样了 不然修改岔开了-,-

#include<bits/stdc++.h>
#define ll long long
#define dprintf if (debug) printf
#define rep(i, j, k) for(int i=j; i<k; i++)
const int debug = 0;
const int maxn = 1e5+5;
const int n = 1e9+1e3;
using namespace std;
map<string, int> f;
struct Tree{
	int l, r, w;
}T[maxn * 100];
int wt[maxn], root[maxn], cnt, top, id, p, v, q;
string s;


inline int getid(string s){
	if (f[s]) return f[s];
	return f[s] = ++top;
}
void update(int old, int &recent, int l, int r, int pos, int x){
	recent = ++cnt;	T[recent] = T[old]; T[recent].w+=x;  
	dprintf("update %d %d %d %d pos = %d x = %d\n", old, recent, l, r, pos, x);
	dprintf("w = %d\n", T[recent].w);
	if (l == r) return;
	int mid = l+r>>1;
	if (pos <= mid) update(T[old].l, T[recent].l, l, mid, pos, x);
	else update(T[old].r, T[recent].r, mid+1, r, pos, x);
}
int query(int o, int l, int r, int ql, int qr){
	dprintf("query %d %d %d ql = %d qr = %d\n", o, l, r, ql, qr);
	if (ql<=l && r<=qr){
		return T[o].w;
	}
	int mid = l+r>>1;
	int ans = 0;
	if (ql <= mid) ans += query(T[o].l, l, mid, ql, qr);
	if (qr >= mid+1) ans += query(T[o].r, mid+1, r, ql, qr);
	return ans;
}

int main(){
	scanf("%d", &q);
	rep(i, 0, q){
		cin>>s;
		wt[i] = wt[i-1]; root[i] = root[i-1];
		if (s[0] == 's'){
			cin>>s>>v;
			id = getid(s);
			p = query(wt[i], 1, n, id, id);
			dprintf("p = %d\n", p);
			if (p) update(root[i], root[i], 1, n, p, -1);
			update(root[i], root[i], 1, n, v, 1);
			update(wt[i], wt[i], 1, n, id, v - p);
			//很重要 不能update(wt[i-1], wt[i], 1, n, id, v - p);
			//因为两次修改
		}
		else if (s[0] == 'q'){
			cin>>s;
			id = getid(s);
			p = query(wt[i], 1, n, id, id);
			if (p > 1) printf("%d\n", query(root[i], 1, n, 1, p-1));
			else if (p == 1) puts("0");
			else puts("-1");
			fflush(stdout);
		}
		else if (s[0] == 'r'){
			cin>>s;
			id = getid(s);
			p = query(wt[i], 1, n, id, id);
			if (p) update(root[i], root[i], 1, n, p, -1);
			update(wt[i], wt[i], 1, n, id, -p);
		}
		else{
			cin>>v;
			root[i] = root[i-v-1];
			wt[i] = wt[i-v-1];
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值