# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
577295 |
2022-06-14T12:52:47 Z |
Ronin13 |
Zamjene (COCI16_zamjene) |
C++14 |
|
2348 ms |
144688 KB |
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;
const ll mod = 1e9 + 7;
ll pr = 1000003;
ll pr1 = 1e9 + 9;
const int NMAX = 1e6 + 1;
map<pll, ll> mp;
ll par[NMAX];
ll p[NMAX];
ll po[NMAX][2];
ll a[NMAX][2];
ll b[NMAX][2];
ll q[NMAX];
ll sz[NMAX];
int bad = 0;
ll curans = 0;
void make_set(int v){
par[v] = v;
sz[v] = 1;
a[v][0] += po[p[v]][0];
b[v][0] += po[q[v]][0];
a[v][1] += po[p[v]][1];
b[v][1] += po[q[v]][1];
if(a[v][0] != b[v][0] && a[v][1] != b[v][1]) bad++;
if(b[v][0] != a[v][0] && b[v][1] != a[v][1])curans += mp[{b[v][0] - a[v][0], b[v][1] - a[v][1]}];
mp[{a[v][0] - b[v][0], a[v][1] - b[v][1]}]++;
}
int find_set(int v){
return v == par[v] ? par[v] : par[v] = find_set(par[v]);
}
void rem(int x){
if(b[x][1] != a[x][1] && b[x][0] != a[x][0] )curans -= mp[{b[x][0] - a[x][0], b[x][1] - a[x][1]}] * sz[x];
mp[{a[x][0] - b[x][0], a[x][1] - b[x][1]}] -= sz[x];
}
void add(int x){
if(b[x][1] != a[x][1] && b[x][0] != a[x][0] )curans += mp[{b[x][0] - a[x][0], b[x][1] - a[x][1]}] * sz[x];
mp[{a[x][0] - b[x][0], a[x][1] - b[x][1]}] += sz[x];
}
void swp(int u, int v){
int x = find_set(u);
int y = find_set(v);
if(x == y) return;
if(a[x][0] != b[x][0] && a[x][1] != b[x][1]) bad--;
if(a[y][0] != b[y][0] && a[x][1] != b[x][1]) bad--;
rem(x);
rem(y);
a[x][0] -= po[p[u]][0];
a[y][0] -= po[p[v]][0];
a[x][1] -= po[p[u]][1];
a[y][1] -= po[p[v]][1];
a[y][0] += po[p[u]][0];
a[x][0] += po[p[v]][0];
a[y][1] += po[p[u]][1];
a[y][1] += po[p[v]][1];
a[x][0] %= mod; a[x][0] += mod; a[x][0] %= mod;
a[y][0] %= mod; a[y][0] += mod; a[y][0] %= mod;
a[x][0] %= mod; a[x][0] += mod; a[x][0] %= mod;
a[y][0] %= mod; a[y][0] += mod; a[y][0] %= mod;
add(x);
add(y);
if(a[x][0] != b[x][0] && a[x][1] != b[x][1]) bad++;
if(a[y][0] != b[y][0] && a[x][1] != b[x][1]) bad++;
}
void add_edge(int u, int v){
u = find_set(u);
v = find_set(v);
if(u == v) return;
if(a[u][0] != b[u][0] && a[u][1] != b[u][1]) bad--;
if(a[v][0] != b[v][0] && b[v][1] != a[v][1]) bad--;
rem(u);
rem(v);
//cout << curans << "\n";
if(sz[u] < sz[v]) swap(u, v);
sz[u] += sz[v];
par[v] = u;
a[u][0] += a[v][0]; a[u][0] %= mod;
a[u][1] += a[v][1]; a[u][1] %= mod;
b[u][1] += b[v][1]; b[u][1] %= mod;
b[u][0] += b[v][0]; b[u][0] %= mod;
if(a[u][0] != b[u][0] && a[u][1] != b[u][1]) bad++;
add(u);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
int m; cin >> m;
po[0][0] = po[0][1] = 1;
for(int i = 1; i < NMAX; i++) po[i][0] = po[i - 1][0] * pr % mod, po[i][1] = po[i - 1][1] * pr1 % mod;
for(int i = 1; i <= n; i++){
cin >> p[i];
q[i] = p[i];
}
sort(q + 1, q + 1 + n);
for(int i = 1; i <= n; i++) make_set(i);
while(m--){
int type; cin >> type;
if(type == 1){
int u, v; cin >> u >> v;
swp(u, v);
swap(p[u], p[v]);
}
if(type == 2){
int u, v; cin >> u >> v;
add_edge(u, v);
}
if(type == 3){
if(bad == 0) cout << "DA\n";
else cout << "NE\n";
}
if(type == 4){
cout << curans << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
16004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
16080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
16096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
16844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
86 ms |
24188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1584 ms |
87096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2348 ms |
144688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1178 ms |
101104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |