题目链接
题目保证数据随机,数据随机真的是太强了,直接可以跑最坏时候是的复杂度。
直接暴力建线段树,然后更新的时候更新到底,查询的时候也是查询到底,因为数据随机,所以其实被处理的次数是很少的,因为要刚好是set里有的,或者是set里没有的,这就使得更新的操作变得很少。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define eps 1e-8
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(x, y) make_pair(x, y)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
set<pair<int, int>> L;
int N, maxL, tree[maxN << 2] = {0};
void pushdown(int rt)
{
if(tree[rt])
{
tree[lsn] += tree[rt]; tree[rsn] += tree[rt];
tree[rt] = 0;
}
}
void pushup(int rt)
{
int tmp = min(tree[lsn], tree[rsn]);
tree[rt] += tmp;
tree[lsn] -= tmp; tree[rsn] -= tmp;
}
void update(int rt, int l, int r, int ql, int qr, int val)
{
if(ql <= l && qr >= r) { tree[rt] += val; return; }
pushdown(rt);
int mid = HalF;
if(qr <= mid) update(QL, val);
else if(ql > mid) update(QR, val);
else { update(QL, val); update(QR, val); }
pushup(rt);
}
int query(int rt, int l, int r)
{
if(tree[rt]) return r - l + 1;
if(l == r) return 0;
int mid = HalF;
return query(Lson) + query(Rson);
}
int main()
{
scanf("%d%d", &N, &maxL);
while(N--)
{
int op, x, y;
scanf("%d%d%d", &op, &x, &y);
if(op == 1)
{
if(L.find(MP(x,y)) != L.end()) continue;
L.insert(MP(x,y));
update(1, 1, maxL, x, y, 1);
}
if(op == 2)
{
if(L.find(MP(x,y)) == L.end()) continue;
L.erase(MP(x,y));
update(1, 1, maxL, x, y, -1);
}
if(op == 3)
{
printf("%d\n", query(1, 1, maxL));
}
}
return 0;
}
由于更新次数很少,所以这样的写法或许会更好
减少了查询所用的次数:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define eps 1e-8
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(x, y) make_pair(x, y)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
set<pair<int, int>> L;
int N, maxL, tree[maxN << 2] = {0}, sum[maxN << 4] = {0};
void pushup(int rt, int l, int r)
{
if(tree[rt]) sum[rt] = r - l + 1;
else sum[rt] = sum[lsn] + sum[rsn];
}
void update(int rt, int l, int r, int ql, int qr, int val)
{
if(ql <= l && qr >= r)
{
tree[rt] += val;
pushup(myself);
return;
}
int mid = HalF;
if(qr <= mid) update(QL, val);
else if(ql > mid) update(QR, val);
else { update(QL, val); update(QR, val); }
pushup(myself);
}
int main()
{
scanf("%d%d", &N, &maxL);
while(N--)
{
int op, x, y;
scanf("%d%d%d", &op, &x, &y);
if(op == 1)
{
if(L.find(MP(x,y)) != L.end()) continue;
L.insert(MP(x,y));
update(1, 1, maxL, x, y, 1);
}
if(op == 2)
{
if(L.find(MP(x,y)) == L.end()) continue;
L.erase(MP(x,y));
update(1, 1, maxL, x, y, -1);
}
if(op == 3)
{
printf("%d\n", sum[1]);
}
}
return 0;
}