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
|
#include <bits/stdc++.h> #include <tr1/unordered_set>
namespace IO {
inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0; return s == t ? -1 : *s++; }
template <typename T> inline void read(T &x) { static char c; static bool iosig; for (c = read(), iosig = false; !isdigit(c); c = read()) { if (c == -1) return; c == '-' ? iosig = true : 0; } for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0'); iosig ? x = -x : 0; }
inline void read(char &c) { while (c = read(), isspace(c) && c != -1) ; }
const int OUT_LEN = 100000;
char obuf[OUT_LEN], *oh = obuf;
inline void print(char c) { oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0; *oh++ = c; }
template <typename T> inline void print(T x) { static int buf[30], cnt; if (x == 0) { print('0'); } else { x < 0 ? (print('-'), x = -x) : 0; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48; while (cnt) print((char)buf[cnt--]); } }
inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }
struct InputOutputStream { ~InputOutputStream() { flush(); }
template <typename T> inline InputOutputStream &operator<<(const T &x) { print(x); return *this; }
template <typename T> inline InputOutputStream &operator>>(T &x) { read(x); return *this; } } io; }
namespace {
typedef unsigned long long ulong;
inline ulong xorShift128Plus() { static ulong seed1 = 495; static ulong seed2 = 233; register ulong x = seed1; register const ulong y = seed2; seed1 = y, x ^= x << 23; seed2 = x ^ y ^ (x >> 17) ^ (y >> 26); return seed2 + y; }
using IO::io;
std::set<int> s; std::tr1::unordered_set<ulong> exist; const int MAXN = 100010;
ulong num[MAXN], size[MAXN], pos[MAXN], h[MAXN]; int n, m, q;
inline void solve() { io >> n >> m >> q; for (register int i = 1; i <= n; i++) num[i] = xorShift128Plus(); s.insert(1); for (register int i = 1; i <= n; i++) h[1] ^= num[i], size[1]++, pos[i] = 1; for (register int i = 1, x, y; i <= q; i++) { register char c; io >> c >> x >> y; switch (c) { case 'C': { if (pos[x] == y) continue; s.erase(pos[x]), s.erase(y); h[pos[x]] ^= num[x], size[pos[x]]--; if (exist.find(h[pos[x]]) == exist.end()) s.insert(pos[x]); h[y] ^= num[x], size[y]++; if (exist.find(h[y]) == exist.end()) s.insert(y); pos[x] = y; break; } case 'W': { register int ans = 0; for (register std::set<int>::iterator it = s.lower_bound(x); it != s.end() && *it <= y; it = s.lower_bound(x)) exist.insert(h[*it]), ans += size[*it], s.erase(it); io << ans << '\n'; break; } } } } }
int main() { #ifdef DBG freopen("sample/1.in", "r", stdin); #endif solve(); return 0; }
|