QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#376517#6509. Not Another Range Query ProblemTx_LcyTL 21ms48676kbC++142.3kb2024-04-04 11:12:282024-04-04 11:12:28

Judging History

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

  • [2024-04-04 11:12:28]
  • 评测
  • 测评结果:TL
  • 用时:21ms
  • 内存:48676kb
  • [2024-04-04 11:12:28]
  • 提交

answer

//A tree without skin will surely die.
//A man without face will be alive.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mid ((l+r)>>1)
#define lowbit(x) (x&-x)
#define all(x) (x).begin(),(x).end()
#define rep(i,j,k) for (int i=j;i<=k;++i)
#define per(i,j,k) for (int i=j;i>=k;--i)
int const N=5e5+10;
int n,q,fa[N],Fa[N],ans[N],rig[N],rk1[N],rk2[N];string s;
struct Tree_Array{
	int c[N];
	inline void clear(){memset(c,0,sizeof(c));}
	inline void update(int x,int v){while (x<N) c[x]+=v,x+=lowbit(x);}
	inline int query(int x){int r=0;while (x) r+=c[x],x-=lowbit(x);return r;}
}T;
inline int find(int x){return (x==fa[x])?(x):(fa[x]=find(fa[x]));}
inline int Find(int x){return (x==Fa[x])?(x):(Fa[x]=Find(Fa[x]));}
vector< pair<int,int> >qry1[N],qry2[N];
inline void solve(){
	cin>>n>>q>>s,s=" "+s;
	rep(i,1,q){
		int l,r,k;cin>>l>>r>>k;
		if (k==0) ans[i]=r-l+1;
		else{
			qry1[k].push_back({i,r});
			qry2[k].push_back({i,l});
		}
	}
	memset(rk1,0x3f,sizeof(rk1));
	{
		set<int>leftpos;
		rep(i,1,n){
			int j=i;
			while (j+1<=n && s[j+1]==s[i]) ++j;
			leftpos.insert(i),i=j;
		}
		rep(i,1,n) T.update(i,1),fa[i]=Fa[i]=i;
		rep(i,1,n){
			if (!leftpos.size()) break;
			set<int>nw;
			for (auto j:leftpos) fa[j]=find(j+1),Fa[j]=Find(j-1),T.update(j,-1);
			for (auto j:leftpos) if (s[find(j)]==s[j] && s[find(j)]!=s[Find(find(j)-1)]) nw.insert(find(j));
			for (auto g:qry1[i])
				rk2[g.first]=T.query(g.second);
			leftpos=nw;
		}
	}
	T.clear();
	{
		set<int>leftpos;
		rep(i,1,n){
			int j=i;
			while (j+1<=n && s[j+1]==s[i]) ++j;
			leftpos.insert(i),rig[i]=j,i=j;
		}
		rep(i,1,n) T.update(i,1);
		rep(i,1,n){
			if (!leftpos.size()) break;
			set<int>nw;
			for (auto j:leftpos){
				auto it=leftpos.find(j);
				if (it==leftpos.end()) T.update(rig[j],-1),--rig[j];
				else{
					++it;
					if (s[*it]!=s[j]) T.update(rig[j],-1),--rig[j];
				}
				if (rig[j]>=j) nw.insert(j);
			}
			for (auto g:qry2[i])
				rk1[g.first]=T.query(g.second-1)+1;
			leftpos=nw;
		}
	}
	rep(i,1,q)
		if (ans[i]) cout<<ans[i]<<'\n';
		else cout<<max(0ll,rk2[i]-rk1[i]+1)<<'\n';
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int t=1;
	// cin>>t;
	while (t--) solve();
	cerr<<"Time: "<<(double)clock()/CLOCKS_PER_SEC<<" s\n";
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 43804kb

input:

9 7
100110001
2 5 1
3 6 1
4 8 2
2 7 1
1 9 1
1 9 0
1 9 8

output:

2
1
1
3
4
9
0

result:

ok 7 numbers

Test #2:

score: 0
Accepted
time: 21ms
memory: 48676kb

input:

100 100000
0000011010101000111011110110000111110101101010111111101011011010111001111010111000001000011000001010
76 99 3
25 84 7
45 83 11
10 12 10
69 86 4
27 28 1
22 42 42
4 86 25
26 91 22
20 81 17
50 78 0
77 93 50
31 50 34
7 46 13
78 89 0
79 98 0
2 84 33
58 93 11
56 75 2
55 77 68
7 9 41
44 46 11
47 ...

output:

8
13
4
0
3
0
0
0
0
4
29
0
0
0
12
20
0
0
5
0
0
0
0
0
0
0
0
10
18
1
0
57
0
0
11
0
3
0
0
3
0
0
0
0
0
0
0
0
0
0
0
0
0
0
19
0
0
0
12
5
0
0
2
0
0
0
0
10
12
0
0
0
5
0
8
0
1
16
0
19
29
40
21
12
26
0
21
6
0
10
18
0
3
0
2
5
0
0
5
0
0
0
51
0
0
0
18
11
0
20
5
9
10
0
16
22
0
20
0
26
0
0
0
0
0
0
11
46
59
2
9
43
1...

result:

ok 100000 numbers

Test #3:

score: -100
Time Limit Exceeded

input:

100000 100000
0000000000000100001000000010000000010100000100000000000000100000010000000000000000000001100010000000100000000000000000000000000000000000010000000000001000000100000010000000000000000000000100001010000000000000100000101000001000010000000110000000001001100001001000001000000000000000000000...

output:


result: