Submission #287285

# Submission time Handle Problem Language Result Execution time Memory
287285 2020-08-31T14:55:31 Z arnold518 Fire (JOI20_ho_t5) C++14
100 / 100
417 ms 30272 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e5;

int N, Q, A[MAXN+10];
ll B[MAXN+10];

struct Data
{
	int y, x, p;
};

vector<Data> AV, QV;
ll ans[MAXN+10];

pll tree[MAXN+10];
pll operator + (const pll &p, const pll &q) { return {p.first+q.first, p.second+q.second}; }
void update(int i, pll p) { for(; i<=N; i+=(i&-i)) tree[i]=tree[i]+p; }
pll query(int i) { pll ret={0, 0}; for(; i>0; i-=(i&-i)) ret=ret+tree[i]; return ret; }

int main()
{
	scanf("%d%d", &N, &Q);
	for(int i=1; i<=N; i++) scanf("%d", &A[i]);
	for(int i=1; i<=N; i++) B[i]=B[i-1]+A[i];
	for(int i=1; i<=Q; i++)
	{
		int l, r, t;
		scanf("%d%d%d", &t, &l, &r);
		ans[i]+=B[r]-B[l-1];
		QV.push_back({t, r, i});
		QV.push_back({t, l-1, -i});
	}
	A[N+1]=1e9;

	vector<int> S;
	for(int i=1; i<=N+1; i++)
	{
		int bef;
		if(!S.empty()) bef=A[S.back()];
		while(!S.empty() && A[S.back()]<=A[i])
		{
			int y1=1, x1=S.back()+1, p=A[S.back()]-bef;
			int y2=i-S.back(), x2=i;
			AV.push_back({y1, x1, p});
			AV.push_back({y2, x2, -p});

			bef=A[S.back()];
			S.pop_back();
		}
		if(!S.empty())
		{
			int y1=1, x1=S.back()+1, p=A[i]-bef;
			int y2=i-S.back(), x2=i;
			AV.push_back({y1, x1, p});
			AV.push_back({y2, x2, -p});
		}
		S.push_back(i);
	}

	sort(AV.begin(), AV.end(), [&](const Data &p, const Data &q) { return p.y-p.x<q.y-q.x; });
	sort(QV.begin(), QV.end(), [&](const Data &p, const Data &q) { return p.y-p.x<q.y-q.x; });

	for(int i=0; i<=N; i++) tree[i]={0, 0};

	for(int i=0, j=0; i<QV.size(); i++)
	{
		for(; j<AV.size() && AV[j].y-AV[j].x<=QV[i].y-QV[i].x; j++)
		{
			update(AV[j].x, pll(AV[j].p, (ll)AV[j].p*(1-AV[j].x)));
		}
		pll ret=query(QV[i].x);
		if(QV[i].p>0) ans[QV[i].p]+=ret.first*QV[i].x+ret.second;
		else ans[-QV[i].p]-=ret.first*QV[i].x+ret.second;
	}

	reverse(AV.begin(), AV.end());
	reverse(QV.begin(), QV.end());

	for(int i=0; i<=N; i++) tree[i]={0, 0};

	for(int i=0, j=0; i<QV.size(); i++)
	{
		for(; j<AV.size() && AV[j].y-AV[j].x>=QV[i].y-QV[i].x+1; j++)
		{
			update(AV[j].y, pll(AV[j].p, (ll)AV[j].p*(1-AV[j].y)));	
		}
		pll ret=query(QV[i].y);
		if(QV[i].p>0) ans[QV[i].p]+=ret.first*QV[i].y+ret.second;
		else ans[-QV[i].p]-=ret.first*QV[i].y+ret.second;
	}
	for(int i=1; i<=Q; i++) printf("%lld\n", ans[i]);
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:71:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for(int i=0, j=0; i<QV.size(); i++)
      |                    ~^~~~~~~~~~
ho_t5.cpp:73:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |   for(; j<AV.size() && AV[j].y-AV[j].x<=QV[i].y-QV[i].x; j++)
      |         ~^~~~~~~~~~
ho_t5.cpp:87:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |  for(int i=0, j=0; i<QV.size(); i++)
      |                    ~^~~~~~~~~~
ho_t5.cpp:89:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |   for(; j<AV.size() && AV[j].y-AV[j].x>=QV[i].y-QV[i].x+1; j++)
      |         ~^~~~~~~~~~
ho_t5.cpp:28:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   28 |  scanf("%d%d", &N, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~
ho_t5.cpp:29:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   29 |  for(int i=1; i<=N; i++) scanf("%d", &A[i]);
      |                          ~~~~~^~~~~~~~~~~~~
ho_t5.cpp:34:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   34 |   scanf("%d%d%d", &t, &l, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 416 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 360 ms 29784 KB Output is correct
3 Correct 319 ms 29504 KB Output is correct
4 Correct 390 ms 29760 KB Output is correct
5 Correct 364 ms 29888 KB Output is correct
6 Correct 389 ms 29428 KB Output is correct
7 Correct 343 ms 30040 KB Output is correct
8 Correct 391 ms 29888 KB Output is correct
9 Correct 374 ms 29932 KB Output is correct
10 Correct 362 ms 29504 KB Output is correct
11 Correct 367 ms 30144 KB Output is correct
12 Correct 340 ms 29248 KB Output is correct
13 Correct 360 ms 29632 KB Output is correct
14 Correct 371 ms 29472 KB Output is correct
15 Correct 360 ms 29780 KB Output is correct
16 Correct 337 ms 29504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 352 ms 28868 KB Output is correct
3 Correct 395 ms 28352 KB Output is correct
4 Correct 367 ms 29248 KB Output is correct
5 Correct 396 ms 28356 KB Output is correct
6 Correct 389 ms 28736 KB Output is correct
7 Correct 364 ms 28736 KB Output is correct
8 Correct 404 ms 28736 KB Output is correct
9 Correct 360 ms 28352 KB Output is correct
10 Correct 393 ms 28120 KB Output is correct
11 Correct 400 ms 29124 KB Output is correct
12 Correct 402 ms 28608 KB Output is correct
13 Correct 384 ms 28992 KB Output is correct
14 Correct 368 ms 28480 KB Output is correct
15 Correct 403 ms 29272 KB Output is correct
16 Correct 370 ms 28608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 306 ms 25308 KB Output is correct
2 Correct 314 ms 25176 KB Output is correct
3 Correct 329 ms 25560 KB Output is correct
4 Correct 314 ms 25432 KB Output is correct
5 Correct 303 ms 25596 KB Output is correct
6 Correct 303 ms 25176 KB Output is correct
7 Correct 325 ms 25436 KB Output is correct
8 Correct 308 ms 25432 KB Output is correct
9 Correct 306 ms 25432 KB Output is correct
10 Correct 308 ms 25196 KB Output is correct
11 Correct 321 ms 25560 KB Output is correct
12 Correct 308 ms 25560 KB Output is correct
13 Correct 304 ms 25432 KB Output is correct
14 Correct 305 ms 25716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 416 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 399 ms 29540 KB Output is correct
34 Correct 409 ms 30144 KB Output is correct
35 Correct 409 ms 29800 KB Output is correct
36 Correct 410 ms 29504 KB Output is correct
37 Correct 391 ms 29504 KB Output is correct
38 Correct 402 ms 29760 KB Output is correct
39 Correct 399 ms 29632 KB Output is correct
40 Correct 396 ms 29540 KB Output is correct
41 Correct 417 ms 29760 KB Output is correct
42 Correct 385 ms 29632 KB Output is correct
43 Correct 364 ms 30244 KB Output is correct
44 Correct 329 ms 30036 KB Output is correct
45 Correct 330 ms 29376 KB Output is correct
46 Correct 360 ms 30020 KB Output is correct
47 Correct 336 ms 29376 KB Output is correct
48 Correct 326 ms 29248 KB Output is correct
49 Correct 325 ms 29544 KB Output is correct
50 Correct 354 ms 30272 KB Output is correct
51 Correct 351 ms 30144 KB Output is correct
52 Correct 333 ms 29760 KB Output is correct
53 Correct 346 ms 29936 KB Output is correct
54 Correct 357 ms 29120 KB Output is correct
55 Correct 396 ms 29268 KB Output is correct
56 Correct 371 ms 29580 KB Output is correct
57 Correct 383 ms 29244 KB Output is correct
58 Correct 396 ms 29888 KB Output is correct
59 Correct 360 ms 29784 KB Output is correct
60 Correct 319 ms 29504 KB Output is correct
61 Correct 390 ms 29760 KB Output is correct
62 Correct 364 ms 29888 KB Output is correct
63 Correct 389 ms 29428 KB Output is correct
64 Correct 343 ms 30040 KB Output is correct
65 Correct 391 ms 29888 KB Output is correct
66 Correct 374 ms 29932 KB Output is correct
67 Correct 362 ms 29504 KB Output is correct
68 Correct 367 ms 30144 KB Output is correct
69 Correct 340 ms 29248 KB Output is correct
70 Correct 360 ms 29632 KB Output is correct
71 Correct 371 ms 29472 KB Output is correct
72 Correct 360 ms 29780 KB Output is correct
73 Correct 337 ms 29504 KB Output is correct
74 Correct 352 ms 28868 KB Output is correct
75 Correct 395 ms 28352 KB Output is correct
76 Correct 367 ms 29248 KB Output is correct
77 Correct 396 ms 28356 KB Output is correct
78 Correct 389 ms 28736 KB Output is correct
79 Correct 364 ms 28736 KB Output is correct
80 Correct 404 ms 28736 KB Output is correct
81 Correct 360 ms 28352 KB Output is correct
82 Correct 393 ms 28120 KB Output is correct
83 Correct 400 ms 29124 KB Output is correct
84 Correct 402 ms 28608 KB Output is correct
85 Correct 384 ms 28992 KB Output is correct
86 Correct 368 ms 28480 KB Output is correct
87 Correct 403 ms 29272 KB Output is correct
88 Correct 370 ms 28608 KB Output is correct
89 Correct 306 ms 25308 KB Output is correct
90 Correct 314 ms 25176 KB Output is correct
91 Correct 329 ms 25560 KB Output is correct
92 Correct 314 ms 25432 KB Output is correct
93 Correct 303 ms 25596 KB Output is correct
94 Correct 303 ms 25176 KB Output is correct
95 Correct 325 ms 25436 KB Output is correct
96 Correct 308 ms 25432 KB Output is correct
97 Correct 306 ms 25432 KB Output is correct
98 Correct 308 ms 25196 KB Output is correct
99 Correct 321 ms 25560 KB Output is correct
100 Correct 308 ms 25560 KB Output is correct
101 Correct 304 ms 25432 KB Output is correct
102 Correct 305 ms 25716 KB Output is correct