QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#376463#6509. Not Another Range Query Problem369PaiTL 29ms29600kbC++143.8kb2024-04-04 10:25:582024-04-04 10:25:58

Judging History

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

  • [2024-04-04 10:25:58]
  • 评测
  • 测评结果:TL
  • 用时:29ms
  • 内存:29600kb
  • [2024-04-04 10:25:58]
  • 提交

answer

#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int N = 5e5 + 5;
int n , q , ans[N]; string s;
struct Node
{
	int l; mutable int r; bool c;
	mutable deque<int>v;
	Node(int l = 0 , int r = 0 , bool c = 0):l(l) , r(r) , c(c){}
	bool operator < (const Node &rhs) const
	{
		return l < rhs.l;
	}
};
set<Node>st;
vector<pair<int , int> >qu[N] , q2[N];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0) , cout.tie(0);
	cin >> n >> q >> s , s = " " + s;
	for(int i = 1 ; i <= q ; i++)
	{
		int l , r , k; cin >> l >> r >> k;
		q2[k].push_back({l - 1 , i});
		qu[k].push_back({r , i});
	}
	auto join = [&](deque<int>&a , const deque<int>&b , bool pos)
	{
		if(pos)for(int x : b)a.push_back(x);
		else for(auto it = b.rbegin() ; it != b.rend() ; it++)a.push_front(*it);
	};
	auto print = [&](string info = "")
	{
		if(!info.empty())cerr << "[" << info << "]:\n";
		for(auto [l , r , c , v] : st)
		{
			cerr << "[" << l << ',' << r << "] " << c << ":";
			for(int x : v)cerr << x << " ";
			cerr << "\n";
		}
	};
	auto it = st.begin();
	for(int i = 1 ; i <= n ; i++)
	{
		if(s[i] != s[i - 1])
			it = st.insert(Node(i , i , s[i] - '0')).first;
		it->r = i , it->v.push_back(i);
	}
	for(int i = 0 ; i <= n && !st.empty() ; i++)
	{
		int j = 0 , m = qu[i].size() , s = 0; 
		sort(qu[i].begin() , qu[i].end());
		for(auto [l , r , c , v] : st)
		{
			for(; j < m && qu[i][j].fi <= r ; j++)
			{
				auto [p , id] = qu[i][j];
				// cerr << "\t" << id << " " << s << "+" << upper_bound(v.begin() , v.end() , p) - v.begin() << "\n";
				ans[id] += (s + upper_bound(v.begin() , v.end() , p) - v.begin());
			}
			s += v.size();
		}
		for(; j < m ; j++)
		{
			auto [p , id] = qu[i][j];
			ans[id] += s;
		}
		// print("query " + to_string(i));
		for(auto &nd : st)nd.v.pop_front();
		for(auto it = next(st.begin()) ; ; it++)
		{
			while(it != st.begin() && prev(it)->v.empty())st.erase(prev(it));
			if(it == st.end())break ;
		}
		// print("pop " + to_string(i));
		for(auto it = st.begin() ; it != st.end() ; it++)
		{
			auto nx = next(it);
			for(; nx != st.end() && nx->c == it->c ; nx = next(it))
			{
				it->r = nx->r;
				if(it->v.size() < nx->v.size())join(it->v , nx->v , 1);
				else join(nx->v , it->v , 0) , swap(it->v , nx->v);
				st.erase(nx);
			}
		}
		// print("merge " + to_string(i));
		// cerr << "\n";
	}
	st.clear() , it = st.begin();
	for(int i = 1 ; i <= n ; i++)
	{
		if(s[i] != s[i - 1])
			it = st.insert(Node(i , i , s[i] - '0')).first;
		it->r = i , it->v.push_back(i);
	}
	for(int i = 0 ; i <= n && !st.empty() ; i++)
	{
		int j = 0 , m = q2[i].size() , s = 0; 
		sort(q2[i].begin() , q2[i].end());
		for(auto [l , r , c , v] : st)
		{
			for(; j < m && q2[i][j].fi <= r ; j++)
			{
				auto [p , id] = q2[i][j];
				// cerr << "\t" << id << " " << s << "+" << upper_bound(v.begin() , v.end() , p) - v.begin() << "\n";
				ans[id] -= (s + upper_bound(v.begin() , v.end() , p) - v.begin());
			}
			s += v.size();
		}
		for(; j < m ; j++)
		{
			auto [p , id] = q2[i][j];
			ans[id] -= s;
		}
		// print("query " + to_string(i));
		for(auto &nd : st)nd.v.pop_back();
		for(auto it = next(st.begin()) ; ; it++)
		{
			while(it != st.begin() && prev(it)->v.empty())st.erase(prev(it));
			if(it == st.end())break ;
		}
		// print("pop " + to_string(i));
		for(auto it = st.begin() ; it != st.end() ; it++)
		{
			auto nx = next(it);
			for(; nx != st.end() && nx->c == it->c ; nx = next(it))
			{
				it->r = nx->r;
				if(it->v.size() < nx->v.size())join(it->v , nx->v , 1);
				else join(nx->v , it->v , 0) , swap(it->v , nx->v);
				st.erase(nx);
			}
		}
		// print("merge " + to_string(i));
		// cerr << "\n";
	}
	for(int i = 1 ; i <= q ; i++)
		cout << max(0 , ans[i]) << "\n";
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 29ms
memory: 29600kb

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: