开O2才可以进1s

65ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include<bits/stdc++.h>
using namespace std;

using ll=long long;
int P;

using i64 = long long;
// assume -P <= x < 2P
int norm(int x) {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
template<class T>
T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
struct Z {
int x;
Z(int x = 0) : x(norm(x)) {}
Z(i64 x) : x(norm(x % P)) {}
int val() const {
return x;
}
Z operator-() const {
return Z(norm(P - x));
}
Z inv() const {
assert(x != 0);
return power(*this, P - 2);
}
Z &operator*=(const Z &rhs) {
x = i64(x) * rhs.x % P;
return *this;
}
Z &operator+=(const Z &rhs) {
x = norm(x + rhs.x);
return *this;
}
Z &operator-=(const Z &rhs) {
x = norm(x - rhs.x);
return *this;
}
Z &operator/=(const Z &rhs) {
return *this *= rhs.inv();
}
friend Z operator*(const Z &lhs, const Z &rhs) {
Z res = lhs;
res *= rhs;
return res;
}
friend Z operator+(const Z &lhs, const Z &rhs) {
Z res = lhs;
res += rhs;
return res;
}
friend Z operator-(const Z &lhs, const Z &rhs) {
Z res = lhs;
res -= rhs;
return res;
}
friend Z operator/(const Z &lhs, const Z &rhs) {
Z res = lhs;
res /= rhs;
return res;
}
friend std::istream &operator>>(std::istream &is, Z &a) {
i64 v;
is >> v;
a = Z(v);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Z &a) {
return os << a.val();
}
};

template < typename T, auto op, auto e, typename F, auto mapping, auto composition,
auto e1 >
class segtree
{
int n;
vector< T > v;
vector< F > lazy;
void build(int now, int l, int r, T a[])
{
if(l == r)
{
v[now] = a[l];
return;
}
int mid = (l + r) / 2;
build(now * 2, l, mid, a);
build(now * 2 + 1, mid + 1, r, a);
v[now] = op(v[now * 2], v[now * 2 + 1]);
}
void pushup(int k) { v[k] = op(v[k * 2], v[k * 2 + 1]); }
void pushdown(int k, int l, int r)
{
v[k] = mapping(v[k], lazy[k], l, r);
if (l != r) {
lazy[k * 2] = composition(lazy[k * 2], lazy[k]);
lazy[k * 2 + 1] = composition(lazy[k * 2 + 1], lazy[k]);
}
lazy[k] = e1();
}

void modify(int now, int ql, int qr, int l, int r, F x)
{
pushdown(now, l, r);
if (l > qr || r < ql) return;
if (l >= ql && r <= qr) {
lazy[now] = x;
pushdown(now, l, r);
return;
}
modify(now * 2, ql, qr, l, (l + r) / 2, x);
modify(now * 2 + 1, ql, qr, (l + r) / 2 + 1, r, x);
pushup(now);
}
T query(int now, int ql, int qr, int l, int r)
{
pushdown(now, l, r);
if (l > qr || r < ql) return e();
if (l >= ql && r <= qr) return v[now];
return op(query(now * 2, ql, qr, l, (l + r) / 2),
query(now * 2 + 1, ql, qr, (l + r) / 2 + 1, r));
}

public:
segtree(T a[], int n) : segtree(n)
{
build(1, 1, n, a);
}
segtree(int n)
{
this->n = n;
v = vector< T >(n << 2, e());
lazy = vector< F >(n << 2, e1());
}

void modify(int l, int r, F x) { modify(1, l, r, 1, n, x); }
T query(int l, int r) { return query(1, l, r, 1, n); }
};

template<typename T>
T MAX(T x, T y) {if(x > y) return x; else return y;}
template<typename T>
T MIN(T x, T y) {if(x < y) return x; else return y;}
template<typename T>
T PLUS(T x, T y) {return x + y;}
template<typename T,typename D>
T LAZY(T x,T lazy,D l,D r){return x + (r - l + 1) * lazy;}

/* Note:
typename T, auto op, T e,
typename F, auto mapping, auto composition,
F e1
*/
using segtree_add=segtree< ll, PLUS<ll>, [](){return 0ll;}, ll,[](ll x, ll lazy, int l, int r) { return x + (r - l + 1) * lazy; }, PLUS<ll>, [](){return 0ll;} >;
//using segtree_min=segtree< ll, MIN<ll>, (ll)1e9, ll,[](ll x, ll lazy, int l, int r) { return x+lazy; },PLUS<ll>, 0 >;
//using segtree_add=segtree< ll, PLUS<ll>, 0, ll,LAZY<ll,int>, PLUS<ll>, 0 >;
struct F{
Z mul;
Z add;
};
using segtree_add_mul=segtree< Z, [](Z x, Z y) { return x + y; }, [](){return Z(0);},
F, [](Z x, F t, int l, int r) { return x * t.mul + (r - l + 1) * t.add; },
[](F t1, F t2)->F { return {(t1.mul * t2.mul), (t1.add * t2.mul + t2.add)}; },
[]()->F{return {1, 0};} >;
ll n,m;

int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>P;
segtree_add_mul s(n);
for(int i=1,x;i<=n;++i)cin>>x,s.modify(i,i,F{0,x});
for(int i=1,op,x,y,z;i<=m;++i){
cin>>op>>x>>y;
if(op==1){
cin>>z;
s.modify(x,y,F{z,0});
}else if(op==2){
cin>>z;
s.modify(x,y,F{1ll,z});
}else {
cout<<s.query(x,y)<<"\n";
}
}
}

把lamda函数换成模板函数可以降到C++17

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<bits/stdc++.h>
using namespace std;

using ll=long long;

template < typename T, auto op, T e, typename F, auto mapping, auto composition,
F e1 >
class segtree
{
int n;
vector< T > v;
vector< F > lazy;
void build(int now, int l, int r, T a[])
{
if(l == r)
{
v[now] = a[l];
return;
}
int mid = (l + r) / 2;
build(now * 2, l, mid, a);
build(now * 2 + 1, mid + 1, r, a);
v[now] = op(v[now * 2], v[now * 2 + 1]);
}
void pushup(int k) { v[k] = op(v[k * 2], v[k * 2 + 1]); }
void pushdown(int k, int l, int r)
{
v[k] = mapping(v[k], lazy[k], l, r);
if (l != r) {
lazy[k * 2] = composition(lazy[k * 2], lazy[k]);
lazy[k * 2 + 1] = composition(lazy[k * 2 + 1], lazy[k]);
}
lazy[k] = e1;
}

void modify(int now, int ql, int qr, int l, int r, F x)
{
pushdown(now, l, r);
if (l > qr || r < ql) return;
if (l >= ql && r <= qr) {
lazy[now] = x;
pushdown(now, l, r);
return;
}
modify(now * 2, ql, qr, l, (l + r) / 2, x);
modify(now * 2 + 1, ql, qr, (l + r) / 2 + 1, r, x);
pushup(now);
}
T query(int now, int ql, int qr, int l, int r)
{
pushdown(now, l, r);
if (l > qr || r < ql) return e;
if (l >= ql && r <= qr) return v[now];
return op(query(now * 2, ql, qr, l, (l + r) / 2),
query(now * 2 + 1, ql, qr, (l + r) / 2 + 1, r));
}

public:
segtree(T a[], int n) : segtree(n)
{
build(1, 1, n, a);
}
segtree(int n)
{
this->n = n;
v = vector< T >(n << 2, e);
lazy = vector< F >(n << 2, e1);
}

void modify(int l, int r, F x) { modify(1, l, r, 1, n, x); }
T query(int l, int r) { return query(1, l, r, 1, n); }
};

