답안 #285481

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
285481 2020-08-29T07:01:43 Z arnold518 Progression (NOI20_progression) C++14
57 / 100
1748 ms 94352 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 = 3e5;

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

struct Node
{
	ll sum, len, cnt, lval, lcnt, rval, rcnt;
	Node() : sum(0), len(0), cnt(0), lval(-1), lcnt(0), rval(-1), rcnt(0) {}
};

Node operator + (const Node &p, const Node &q)
{
	Node ret;
	ret.sum=p.sum+q.sum;
	ret.len=p.len+q.len;
	ret.lval=p.lval;
	ret.lcnt=p.lcnt;
	if(p.lcnt==p.len && q.lval==p.lval) ret.lcnt=p.lcnt+q.lcnt;
	ret.rval=q.rval;
	ret.rcnt=q.rcnt;
	if(q.rcnt==q.len && p.rval==q.rval) ret.rcnt=p.rcnt+q.rcnt;
	ret.cnt=max(p.cnt, q.cnt);
	if(p.rval==q.lval) ret.cnt=max(ret.cnt, p.rcnt+q.lcnt);
	return ret;	
}

Node tree[MAXN*4+10];
pll lazy[MAXN*4+10];

void busy(int node, int tl, int tr)
{
	if(lazy[node].first==0) return;
	else if(lazy[node].first==1)
	{
		tree[node].sum+=lazy[node].second*tree[node].len;
		tree[node].lval+=lazy[node].second;
		tree[node].rval+=lazy[node].second;

		if(tl!=tr)
		{
			if(lazy[node*2].first==0) lazy[node*2]=lazy[node];
			else lazy[node*2].second+=lazy[node].second;

			if(lazy[node*2+1].first==0) lazy[node*2+1]=lazy[node];
			else lazy[node*2+1].second+=lazy[node].second;			
		}
	}
	else if(lazy[node].first==2)
	{
		tree[node].sum=lazy[node].second*tree[node].len;
		tree[node].cnt=tree[node].lcnt=tree[node].rcnt=tree[node].len;
		tree[node].lval=tree[node].rval=lazy[node].second;

		if(tl!=tr)
		{
			lazy[node*2]=lazy[node];
			lazy[node*2+1]=lazy[node];
		}
	}
	lazy[node]=pll(0, 0);
}

void init(int node, int tl, int tr)
{
	if(tl==tr)
	{
		tree[node].sum=A[tl];
		tree[node].len=1;
		tree[node].cnt=tree[node].lcnt=tree[node].rcnt=1;
		tree[node].lval=tree[node].rval=A[tl];
		return;
	}
	int mid=tl+tr>>1;
	init(node*2, tl, mid);
	init(node*2+1, mid+1, tr);
	tree[node]=tree[node*2]+tree[node*2+1];
}

void update1(int node, int tl, int tr, int l, int r, ll k)
{
	l=max(1, l); r=min(N, r);
	if(l>r) return;
	busy(node, tl, tr);
	if(r<tl || tr<l) return;
	if(l<=tl && tr<=r)
	{
		lazy[node]=pll(1, k);
		busy(node, tl, tr);
		return;
	}
	int mid=tl+tr>>1;
	update1(node*2, tl, mid, l, r, k);
	update1(node*2+1, mid+1, tr, l, r, k);
	tree[node]=tree[node*2]+tree[node*2+1];
}

void update2(int node, int tl, int tr, int l, int r, ll k)
{
	l=max(1, l); r=min(N, r);
	if(r>N) return;
	busy(node, tl, tr);
	if(r<tl || tr<l) return;
	if(l<=tl && tr<=r)
	{
		lazy[node]=pll(2, k);
		busy(node, tl, tr);
		return;
	}
	int mid=tl+tr>>1;
	update2(node*2, tl, mid, l, r, k);
	update2(node*2+1, mid+1, tr, l, r, k);
	tree[node]=tree[node*2]+tree[node*2+1];
}

Node query(int node, int tl, int tr, int l, int r)
{
	l=max(1, l); r=min(N, r);
	if(l>r) return Node();
	busy(node, tl, tr);
	if(l==tl && tr==r) return tree[node];
	int mid=tl+tr>>1;
	if(r<=mid) return query(node*2, tl, mid, l, r);
	if(mid+1<=l) return query(node*2+1, mid+1, tr, l, r);
	return query(node*2, tl, mid, l, mid)+query(node*2+1, mid+1, tr, mid+1, r);
}

void debug(int node, int tl, int tr)
{
	busy(node, tl, tr);
	if(tl==tr) return;
	int mid=tl+tr>>1;
	debug(node*2, tl, mid);
	debug(node*2+1, mid+1, tr);
	tree[node]=tree[node*2]+tree[node*2+1];
	Node &t=tree[node];
	printf("DEBUG %d %d : sum %lld len %lld cnt %lld lval %lld lcnt %lld rval %lld rcnt %lld\n", tl, tr, t.sum, t.len, t.cnt, t.lval, t.lcnt, t.rval, t.rcnt);
}

int main()
{
	scanf("%d%d", &N, &Q);
	for(int i=1; i<=N; i++) scanf("%lld", &A[i]);

	for(int i=N; i>=1; i--) A[i]=A[i]-A[i-1];
	init(1, 1, N);

	while(Q--)
	{
		//for(int i=1; i<=N; i++) printf("%lld ", query(1, 1, N, 1, i).sum); printf("\n");
		//debug(1, 1, N);
		int t, l, r, s, c;
		scanf("%d", &t);
		if(t==1)
		{
			scanf("%d%d%d%d", &l, &r, &s, &c);
			int t=query(1, 1, N, 1, r+1).sum;
			update1(1, 1, N, l+1, r, c);
			update1(1, 1, N, l, l, s);
			t-=query(1, 1, N, 1, r).sum;
			update2(1, 1, N, r+1, r+1, t);
		}
		else if(t==2)
		{
			scanf("%d%d%d%d", &l, &r, &s, &c);
			update2(1, 1, N, l+1, r, c);
			update2(1, 1, N, l, l, -query(1, 1, N, 1, l-1).sum+s);
			t-=query(1, 1, N, 1, r).sum;
			update2(1, 1, N, r+1, r+1, t);
		}
		else if(t==3)
		{
			int l, r;
			scanf("%d%d", &l, &r);
			if(l==r) printf("1\n");
			else printf("%d\n", query(1, 1, N, l+1, r).cnt+1);
			Node t=query(1, 1, N, l+1, r);
			//printf("Query %lld %lld %lld %lld %lld %lld %lld\n", t.sum, t.len, t.cnt, t.lval, t.lcnt, t.rval, t.rcnt);
		}
	}
}

Compilation message

Progression.cpp: In function 'void init(int, int, int)':
Progression.cpp:81:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   81 |  int mid=tl+tr>>1;
      |          ~~^~~
Progression.cpp: In function 'void update1(int, int, int, int, int, ll)':
Progression.cpp:99:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |  int mid=tl+tr>>1;
      |          ~~^~~
Progression.cpp: In function 'void update2(int, int, int, int, int, ll)':
Progression.cpp:117:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  117 |  int mid=tl+tr>>1;
      |          ~~^~~
Progression.cpp: In function 'Node query(int, int, int, int, int)':
Progression.cpp:129:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  129 |  int mid=tl+tr>>1;
      |          ~~^~~
Progression.cpp: In function 'void debug(int, int, int)':
Progression.cpp:139:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  139 |  int mid=tl+tr>>1;
      |          ~~^~~
Progression.cpp: In function 'int main()':
Progression.cpp:183:18: warning: format '%d' expects argument of type 'int', but argument 2 has type 'll' {aka 'long long int'} [-Wformat=]
  183 |    else printf("%d\n", query(1, 1, N, l+1, r).cnt+1);
      |                 ~^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                  |                               |
      |                  int                             ll {aka long long int}
      |                 %lld
Progression.cpp:184:9: warning: variable 't' set but not used [-Wunused-but-set-variable]
  184 |    Node t=query(1, 1, N, l+1, r);
      |         ^
Progression.cpp:149:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  149 |  scanf("%d%d", &N, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~
Progression.cpp:150:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  150 |  for(int i=1; i<=N; i++) scanf("%lld", &A[i]);
      |                          ~~~~~^~~~~~~~~~~~~~~
Progression.cpp:160:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  160 |   scanf("%d", &t);
      |   ~~~~~^~~~~~~~~~
Progression.cpp:163:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  163 |    scanf("%d%d%d%d", &l, &r, &s, &c);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Progression.cpp:172:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  172 |    scanf("%d%d%d%d", &l, &r, &s, &c);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Progression.cpp:181:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  181 |    scanf("%d%d", &l, &r);
      |    ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 602 ms 77152 KB Output is correct
2 Correct 270 ms 69312 KB Output is correct
3 Correct 265 ms 69472 KB Output is correct
4 Correct 267 ms 69404 KB Output is correct
5 Correct 276 ms 69496 KB Output is correct
6 Correct 272 ms 69388 KB Output is correct
7 Correct 293 ms 69416 KB Output is correct
8 Correct 39 ms 66040 KB Output is correct
9 Correct 38 ms 66040 KB Output is correct
10 Correct 39 ms 66040 KB Output is correct
11 Correct 611 ms 77224 KB Output is correct
12 Correct 607 ms 77048 KB Output is correct
13 Correct 602 ms 77692 KB Output is correct
14 Correct 627 ms 77528 KB Output is correct
15 Correct 606 ms 77304 KB Output is correct
16 Correct 608 ms 77004 KB Output is correct
17 Correct 606 ms 77148 KB Output is correct
18 Correct 607 ms 77180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 66168 KB Output is correct
2 Correct 37 ms 66040 KB Output is correct
3 Incorrect 42 ms 66160 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 611 ms 75640 KB Output is correct
2 Correct 199 ms 68800 KB Output is correct
3 Correct 195 ms 68728 KB Output is correct
4 Correct 164 ms 68728 KB Output is correct
5 Correct 218 ms 69108 KB Output is correct
6 Correct 203 ms 68984 KB Output is correct
7 Correct 211 ms 68984 KB Output is correct
8 Correct 37 ms 66040 KB Output is correct
9 Correct 40 ms 66040 KB Output is correct
10 Correct 42 ms 66200 KB Output is correct
11 Correct 668 ms 74312 KB Output is correct
12 Correct 566 ms 75512 KB Output is correct
13 Correct 678 ms 74140 KB Output is correct
14 Correct 660 ms 74104 KB Output is correct
15 Correct 567 ms 75560 KB Output is correct
16 Correct 685 ms 76304 KB Output is correct
17 Correct 663 ms 76152 KB Output is correct
18 Correct 673 ms 76320 KB Output is correct
19 Correct 565 ms 75512 KB Output is correct
20 Correct 691 ms 75512 KB Output is correct
21 Correct 575 ms 75640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1100 ms 90872 KB Output is correct
2 Incorrect 287 ms 69496 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 611 ms 75640 KB Output is correct
2 Correct 199 ms 68800 KB Output is correct
3 Correct 195 ms 68728 KB Output is correct
4 Correct 164 ms 68728 KB Output is correct
5 Correct 218 ms 69108 KB Output is correct
6 Correct 203 ms 68984 KB Output is correct
7 Correct 211 ms 68984 KB Output is correct
8 Correct 37 ms 66040 KB Output is correct
9 Correct 40 ms 66040 KB Output is correct
10 Correct 42 ms 66200 KB Output is correct
11 Correct 668 ms 74312 KB Output is correct
12 Correct 566 ms 75512 KB Output is correct
13 Correct 678 ms 74140 KB Output is correct
14 Correct 660 ms 74104 KB Output is correct
15 Correct 567 ms 75560 KB Output is correct
16 Correct 685 ms 76304 KB Output is correct
17 Correct 663 ms 76152 KB Output is correct
18 Correct 673 ms 76320 KB Output is correct
19 Correct 565 ms 75512 KB Output is correct
20 Correct 691 ms 75512 KB Output is correct
21 Correct 575 ms 75640 KB Output is correct
22 Correct 1604 ms 94352 KB Output is correct
23 Correct 303 ms 69368 KB Output is correct
24 Correct 305 ms 69368 KB Output is correct
25 Correct 317 ms 69448 KB Output is correct
26 Correct 306 ms 69368 KB Output is correct
27 Correct 303 ms 69368 KB Output is correct
28 Correct 303 ms 69496 KB Output is correct
29 Correct 39 ms 66040 KB Output is correct
30 Correct 41 ms 66040 KB Output is correct
31 Correct 39 ms 66040 KB Output is correct
32 Correct 1600 ms 91496 KB Output is correct
33 Correct 1516 ms 94068 KB Output is correct
34 Correct 1634 ms 91528 KB Output is correct
35 Correct 1625 ms 91400 KB Output is correct
36 Correct 1330 ms 91252 KB Output is correct
37 Correct 1323 ms 91256 KB Output is correct
38 Correct 1300 ms 91172 KB Output is correct
39 Correct 1603 ms 94208 KB Output is correct
40 Correct 1735 ms 94312 KB Output is correct
41 Correct 1748 ms 94236 KB Output is correct
42 Correct 1731 ms 94288 KB Output is correct
43 Correct 1644 ms 94160 KB Output is correct
44 Correct 1540 ms 94160 KB Output is correct
45 Correct 1520 ms 94200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 602 ms 77152 KB Output is correct
2 Correct 270 ms 69312 KB Output is correct
3 Correct 265 ms 69472 KB Output is correct
4 Correct 267 ms 69404 KB Output is correct
5 Correct 276 ms 69496 KB Output is correct
6 Correct 272 ms 69388 KB Output is correct
7 Correct 293 ms 69416 KB Output is correct
8 Correct 39 ms 66040 KB Output is correct
9 Correct 38 ms 66040 KB Output is correct
10 Correct 39 ms 66040 KB Output is correct
11 Correct 611 ms 77224 KB Output is correct
12 Correct 607 ms 77048 KB Output is correct
13 Correct 602 ms 77692 KB Output is correct
14 Correct 627 ms 77528 KB Output is correct
15 Correct 606 ms 77304 KB Output is correct
16 Correct 608 ms 77004 KB Output is correct
17 Correct 606 ms 77148 KB Output is correct
18 Correct 607 ms 77180 KB Output is correct
19 Correct 38 ms 66168 KB Output is correct
20 Correct 37 ms 66040 KB Output is correct
21 Incorrect 42 ms 66160 KB Output isn't correct
22 Halted 0 ms 0 KB -