1679C - Rooks Defenders - CodeForces Solution


data structures implementation *1400

Please click on ads to support us..

Python Code:

from sys import stdin
input = stdin.buffer.readline


class FenwickTree:
	def __init__(self, n):
		self.n = n
		self.fenwick_tree = [0 for _ in range(self.n + 1)]

	def update(self, i, v):
		i += 1

		while i <= self.n:
			self.fenwick_tree[i] += v
			i += i & -i

	def get_sum(self, i):
		s = 0
		i += 1

		while i > 0:
			s += self.fenwick_tree[i]
			i -= i & -i

		return s

def func():
	r = [0 for __ in range(n+1)]
	c = [0 for __ in range(n+1)]

	row = FenwickTree(n + 1)
	col = FenwickTree(n + 1)

	for i in range(q):
		t, *inp = list(map(int, input().split()))
		if t == 1:
			x, y = inp
			if r[y] == 0:
				row.update(y, 1)
			if c[x] == 0:
				col.update(x, 1)
			r[y] += 1
			c[x] += 1

		elif t == 2:
			x, y = inp
			if r[y] == 1:
				row.update(y, -1)
			if c[x] == 1:
				col.update(x, -1)
			r[y] -= 1
			c[x] -= 1

		else:
			x1, y1, x2, y2 = inp
			if row.get_sum(y2) - row.get_sum(y1 - 1) == y2 - y1 + 1:
				print('YES')
			elif col.get_sum(x2) - col.get_sum(x1 - 1) == x2 - x1 + 1:
				print('YES')
			else:
				print('NO')


n, q = map(int, input().split())
func()

C++ Code:

#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N = 2e5+10;

struct node{
	int l,r;
	int v;
	int sum;// 和 
}tr[4*N],trr[4*N];

void build(struct node tr[],int u, int l, int r)
{
	tr[u] = {l,r};
	if(l == r) return ;
	int mid = l + r >> 1;
	build(tr,u << 1,l,mid),build(tr,u << 1 | 1,mid + 1,r);
}

void pushup(struct node tr[],int u)
{
	tr[u].sum = tr[u<<1].sum + tr[u<<1|1].sum;
}

int query(struct node tr[],int u, int l, int r)
{
	if(tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
	
	int sum = 0;
	int mid = tr[u].l + tr[u].r >> 1;
	if(l <= mid) sum += query(tr,u << 1,l,r);
	if(mid < r) sum += query(tr,u << 1 | 1,l,r);
	return sum;
}

void modify(struct node tr[],int u, int x, int v)
{
	if(tr[u].l == x && tr[u].r == x) 
	{
		tr[u].v += v;
		tr[u].sum = (tr[u].v > 0);
	}
	else
	{
		int mid = tr[u].l + tr[u].r >> 1;
		if(x <= mid) modify(tr,u << 1, x, v);
		else modify(tr,u << 1 | 1, x, v);
		pushup(tr,u);
	}
}

void solve()
{
	int n,q;
	cin >> n >> q;
	build(tr,1,1,n), build(trr,1,1,n);
	for(int i = 0; i < q; i ++)
	{
		int t;
		scanf("%lld",&t);
		if(t == 1)
		{
			int x,y;
			scanf("%lld %lld",&x,&y);
			modify(tr,1,x,1);
			modify(trr,1,y,1);
		}
		else if(t == 2)
		{
			int x,y;
			scanf("%lld %lld",&x,&y);
			modify(tr,1,x,-1);
			modify(trr,1,y,-1);
		}
		else
		{
			int x,y,xx,yy;
			scanf("%lld %lld %lld %lld",&x,&y,&xx,&yy);
			int cntx = query(tr,1,x,xx), cnty = query(trr,1,y,yy);
			if(cntx == xx - x + 1 || cnty == yy - y + 1) printf("Yes\n");
			else printf("No\n");
		}
	}
}

int main()
{
   int T = 1;
//   cin >> T;
   while(T--) solve();
} 


Comments

Submit
0 Comments
More Questions

1720A - Burenka Plays with Fractions
3A - Shortest path of the king
1720C - Corners
574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils
1670A - Prof Slim
1189A - Keanu Reeves
678A - Johny Likes Numbers
1699C - The Third Problem
1697D - Guess The String
754B - Ilya and tic-tac-toe game
760A - Petr and a calendar
1573A - Countdown
166A - Rank List
1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum