Submission #29938675


Source Code Expand

#include <iostream>
#include <algorithm>

using namespace std;
using u64 = unsigned long long;


constexpr u64 bitfreq(int ond, int offd, int t){
	return t ? (bitfreq(ond, offd, t-1) << offd) << ond | ~((~(u64)0)<<ond) : 0;
}
constexpr u64 bitfreq(int ond, int offd, int t, int d){
	return bitfreq(ond, offd, t) << d;
}

const int B = 12;
const int BS = 5;
const u64 MaskAll = bitfreq(1,0,B*BS);

int N;
u64 A[1024][128];

u64 buf[4][B];
void rotate_buf(){
	for(int t=0; t<4; t++){
		for(int i : { 0,2,4,6,8,10 }){
			u64 a = buf[t][i], b = buf[t][i+1];
			buf[t][i+1] = (a & bitfreq(BS,BS,B/2)) | ((b & bitfreq(BS,BS,B/2)) << BS);
			buf[t][i] = ((a & bitfreq(BS,BS,B/2,BS)) >> BS) | (b & bitfreq(BS,BS,B/2,BS));
		}
		for(int i : { 0,1,4,5,8,9 }){
			u64 a = buf[t][i], b = buf[t][i+2];
			buf[t][i+2] = (a & bitfreq(10,10,3)) | ((b & bitfreq(10,10,3)) << 10);
			buf[t][i] = ((a & bitfreq(10,10,3,10)) >> 10) | (b & bitfreq(10,10,3,10));
		}
		for(int i : { 0,1,2,3 }){
			u64 a = buf[t][i], b = buf[t][i+4], c = buf[t][i+8];
			constexpr u64 m1 = bitfreq(20,40,1,0);
			constexpr u64 m2 = bitfreq(20,40,1,20);
			constexpr u64 m3 = bitfreq(20,40,1,40);
			buf[t][i+8] = ((a & m1)) | ((b & m1) << 20) | ((c & m1) << 40);
			buf[t][i+4] = ((a & m2) >> 20) | ((b & m2)) | ((c & m2) << 20);
			buf[t][i+0] = ((a & m3) >> 40) | ((b & m3) >> 20) | ((c & m3));
		}
	}
}

void load_buf(int id, int ly, int lx){
	int x = lx / B;
	int bsl = (lx - x*B) * BS;
	int bsr = (B * BS - bsl);
	if(lx == x * B){
		for(int y=0; y<B; y++) buf[id][y] = A[ly+y][x];
	}
	else{
		for(int y=0; y<B; y++){
			buf[id][y] = (A[ly+y][x] >> bsl) | (A[ly+y][x+1] << bsr);
			buf[id][y] &= MaskAll;
		}
	}
}
void set_buf(int id, int ly, int lx){
	int x = lx / B;
	int bsl = (lx - x*B) * BS;
	int bsr = (B * BS - bsl);
	if(lx == x * B){
		for(int y=0; y<B; y++) A[ly+y][x] = buf[id][y];
	}
	else{
		for(int y=0; y<B; y++){
			A[ly+y][x] = (A[ly+y][x] & ~(MaskAll << bsl)) | (buf[id][y] << bsl) & MaskAll;
			A[ly+y][x+1] = (A[ly+y][x+1] & ~(MaskAll >> bsr)) | (buf[id][y] >> bsr) & MaskAll;
		}
	}
}

u64 get(int y, int x){
	return (A[y][x/B] >> (x%B*BS)) & 31;
}
void set(int y, int x, u64 d){
	A[y][x/B] = (A[y][x/B] & ~((u64)31 << (x%B*BS))) | (d << (x%B*BS));
}


