Submission #528887

#TimeUsernameProblemLanguageResultExecution timeMemory
528887LucaDantasWorst Reporter 3 (JOI18_worst_reporter3)C++17
100 / 100
570 ms31768 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

constexpr int maxn = 5e5+10;

int d[maxn], qtd[maxn];

int32_t main() {
	int n, q; scanf("%lld %lld", &n, &q);
	d[0] = 1;
	for(int i = 1; i <= n; i++)
		scanf("%lld", d+i);

	vector<int> st; st.push_back(0);
	for(int i = 1; i <= n; i++) {
		if(d[st.back()] < d[i])
			st.push_back(i);
	}

	qtd[0] = 1;
	for(int i = 1; i < st.size(); i++) {
		int last = st[i-1], aq = st[i];
		qtd[st[i]] = ((d[st[i]] + qtd[st[i-1]] - 1) / qtd[st[i-1]]) * qtd[st[i-1]];
	}

	for(int i = 1; i <= n; i++)
		if(!qtd[i]) qtd[i] = qtd[i-1];

	/* for(int i = 0; i <= n; i++)
		printf("%lld ", qtd[i]);
	puts(""); */

	for(int i = 0; i < q; i++) {
		int t, L, R; scanf("%lld %lld %lld", &t, &L, &R);

		// printf("\ntempo %lld\n", t);

		if(t < L) { puts("0"); continue; }

		int l = 0, r = n, GeqL = -1; // quero o primeiro cara maior ou igual a l
		while(l <= r) {
			int m = (l+r) >> 1;
			int pos = t - (t % qtd[m]) - m;
			if(pos >= L) GeqL = m, l = m+1;
			else r = m-1;
			// printf("%lld -> %lld\n", m, pos);
		}

		assert(GeqL != -1);

		if(t <= R) { printf("%lld\n", GeqL+1); continue; }

		l = 0, r = n;
		int GR = 0;
		while(l <= r) {
			int m = (l+r) >> 1;
			int pos = t - (t % qtd[m]) - m;
			if(pos > R) GR = m, l = m+1;
			else r = m-1;
			// printf("%lld -> %lld\n", m, pos);
		}

		assert(GR != -1);

		// printf("%lld %lld\n", GeqL, GR);
		printf("%lld\n", GeqL - GR);
	}
}

Compilation message (stderr)

worst_reporter3.cpp: In function 'int32_t main()':
worst_reporter3.cpp:23:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  for(int i = 1; i < st.size(); i++) {
      |                 ~~^~~~~~~~~~~
worst_reporter3.cpp:24:7: warning: unused variable 'last' [-Wunused-variable]
   24 |   int last = st[i-1], aq = st[i];
      |       ^~~~
worst_reporter3.cpp:24:23: warning: unused variable 'aq' [-Wunused-variable]
   24 |   int last = st[i-1], aq = st[i];
      |                       ^~
worst_reporter3.cpp:11:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |  int n, q; scanf("%lld %lld", &n, &q);
      |            ~~~~~^~~~~~~~~~~~~~~~~~~~~
worst_reporter3.cpp:14:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |   scanf("%lld", d+i);
      |   ~~~~~^~~~~~~~~~~~~
worst_reporter3.cpp:36:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |   int t, L, R; scanf("%lld %lld %lld", &t, &L, &R);
      |                ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...