QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#211625#4622. Mario Partyyiyiyi#AC ✓2001ms52692kbC++172.3kb2023-10-12 19:31:202023-10-12 19:31:20

Judging History

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

  • [2023-10-12 19:31:20]
  • 评测
  • 测评结果:AC
  • 用时:2001ms
  • 内存:52692kb
  • [2023-10-12 19:31:20]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define rep(i,s,t) for(int i=(s);i<=(t);i++)
#define rev_rep(i,s,t) for(int i=(s);i>=(t);i--)
using namespace std;
ll ci(){
	ll x; scanf("%lld",&x); return x;
}

enum{N=2000023};

class UNI{
public:
	int fa[N];
	void init(int n){
		rep(i,1,n) fa[i] = i;
	}
	int fd(int x){
		return fa[x]==x ? x : fa[x]=fd(fa[x]);
	}
	int operator[](int x){ return fd(x); }
	bool merge(int x,int y){
		int fx=fd(x), fy=fd(y);
		if( fx!=fy ){
			fa[fx] = fy;
			return 1;
		} return 0;
	}
}uni;

using Set = set<pair<int,int> >;
Set st;
int tag = 0;

struct sth{
	int p, x, id;
	bool operator<(const sth&e) const {
		if( p!=e.p ) return p<e.p;
		return x>e.x;
	}
}d[N];

int I[N];
int ans[N];

int a[N];

signed main(){
	int T = ci();
	while( T-- ){
		int n = ci(), m = ci();
		rep(i,1,n) a[i] = ci();
		int tot = 0;
		rep(i,1,m){
			int l = ci(), r = ci(), x = ci();
			d[++tot] = {l,x,i};
			d[++tot] = {r,-1,i};
		}
		st.clear();
		st.insert({1e9,-1});
		tag = 0;
		sort(d+1, d+tot+1);
		int j = 1;
		uni.init(m);
		rep(i,1,n){
		//printf("line %d:\n", __LINE__);
			tag += a[i];
			vector<pair<int,int> > v;
			while( st.begin()->first+tag < 0 ){
				v.push_back({st.begin()->first+tag-a[i], st.begin()->second});
				st.erase(st.begin());
			}
			for(auto o:v){
				int x = o.first, id = o.second;
		//printf("line %d: %d %d\n", __LINE__, x,id);
				auto it = st.lower_bound({x-tag, -1});
				if( it->first == x-tag ){
		//printf("line %d: %d %d merge %d\n", __LINE__, x,id, it->second);
					uni.merge(id, it->second);
				}else{
					I[id] = x-tag;
					st.insert({x-tag,id});
				}
			}
			while( j<=tot && d[j].p<=i ){
				int x = d[j].x, id = d[j].id;
		//printf("line %d: %d %d\n", __LINE__, x,id);
				if( x==-1 ){
		//printf("line %d: id %d %d\n", __LINE__, id, uni[id]);
		//rep(i,1,m) printf("%lld ", uni[i]); puts("");
					ans[id] = I[uni[id]] + tag;
		//printf("line %d: id %d\n", __LINE__, id);
				}else{
					auto it = st.lower_bound({x-tag, -1});
					if( it->first == x-tag ){
						uni.merge(id, it->second);
					}else{
						I[id] = x-tag;
						st.insert({x-tag,id});
					}
				}
				j++;
			}
		}
		rep(i,1,m) printf("%d\n", ans[i]);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2001ms
memory: 52692kb

input:

4
500000 500000
2 -1 2 -1 1 -1 -4 -2 0 -3 4 1 -3 3 5 1 -3 -2 0 3 -2 0 -1 0 0 -3 -2 0 0 1 -2 -3 -4 2 5 0 -1 -2 2 2 -2 -1 2 0 0 -2 3 -2 -4 -3 -3 -1 0 2 1 0 2 -1 -3 0 -2 3 2 -2 1 4 -1 -1 -2 0 2 -2 1 -4 -5 -1 -2 -3 -2 1 4 2 -3 0 1 -1 2 2 0 5 -3 0 2 -1 5 -3 5 -4 -4 0 2 2 1 -1 -6 0 2 -3 2 3 0 0 -2 3 4 2 1...

output:

1363
427
212
818
736
132
561
623
81
800
12
971
218
28
278
737
581
363
25
231
216
642
684
283
1033
982
469
106
156
197
992
384
230
112
226
38
31
561
637
43
623
246
397
1640
1252
1354
4
1338
168
266
364
68
10
435
1391
229
106
0
1114
1416
201
416
206
113
474
190
612
206
109
670
34
0
804
604
376
164
251...

result:

ok 2000000 lines