void rotate(int ly, int lx, int d){
	int rx = lx + d, ry = ly + d;
	int dd = (d/2) / B * B;
	for(int dx=0; dx<dd; dx+=B) for(int dy=0; dy<dd; dy+=B){
		load_buf(0, ly+dy  ,lx+dx  );
		load_buf(1, ry-dx-B,lx+dy  );
		load_buf(2, ry-dy-B,rx-dx-B);
		load_buf(3, ly+dx  ,rx-dy-B);
		rotate_buf();
		set_buf(3, ly+dy  ,lx+dx  );
		set_buf(0, ry-dx-B,lx+dy  );
		set_buf(1, ry-dy-B,rx-dx-B);
		set_buf(2, ly+dx  ,rx-dy-B);
	}
	for(int dx=dd; dx<d-dd; dx++) for(int dy=0; dy<dd; dy++){
		u64 a = get(ly+dy, lx+dx);
		set(ly+dy, lx+dx, get(ly+dx, rx-dy-1));
		set(ly+dx, rx-dy-1, get(ry-dy-1, rx-dx-1));
		set(ry-dy-1, rx-dx-1, get(ry-dx-1, lx+dy));
		set(ry-dx-1, lx+dy, a);
	}
	for(int dx=dd; dx<(d+1)/2; dx++) for(int dy=dd; dy<d/2; dy++){
		u64 a = get(ly+dy, lx+dx);
		set(ly+dy, lx+dx, get(ly+dx, rx-dy-1));
		set(ly+dx, rx-dy-1, get(ry-dy-1, rx-dx-1));
		set(ry-dy-1, rx-dx-1, get(ry-dx-1, lx+dy));
		set(ry-dx-1, lx+dy, a);
	}
}


#include <string>

void input(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> N;
	int Q; cin >> Q;
	for(int y=0; y<N; y++){
		string S; cin >> S;
		for(int x=0; x<N; x++){
			u64 d = (S[x] - 'a');
			A[y][x/B] |= d << (x%B*BS);
		}
	}
	for(int i=0; i<Q; i++){
		int lx,ly,d; cin >> ly >> lx >> d; ly--; lx--;
		rotate(ly,lx,d);
	}
}

void output(){
	for(int y=0; y<N; y++){
		string S;
		for(int x=0; x<N; x++){
			u64 d = (A[y][x/B] >> (x%B*BS)) & 31;
			S.push_back('a' + d);
		}
		cout << S << '\n';
	}
}

int main(){
	input();
	output();
	return 0;
}


Submission Info

Submission Time
Task rotate - 回転
User Nachia
Language C++ (GCC 9.2.1)
Score 100
Code Size 3891 Byte
Status AC
Exec Time 1203 ms
Memory 4620 KiB

Compile Error

./Main.cpp: In function ‘void set_buf(int, int, int)’:
./Main.cpp:70:72: warning: suggest parentheses around arithmetic in operand of ‘|’ [-Wparentheses]
   70 |    A[ly+y][x] = (A[ly+y][x] & ~(MaskAll << bsl)) | (buf[id][y] << bsl) & MaskAll;
      |                                                    ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
./Main.cpp:71:76: warning: suggest parentheses around arithmetic in operand of ‘|’ [-Wparentheses]
   71 |    A[ly+y][x+1] = (A[ly+y][x+1] & ~(MaskAll >> bsr)) | (buf[id][y] >> bsr) & MaskAll;
      |                                                        ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~

Judge Result

Set Name Set01 Set02 Set03 Set04 Set05 Set06 Set07 Set08 Set09 Set10
Score / Max Score 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10
Status
AC × 2
AC × 2
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
Set Name Test Cases
Set01 01-01, 01-02
Set02 02-01, 02-02
Set03 03-01
Set04 04-01
Set05 05-01
Set06 06-01
Set07 07-01
Set08 08-01
Set09 09-01
Set10 10-01
Case Name Status Exec Time Memory
01-01 AC 9 ms 3688 KiB
01-02 AC 4 ms 3580 KiB
02-01 AC 716 ms 4620 KiB
02-02 AC 8 ms 3468 KiB
03-01 AC 775 ms 4552 KiB
04-01 AC 841 ms 4588 KiB
05-01 AC 893 ms 4620 KiB
06-01 AC 944 ms 4556 KiB
07-01 AC 1008 ms 4616 KiB
08-01 AC 1061 ms 4556 KiB
09-01 AC 1111 ms 4556 KiB
10-01 AC 1203 ms 4516 KiB