QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#345866#8305. Acceleratorblack_moonrise#TL 0ms0kbC++143.0kb2024-03-07 16:29:342024-03-07 16:29:34

Judging History

你现在查看的是最新测评结果

  • [2024-03-07 16:29:34]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-03-07 16:29:34]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define MP make_pair
#define PB push_back
#define FF first
#define SS second
typedef long long ll;
const int MAXN = 200111;
const int K = 11;
int n, m;
struct SGT {
	struct node {
		pair<int,int> mn[K];
		void set1(pair<int,int> v = MP(1e9, 1e9)) {
			mn[0] = v;
			for (int i=1; i<K; i++) mn[i] = MP(1e9, 1e9);
		}
		node() {
			set1();
		}
		node operator + (const node &t) const {
			node ret;
			pair<int,int> tmp[K+K];
			merge(mn, mn+K, t.mn, t.mn+K, tmp);
			for (int i=0; i<K; i++) ret.mn[i] = tmp[i];
			return ret;
		}
	} a[MAXN * 4];
	#define ls p<<1
	#define rs p<<1|1
	void update(int p) {
		a[p] = a[ls] + a[rs];
	}
	void modify(int x, pair<int,int> v, int l, int r, int p) {
		if (l==r) {
			a[p].set1(v);
			return;
		}
		int m = l+r>>1;
		if (x<=m) modify(x, v, l, m, ls);
		else modify(x, v, m+1, r, rs);
		update(p);
	}
	node query(int x, int y, int l, int r, int p) {
		if (x<=l && r<=y) {
			return a[p];
		}
		node ret;
		int m = l+r>>1;
		if (x<=m && m < y) return query(x, y, l, m, ls) + query(x, y, m+1, r, rs);
		else if (x<=m) return query(x, y, l, m, ls);
		else return query(x, y, m+1, r, rs);
	}
}T;
struct BIT {
	ll a[MAXN];
	void add(int x, ll v) {
		for (int i=x; i<MAXN; i+=i&(-i)) {
			a[i] += v;
		}
	}
	ll query(int x) {
		ll sum = 0;
		for (int i=x; i>0; i-=i&(-i)) {
			sum += a[i];
		}
		return sum;
	}
}B;
map<int,set<int> > pos;
int a[MAXN], b[MAXN];
int nxt[MAXN];
void modifyseg(int l, int r, int coef) {
	if (coef==1) {
		cout<<"modify:"<<l<<","<<r<<endl;
		T.modify(l, MP(r, l), 1, n, 1);
	}
}
void modify(int x, int c, int coef) {
	int pre = -1, nxt = -1;
	if (coef==1) {
		pos[c].insert(x);
	}
	auto it = pos[c].lower_bound(x);
	auto it2 = it;
	if (it!=pos[c].begin()) {
		it2--;
		pre = *it2;
		modifyseg(*it2, *it, coef);
	}
	it2 = it; it2++;
	if (it2 != pos[c].end()) {
		nxt = *it2;
		modifyseg(*it, *it2, coef);
	}
	if (coef==-1) {
		pos[c].erase(x);
	}
	modifyseg(pre, nxt, -coef);
}
void replace(int x, int c, int v, bool del = true) {
	// cout<<"replace:"<<x<<","<<c<<","<<v<<endl;
	if (del) modify(x, a[x], -1);
	a[x] = c;
	modify(x, a[x], 1);

	B.add(x, v - b[x]);
	b[x] = v;
}
ll query(int x, int c) {
	cout<<"query:"<<x<<","<<c<<endl;
	SGT::node ret = T.query(x, n, 1, n, 1);
	int r = min(n+1, ret.mn[c].FF);
	ll ans = B.query(r-1) - B.query(x-1);
	for (int i=0; i<c; i++) {
		auto p = ret.mn[i];
		cout<<p.FF<<","<<p.SS<<" ";
		if (p.FF <= n) {
			ans -= min(b[p.FF], b[p.SS]);
		}
	}

	cout<<endl;
	return ans;
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i=1; i<=n; i++) {
		int c, v;
		scanf("%d%d", &c, &v);
		replace(i, c, v, false);
	}

	for (int i=1; i<=m; i++) {
		int op, x, y, z;
		scanf("%d%d%d", &op, &x, &y);
		cout<<"==="<<op<<","<<x<<","<<y<<endl;
		if (op==1) {
			scanf("%d", &z);
			replace(x, y, z);
		} else {
			printf("%lld\n", query(x, y));
		}
	}
	return 0;
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

3
3
1 2 3
1
10
4
5 5 5 5

output:

===5,5,5
query:5,5

result: