Submission #205999

# Submission time Handle Problem Language Result Execution time Memory
205999 2020-03-01T19:47:21 Z arnold518 Fire (JOI20_ho_t5) C++14
100 / 100
378 ms 41544 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;

struct Query { ll x, y, idx; };
struct Point { ll x, y, v; };

struct Line
{
	ll a, b;
	Line() : a(0), b(0) {}
	Line(ll a, ll b) : a(a), b(b) {}
};
Line operator + (const Line &p, const Line &q) { return Line(p.a+q.a, p.b+q.b); }

int N, Q;
ll A[MAXN+10], B[MAXN+10];
vector<Query> query;
vector<Point> point;

struct BIT
{
	Line tree[MAXN+10];
	BIT() { for(int i=0; i<MAXN+10; i++) tree[i]=Line(); }
	void update(int i, Line p) { for(; i<=N+1; i+=(i&-i)) tree[i]=tree[i]+p; }
	Line query(int i) { Line ret; for(; i>0; i-=(i&-i)) ret=ret+tree[i]; return ret; }
} bit;

ll ans[MAXN+10];

int main()
{
	int i, j, k;

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

	vector<int> S;
	for(i=1; i<=N; i++)
	{
		while(!S.empty() && A[S.back()]<=A[i])
		{
			if(S.size()>=2)
			{
				int p=S[S.size()-2], q=S[S.size()-1];
				point.push_back({q, q-p, A[p]-A[q]});
				point.push_back({i, i-p, -A[p]+A[q]});
			}
			S.pop_back();
		}
		S.push_back(i);
	}
	while(!S.empty())
	{
		if(S.size()>=2)
		{
			int p=S[S.size()-2], q=S[S.size()-1];
			point.push_back({q, q-p, A[p]-A[q]});
			point.push_back({i, i-p, -A[p]+A[q]});
		}
		S.pop_back();
	}

	bit=BIT();
	sort(point.begin(), point.end(), [&](const Point &p, const Point &q) { return p.y-p.x<q.y-q.x; });
	sort(query.begin(), query.end(), [&](const Query &p, const Query &q) { return p.y-p.x<q.y-q.x; });

	for(i=0, j=0; i<query.size(); i++)
	{
		for(; j<point.size() && point[j].y-point[j].x<=query[i].y-query[i].x; j++) bit.update(point[j].x, {point[j].v, -point[j].v*(point[j].x-1)});
		Line t=bit.query(query[i].x);
		if(query[i].idx>0) ans[query[i].idx]+=t.a*query[i].x+t.b;
		else ans[-query[i].idx]-=t.a*query[i].x+t.b;
	}

	bit=BIT();
	sort(point.begin(), point.end(), [&](const Point &p, const Point &q) { return p.y-p.x>q.y-q.x; });
	sort(query.begin(), query.end(), [&](const Query &p, const Query &q) { return p.y-p.x>q.y-q.x; });

	for(i=0, j=0; i<query.size(); i++)
	{
		for(; j<point.size() && point[j].y-point[j].x>query[i].y-query[i].x; j++) bit.update(point[j].y, {point[j].v, -point[j].v*(point[j].y-1)});
		Line t=bit.query(query[i].y);
		if(query[i].idx>0) ans[query[i].idx]+=t.a*query[i].y+t.b;
		else ans[-query[i].idx]-=t.a*query[i].y+t.b;
	}

	for(i=1; i<=Q; i++) printf("%lld\n", ans[i]);
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:81:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0, j=0; i<query.size(); i++)
                ~^~~~~~~~~~~~~
ho_t5.cpp:83:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(; j<point.size() && point[j].y-point[j].x<=query[i].y-query[i].x; j++) bit.update(point[j].x, {point[j].v, -point[j].v*(point[j].x-1)});
         ~^~~~~~~~~~~~~
ho_t5.cpp:93:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0, j=0; i<query.size(); i++)
                ~^~~~~~~~~~~~~
ho_t5.cpp:95:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(; j<point.size() && point[j].y-point[j].x>query[i].y-query[i].x; j++) bit.update(point[j].y, {point[j].v, -point[j].v*(point[j].y-1)});
         ~^~~~~~~~~~~~~
ho_t5.cpp:38:12: warning: unused variable 'k' [-Wunused-variable]
  int i, j, k;
            ^
ho_t5.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &Q);
  ~~~~~^~~~~~~~~~~~~~~~
ho_t5.cpp:41:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=N; i++) scanf("%lld", &A[i]), B[i]=B[i-1]+A[i];
                      ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
ho_t5.cpp:45:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld%lld", &t, &l, &r);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6520 KB Output is correct
2 Correct 9 ms 6648 KB Output is correct
3 Correct 10 ms 6648 KB Output is correct
4 Correct 10 ms 6648 KB Output is correct
5 Correct 10 ms 6648 KB Output is correct
6 Correct 10 ms 6648 KB Output is correct
7 Correct 9 ms 6652 KB Output is correct
8 Correct 10 ms 6648 KB Output is correct
9 Correct 10 ms 6776 KB Output is correct
10 Correct 10 ms 6608 KB Output is correct
11 Correct 10 ms 6648 KB Output is correct
12 Correct 9 ms 6648 KB Output is correct
13 Correct 10 ms 6648 KB Output is correct
14 Correct 10 ms 6648 KB Output is correct
15 Correct 10 ms 6648 KB Output is correct
16 Correct 10 ms 6648 KB Output is correct
17 Correct 10 ms 6648 KB Output is correct
18 Correct 9 ms 6648 KB Output is correct
19 Correct 11 ms 6648 KB Output is correct
20 Correct 10 ms 6648 KB Output is correct
21 Correct 10 ms 6648 KB Output is correct
22 Correct 10 ms 6648 KB Output is correct
23 Correct 10 ms 6648 KB Output is correct
24 Correct 10 ms 6648 KB Output is correct
25 Correct 10 ms 6648 KB Output is correct
26 Correct 10 ms 6648 KB Output is correct
27 Correct 10 ms 6648 KB Output is correct
28 Correct 10 ms 6652 KB Output is correct
29 Correct 10 ms 6648 KB Output is correct
30 Correct 10 ms 6648 KB Output is correct
31 Correct 10 ms 6648 KB Output is correct
32 Correct 10 ms 6648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6520 KB Output is correct
2 Correct 318 ms 35632 KB Output is correct
3 Correct 316 ms 36984 KB Output is correct
4 Correct 336 ms 35784 KB Output is correct
5 Correct 328 ms 36040 KB Output is correct
6 Correct 373 ms 36808 KB Output is correct
7 Correct 326 ms 38092 KB Output is correct
8 Correct 363 ms 40008 KB Output is correct
9 Correct 331 ms 41544 KB Output is correct
10 Correct 322 ms 39808 KB Output is correct
11 Correct 336 ms 38344 KB Output is correct
12 Correct 315 ms 36680 KB Output is correct
13 Correct 324 ms 35664 KB Output is correct
14 Correct 336 ms 35548 KB Output is correct
15 Correct 323 ms 35656 KB Output is correct
16 Correct 324 ms 35528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6520 KB Output is correct
2 Correct 336 ms 36048 KB Output is correct
3 Correct 346 ms 35640 KB Output is correct
4 Correct 343 ms 35912 KB Output is correct
5 Correct 353 ms 35528 KB Output is correct
6 Correct 354 ms 35656 KB Output is correct
7 Correct 338 ms 35792 KB Output is correct
8 Correct 355 ms 35912 KB Output is correct
9 Correct 336 ms 35660 KB Output is correct
10 Correct 354 ms 35528 KB Output is correct
11 Correct 353 ms 35916 KB Output is correct
12 Correct 343 ms 35528 KB Output is correct
13 Correct 349 ms 35960 KB Output is correct
14 Correct 331 ms 35824 KB Output is correct
15 Correct 357 ms 35656 KB Output is correct
16 Correct 329 ms 35528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 273 ms 32072 KB Output is correct
2 Correct 273 ms 32072 KB Output is correct
3 Correct 269 ms 32456 KB Output is correct
4 Correct 271 ms 32328 KB Output is correct
5 Correct 270 ms 32332 KB Output is correct
6 Correct 274 ms 32072 KB Output is correct
7 Correct 269 ms 32328 KB Output is correct
8 Correct 276 ms 32480 KB Output is correct
9 Correct 275 ms 32324 KB Output is correct
10 Correct 284 ms 32200 KB Output is correct
11 Correct 273 ms 32624 KB Output is correct
12 Correct 279 ms 32452 KB Output is correct
13 Correct 275 ms 32328 KB Output is correct
14 Correct 271 ms 32456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6520 KB Output is correct
2 Correct 9 ms 6648 KB Output is correct
3 Correct 10 ms 6648 KB Output is correct
4 Correct 10 ms 6648 KB Output is correct
5 Correct 10 ms 6648 KB Output is correct
6 Correct 10 ms 6648 KB Output is correct
7 Correct 9 ms 6652 KB Output is correct
8 Correct 10 ms 6648 KB Output is correct
9 Correct 10 ms 6776 KB Output is correct
10 Correct 10 ms 6608 KB Output is correct
11 Correct 10 ms 6648 KB Output is correct
12 Correct 9 ms 6648 KB Output is correct
13 Correct 10 ms 6648 KB Output is correct
14 Correct 10 ms 6648 KB Output is correct
15 Correct 10 ms 6648 KB Output is correct
16 Correct 10 ms 6648 KB Output is correct
17 Correct 10 ms 6648 KB Output is correct
18 Correct 9 ms 6648 KB Output is correct
19 Correct 11 ms 6648 KB Output is correct
20 Correct 10 ms 6648 KB Output is correct
21 Correct 10 ms 6648 KB Output is correct
22 Correct 10 ms 6648 KB Output is correct
23 Correct 10 ms 6648 KB Output is correct
24 Correct 10 ms 6648 KB Output is correct
25 Correct 10 ms 6648 KB Output is correct
26 Correct 10 ms 6648 KB Output is correct
27 Correct 10 ms 6648 KB Output is correct
28 Correct 10 ms 6652 KB Output is correct
29 Correct 10 ms 6648 KB Output is correct
30 Correct 10 ms 6648 KB Output is correct
31 Correct 10 ms 6648 KB Output is correct
32 Correct 10 ms 6648 KB Output is correct
33 Correct 366 ms 35656 KB Output is correct
34 Correct 369 ms 36068 KB Output is correct
35 Correct 361 ms 35656 KB Output is correct
36 Correct 377 ms 35828 KB Output is correct
37 Correct 356 ms 35524 KB Output is correct
38 Correct 378 ms 35656 KB Output is correct
39 Correct 356 ms 35528 KB Output is correct
40 Correct 356 ms 35784 KB Output is correct
41 Correct 363 ms 35672 KB Output is correct
42 Correct 363 ms 35784 KB Output is correct
43 Correct 346 ms 35912 KB Output is correct
44 Correct 316 ms 35656 KB Output is correct
45 Correct 317 ms 35672 KB Output is correct
46 Correct 332 ms 35656 KB Output is correct
47 Correct 313 ms 35552 KB Output is correct
48 Correct 311 ms 35400 KB Output is correct
49 Correct 309 ms 35528 KB Output is correct
50 Correct 326 ms 35984 KB Output is correct
51 Correct 330 ms 35916 KB Output is correct
52 Correct 318 ms 35788 KB Output is correct
53 Correct 327 ms 35784 KB Output is correct
54 Correct 327 ms 35616 KB Output is correct
55 Correct 352 ms 35400 KB Output is correct
56 Correct 332 ms 35656 KB Output is correct
57 Correct 344 ms 35528 KB Output is correct
58 Correct 350 ms 35788 KB Output is correct
59 Correct 318 ms 35632 KB Output is correct
60 Correct 316 ms 36984 KB Output is correct
61 Correct 336 ms 35784 KB Output is correct
62 Correct 328 ms 36040 KB Output is correct
63 Correct 373 ms 36808 KB Output is correct
64 Correct 326 ms 38092 KB Output is correct
65 Correct 363 ms 40008 KB Output is correct
66 Correct 331 ms 41544 KB Output is correct
67 Correct 322 ms 39808 KB Output is correct
68 Correct 336 ms 38344 KB Output is correct
69 Correct 315 ms 36680 KB Output is correct
70 Correct 324 ms 35664 KB Output is correct
71 Correct 336 ms 35548 KB Output is correct
72 Correct 323 ms 35656 KB Output is correct
73 Correct 324 ms 35528 KB Output is correct
74 Correct 336 ms 36048 KB Output is correct
75 Correct 346 ms 35640 KB Output is correct
76 Correct 343 ms 35912 KB Output is correct
77 Correct 353 ms 35528 KB Output is correct
78 Correct 354 ms 35656 KB Output is correct
79 Correct 338 ms 35792 KB Output is correct
80 Correct 355 ms 35912 KB Output is correct
81 Correct 336 ms 35660 KB Output is correct
82 Correct 354 ms 35528 KB Output is correct
83 Correct 353 ms 35916 KB Output is correct
84 Correct 343 ms 35528 KB Output is correct
85 Correct 349 ms 35960 KB Output is correct
86 Correct 331 ms 35824 KB Output is correct
87 Correct 357 ms 35656 KB Output is correct
88 Correct 329 ms 35528 KB Output is correct
89 Correct 273 ms 32072 KB Output is correct
90 Correct 273 ms 32072 KB Output is correct
91 Correct 269 ms 32456 KB Output is correct
92 Correct 271 ms 32328 KB Output is correct
93 Correct 270 ms 32332 KB Output is correct
94 Correct 274 ms 32072 KB Output is correct
95 Correct 269 ms 32328 KB Output is correct
96 Correct 276 ms 32480 KB Output is correct
97 Correct 275 ms 32324 KB Output is correct
98 Correct 284 ms 32200 KB Output is correct
99 Correct 273 ms 32624 KB Output is correct
100 Correct 279 ms 32452 KB Output is correct
101 Correct 275 ms 32328 KB Output is correct
102 Correct 271 ms 32456 KB Output is correct