QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#879030#971. Binary Search TreedengrkWA 0ms9796kbC++143.6kb2025-02-01 19:56:572025-02-01 19:56:59

Judging History

This is the latest submission verdict.

  • [2025-02-01 19:56:59]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 9796kb
  • [2025-02-01 19:56:57]
  • Submitted

answer

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int N=200003;
struct Operation{
	int u,x,t;
}op[N*2];
struct Question{
	int u,x,t,p;
}qu[N];
int n,m,q,p,a[N],b[N]; long long res[N];
namespace SGT{
	struct Node{
		int l,r,minn; long long pre,suf;
	}node[N*3];
	long long fetch_pre(int u,long long x){
		if(node[u].minn>=x) return 0;
		if(node[u].l==node[u].r) return node[u].pre;
		if(node[u*2].minn>=x) return fetch_pre(u*2+1,x);
		return fetch_pre(u*2,x)+node[u].pre-node[u*2].pre;
	}
	long long fetch_suf(int u,long long x){
		if(node[u].minn>=x) return 0;
		if(node[u].l==node[u].r) return node[u].suf;
		if(node[u*2+1].minn>=x) return fetch_suf(u*2,x);
		return fetch_suf(u*2+1,x)+node[u].suf-node[u*2+1].suf;
	}
	void push_up(int u){
		node[u].minn=min(node[u*2].minn,node[u*2+1].minn);
		node[u].pre=node[u*2].pre+fetch_pre(u*2+1,node[u*2].minn);
		node[u].suf=node[u*2+1].suf+fetch_suf(u*2,node[u*2+1].minn);
	}
	void build(int u,int l,int r,int x){
		node[u]={l,r,x,a[l],a[r]};
		if(l<r){
			int mid=(l+r)/2;
			build(u*2  ,l,mid  ,x);
			build(u*2+1,mid+1,r,x);
		}
	}
	void modify(int u,int k,int x){
		if(node[u].l==node[u].r)
			node[u].minn=x,node[u].pre=node[u].suf=a[k];
		else{
			int mid=(node[u].l+node[u].r)/2;
			modify(u*2+(k>mid),k,x); push_up(u);
		}
	}
	long long query_pre(int u,int l,int r,int& x){
		if(node[u].l>=l&&node[u].r<=r){
			long long ans=fetch_pre(u,x);
			x=min(x,node[u].minn); return ans;
		}else{
			long long ans=0;
			int mid=(node[u].l+node[u].r)/2;
			if(l<=mid) ans+=query_pre(u*2  ,l,r,x);
			if(r> mid) ans+=query_pre(u*2+1,l,r,x);
			return ans;
		}
	}
	long long query_suf(int u,int l,int r,int& x){
		if(node[u].l>=l&&node[u].r<=r){
			long long ans=fetch_suf(u,x);
			x=min(x,node[u].minn); return ans;
		}else{
			long long ans=0;
			int mid=(node[u].l+node[u].r)/2;
			if(r> mid) ans+=query_suf(u*2+1,l,r,x);
			if(l<=mid) ans+=query_suf(u*2  ,l,r,x);
			return ans;
		}
	}
}
int binary(int x){
	int l=1,r=p+1;
	while(l<r){
		int mid=(l+r)/2;
		(a[mid]>=x)?(r=mid):(l=mid+1);
	}
	return r;
}
int read(){
	char ch; int x=0;
	do ch=getchar();
	while(ch<'0'||ch>'9');
	while(ch>='0'&&ch<='9')
		x=x*10+(ch-'0'),ch=getchar();
	return x;
}
void write(int x){
	char a[12]; int n=0,i;
	do a[++n]=x%10+'0',x/=10; while(x>0);
	for(i=n;i>0;i--) putchar(a[i]);
	putchar('\n');
}
int main(){
//	freopen("BST.in","r",stdin);
//	freopen("BST.out","w",stdout);
	int i,j,k,s1,l,r,x,s2,s3;
	n=read(),s1=read();
	for(i=1;i<=s1;i++)
		switch(read()){
			case 1:
				l=read(),r=read(),x=read();
				op[++m]={l,x,i},op[++m]={r+1,x,-i};
				a[++p]=x; break;
			case 2:
				k=read(),x=read();
				q++,qu[q]={k,x,i,q},a[++p]=x; break;
		}
	sort(a+1,a+p+1),p=unique(a+1,a+p+1)-a-1;
	for(i=1;i<=m;i++) op[i].x=binary(op[i].x);
	for(i=1;i<=q;i++) qu[i].x=binary(qu[i].x);
	sort(op+1,op+m+1,[&](const Operation& a,const Operation& b){ return a.u<b.u; });
	sort(qu+1,qu+m+1,[&](const Question& a,const Question& b){ return a.u<b.u; });
	for(i=1;i<=p;i++) b[i]=s1+1;
	SGT::build(1,1,p,s1+1);
	for(i=j=k=1;i<=n&&k<=q;i++){
		while(j<=m&&op[j].u==i)
			if(op[j].t>0) SGT::modify(1,op[j].x,b[op[j].x]=op[j].t),j++;
			else SGT::modify(1,op[j].x,b[op[j].x]=s1+1),j++;
		while(k<=q&&qu[k].u==i){
			s3=min(b[qu[k].x]+1,qu[k].t);
			if(qu[k].x>0) res[qu[k].p]+=SGT::query_suf(1,1,qu[k].x,s2=s3);
			if(qu[k].x<p) res[qu[k].p]+=SGT::query_pre(1,qu[k].x+1,p,s2=s3); k++;
		}
	}
	for(i=1;i<=q;i++) write(res[i]);
//	fclose(stdin);
//	fclose(stdout);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9796kb

input:

3 9
1 1 2 2
1 1 3 1
1 2 3 3
2 1 2
2 1 4
2 2 2
2 2 4
2 3 2
2 3 4

output:

2
2
2
5
4
4

result:

ok 6 tokens

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 7752kb

input:

463 469
1 133 459 764026743
2 183 117202921
1 158 440 909903500
2 435 764026743
2 211 764026743
2 42 367154098
1 69 368 922135695
2 402 822018114
1 202 356 216161481
2 164 868998762
2 241 425329855
2 418 359511579
1 93 193 705174793
1 152 400 189968120
2 249 216161481
2 203 705174793
2 163 705174793...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 1st words differ - expected: '764026743', found: '0'