template<typename T>
T MAX(T x, T y) {if(x > y) return x; else return y;}
template<typename T>
T MIN(T x, T y) {if(x < y) return x; else return y;}
template<typename T>
T PLUS(T x, T y) {return x + y;}
template<typename T,typename D>
T LAZY(T x,T lazy,D l,D r){return x + (r - l + 1) * lazy;}

/* Note:
typename T, auto op, T e,
typename F, auto mapping, auto composition,
F e1
*/
//using segtree_add=segtree< ll, PLUS<ll>, 0, ll,[](ll x, ll lazy, int l, int r) { return x + (r - l + 1) * lazy; }, PLUS<ll>, 0 >;
//using segtree_min=segtree< ll, MIN<ll>, (ll)1e9, ll,[](ll x, ll lazy, int l, int r) { return x+lazy; },PLUS<ll>, 0 >;
using segtree_add=segtree< ll, PLUS<ll>, 0, ll,LAZY<ll,int>, PLUS<ll>, 0 >;
ll n,m,a[(int)1e5+9];

int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;++i)cin>>a[i];
segtree_add seg(a,n);
for(int i=1,op,x,y,z;i<=m;++i){
cin>>op>>x>>y;
if(op&1){
cin>>z;
seg.modify(x,y,z);
}else cout<<seg.query(x,y)<<"\n";
}
}

参考:将线段树封装成模板

线段树封装成模板需要C++20 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include<bits/stdc++.h>
using namespace std;

using ll=long long;

template < typename T, auto op, T e, typename F, auto mapping, auto composition,
F e1 >
class segtree
{
int n;
vector< T > v;
vector< F > lazy;
void build(int now, int l, int r, T a[])
{
if(l == r)
{
v[now] = a[l];
return;
}
int mid = (l + r) / 2;
build(now * 2, l, mid, a);
build(now * 2 + 1, mid + 1, r, a);
v[now] = op(v[now * 2], v[now * 2 + 1]);
}
void pushup(int k) { v[k] = op(v[k * 2], v[k * 2 + 1]); }
void pushdown(int k, int l, int r)
{
v[k] = mapping(v[k], lazy[k], l, r);
if (l != r) {
lazy[k * 2] = composition(lazy[k * 2], lazy[k]);
lazy[k * 2 + 1] = composition(lazy[k * 2 + 1], lazy[k]);
}
lazy[k] = e1;
}

void modify(int now, int ql, int qr, int l, int r, F x)
{
pushdown(now, l, r);
if (l > qr || r < ql) return;
if (l >= ql && r <= qr) {
lazy[now] = x;
pushdown(now, l, r);
return;
}
modify(now * 2, ql, qr, l, (l + r) / 2, x);
modify(now * 2 + 1, ql, qr, (l + r) / 2 + 1, r, x);
pushup(now);
}
T query(int now, int ql, int qr, int l, int r)
{
pushdown(now, l, r);
if (l > qr || r < ql) return e;
if (l >= ql && r <= qr) return v[now];
return op(query(now * 2, ql, qr, l, (l + r) / 2),
query(now * 2 + 1, ql, qr, (l + r) / 2 + 1, r));
}

public:
segtree(T a[], int n) : segtree(n)
{
build(1, 1, n, a);
}
segtree(int n)
{
this->n = n;
v = vector< T >(n << 2, e);
lazy = vector< F >(n << 2, e1);
}

void modify(int l, int r, F x) { modify(1, l, r, 1, n, x); }
T query(int l, int r) { return query(1, l, r, 1, n); }
};

template<typename T>
T MAX(T x, T y) {if(x > y) return x; else return y;}
template<typename T>
T MIN(T x, T y) {if(x < y) return x; else return y;}
template<typename T>
T PLUS(T x, T y) {return x + y;}

/* Note:
typename T, auto op, T e,
typename F, auto mapping, auto composition,
F e1
*/
using segtree_add=segtree< ll, PLUS<ll>, 0, ll,[](ll x, ll lazy, int l, int r) { return x + (r - l + 1) * lazy; }, PLUS<ll>, 0 >;
using segtree_min=segtree< ll, MIN<ll>, (ll)1e9, ll,[](ll x, ll lazy, int l, int r) { return x+lazy; },PLUS<ll>, 0 >;

ll n,m,a[(int)1e5+9];

int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;++i)cin>>a[i];
segtree_add seg(a,n);
for(int i=1,op,x,y,z;i<=m;++i){
cin>>op>>x>>y;
if(op&1){
cin>>z;
seg.modify(x,y,z);
}else cout<<seg.query(x,y)<<"\n";
}
}

以后小t出的题还是赶紧跑,D是个原味大模拟。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll tt,n;
string s;
map<char,int> cov;
map<int,char> qsb;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>tt;
cov['n']=1,cov['i']=2,cov['w']=3;
qsb[1]='n',qsb[2]='i',qsb[3]='w';
f(sb,1,tt){
cin>>n;
map<int,map<int,vi>>tmp;
f(i,1,n){
//cerr<<i<<"??";
cin>>s;
map<char,int> cnt;
for(auto it:s)cnt[it]+=1;
for(auto [aa,bb]:cnt){
if(bb==2){
if(cnt['n']==0)tmp[cov[aa]][1].push_back(i);
if(cnt['i']==0)tmp[cov[aa]][2].push_back(i);
if(cnt['w']==0)tmp[cov[aa]][3].push_back(i);
break;
}
if(bb==3){
if(cnt['n']==0)tmp[cov[aa]][1].push_back(i);
if(cnt['i']==0)tmp[cov[aa]][2].push_back(i);
if(cnt['w']==0)tmp[cov[aa]][3].push_back(i);
break;
}
}
}
//you_meiyou_youdexuhao
vector<array<int,4>> ans;
f(i,1,3){
for(auto &[__,vv]:tmp[i]){
while(!vv.empty()){
if(!tmp[__][i].empty()){
ans.push_back({i,vv[0],__,tmp[__][i][0]});
vv.erase(vv.begin()),tmp[__][i].erase(tmp[__][i].begin());
}else break;
}
}
}
f(i,1,3){
for(auto &[__,vv]:tmp[i]){
while(!vv.empty()){
for(auto &[aa,bb]:tmp[__]){
if(!bb.empty()){
ans.push_back({i,vv[0],__,bb[0]});
vv.erase(vv.begin());
if(aa!=i)tmp[i][aa].push_back(bb[0]);
bb.erase(bb.begin());
}
}
}
}
}
cout<<ans.size()<<"\n";
for(auto [aa,bb,cc,dd]:ans)cout<<bb<<" "<<qsb[aa]<<" "<<dd<<" "<<qsb[cc]<<"\n";
}
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<bits/stdc++.h>

using namespace std;

using ll=long long;

ll query(ll x){
cout<<"walk "<<x<<endl;
cin>>x;
return x;
}

void guess(ll x){
cout<<"guess "<<x<<endl;
exit(0);
}

int main(){
std::random_device rd;
std::default_random_engine eng(rd());
std::uniform_int_distribution<int> num(1,(int)1e9);
ll ma=0,x;
for(int i=1;i<=3333;++i)ma=max(ma,query(num(eng)));
ll step=0;
map<ll,ll>cn;
for(int i=1;i<=3333;++i){
step+=1,x=query(1);
if(cn.count(x))guess(step-cn[x]);
else cn[x]=step;
}
step+=ma,x=query(ma);
if(cn.count(x))guess(step-cn[x]);
else cn[x]=step;
for(int i=1;i<=3333;++i){
step+=3333,x=query(3333);
if(cn.count(x))guess(step-cn[x]);
else cn[x]=step;
}
guess(ma);
}
0%